initial commit

master
Bryan Heden 4 years ago
commit eda2466c6e

@ -0,0 +1,169 @@
<?php
require_once(dirname(__FILE__) . "/../../common.inc.php");
pre_init();
init_session();
grab_request_vars();
check_prereqs();
check_authentication(false);
generate_report();
function generate_report()
{
/*
The first is a report that would show the following services, if any of them have been in non-OK state for more than 24 hours:
Day Mode
Night Mode
Light Mode Day
Light Mode Twilight
Light Mode Night
The report would show:
Host Name, Host Address, Service Description, Status Information, and Duration (hours)
*/
$default_services = "
Day Mode
Night Mode
Light Mode Day
Light Mode Twilight
Light Mode Night
";
$default_hours = 24;
$services = grab_request_var("services", $default_services);
$hours = floatval(grab_request_var("hours", $default_hours));
if ($hours <= 0) {
$hours = 1;
}
$service_array = explode("\n", $services);
$service_array = array_map(function($el) { return trim($el); }, $service_array);
$service_array = array_filter($service_array, function($el) { return !empty($el); });
do_page_start(array("page_title" => "Non-OK Services Report"), true);
echo '
<form method="get">
<div class="well report-options form-inline">
<div class="reportoptionpicker">
<div class="subtext">Leave blank for all services</div>
<div class="input-group" style="margin-right: 10px;">
<label class="input-group-addon">Services to Check</label>
<textarea class="form-control" name="services">'. implode("\n", $service_array) .'</textarea>
</div>
<div class="input-group" style="margin-right: 10px;">
<label class="input-group-addon">Services non-OK time (hours)</label>
<input type="text" class="form-control" name="hours" value="' . $hours . '" style="width: 50px;">
</div>
<button type="submit" id="run" class="btn btn-sm btn-primary" name="reportbtn">Run</button>
<div style="clear: both;"></div>
</div>
</div>
</form>
<style>
th,td {
padding: 2px 10px;
}
textarea {
width: 400px;
height: 7em !important;
}
</style>
';
$services = get_objects_down_services($service_array, $hours);
if ($services != false) {
echo "<table>";
foreach ($services as $i => $service) {
$d = "d";
if ($i == 0) {
$d = "h";
}
echo "<tr><t$d>${service['host_name']}</t$d><t$d>${service['host_address']}</t$d><t$d>${service['svc_desc']}</t$d><t$d>${service['svc_status']}</t$d><t$d>${service['duration']}</t$d></tr>";
}
echo "</table>";
}
else {
echo "<p>No Non-OK Services (for $hours hours or greater) found!</p>";
}
}
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;
}

@ -0,0 +1,75 @@
<?php
require_once(dirname(__FILE__) . '/../componenthelper.inc.php');
report_non_ok_svc_init();
function report_non_ok_svc_init()
{
$desc = "Report on specified services in non-OK state";
if (!report_non_ok_svc_check_version()) {
$desc .= " <strong>Error: This component requires Nagios XI 5.6.0 or later.</strong>";
}
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
));
}
Loading…
Cancel
Save