1<?php 2 3########################################################################################## 4# Author; Vladimir Vuksan 5# This is a Ganglia Nagios plugins that alerts based on values extracted from Ganglia 6# 7# This script checks whether metric value(s) are the same across a range of 8# 9# You need to supply following GET values 10# 11# hreg = "Host regular expression" 12# checks = "Comma delimited list of checks" 13# Example would be if you wanted to make sure number that subversion tag on all 14# deployed files was the same 15# 16# ?hreg=apache\tomcat&checks=svn_revision 17# 18########################################################################################## 19$conf['ganglia_dir'] = dirname(dirname(__FILE__)); 20 21include_once $conf['ganglia_dir'] . "/eval_conf.php"; 22 23# To turn on debug set to 1 24$debug = 0; 25 26$host_reg="cache-|varnish-"; 27 28if ( isset($_GET['hreg']) && isset($_GET['checks'])) { 29 $host_reg = $_GET['hreg']; 30 $check_metrics = explode(",", $_GET['checks']); 31} else { 32 die("You need to supply hreg (host regex)"); 33} 34 35global $metrics; 36 37if($conf['nagios_cache_enabled'] && file_exists($conf['nagios_cache_file'])){ 38 // check for the cached file 39 // snag it and return it if it is still fresh 40 $time_diff = time() - filemtime($conf['nagios_cache_file']); 41 $expires_in = $conf['nagios_cache_time'] - $time_diff; 42 if( $time_diff < $conf['nagios_cache_time']){ 43 if ( $debug == 1 ) { 44 error_log("DEBUG: Fetching data from cache. Expires in " . $expires_in . " seconds.\n"); 45 } 46 $metrics = unserialize(file_get_contents($conf['nagios_cache_file'])); 47 } 48} 49 50if ( ! is_array( $metrics ) ) { 51 52 if ( $debug == 1 ) { 53 error_log("DEBUG: Querying GMond for new data\n"); 54 } 55 $context = "cluster"; 56 include_once $conf['ganglia_dir'] . "/functions.php"; 57 include_once $conf['ganglia_dir'] . "/ganglia.php"; 58 include_once $conf['ganglia_dir'] . "/get_ganglia.php"; 59 # Massage the metrics to minimize the cache file by caching only attributes 60 # we care about 61 foreach ( $metrics as $mhost => $host_metrics ) { 62 foreach ( $host_metrics as $name => $attributes ) { 63 $new_metrics[$mhost][$name]['VAL'] = $metrics[$mhost][$name]['VAL']; 64 if ( isset($metrics[$mhost][$name]['UNITS']) ) 65 $new_metrics[$mhost][$name]['UNITS'] = $metrics[$mhost][$name]['UNITS']; 66 } 67 } 68 file_put_contents($conf['nagios_cache_file'], serialize($new_metrics)); 69 unset($metrics); 70 $metrics = $new_metrics; 71 unset($new_metrics); 72 73} 74 75# Get a list of all hosts 76$ganglia_hosts_array = array_keys($metrics); 77 78$results_ok = array(); 79$results_notok = array(); 80 81# Initialize results array 82foreach ( $check_metrics as $index => $metric_name ) { 83 $results[$metric_name]["values"] = array(); 84} 85 86 87// Loop through all hosts looking for matches 88foreach ( $ganglia_hosts_array as $index => $hostname ) { 89 90 // Find matching hosts and make sure they are alive 91 if ( preg_match("/" . $host_reg . "/", $hostname) && ( time() - $metrics[$hostname]['last_reported_timestamp']['VAL']) < 60) { 92 93 // Now let's look at all the metrics 94 foreach ( $check_metrics as $index => $metric_name ) { 95 // Check for the existence of a metric 96 if ( isset($metrics[$hostname][$metric_name]['VAL']) ) { 97 $metric_value = $metrics[$hostname][$metric_name]['VAL']; 98 // First we check if we have seen the value already. If not add the value to the 99 // values array and add the host to members array 100 if ( ! in_array($metric_value, $results[$metric_name]["values"] ) ) { 101 $results[$metric_name]["values"][] = $metric_value; 102 } 103 // We have seen the value before 104 // Find index of the value 105 $value_index = array_search($metric_value, $results[$metric_name]["values"]); 106 $results[$metric_name]["members"][$value_index][] = $hostname; 107 108 109 } // end of if ( isset($metrics[$hostname][$metric_name]['VAL']) ) 110 111 } // end of foreach ( $check_metrics as $index => $metric_name ) 112 113 } // end of if ( preg_match("/" . $host_reg 114 115} // end of foreach ( $ganglia_hosts_array as $index => $hostname ) { 116 117$ok=true; 118 119$output = ""; 120 121foreach ( $results as $metric_name => $metrics_results ) { 122 if ( count($metrics_results["values"]) > 1 ) { 123 $ok=false; 124 $output .= " CRIT " . $metric_name . " differs values => "; 125 foreach ( $metrics_results["values"] as $index => $value ) { 126 $output .= $value . " ( " . join("," , $metrics_results["members"][$index]) . " ) "; 127 } 128 } else { 129 $output .= ", " .$metric_name . " same => " . count($metrics_results["members"][0]) . " nodes"; 130 } 131} 132 133if ( $ok === true ) { 134 print "OK|" . $output; 135 exit(0); 136} else { 137 print "CRITICAL|" . $output; 138 exit(2); 139} 140 141?> 142