From 7d001b71640c91a9b02c6aca6dab2c248acb1633 Mon Sep 17 00:00:00 2001 From: Bryan Heden Date: Mon, 22 May 2023 12:16:07 -0500 Subject: [PATCH] initial release --- CHANGELOG.md | 3 + README.md | 15 + index.php | 75 +++++ report_notifications_disabled.inc.php | 425 ++++++++++++++++++++++++++ 4 files changed, 518 insertions(+) create mode 100644 CHANGELOG.md create mode 100644 index.php create mode 100644 report_notifications_disabled.inc.php diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..72ba701 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +# 1.0.0 + +* Initial release diff --git a/README.md b/README.md index f6515ed..7e6f9ad 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,17 @@ # report_notifications_disabled +> A simple report that prints tables with Hosts and Services with their notifications disabled. + +## Information + +You can find a link to the report under "Available Reports" section on the "Reports" tab. + +There is a configurable option to ignore specific hosts and services. By default, it ignores the following services: + +* Night Mode +* Day Mode +* Light Mode - Day +* Light Mode - Night +* Light Mode - Twilight + +But, you can add hosts and services to the list if you'd like to ignore additional resources. The configuration option is under the "Admin" tab, in the "System Extensions" link section, under "Manage Components". diff --git a/index.php b/index.php new file mode 100644 index 0000000..d76ef13 --- /dev/null +++ b/index.php @@ -0,0 +1,75 @@ + "Notifications Disabled Report"), true); + + echo "
Disabled Hosts
"; + echo report_notifications_disabled_get_host_table($hosts); + + echo "
"; + + echo "
Disabled Services
"; + echo report_notifications_disabled_get_service_table($services); +} + +function report_notifications_disabled_get_host_table($data) +{ + $header_row = ""; + $header_row .= "Host"; + $header_row .= "User"; + $header_row .= "When"; + $header_row .= ""; + + return "" . $header_row . report_notifications_disabled_get_rows($data) . "
"; +} + +function report_notifications_disabled_get_service_table($data) +{ + $header_row = ""; + $header_row .= "Host"; + $header_row .= "Service"; + $header_row .= "User"; + $header_row .= "When"; + $header_row .= ""; + + return "" . $header_row . report_notifications_disabled_get_rows($data) . "
"; +} + +function report_notifications_disabled_get_rows($data) +{ + $rows = ""; + foreach ($data as $row_data) { + $rows .= report_notifications_disabled_get_row($row_data); + } + return $rows; +} + +function report_notifications_disabled_get_row($row_data) +{ + $row = ""; + $row .= "" . $row_data["host_name"] . ""; + + if (!empty($row_data["service_description"])) { + $row .= "" . $row_data["service_description"] . ""; + } + + $row .= "" . $row_data["user"] . ""; + $row .= "" . $row_data["when"] . ""; + + return $row; +} diff --git a/report_notifications_disabled.inc.php b/report_notifications_disabled.inc.php new file mode 100644 index 0000000..e906168 --- /dev/null +++ b/report_notifications_disabled.inc.php @@ -0,0 +1,425 @@ +Error: This component requires Nagios XI 5.6.0 or later."; + } + + register_component("report_notifications_disabled", array( + COMPONENT_NAME => "report_notifications_disabled", + COMPONENT_AUTHOR => "hedenface", + COMPONENT_DESCRIPTION => $desc, + COMPONENT_TITLE => "Notifications Disabled Report", + COMPONENT_VERSION => "1.0.0", + COMPONENT_DATE => "05/22/2023", + COMPONENT_CONFIGFUNCTION => "report_notifications_disabled_config", + ) + ); + + if (!report_notifications_disabled_check_version()) { + return; + } + + register_callback(CALLBACK_MENUS_INITIALIZED, 'report_notifications_disabled_component_addmenu'); +} + +function report_notifications_disabled_check_version() +{ + if (function_exists('get_product_release')) { + if (get_product_release() >= 5600) { + return true; + } + } + + return false; +} + +function report_notifications_disabled_component_addmenu() +{ + $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" => "Notifications Disabled", + "id" => "menu-reports-nagiosxi", + "order" => $new_order, + "opts" => array( + "href" => get_component_url_base("report_notifications_disabled") . "/index.php", + ) + )); + + add_menu_item(MENU_REPORTS, array( + "type" => "linkspacer", + "id" => "menu-reports-notifications-disabled-report-spacer", + "order" => $new_order + 0.1 + )); +} + +/**** + Component Configuration Functions + ****/ + +function report_notifications_disabled_config($mode = "", $inargs, &$outargs, &$result) +{ + $result = 0; + $output = ""; + + switch ($mode) { + + case COMPONENT_CONFIGMODE_GETSETTINGSHTML: + + $settings_raw = get_option("report_notifications_disabled_options"); + if ($settings_raw == "") { + $settings = array(); + } + else { + $settings = unserialize($settings_raw); + } + + // initial values + $ignore_host_list = grab_array_var($settings, "ignore_host_list", report_notifications_disabled_default_hosts_to_ignore()); + $ignore_service_list = grab_array_var($settings, "ignore_service_list", report_notifications_disabled_default_services_to_ignore()); + + // just to make sure.. + if (empty($ignore_host_list)) { + $ignore_host_list = report_notifications_disabled_default_hosts_to_ignore(); + } + if (empty($ignore_service_list)) { + $ignore_service_list = report_notifications_disabled_default_services_to_ignore(); + } + + // values passed to us + $ignore_host_list = grab_array_var($inargs, "ignore_host_list", $ignore_host_list); + $ignore_service_list = grab_array_var($inargs, "ignore_service_list", $ignore_service_list); + + $output = ' +

Use the Notifications Disabled Report configuration to adjust specific Services to ignore

+
Notifications Disabled Report
+ + + + + + + + + +
+ + + +
+ + + +
'; + + break; + + case COMPONENT_CONFIGMODE_SAVESETTINGS: + + // get variables + $ignore_host_list = grab_array_var($inargs, "ignore_host_list", report_notifications_disabled_default_hosts_to_ignore()); + $ignore_service_list = grab_array_var($inargs, "ignore_service_list", report_notifications_disabled_default_services_to_ignore()); + + // just to make sure.. + if (empty($ignore_host_list)) { + $ignore_host_list = report_notifications_disabled_default_hosts_to_ignore(); + } + if (empty($ignore_service_list)) { + $ignore_service_list = report_notifications_disabled_default_services_to_ignore(); + } + + // save settings + $settings = array( + "ignore_host_list" => $ignore_host_list, + "ignore_service_list" => $ignore_service_list, + ); + set_option("report_notifications_disabled_options", serialize($settings)); + + break; + + default: + break; + } + + return $output; +} + +function report_notifications_ignore_host_list() +{ + $settings_raw = get_option("report_notifications_disabled_options"); + if ($settings_raw == "") { + $settings = array(); + } + else { + $settings = unserialize($settings_raw); + } + + $ignore_host_list = grab_array_var($settings, "ignore_host_list", report_notifications_disabled_default_hosts_to_ignore()); + + if (empty($ignore_host_list)) { + $ignore_host_list = report_notifications_disabled_default_hosts_to_ignore(); + } + + return $ignore_host_list; +} + +function report_notifications_ignore_service_list() +{ + $settings_raw = get_option("report_notifications_disabled_options"); + if ($settings_raw == "") { + $settings = array(); + } + else { + $settings = unserialize($settings_raw); + } + + $ignore_service_list = grab_array_var($settings, "ignore_service_list", report_notifications_disabled_default_services_to_ignore()); + + if (empty($ignore_service_list)) { + $ignore_service_list = report_notifications_disabled_default_services_to_ignore(); + } + + return $ignore_service_list; +} + +/**** + Report Functions + ****/ + +function report_notifications_disabled_get_hosts() +{ + $all_hosts = get_data_host_status(array("notifications_enabled" => 0)); + $hosts = array(); + + foreach ($all_hosts as $host) { + if (in_array($host["host_name"], split(",", report_notifications_ignore_host_list()))) { + continue; + } + + $audit = report_notifications_disabled_search_audit_logs(REPORT_NOTIFICATIONS_DISABLED_HOW_FAR_BACK_IN_DAYS, $host["host_name"]); + if ($audit === false || count($audit) == 0) { + $user = "No user found"; + $when = ""; + } else { + $user = $audit["user"]; + $when = $audit["log_time"]; + } + + $hosts[] = array( + "host_name" => $host["host_name"], + "user" => $user, + "when" => $when, + ); + } + + return $hosts; +} + +function report_notifications_disabled_get_services() +{ + $all_services = get_data_service_status(array("notifications_enabled" => 0)); + $services = array(); + + foreach ($all_services as $service) { + if (in_array($service["host_name"], split(",", report_notifications_ignore_host_list()))) { + continue; + } + if (in_array($service["service_description"], split(",", report_notifications_ignore_service_list()))) { + continue; + } + + $audit = report_notifications_disabled_search_audit_logs(REPORT_NOTIFICATIONS_DISABLED_HOW_FAR_BACK_IN_DAYS, $service["host_name"], $service["service_description"]); + if ($audit === false || count($audit) == 0) { + $user = "No user found"; + $when = ""; + } else { + $user = $audit["user"]; + $when = $audit["log_time"]; + } + + $services[] = array( + "host_name" => $service["host_name"], + "service_description" => $service["service_description"], + "user" => $user, + "when" => $when, + ); + } + + return $services; +} + +function report_notifications_disabled_search_audit_logs($how_far_back_in_days, $host_name, $service_description = null) +{ + // we start with the last 24 hours, then go back as far as specified in the function call + $starttime = time() - (24 * 60 * 60); + $endtime = time(); + + for ($i = 0; $i < $how_far_back_in_days; $i++) { + if (empty($service_description) == true) { + $audit_logs = report_notifications_disabled_get_auditlog(array( + "starttime" => $starttime, + "endtime" => $endtime, + "type" => AUDITLOGTYPE_INFO, + "message" => "lk:Submitted a command to Nagios Core: DISABLE_HOST_NOTIFICATIONS;$host_name", + )); + } else { + $audit_logs = report_notifications_disabled_get_auditlog(array( + "starttime" => $starttime, + "endtime" => $endtime, + "type" => AUDITLOGTYPE_INFO, + "message" => "lk:Submitted a command to Nagios Core: DISABLE_SVC_NOTIFICATIONS;$host_name;$service_description", + )); + } + + // we want the latest + if (count($audit_logs) >= 1) { + return $audit_logs[0]; + } + + // otherwise, we adjust starttime and endtime + $starttime -= (24 * 60 * 60); + $endtime -= (24 * 60 * 60); + } + + // if we have nothing, return false + return false; +} + +// taken from get_auditlog_xml_output (without the xml part, along with some adjustments) +// no functionality exists natively for returning auditlog as an array +function report_notifications_disabled_get_auditlog($request_args) +{ + global $sqlquery; + global $db_tables; + global $request; + + // WE HAVE TO MUNGE THINGS HERE BECAUSE THE TIMESTAMPS CONTAINS COLONS AND WILL GET MESSED UP BY THE NORMAL SQL LIMITING LOGIC... + $sqlmods = ""; + // by default, only show last 24 hours worth of logs + $starttime = grab_array_var($request_args, "starttime", time() - (24 * 60 * 60)); + unset($request_args["starttime"]); + $sqlmods .= " AND " . $db_tables[DB_NAGIOSXI]["auditlog"] . ".log_time >= '" . get_datetime_string($starttime, DT_SQL_DATE_TIME) . "'"; + // optional end time + $endtime = grab_array_var($request_args, "endtime", ""); + if ($endtime != "") { + $sqlmods .= " AND " . $db_tables[DB_NAGIOSXI]["auditlog"] . ".log_time <= '" . get_datetime_string($endtime, DT_SQL_DATE_TIME) . "'"; + } + + $audit_log = array(); + + // generate query + $fieldmap = array( + "auditlog_id" => $db_tables[DB_NAGIOSXI]["auditlog"] . ".auditlog_id", + "log_time" => $db_tables[DB_NAGIOSXI]["auditlog"] . ".log_time", + "source" => $db_tables[DB_NAGIOSXI]["auditlog"] . ".source", + "user" => $db_tables[DB_NAGIOSXI]["auditlog"] . ".user", + "type" => $db_tables[DB_NAGIOSXI]["auditlog"] . ".type", + "message" => $db_tables[DB_NAGIOSXI]["auditlog"] . ".message", + "ip_address" => $db_tables[DB_NAGIOSXI]["auditlog"] . ".ip_address", + "details" => $db_tables[DB_NAGIOSXI]["auditlog"] . ".details", + ); + $args = array( + "sql" => $sqlquery['GetAuditLog'] . $sqlmods, + "fieldmap" => $fieldmap, + "useropts" => $request_args, // FOR NON-BACKEND-CALLS + ); + $sql = generate_sql_query(DB_NAGIOSXI, $args); + + if (!($rs = exec_sql_query(DB_NAGIOSXI, $sql))) { + handle_backend_db_error(DB_NAGIOSXI); + } else { + while (!$rs->EOF) { + + $typestr = intval($rs->fields["type"]); + switch ($typestr) { + case AUDITLOGTYPE_ADD: + $typestr = "ADD"; + break; + case AUDITLOGTYPE_DELETE: + $typestr = "DELETE"; + break; + case AUDITLOGTYPE_MODIFY: + $typestr = "MODIFY"; + break; + case AUDITLOGTYPE_CHANGE: + $typestr = "CHANGE"; + break; + case AUDITLOGTYPE_SECURITY: + $typestr = "SECURITY"; + break; + case AUDITLOGTYPE_INFO: + $typestr = "INFO"; + break; + case AUDITLOGTYPE_OTHER: + $typestr = "OTHER"; + break; + default: + break; + } + + $audit_log[] = array( + "id" => get_xml_db_field_val($rs, "auditlog_id"), + "log_time" => get_xml_db_field_val($rs, "log_time"), + "source" => get_xml_db_field_val($rs, "source"), + "user" => get_xml_db_field_val($rs, "user"), + "type" => get_xml_db_field_val($rs, "type"), + "typestr" => $typestr, + "message" => get_xml_db_field_val($rs, "message"), + "details" => get_xml_db_field_val($rs, "details"), + "ip_address" => get_xml_db_field_val($rs, "ip_address"), + ); + + $rs->MoveNext(); + } + } + + return $audit_log; +}