I've been tasked to find a solution, at no charge, that will enable our department to do Start of Day Web Reports/Emails.
So far I've made progress by getting MON installed. Mon has a function that I can setup a CRON job for that will create a text file for me daily.
Here is a samle record from the file:
group=Routers service=ping opstatus=1 last_opstatus=1 exitval=0 timer=20 last_success=1038952571 last_trap=0 last_check=1038952570 ack=0 ackcomment='' alerts_sent=0 depstatus=0 depend='' monitor='fping.monitor -r 3 -t 60000' last_summary='' last_detail='start time: Tue Dec 3 16:56:10 2002\0aend time : Tue Dec 3 16:56:10 2002\0aduration : 0 seconds\0a\0a------------------------------------------------------------------------------\0areachable hosts rtt\0a------------------------------------------------------------------------------\0a192.168.94.242 2.26 ms\0a192.168.94.250 1.26 ms\0a192.168.94.28 1.05 ms\0a192.168.94.29 1.23 ms\0a' interval=300
What I am trying to do is use PHP (since I have little xp with it and none with PERL) to use that file, Parse it, then upload the data to a MySQL database.
Using this code from some research, I am able to at least parse the data:
<?php
// Define all variables you want to search for
$vars = array(
'group=',
'service=',
'opstatus=',
'last_optstatus=',
'exitval=',
'timer=',
'last_success=',
'last_trap=',
'last_check=',
'ack=',
'ackcomment=',
'alerts_sent=',
'depstatus=',
'depend=',
'monitor=',
'last_summary=',
'last_detail=',
'start time:',
'end time :',
'duration :',
'interval=',
);
// read the file as an array and join the lines; now it is one string
$filename = "log.txt";
$file = join( '', file( $filename ));
// replace each var with a token, using a character that
// normally does not appear, like ¦ (ascii 178)
foreach( $vars as $key => $val ) {
$file = preg_replace( "/$val/i", "¦¦".$key."¦", $file );
}
// mark the end of the last item
$file .= '¦';
// now you can easily extract each variable
if( preg_match_all( "/¦(\d+)¦([¦]+)¦/s", $file, $matches, PREG_SET_ORDER )) {
foreach( $matches as $match ) {
echo "<br>{$vars[$match[1]]} =>> " . strip_tags($match[2]);
}
}
?>
I would think that I should be able to replace the echo line near the bottom with some code that will create an array of INSERT statements. But I'm at a loss.
Can anyone provide some insight please?
Ho...