Last active
September 5, 2017 03:38
-
-
Save Mossman1215/19ff4ec5524711a5b80ad836462a409b to your computer and use it in GitHub Desktop.
Aggregate Number of High CPU alerts from Nagios Logs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#grep the logs like this | |
#grep "SERVICE ALERT: <instanceid>;Load;.* : CRITICAL" /var/log/nagios3/archives/nagios-* > critical_load.txt | |
<?php | |
$file = file('~/critical_load.txt'); | |
#initialise empty hashmap | |
$map = array(); | |
$hours = array(); | |
foreach($file as $line){ | |
#read timestamp | |
$guffpos = strpos($line,'[')+1; | |
$timestamp = substr($line,$guffpos,10); | |
#make a hour minute string "H:i" | |
$hr = date('H:',$timestamp); | |
$min = date(':i',$timestamp); | |
#increment count in hashmap of "H:i" | |
if(isset($map[$hr])){ | |
$map[$hr]=$map[$hr].$min." "; | |
}else{ | |
$map[$hr]=$min." "; | |
} | |
} | |
#print hashmap (nicely) | |
$i=0; | |
$keys = array_keys($map); | |
echo "Aggregated Load Errors by hour:".PHP_EOL; | |
echo "Hour"." "."NumErrors".PHP_EOL; | |
foreach($map as $str){ | |
$count = strlen($str)/4; | |
echo $keys[$i]." ".$count.PHP_EOL; | |
$i++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment