= $crit) { echo "CRITICAL: total:<" . $counts['total'] . ">, affected:<" . $counts['actual'] . ">\n"; exit (2); } if ($percent >= $warn) { echo "WARNING: total:<" . $counts['total'] . ">, affected:<" . $counts['actual'] . ">\n"; exit (1); } echo "OK: total:<" . $counts['total'] . ">, affected:<" . $counts['actual'] . ">\n"; exit(0); # Functions /* print usage */ function usage () { echo "Usage: $0 -f -t type(host/service) -s -n -w -c \n"; } /* Query host count */ function query_host_count ($status_file_content, $status_code) { $num_matches = preg_match_all("/hoststatus \{([\S\s]*?)\}/", $status_file_content, $matches, PREG_PATTERN_ORDER); $hostcounts_object = array (); $total_hosts = 0; $hosts = 0; foreach ($matches[0] as $object) { $total_hosts++; if (getParameter($object, "current_state") == $status_code) { $hosts++; } } $hostcounts_object['total'] = $total_hosts; $hostcounts_object['actual'] = $hosts; return $hostcounts_object; } /* Query Alert counts */ function query_alert_count ($status_file_content, $service_name, $status_code) { $num_matches = preg_match_all("/servicestatus \{([\S\s]*?)\}/", $status_file_content, $matches, PREG_PATTERN_ORDER); $alertcounts_objects = array (); $total_alerts=0; $alerts=0; foreach ($matches[0] as $object) { if (getParameter($object, "service_description") == $service_name) { $total_alerts++; if (getParameter($object, "current_state") >= $status_code) { $alerts++; } } } $alertcounts_objects['total'] = $total_alerts; $alertcounts_objects['actual'] = $alerts; return $alertcounts_objects; } function get_service_type($service_description) { $pieces = explode("::", $service_description); switch ($pieces[0]) { case "NAMENODE": $pieces[0] = "HDFS"; break; case "JOBTRACKER": $pieces[0] = "MAPREDUCE"; break; case "HBASEMASTER": $pieces[0] = "HBASE"; break; case "SYSTEM": case "HDFS": case "MAPREDUCE": case "HBASE": break; default: $pieces[0] = "UNKNOWN"; } return $pieces[0]; } function getParameter($object, $key) { $pattern="/\s" . $key . "[\s= ]*([\S, ]*)\n/"; $num_mat = preg_match($pattern, $object, $matches); $value = ""; if ($num_mat) { $value = $matches[1]; } return $value; } function indent($json) { $result = ''; $pos = 0; $strLen = strlen($json); $indentStr = ' '; $newLine = "\n"; $prevChar = ''; $outOfQuotes = true; for ($i=0; $i<=$strLen; $i++) { // Grab the next character in the string. $char = substr($json, $i, 1); // Are we inside a quoted string? if ($char == '"' && $prevChar != '\\') { $outOfQuotes = !$outOfQuotes; // If this character is the end of an element, // output a new line and indent the next line. } else if(($char == '}' || $char == ']') && $outOfQuotes) { $result .= $newLine; $pos --; for ($j=0; $j<$pos; $j++) { $result .= $indentStr; } } // Add the character to the result string. $result .= $char; // If the last character was the beginning of an element, // output a new line and indent the next line. if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) { $result .= $newLine; if ($char == '{' || $char == '[') { $pos ++; } for ($j = 0; $j < $pos; $j++) { $result .= $indentStr; } } $prevChar = $char; } return $result; } /* JSON documment format */ /* { "programstatus":{ "last_command_check":"1327385743" }, "hostcounts":{ "up_nodes":"", "down_nodes":"" }, "hoststatus":[ { "host_name"="ip-10-242-191-48.ec2.internal", "current_state":"0", "last_hard_state":"0", "plugin_output":"PING OK - Packet loss = 0%, RTA = 0.04 ms", "last_check":"1327385564", "current_attempt":"1", "last_hard_state_change":"1327362079", "last_time_up":"1327385574", "last_time_down":"0", "last_time_unreachable":"0", "is_flapping":"0", "last_check":"1327385574", "servicestatus":[ ] } ], "servicestatus":[ { "service_type":"HDFS", {HBASE, MAPREDUCE, HIVE, ZOOKEEPER} "service_description":"HDFS Current Load", "host_name"="ip-10-242-191-48.ec2.internal", "current_attempt":"1", "current_state":"0", "plugin_output":"PING OK - Packet loss = 0%, RTA = 0.04 ms", "last_hard_state_change":"1327362079", "last_time_ok":"1327385479", "last_time_warning":"0", "last_time_unknown":"0", "last_time_critical":"0", "last_check":"1327385574", "is_flapping":"0" } ] } */ ?>