In my day to day job, I often have to write monitoring scripts. Recently we had to write a script that would check if a daemon wrote in its log in the last 5 minutes. This particular daemon had the habit of freezing. So you would see it in the process list but it would not do anything.
Therefore, to ensure that this process is alive and working, we have to check the last entry it creates in the log file and see if it is newer than 5 mns ( It is supposed to write at least 4 entries per minutes). Here is the litlle perl script to do that check :
##########################################
# Nouveau check pour DaemonA
&debug ( "=-=-=-=-= Verification de DaemonA =-=-=-=-= " );
$last_Order=`grep "DaemonA" /var/logs/efixeng/enginelog4j.log | tail -1`;
if ( $last_Order == "" )
{
&debug ( "We did not find any log entry for DaemonA");
$ORDER_TIME = 0;
} else {
&debug ( "last log entry was $last_Order" );
($order_date,$order_heure,$trash) = split (/ /,$last_Order);
&debug ( "date is $order_date, heure is $order_heure");
# Setting up date
($ohours,$ominutes,$osecondes)=split(/:/,$order_heure);
($oyear,$omonth,$oday)=split(/\./,$order_date);
$ORDER_TIME = timelocal($osecondes, $ominutes, $ohours, $oday, $omonth, $oyear);
&debug ( "Epoch time for the last occurence of DaemonA is $ORDER_TIME");
}
$CURRENT_TIME = timelocal(`date +%S`,`date +%M`, `date +%H`, `date +%d`, `date +%m`, `date +%Y`);
&debug ( "Current Epoch time is $CURRENT_TIME");
$diff = $CURRENT_TIME - $ORDER_TIME;
if ( $diff < 300 )
{
&debug ( "Last Occurence of DaemonA was $diff secondes ago. Everything is OK");
$message .= "\nLe Daemon DaemonA a ecrit une entree il y a $diff secondes. tout est OK.\n";
} else {
&debug ( "[ERREUR!] Last Occurence of DaemonA was $diff secondes ago.");
$summary .= "\n[ERREUR!] Le DaemonA Daemon ne roule pas ($cdate $cheure) !!";
$summary .= "\n[ERREUR!] La derniere ecriture dans le log etait il y a $diff secondes !!";
$color = "red";
};
# fin de la verification
##########################################