commit eda2466c6e55c85f7096295cd9a72a8314acc1a5 Author: Bryan Heden Date: Thu Aug 5 12:46:41 2021 -0500 initial commit diff --git a/report_non_ok_svc/index.php b/report_non_ok_svc/index.php new file mode 100644 index 0000000..05884b8 --- /dev/null +++ b/report_non_ok_svc/index.php @@ -0,0 +1,169 @@ + "Non-OK Services Report"), true); + echo ' +
+
+
+
Leave blank for all services
+
+ + +
+
+ + +
+ +
+
+
+
+ + '; + + $services = get_objects_down_services($service_array, $hours); + if ($services != false) { + + echo ""; + + foreach ($services as $i => $service) { + $d = "d"; + if ($i == 0) { + $d = "h"; + } + echo "${service['host_name']}${service['host_address']}${service['svc_desc']}${service['svc_status']}${service['duration']}"; + } + + echo "
"; + } + else { + echo "

No Non-OK Services (for $hours hours or greater) found!

"; + } +} + + +function get_objects_down_services($services, $hours, $return_headers = true) +{ + $arr = array(); + + if (!is_array($services)) { + echo "passed variable to get_objects_down_services() must be an array of service names\n"; + return false; + } + + $services_xml = get_xml_service_status(array()); + + if (!$services_xml) { + echo "Something went wrong getting service status!\n"; + return false; + } + + if ($return_headers == true) { + $arr[] = array( + "host_name" => "Host Name", + "host_address" => "Host Address", + "svc_desc" => "Service Description", + "svc_status" => "Status Information", + "duration" => "Duration (hours)", + ); + } + + $count = 0; + foreach ($services_xml->servicestatus as $svc) { + + $svc_arr = array(); + + if ($svc->current_state == STATE_OK) { + continue; + } + if (!empty($services) && !in_array((string) $svc->name, $services)) { + continue; + } + + $svc_arr['host_name'] = (string) $svc->host_name; + $svc_arr['host_address'] = (string) $svc->host_address; + $svc_arr['svc_desc'] = (string) $svc->name; + $svc_arr['svc_status'] = (string) $svc->status_text; + + $state_change_time = strtotime($svc->last_state_change); + + // something went wrong with strtotime.. + // likely means it's just always been down i suspect + if ($state_change_time == false || $state_change_time == -1) { + $state_change_time = strtotime($svc->status_update_time); + + // something seriously wrong at this point.. + if ($state_change_time == false || $state_change_time == -1) { + continue; + } + } + + $svc_arr['duration'] = time() - $state_change_time; + + if ($svc_arr['duration'] < (floatval($hours) * 60 * 60 )) { + continue; + } + + $svc_arr['duration'] = round(floatval((float) $svc_arr['duration'] / (float)(60 * 60)), 2); + $count++; + $arr[] = $svc_arr; + } + + if ($count == 0) { + return false; + } + return $arr; +} diff --git a/report_non_ok_svc/report_non_ok_svc.inc.php b/report_non_ok_svc/report_non_ok_svc.inc.php new file mode 100644 index 0000000..ede4c8e --- /dev/null +++ b/report_non_ok_svc/report_non_ok_svc.inc.php @@ -0,0 +1,75 @@ +Error: This component requires Nagios XI 5.6.0 or later."; + } + + register_component("report_non_ok_svc", array( + COMPONENT_NAME => "report_non_ok_svc", + COMPONENT_AUTHOR => "hedenface", + COMPONENT_DESCRIPTION => $desc, + COMPONENT_TITLE => "Services Non-OK Report", + COMPONENT_VERSION => "0.0.0", + COMPONENT_DATE => "31/07/2021", + ) + ); + + if (!report_non_ok_svc_check_version()) { + return; + } + + register_callback(CALLBACK_MENUS_INITIALIZED, 'report_non_ok_svc_component_addmenu'); +} + + +function report_non_ok_svc_check_version() +{ + if (function_exists('get_product_release')) { + if (get_product_release() >= 5600) { + return true; + } + } + + return false; +} + + +function report_non_ok_svc_component_addmenu() +{ + $component_url = get_component_url_base("report_non_ok_svc"); + + $menu_section = find_menu_item(MENU_REPORTS, "menu-reports-nagiosxi", "id"); + if ($menu_section == null) { + return false; + } + + $order = grab_array_var($menu_section, "order", ""); + $new_order = $order + 0.1; + if ($new_order < 0) { + return false; + } + + add_menu_item(MENU_REPORTS, array( + "type" => "link", + "title" => "Non-OK Services", + "id" => "menu-report-non-ok", + "order" => $new_order, + "opts" => array( + "href" => $component_url . "/index.php", + ) + )); + + add_menu_item(MENU_REPORTS, array( + "type" => "linkspacer", + "id" => "menu-reports-non-ok-spacer", + "order" => $new_order + 0.1 + )); +}