I am new to php and html. Using php, I'm trying to create a script which will search within a text log file and grab data like URL and Overall server status, and if the overall status is Ok it displays the ok in green or if there is a WARN or ERROR, then it has to display the error that is in the next line. Here is the example of the output in text file. I am looking to write a script to display the first line that is the Public URI, then there are different diagnostics run on the server.

For example

Public URL: https://xxxx.xxxx.xxxxx.com:8xxx/xxx
Server version: 6.0
Build id: RJF-I20150519-1056
Overall server status: WARN
Uptime: 6466896664


Cleanup task for unused Services indexes
A set of diagnostics related to the cleanup task for Services indexes
Test completed at Oct 11, 2019 11:02:40 AM
Overall status: OK


System Clock
A diagnostic to ensure that your system clock is accurate compared with the time provided by an NTP provider.
Overall status: WARN
CRJAZ2108W An NTP server is not configured to use for system clock verification. If unsynchronized clocks are on different application servers, problems might occur. To configure the NTP server to be used to verify the accuracy of the system clock, go to the Advanced Properties page, and for the NTP Server Address property, enter the address of an NTP server. To disable this diagnostic, click Disable.
using php I want to display the output to a webpage to look like a table to show the URL and status and color coding the status

$URL= shell_exec( "egrep -ih 'URL|overall status| warn' *.txt" );

echo nl2br ($URL);
and Output is

Public URL: https://xxxx.xxxx.xxxxx.com:8xxx/xxx
Overall server status WARN
Counter Content Service diagnostic OK
License Server Diagnostics OK
Cleanup task for unused Services indexes OK
System Clock WARN
Database Indices OK
is there a way to get the next line of message if a status is WARN and color code it

    You can use file_get_contents() to get the entire contents of a text file, or even use file() to read a file into an array (without using file_get_contents).

    Looping through the data, you can use strstr() to check for the existence of your desired keywords ... not that there's necessarily anything wrong with grep and shell_exec, but I'd prefer PHP for the whole thing instead of a mixed system.

    This is ad hoc code ... you'll want to modify the HTML, I'm sure, to fit whatever your idea of a report layout should be; but perhaps it will help you get started:

    $data = file("somefile.txt");
    $next = false;
    $eol   = "<br>";
    
    foreach ($data as $line) {
    
        // if $next is true, "display the error that is in the next line"
        if ($next) {
            echo $line . $eol;
        }
        $next = false; // reset variable
        
        // display the first line that is the Public URI
        if (strstr($line, "Public URL")) {
            echo $line . $eol;
        }
        // if the overall status is OK, display the OK in green
        if (strstr($line, "Overall") && strstr($line, "OK")) {
            echo "<span class='green'>$line</span>" . $eol;
        }
        // if there is a WARN or ERROR, set $next to true to print the next line
        if (strstr($line, "Overall") && (strstr($line, "WARN") || strstr($line, "ERROR")) {
            $next = true;
        }
    }

    Hi Dalecosp,

    Thank you for the help with the code. I tried to code. I updated the input file and ran the code, the output is showing a blank page, wanted to check if I have to any other changes to the code?

    Thanks in advance

    Regards

    Kumar

    dalecosp

    Assuming log files might get very large (for undefined values of "very" and "large"), I might opt for fopen() and then fgets() in a while loop, as opposed to loading the entire file into memory in the PHP process. (I.e., you just read one line at a time from the file.)

    kumar1

    I apologize ... missing a closing parenthesis here:

    if (strstr($line, "Overall") && (strstr($line, "WARN") || strstr($line, "ERROR"))
    // should be
    if (strstr($line, "Overall") && (strstr($line, "WARN") || strstr($line, "ERROR")))

    Of course, my code was meant as an example.

    NogDog

    Yes, for a large file that would be a better solution.

      Write a Reply...