doushen2154 2012-11-20 19:29
浏览 75

在php函数中过滤syslog,然后在JS div中显示内容?

Let's revise this question with a new approach...I have three files: logtail.php, ajax.js and index.php. My goal is to create a syslog viewer (Linux).

On index.php I made a div where I want to display only the filtered contents of the syslog. I must filter the contents in logtail.php. I have to use a shell_exec and | grep the contents with multiple different regexes. Right now I | grep the entire syslog file and it displays live in the log viewer, but my filters are not working as planned.

I need help figuring out how to use $_GET to grab only the contents from the syslog that the user wants to see. I have a text field and submit button prepared for that in my index.php file. Should I use functions (tried this already)? Or is there a better approach? Can you give me some examples?

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("
", $syslogContent);
    rsort($contentArray);
    print_r($contentArray);
}

ajax.js (working properly)

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.
";
    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("
");
            eval(currentLogValue);
            logDiv = document.getElementById("log");
            var logLine = ' ';
            for (i = 0; i < currentLogValue.length - 1; i++) {
                logLine += currentLogValue[i] + "<br/>
";
            }
            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>
  • 写回答

1条回答 默认 最新

  • doufan8805 2012-11-20 19:59
    关注

    I would go further than simple pooling. I would pipe logs to some script. Lets say info log

     *.info                        |/somescript.php
    

    And here I would do real time with Strophe and Php XMPP.

    Tell me what you think?

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值