From acb6c91ccd4d0e57beb3afda517f53ded39aab37 Mon Sep 17 00:00:00 2001 From: Bryan Heden Date: Thu, 5 Aug 2021 12:47:13 -0500 Subject: [PATCH] initial commit --- report_svc_status_lookup/index.php | 175 ++++++++++++++++++ .../report_svc_status_lookup.inc.php | 69 +++++++ 2 files changed, 244 insertions(+) create mode 100644 report_svc_status_lookup/index.php create mode 100644 report_svc_status_lookup/report_svc_status_lookup.inc.php diff --git a/report_svc_status_lookup/index.php b/report_svc_status_lookup/index.php new file mode 100644 index 0000000..5c64f64 --- /dev/null +++ b/report_svc_status_lookup/index.php @@ -0,0 +1,175 @@ + "Non-OK Services Report"), true); + echo ' +
+
+
+
Leave blank for all statuses
+
+ + +
+
+ + +
+ +
+
+
+
+ + '; + + $services = get_objects_services($status_array, $days); + 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 services that match listed statuses whose status has been that for $days days found!

"; + } +} + + +function replace_characters($str) +{ + $str = html_entity_decode($str, ENT_QUOTES); + $str = str_replace("'", "'", $str); + return $str; +} + + +function get_objects_services($statuses, $days, $return_headers = true) +{ + $arr = array(); + + if (!is_array($statuses)) { + echo "passed variable to get_objects_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 (days)", + ); + } + + $count = 0; + foreach ($services_xml->servicestatus as $svc) { + + $svc_arr = array(); + + if (!empty($statuses)) { + $found = false; + foreach ($statuses as $status) { + $status = replace_characters($status); + $svc_status = replace_characters((string) $svc->status_text); + if ($status == $svc_status) { + $found = true; + } + // echo "
";
+                // var_dump($status);
+                // var_dump($svc_status);
+                // echo "
"; + } + if ($found != true) { + 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($days) * 24 * 60 * 60)) { + continue; + } + + $svc_arr['duration'] = round(floatval((float) $svc_arr['duration'] / (float)(24 * 60 * 60)), 2); + $count++; + $arr[] = $svc_arr; + } + + if ($count == 0) { + return false; + } + return $arr; +} diff --git a/report_svc_status_lookup/report_svc_status_lookup.inc.php b/report_svc_status_lookup/report_svc_status_lookup.inc.php new file mode 100644 index 0000000..db4d22e --- /dev/null +++ b/report_svc_status_lookup/report_svc_status_lookup.inc.php @@ -0,0 +1,69 @@ +Error: This component requires Nagios XI 5.6.0 or later."; + } + + register_component("report_svc_status_lookup", array( + COMPONENT_NAME => "report_svc_status_lookup", + COMPONENT_AUTHOR => "hedenface", + COMPONENT_DESCRIPTION => $desc, + COMPONENT_TITLE => "Services Non-OK Report", + COMPONENT_VERSION => "0.0.0", + COMPONENT_DATE => "31/07/2021", + ) + ); + + if (!report_svc_status_lookup_check_version()) { + return; + } + + register_callback(CALLBACK_MENUS_INITIALIZED, 'report_svc_status_lookup_component_addmenu'); +} + + +function report_svc_status_lookup_check_version() +{ + if (function_exists('get_product_release')) { + if (get_product_release() >= 5600) { + return true; + } + } + + return false; +} + + +function report_svc_status_lookup_component_addmenu() +{ + $component_url = get_component_url_base("report_svc_status_lookup"); + + $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" => "Service Status Lookup", + "id" => "menu-report-svc-lookup", + "order" => $new_order, + "opts" => array( + "href" => $component_url . "/index.php", + ) + )); +}