Page 1 of 1

How to monitor a /var/log file

Posted: Wed Mar 21, 2018 10:13 am
by kainaat
I have a .csv log file which I would like to monitor for error codes and print a message when an error code is found.

PHP script:

Code: Select all

<?php
 $command = "ssh -p 97 -i /var/www/html/test.rsa pi@192.168.xx.xx 
 tail -1 /var/log/playlog.csv";
 $output = shell_exec($command);
 echo "Test PI <p></p>Status: <p></p><pre>Currently Playing: $output</pre> ";
 ?>
Webpage outputs:

Code: Select all

Test PI
Status:
Currently Playing:
2018-03-20 10:30:13,NM_Test.h264,-0
So the code 0 means there are no errors and everything is working smoothly. But if the error code is something like -2 than I want status to print 'Missing file':

Code: Select all

Test PI
Status: Missing file
Currently Playing:
2018-03-20 10:30:13,NM_Test.h264,-2
And a different message depending on a different error code.

How can I achieve this?

Re: How to monitor a /var/log file

Posted: Wed Mar 21, 2018 11:02 am
by evilkitty
php has 2 way to run shell commands shell exec and exec, one has no output text
i believe a exit state of >0 is a error
if you use ''cat /var/log/filename > /dev/null;echo $?' shell will only return the exit status number

you also should use this function
http://php.net/manual/en/function.escapeshellcmd.php
iirc it does not handle line beaks, you may want to handle those yourself

Code: Select all

str_replace("\\n","\n",shell_exec('cat /dev/urandom'));// do not cat that file its length is infinite
I would have the client run a script to send post/get data to the pi instead of using sell_exec

You could use ssh to call a script on the client to check of the file exist 1st and return appropriate data

Code: Select all

#!/bin/sh
file='/var/log/playlog.csv'
if [ -f $file  ];then
  tail -1 $file
else
  echo 404 error
fi

Re: How to monitor a /var/log file

Posted: Wed Mar 21, 2018 12:37 pm
by kainaat
Your bash script is a far better way to achieve what I attempted to do using PHP. So I will use your script instead.

However, I'm still a stuck on a solution to my original question. To clarify, I have a player.bin file which writes to playlog.csv file of every video the omx player plays.

So for me a 0 indicates there are no errors present, but a -2 would indicate my player is expecting to see a .h264 file inside /var/lib/surround/.

I was thinking of running the command:

Code: Select all

 tail -1 /var/log/playlog.csv 
And then somehow running a grep/awk/sed command to retrieve a match after the second comma:

E.g.
if it sees -2 -> 2018-03-21 11:17:29,NM_Test.h264,-2
echo "Missing file"
or
if it sees -3 -> 2018-03-21 11:17:29,NM_Test.h264,-3
echo "different message"

else
if it sees 0 -> 2018-03-21 11:17:29,NM_Test.h264,0
echo "no errors reported"

This is the bit I am stuck at :(
evilkitty wrote:
Wed Mar 21, 2018 11:02 am
php has 2 way to run shell commands shell exec and exec, one has no output text
i believe a exit state of >0 is a error
if you use ''cat /var/log/filename > /dev/null;echo $?' shell will only return the exit status number

you also should use this function
http://php.net/manual/en/function.escapeshellcmd.php
iirc it does not handle line beaks, you may want to handle those yourself

Code: Select all

str_replace("\\n","\n",shell_exec('cat /dev/urandom'));// do not cat that file its length is infinite
I would have the client run a script to send post/get data to the pi instead of using sell_exec

You could use ssh to call a script on the client to check of the file exist 1st and return appropriate data

Code: Select all

#!/bin/sh
file='/var/log/playlog.csv'
if [ -f $file  ];then
  tail -1 $file
else
  echo 404 error
fi

Re: How to monitor a /var/log file

Posted: Wed Mar 21, 2018 1:45 pm
by PingoBags
What if you create a function to output the custom errors for you?

Code: Select all

<?php
function ReturnError($res)
{
  switch ($res)
  {
    case "0":
    return " no errors reported";

    case "1":
    return " some error";
                 
    case "2":
    return " Missing file";

    case "3":
    return " some error";
   }
}

$command = "ssh -p 97 -i /var/www/html/test.rsa pi@192.168.xx.xx 
tail -1 /var/log/playlog.csv";
$output = shell_exec($command);
echo "Test PI <p></p>Status: <p></p><pre>Currently Playing: ".substr($output, 0, -2).ReturnError(substr($output, -1));
?>
If there is no error, it should output

Code: Select all

Test PI
Status:
Currently Playing:
2018-03-20 10:30:13,NM_Test.h264, no errors reported
If error 2

Code: Select all

Test PI
Status:
Currently Playing:
2018-03-20 10:30:13,NM_Test.h264, Missing file

Re: How to monitor a /var/log file

Posted: Wed Mar 21, 2018 3:57 pm
by kainaat
This is exactly what I need!!! Thank you!

But I can't get seem to get the corresponding case message to display:

Code: Select all

Currently playing: 2018-03-21 15:21:13,NM_Test.h264,
Where am I going wrong?
PingoBags wrote:
Wed Mar 21, 2018 1:45 pm
What if you create a function to output the custom errors for you?

Code: Select all

<?php
function ReturnError($res)
{
  switch ($res)
  {
    case "0":
    return " no errors reported";

    case "1":
    return " some error";
                 
    case "2":
    return " Missing file";

    case "3":
    return " some error";
   }
}

$command = "ssh -p 97 -i /var/www/html/test.rsa pi@192.168.xx.xx 
tail -1 /var/log/playlog.csv";
$output = shell_exec($command);
echo "Test PI <p></p>Status: <p></p><pre>Currently Playing: ".substr($output, 0, -2).ReturnError(substr($output, -1));
?>
If there is no error, it should output

Code: Select all

Test PI
Status:
Currently Playing:
2018-03-20 10:30:13,NM_Test.h264, no errors reported
If error 2

Code: Select all

Test PI
Status:
Currently Playing:
2018-03-20 10:30:13,NM_Test.h264, Missing file

Re: How to monitor a /var/log file

Posted: Wed Mar 21, 2018 5:12 pm
by PingoBags
What exactly does $output print out, not from the code I posted, but the original code you have.
I assumed it printed

Code: Select all

2018-03-21 15:21:13,NM_Test.h264,-0
or whatever the code is at the end.

Add an extra line to ReturnError function so it looks like this.
See if it prints out anything. It might give you a better idea.

Code: Select all

function ReturnError($res)
{
  switch ($res)
  {
    case "0":
    return " no errors reported";

    case "1":
    return " some error";
                 
    case "2":
    return " Missing file";

    case "3":
    return " some error";

    //if the error code is not blank, return the value else return (Error code blank)
    default:
    return empty($res) ? " Error code blank" : "-".$res;
   }
}

Re: How to monitor a /var/log file

Posted: Thu Mar 22, 2018 12:54 pm
by kainaat
Sorry if I haven't been clear, but if the error code is 0, my log file outputs:

Code: Select all

2018-03-22 12:28:34,NM_Test.h264,0
For any other code, it'll be a minus number:

Code: Select all

2018-03-22 12:28:34,NM_Test.h264,-2

When I added the default case I got:

Code: Select all

Currently Playing: 2018-03-22 12:34:53,NM_Test.h264,--
I assumed the function can't see the switch cases so I parsed the $res variable (at the end):

Code: Select all

echo "Test PI <p></p>Status: <p></p><pre>Currently Playing: ".substr($res, 0, -2).ReturnError(substr($res, -1));
Output:

Code: Select all

Currently Playing: 2018-03-22 12:42:24,NM_Test.h264,- Error code blank
My current set up:

Currently my log file is outputting:

Code: Select all

2018-03-22 12:43:21,NM_Test.h264,-2
So I've set my cases up like:

Code: Select all

function ReturnError($res)
{
  switch ($res)
  {
    case "0":
    return " no errors reported";

    case "1":
    return " some error";
	
    case "2":
    return " Missing file";

    case "3":
    return " some error";
   
   //all the way to case 15
   }
}
I'll keep playing around with it, but is there anything you can see different that might explain where I'm going wrong?

Re: How to monitor a /var/log file

Posted: Thu Mar 22, 2018 5:40 pm
by PingoBags
I can't see a problem off hand but I am wondering why you used $res as your variable when calling ReturnError.
You should be using $output.

Also I though the code returned from your output was -0, with the minus. It should have still output something but I changed it anyway to reflect the true error code values.

Your new ReturnError would be written like this.
Use the minus for all the error codes besides the 0

Code: Select all

function ReturnError($res)
{
  switch ($res)
  {
    case "0":
    return " no errors reported";

    case "-1":
    return " some error";
	
    case "-2":
    return " Missing file";

    case "-3":
    return " some error";
   
   //all the way to case 15
   }
}
I broke the code down for easier reading and debugging

Code: Select all

if($idx = strripos($output,','))//Get the last index of ',' in your output string
{
  $ErrorCode = substr($output,$idx + 1,(strlen($output) - $idx) - 1);//using the found index, get the error code using substring
  $Playlist = substr($output, 0, $idx + 1);//Get the rest of the output string, minus the error code 
  echo "Currently Playing: ".$Playlist.ReturnError($ErrorCode);//The ReturnError function just replaces the error code with a custom error
}
Since I don't have your output file, I used my own for testing

Code: Select all

$output = "2018-03-22 12:43:21,NM_Test.h264,0";
Currently Playing: 2018-03-22 12:43:21,NM_Test.h264, no errors reported
With error

Code: Select all

$output = "2018-03-22 12:43:21,NM_Test.h264,-2";
Currently Playing: 2018-03-22 12:43:21,NM_Test.h264, Missing file

Re: How to monitor a /var/log file

Posted: Fri Mar 23, 2018 12:27 pm
by kainaat
It works.. hurray!!

Using your code and changing:

Code: Select all

 $output = shell_exec($command);
To just using exec:

Code: Select all

$output = exec($command);
And now I can see the error messages!

Many thanks for your support!

Re: How to monitor a /var/log file

Posted: Fri Mar 23, 2018 4:27 pm
by PingoBags
Glad to help