Filter syslog in php functions, then display contents in JS div?

Posted by qx3rt on Stack Overflow See other posts from Stack Overflow or by qx3rt
Published on 2012-11-20T19:29:18Z Indexed on 2012/11/21 17:00 UTC
Read the original article Hit count: 183

Filed under:
|
|
|
|

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("\n", $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.\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>

© Stack Overflow or respective owner

Related posts about php

Related posts about JavaScript