HDP_MON_RESPONSE_OPTION_VALUE__PROPERTIES_UNCACHEABLE, HDP_MON_RESPONSE_OPTION_KEY__TYPE => isset( $jsonpFunctionName ) && $jsonpFunctionName != "" ? HDP_MON_RESPONSE_OPTION_VALUE__TYPE_JAVASCRIPT : HDP_MON_RESPONSE_OPTION_VALUE__TYPE_JSON ) ); if( isset( $jsonpFunctionName ) ) { echo "$jsonpFunctionName( $response_data );"; } else { echo $response_data; } } /* alert_type { ok, non-ok, warning, critical, all } */ define ("all", "-2"); define ("nok", "-1"); define ("ok", "0"); define ("warn", "1"); define ("critical", "2"); define ("HDFS_SERVICE_CHECK", "NAMENODE::NameNode process down"); define ("MAPREDUCE_SERVICE_CHECK", "JOBTRACKER::JobTracker process down"); define ("HBASE_SERVICE_CHECK", "HBASEMASTER::HBaseMaster process down"); define ("ZOOKEEPER_SERVICE_CHECK", "ZOOKEEPER::Percent ZooKeeper Servers down"); define ("HIVE_METASTORE_SERVICE_CHECK", "HIVE-METASTORE::Hive Metastore status check"); define ("OOZIE_SERVICE_CHECK", "OOZIE::Oozie Server status check"); define ("WEBHCAT_SERVICE_CHECK", "WEBHCAT::WebHCat Server status check"); define ("PUPPET_SERVICE_CHECK", "PUPPET::Puppet agent down"); /* If SUSE, status file is under /var/lib/nagios */ if (file_exists("/etc/SuSE-release")) { $status_file="/var/nagios/status.dat"; } else { $status_file="/var/nagios/status.dat"; } $q1=""; if (array_key_exists('q1', $_GET)) { $q1=$_GET["q1"]; } $q2=""; if (array_key_exists('q2', $_GET)) { $q2=$_GET["q2"]; } $alert_type=""; if (array_key_exists('alert_type', $_GET)) { $alert_type=$_GET["alert_type"]; } $host=""; if (array_key_exists('host_name', $_GET)) { $host=$_GET["host_name"]; } $indent=""; if (array_key_exists('indent', $_GET)) { $indent=$_GET["indent"]; } $result = array(); $status_file_content = file_get_contents($status_file); if ($q1 == "alerts") { /* Add the service status object to result array */ $result['alerts'] = query_alerts ($status_file_content, $alert_type, $host); } if ($q2 == "hosts") { /* Add the service status object to result array */ $result['hosts'] = query_hosts ($status_file_content, $alert_type, $host); } /* Add host count object to the results */ $result['hostcounts'] = query_host_count ($status_file_content); /* Add services runtime states */ $result['servicestates'] = query_service_states ($status_file_content); /* Return results */ if ($indent == "true") { hdp_mon_generate_response(indent(json_encode($result))); } else { hdp_mon_generate_response(json_encode($result)); } # Functions /* Query service states */ function query_service_states ($status_file_content) { $num_matches = preg_match_all("/servicestatus \{([\S\s]*?)\}/", $status_file_content, $matches, PREG_PATTERN_ORDER); $services_object = array (); $services_object["PUPPET"] = 0; foreach ($matches[0] as $object) { if (getParameter($object, "service_description") == HDFS_SERVICE_CHECK) { $services_object["HDFS"] = getParameter($object, "last_hard_state"); if ($services_object["HDFS"] >= 1) { $services_object["HDFS"] = 1; } continue; } if (getParameter($object, "service_description") == MAPREDUCE_SERVICE_CHECK) { $services_object["MAPREDUCE"] = getParameter($object, "last_hard_state"); if ($services_object["MAPREDUCE"] >= 1) { $services_object["MAPREDUCE"] = 1; } continue; } if (getParameter($object, "service_description") == HBASE_SERVICE_CHECK) { $services_object["HBASE"] = getParameter($object, "last_hard_state"); if ($services_object["HBASE"] >= 1) { $services_object["HBASE"] = 1; } continue; } if (getParameter($object, "service_description") == HIVE_METASTORE_SERVICE_CHECK) { $services_object["HIVE-METASTORE"] = getParameter($object, "last_hard_state"); if ($services_object["HIVE-METASTORE"] >= 1) { $services_object["HIVE-METASTORE"] = 1; } continue; } if (getParameter($object, "service_description") == OOZIE_SERVICE_CHECK) { $services_object["OOZIE"] = getParameter($object, "last_hard_state"); if ($services_object["OOZIE"] >= 1) { $services_object["OOZIE"] = 1; } continue; } if (getParameter($object, "service_description") == WEBHCAT_SERVICE_CHECK) { $services_object["WEBHCAT"] = getParameter($object, "last_hard_state"); if ($services_object["WEBHCAT"] >= 1) { $services_object["WEBHCAT"] = 1; } continue; } /* In case of zookeeper, service is treated running if alert is ok or warning (i.e partial * instances of zookeepers are running */ if (getParameter($object, "service_description") == ZOOKEEPER_SERVICE_CHECK) { $services_object["ZOOKEEPER"] = getParameter($object, "last_hard_state"); if ($services_object["ZOOKEEPER"] <= 1) { $services_object["ZOOKEEPER"] = 0; } continue; } if (getParameter($object, "service_description") == PUPPET_SERVICE_CHECK) { $state = getParameter($object, "last_hard_state"); if ($state >= 1) { $services_object["PUPPET"]++; } continue; } } if ($services_object["PUPPET"] >= 1) { $services_object["PUPPET"] = 1; } $services_object = array_map('strval', $services_object); return $services_object; } /* Query host count */ function query_host_count ($status_file_content) { $num_matches = preg_match_all("/hoststatus \{([\S\s]*?)\}/", $status_file_content, $matches, PREG_PATTERN_ORDER); $hostcounts_object = array (); $up_hosts = 0; $down_hosts = 0; foreach ($matches[0] as $object) { if (getParameter($object, "last_hard_state") != ok) { $down_hosts++; } else { $up_hosts++; } } $hostcounts_object['up_hosts'] = $up_hosts; $hostcounts_object['down_hosts'] = $down_hosts; $hostcounts_object = array_map('strval', $hostcounts_object); return $hostcounts_object; } /* Query Hosts */ function query_hosts ($status_file_content, $alert_type, $host) { $hoststatus_attributes = array ("host_name", "current_state", "last_hard_state", "plugin_output", "last_check", "current_attempt", "last_hard_state_change", "last_time_up", "last_time_down", "last_time_unreachable", "is_flapping", "last_check"); $num_matches = preg_match_all("/hoststatus \{([\S\s]*?)\}/", $status_file_content, $matches, PREG_PATTERN_ORDER); $hosts_objects = array (); $i = 0; foreach ($matches[0] as $object) { $hoststatus = array (); $chost = getParameter($object, "host_name"); if (empty($host) || $chost == $host) { foreach ($hoststatus_attributes as $attrib) { $hoststatus[$attrib] = htmlentities(getParameter($object, $attrib), ENT_COMPAT); } $hoststatus['alerts'] = query_alerts ($status_file_content, $alert_type, $chost); if (!empty($host)) { $hosts_objects[$i] = $hoststatus; $i++; break; } } if (!empty($hoststatus)) { $hosts_objects[$i] = $hoststatus; $i++; } } /* echo "COUNT : " . count ($services_objects) . "\n"; */ return $hosts_objects; } /* Query Alerts */ function query_alerts ($status_file_content, $alert_type, $host) { $servicestatus_attributes = array ("service_description", "host_name", "current_attempt", "current_state", "plugin_output", "last_hard_state_change", "last_hard_state", "last_time_ok", "last_time_warning", "last_time_unknown", "last_time_critical", "is_flapping", "last_check"); $num_matches = preg_match_all("/servicestatus \{([\S\s]*?)\}/", $status_file_content, $matches, PREG_PATTERN_ORDER); #echo $matches[0][0] . ", " . $matches[0][1] . "\n"; #echo $matches[1][0] . ", " . $matches[1][1] . "\n"; $services_objects = array (); $i = 0; foreach ($matches[0] as $object) { $servicestatus = array (); switch ($alert_type) { case "all": if (empty($host) || getParameter($object, "host_name") == $host) { foreach ($servicestatus_attributes as $attrib) { $servicestatus[$attrib] = htmlentities(getParameter($object, $attrib), ENT_COMPAT); } $servicestatus['service_type'] = get_service_type($servicestatus['service_description']); $srv_desc = explode ("::",$servicestatus['service_description'],2); $servicestatus['service_description'] = $srv_desc[1]; } break; case "nok": if (getParameter($object, "last_hard_state") != ok && (empty($host) || getParameter($object, "host_name") == $host)) { foreach ($servicestatus_attributes as $attrib) { $servicestatus[$attrib] = htmlentities(getParameter($object, $attrib), ENT_COMPAT); } $servicestatus['service_type'] = get_service_type($servicestatus['service_description']); $srv_desc = explode ("::",$servicestatus['service_description'],2); $servicestatus['service_description'] = $srv_desc[1]; } break; case "ok": if (getParameter($object, "last_hard_state") == ok && (empty($host) || getParameter($object, "host_name") == $host)) { foreach ($servicestatus_attributes as $attrib) { $servicestatus[$attrib] = htmlentities(getParameter($object, $attrib), ENT_COMPAT); } $servicestatus['service_type'] = get_service_type($servicestatus['service_description']); $srv_desc = explode ("::",$servicestatus['service_description'],2); $servicestatus['service_description'] = $srv_desc[1]; } break; case "warn": if (getParameter($object, "last_hard_state") == warn && (empty($host) || getParameter($object, "host_name") == $host)) { foreach ($servicestatus_attributes as $attrib) { $servicestatus[$attrib] = htmlentities(getParameter($object, $attrib), ENT_COMPAT); } $servicestatus['service_type'] = get_service_type($servicestatus['service_description']); $srv_desc = explode ("::",$servicestatus['service_description'],2); $servicestatus['service_description'] = $srv_desc[1]; } break; case "critical": if (getParameter($object, "last_hard_state") == critical && (empty($host) || getParameter($object, "host_name") == $host)) { foreach ($servicestatus_attributes as $attrib) { $servicestatus[$attrib] = htmlentities(getParameter($object, $attrib), ENT_COMPAT); } $servicestatus['service_type'] = get_service_type($servicestatus['service_description']); $srv_desc = explode ("::",$servicestatus['service_description'],2); $servicestatus['service_description'] = $srv_desc[1]; } break; } if (!empty($servicestatus)) { $services_objects[$i] = $servicestatus; $i++; } } /* echo "COUNT : " . count ($services_objects) . "\n"; */ return $services_objects; } function get_service_type($service_description) { $pieces = explode("::", $service_description); switch ($pieces[0]) { case "DATANODE": case "NAMENODE": $pieces[0] = "HDFS"; break; case "JOBTRACKER": case "TASKTRACKER": $pieces[0] = "MAPREDUCE"; break; case "HBASEMASTER": case "REGIONSERVER": $pieces[0] = "HBASE"; break; case "HIVE-METASTORE": $pieces[0] = "HIVE"; break; case "ZKSERVERS": $pieces[0] = "ZOOKEEPER"; break; case "AMBARI": $pieces[0] = "AMBARI"; break; case "NAGIOS": case "HDFS": case "MAPREDUCE": case "HBASE": case "ZOOKEEPER": case "OOZIE": case "WEBHCAT": case "GANGLIA": case "PUPPET": 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; } ?>