3

让我们用一种新方法来修改这个问题……我有三个文件:logtail.php、ajax.js 和 index.php。我的目标是创建一个系统日志查看器(Linux)。

在 index.php 上,我创建了一个 div,我只想显示过滤后的 syslog 内容。我必须过滤 logtail.php 中的内容。我必须使用 shell_exec 和 | 使用多个不同的正则表达式 grep 内容。现在我| grep 整个 syslog 文件,它会在日志查看器中实时显示,但我的过滤器没有按计划工作。

我需要帮助弄清楚如何使用 $_GET 仅从系统日志中获取用户想要查看的内容。我的 index.php 文件中有一个为此准备的文本字段和提交按钮。我应该使用函数(已经尝试过)吗?还是有更好的方法?你能给我一些例子吗?

logtail.php

//Executes a shell script to grab all file contents from syslog on the device
//Explodes that content into an array by new line, sorts from most recent entry to oldest entry

if (file_exists($filename = '/var/log/syslog')) {
    $syslogContent = shell_exec("cat $filename | grep -e '.*' $filename");
    $contentArray = explode("\n", $syslogContent);
    rsort($contentArray);
    print_r($contentArray);
}

ajax.js(正常工作)

function createRequest() {
    var request = null;
    try {
        request = new XMLHttpRequest();
    } catch (trymicrosoft) {
        try {
            request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (othermicrosoft) {
            try {
                request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (failed) {
                request = null;
            }
        }
    }

    if (request == null) {
        return alert("Error creating request object!");
    } else {
        return request;
    }
}

var request = createRequest();

function getLog(timer) {
    var url = 'logtail.php';
    request.open("GET", url, true);
    request.onreadystatechange = updatePage;
    request.send(null);
    startTail(timer);
}

function startTail(timer) {
    if (timer == "stop") {
        stopTail();
    } else {
        t = setTimeout("getLog()",1000);
    }
}

function stopTail() {
    clearTimeout(t);
    var pause = "The log viewer has been paused. To begin viewing again, click the Start Log button.\n";
    logDiv = document.getElementById("log");
    var newNode = document.createTextNode(pause);
    logDiv.replaceChild(newNode,logDiv.childNodes[0]);
}

function updatePage() {
    if (request.readyState == 4) {
        if (request.status == 200) {
            var currentLogValue = request.responseText.split("\n");
            eval(currentLogValue);
            logDiv = document.getElementById("log");
            var logLine = ' ';
            for (i = 0; i < currentLogValue.length - 1; i++) {
                logLine += currentLogValue[i] + "<br/>\n";
            }
            logDiv.innerHTML = logLine;
        } else
            alert("Error! Request status is " + request.status);
    }
}

index.php

    <script type="text/javascript" src="scripts/ajax.js"></script>
        <button style="margin-left:25px;" onclick="getLog('start');">Start Log</button>
        <button onclick="stopTail();">Stop Log</button>
            <form action="" method="get"> //This is where the filter options would be
                Date & Time (ex. Nov 03 07:24:57): <input type="text" name="dateTime" />
                <input type="submit" value="submit" />
            </form>
    <br>
    <div id="log" style="...">
    //This is where the log contents are displayed
    </div>
4

1 回答 1

0

我会比简单的池更进一步。我会将日志通过管道传输到某个脚本。让我们说信息日志

 *.info                        |/somescript.php

在这里,我将实时使用StrophePhp XMPP

告诉我你的想法?

于 2012-11-20T19:59:40.910 回答