From 5462099c20d25c88504720215262cc89323dba23 Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Sun, 11 Dec 2011 03:59:58 -0700 Subject: [PATCH] Initial commit --- ArduinoNagiosDisplay.pde | 182 +++++++++++++++++ nag.php | 418 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 600 insertions(+) create mode 100644 ArduinoNagiosDisplay.pde create mode 100644 nag.php diff --git a/ArduinoNagiosDisplay.pde b/ArduinoNagiosDisplay.pde new file mode 100644 index 0000000..92ebdfd --- /dev/null +++ b/ArduinoNagiosDisplay.pde @@ -0,0 +1,182 @@ +/* + * Arduino Nagios Display + * Copyright Will Bradley, 2011 + * Licensed under Creative Commons Attribution-ShareAlike (CC BY-SA) license. + * based on Arduino SimpleClient example + * and shiftOutCode, Hello World example by Carlyn Maw, Tom Igoe, and David A. Mellis. + * (no license information found in examples) + * + * This program connects to a matching script on your Nagios server + * (it must return a string like $00001000200002100000 -- see included php script.) + * + * YOU'LL NEED TO MODIFY the Local IP, Subnet, SSID, Security Type, + * Passphrase or WEP key, and the IP, port, hostname, and URL of Nagios. + * + * Pins 5,7,8: Latch, Clock, and Data pins for five daisy chained 595 Shift Registers + * Pins 8, 9, 10, 11, 12, 13: Normal WiShield or Diamondback pins + * + * If you want to change this script to something besides an 8x5 LED matrix, look at the magic numbers of 5, 7, and 8 in the printData function below. + */ + +//Pin connected to ST_CP of 74HC595 +int latchPin = 5; +//Pin connected to SH_CP of 74HC595 +int clockPin = 7; +////Pin connected to DS of 74HC595 +int dataPin = 6; + +#include + +#define WIRELESS_MODE_INFRA 1 +#define WIRELESS_MODE_ADHOC 2 + +// Wireless configuration parameters ---------------------------------------- +unsigned char local_ip[] = {192,168,1,3}; // IP address of WiShield +unsigned char gateway_ip[] = {192,168,1,1}; // router or gateway IP address +unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network +const prog_char ssid[] PROGMEM = {"MyNetworkName"}; // max 32 bytes + +unsigned char security_type = 0; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2 -- edit WEP or WPA pass/keys below accordingly + +// WPA/WPA2 passphrase +const prog_char security_passphrase[] PROGMEM = {"myWPApassword"}; // max 64 characters + +// WEP 128-bit keys +// sample HEX keys +prog_uchar wep_keys[] PROGMEM = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3 + }; + +// setup the wireless mode +// infrastructure - connect to AP +// adhoc - connect to another WiFi device +unsigned char wireless_mode = WIRELESS_MODE_INFRA; + +unsigned char ssid_len; +unsigned char security_passphrase_len; + +// IP Address for your Nagios server +uint8 ip[] = {192,168,1,2}; + +// Create request object; put Nagios' port, hostname, and URL path here. See the attached nag.php script. +GETrequest getData(ip, 80, "my-nagios-server.com", "/nag.php?minimal=1"); + + +// End of configuration parameters ---------------------------------------- + +void displayData(int numberToDisplay) { + // take the latchPin low so + // the LEDs don't change while you're sending in bits: + digitalWrite(latchPin, LOW); + // shift out the bits: + shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay); + + //take the latch pin high so the LEDs will light up: + digitalWrite(latchPin, HIGH); + // pause before next value: + delay(1); +} + +// Function that prints data from the server +void printData(char* data, int len) { + String matrix; + + digitalWrite(latchPin, LOW); // start writing to LEDs + + // Print the data returned by the server + // Note that the data is not null-terminated, may be broken up into smaller packets, and + // includes the HTTP header. + for(int i=0;i $val) + { + $key = xmlname($key); + $val = xmlstring($val); + $ret .= ' <'.$key.'>'.$val.''."\n"; + } + $ret .= ''."\n"; + } +*/ + // hosts + foreach($hosts as $hostName => $hostArray) + { + if($minimal) { + $ret .= $hostArray['current_state']; + } + else { + $ret .= "
".$hostArray['host_name']."
"; + $ret .= "
".$hostArray['current_state']."
"; + } +/* + $current_state = $hostArray['current_state']; + $ret .= ' '."\n"; + foreach($hostArray as $key => $val) + { + $key = xmlname($key); + $val = xmlstring($val); + $ret .= ' <'.$key.'>'.$val.''."\n"; + } + $ret .= ' '."\n"; +*/ + } + + // loop through the services + foreach ($services as $hostName => $service) + { + foreach ($service as $serviceDesc => $serviceArray) + { + if($minimal) { + $ret .= $serviceArray['current_state']; + } + else { + $ret .= "
".$serviceArray['host_name']." ".$serviceArray['service_description']."
"; + $ret .= "
".$serviceArray['current_state']."
"; + } + } + } + +if(!$minimal){ + $ret .= ""; +} +return $ret; +} + +// replace reserved characters in key for name +function xmlname($s) +{ + $s = str_replace("<", "<", $s); + $s = str_replace(">", ">", $s); + $s = str_replace("&", "&", $s); + return $s; +} + +function xmlstring($s) +{ + $s = str_replace("<", "<", $s); + $s = str_replace(">", ">", $s); + $s = str_replace("&", "&", $s); + return $s; +} + +// figure out what version the file is +function getFileVersion($statusFile) +{ + global $created_ts; + $version = 2; + + $fh = fopen($statusFile, 'r'); + $inInfo = false; + while($line = fgets($fh)) + { + if(trim($line) == "info {") + { + $inInfo = true; + } + elseif(trim($line) == "}") + { + $inInfo = false; + break; + } + + if($inInfo) + { + $vals = explode("=", $line); + if(trim($vals[0]) == "created") + { + $created = $vals[1]; + } + elseif(trim($vals[0]) == "version") + { + if(substr($vals[1], 0, 1) == "3") + { + $version = 3; + } + } + } + } + return $version; +} + +// parse nagios2 status.dat +function getData2($statusFile) +{ + // the keys to get from host status: + $host_keys = array('host_name', 'has_been_checked', 'check_execution_time', 'check_latency', 'check_type', 'current_state', 'current_attempt', 'state_type', 'last_state_change', 'last_time_up', 'last_time_down', 'last_time_unreachable', 'last_notification', 'next_notification', 'no_more_notifications', 'current_notification_number', 'notifications_enabled', 'problem_has_been_acknowledged', 'acknowledgement_type', 'active_checks_enabled', 'passive_checks_enabled', 'last_update'); + + // keys to get from service status: + $service_keys = array('host_name', 'service_description', 'has_been_checked', 'check_execution_time', 'check_latency', 'current_state', 'state_type', 'last_state_change', 'last_time_ok', 'last_time_warning', 'last_time_unknown', 'last_time_critical', 'plugin_output', 'last_check', 'notifications_enabled', 'active_checks_enabled', 'passive_checks_enabled', 'problem_has_been_acknowledged', 'acknowledgement_type', 'last_update', 'is_flapping'); + + # open the file + $fh = fopen($statusFile, 'r'); + + # variables to keep state + $inSection = false; + $sectionType = ""; + $lineNum = 0; + $sectionData = array(); + + $hostStatus = array(); + $serviceStatus = array(); + + #variables for total hosts and services + $typeTotals = array(); + + # loop through the file + while($line = fgets($fh)) + { + $lineNum++; // increment counter of line number, mainly for debugging + $line = trim($line); // strip whitespace + if($line == ""){ continue;} // ignore blank line + if(substr($line, 0, 1) == "#"){ continue;} // ignore comment + + // ok, now we need to deal with the sections + + if(! $inSection) + { + // we're not currently in a section, but are looking to start one + if(strstr($line, " ") && (substr($line, -1) == "{")) // space and ending with {, so it's a section header + { + $sectionType = substr($line, 0, strpos($line, " ")); // first word on line is type + $inSection = true; + // we're now in a section + $sectionData = array(); + + // increment the counter for this sectionType + if(isset($typeTotals[$sectionType])){$typeTotals[$sectionType]=$typeTotals[$sectionType]+1;}else{$typeTotals[$sectionType]=1;} + + } + } + + if($inSection && $line == "}") // closing a section + { + if($sectionType == "service") + { + $serviceStatus[$sectionData['host_name']][$sectionData['service_description']] = $sectionData; + } + if($sectionType == "host") + { + $hostStatus[$sectionData["host_name"]] = $sectionData; + } + $inSection = false; + $sectionType = ""; + continue; + } + else + { + // we're currently in a section, and this line is part of it + $lineKey = substr($line, 0, strpos($line, "=")); + $lineVal = substr($line, strpos($line, "=")+1); + + // add to the array as appropriate + if($sectionType == "service") + { + if(in_array($lineKey, $service_keys)) + { + $sectionData[$lineKey] = $lineVal; + } + } + elseif($sectionType == "host") + { + if(in_array($lineKey, $host_keys)) + { + $sectionData[$lineKey] = $lineVal; + } + } + // else continue on, ignore this section, don't save anything + } + + } + + fclose($fh); + + $retArray = array("hosts" => $hostStatus, "services" => $serviceStatus); + + return $retArray; +} + +// parse nagios3 status.dat +function getData3($statusFile) +{ + global $debug; + // the keys to get from host status: + $host_keys = array('host_name', 'modified_attributes', 'check_command', 'check_period', 'notification_period', 'check_interval', 'retry_interval', 'event_handler', 'has_been_checked', 'should_be_scheduled', 'check_execution_time', 'check_latency', 'check_type', 'current_state', 'last_hard_state', 'last_event_id', 'current_event_id', 'current_problem_id', 'last_problem_id', 'plugin_output', 'long_plugin_output', 'performance_data', 'last_check', 'next_check', 'check_options', 'current_attempt', 'max_attempts', 'state_type', 'last_state_change', 'last_hard_state_change', 'last_time_up', 'last_time_down', 'last_time_unreachable', 'last_notification', 'next_notification', 'no_more_notifications', 'current_notification_number', 'current_notification_id', 'notifications_enabled', 'problem_has_been_acknowledged', 'acknowledgement_type', 'active_checks_enabled', 'passive_checks_enabled', 'event_handler_enabled', 'flap_detection_enabled', 'failure_prediction_enabled', 'process_performance_data', 'obsess_over_host', 'last_update', 'is_flapping', 'percent_state_change', 'scheduled_downtime_depth'); + // keys to get from service status: + $service_keys = array('host_name', 'service_description', 'modified_attributes', 'check_command', 'check_period', 'notification_period', 'check_interval', 'retry_interval', 'event_handler', 'has_been_checked', 'should_be_scheduled', 'check_execution_time', 'check_latency', 'check_type', 'current_state', 'last_hard_state', 'last_event_id', 'current_event_id', 'current_problem_id', 'last_problem_id', 'current_attempt', 'max_attempts', 'state_type', 'last_state_change', 'last_hard_state_change', 'last_time_ok', 'last_time_warning', 'last_time_unknown', 'last_time_critical', 'plugin_output', 'long_plugin_output', 'performance_data', 'last_check', 'next_check', 'check_options', 'current_notification_number', 'current_notification_id', 'last_notification', 'next_notification', 'no_more_notifications', 'notifications_enabled', 'active_checks_enabled', 'passive_checks_enabled', 'event_handler_enabled', 'problem_has_been_acknowledged', 'acknowledgement_type', 'flap_detection_enabled', 'failure_prediction_enabled', 'process_performance_data', 'obsess_over_service', 'last_update', 'is_flapping', 'percent_state_change', 'scheduled_downtime_depth'); + + # open the file + $fh = fopen($statusFile, 'r'); + + # variables to keep state + $inSection = false; + $sectionType = ""; + $lineNum = 0; + $sectionData = array(); + + $hostStatus = array(); + $serviceStatus = array(); + $programStatus = array(); + + #variables for total hosts and services + $typeTotals = array(); + + # loop through the file + while($line = fgets($fh)) + { + $lineNum++; // increment counter of line number, mainly for debugging + $line = trim($line); // strip whitespace + if($line == ""){ continue;} // ignore blank line + if(substr($line, 0, 1) == "#"){ continue;} // ignore comment + + // ok, now we need to deal with the sections + if(! $inSection) + { + // we're not currently in a section, but are looking to start one + if(substr($line, strlen($line)-1, 1) == "{") // space and ending with {, so it's a section header + { + $sectionType = substr($line, 0, strpos($line, " ")); // first word on line is type + $inSection = true; + // we're now in a section + $sectionData = array(); + + // increment the counter for this sectionType + if(isset($typeTotals[$sectionType])){$typeTotals[$sectionType]=$typeTotals[$sectionType]+1;}else{$typeTotals[$sectionType]=1;} + + } + } + elseif($inSection && trim($line) == "}") // closing a section + { + if($sectionType == "servicestatus") + { + $serviceStatus[$sectionData['host_name']][$sectionData['service_description']] = $sectionData; + } + elseif($sectionType == "hoststatus") + { + $hostStatus[$sectionData["host_name"]] = $sectionData; + } + elseif($sectionType == "programstatus") + { + $programStatus = $sectionData; + } + $inSection = false; + $sectionType = ""; + continue; + } + else + { + // we're currently in a section, and this line is part of it + $lineKey = substr($line, 0, strpos($line, "=")); + $lineVal = substr($line, strpos($line, "=")+1); + + // add to the array as appropriate + if($sectionType == "servicestatus" || $sectionType == "hoststatus" || $sectionType == "programstatus") + { + if($debug){ echo "LINE ".$lineNum.": lineKey=".$lineKey."= lineVal=".$lineVal."=\n";} + $sectionData[$lineKey] = $lineVal; + } + // else continue on, ignore this section, don't save anything + } + + } + + fclose($fh); + + $retArray = array("hosts" => $hostStatus, "services" => $serviceStatus, "program" => $programStatus); + return $retArray; +} + + +// this formats the age of a check in seconds into a nice textual description +function ageString($seconds) +{ + $age = ""; + if($seconds > 86400) + { + $days = (int)($seconds / 86400); + $seconds = $seconds - ($days * 86400); + $age .= $days." days "; + } + if($seconds > 3600) + { + $hours = (int)($seconds / 3600); + $seconds = $seconds - ($hours * 3600); + $age .= $hours." hours "; + } + if($seconds > 60) + { + $minutes = (int)($seconds / 60); + $seconds = $seconds - ($minutes * 60); + $age .= $minutes." minutes "; + } + $age .= $seconds." seconds "; + return $age; +} + + +/* + // the keys to get from host status: + $host_keys = array('host_name', 'has_been_checked', 'check_execution_time', 'check_latency', 'check_type', 'current_state', 'current_attempt', 'state_type', 'last_state_change', 'last_time_up', 'last_time_down', 'last_time_unreachable', 'last_notification', 'next_notification', 'no_more_notifications', 'current_notification_number', 'notifications_enabled', 'problem_has_been_acknowledged', 'acknowledgement_type', 'active_checks_enabled', 'passive_checks_enabled', 'last_update'); + + // keys to get from service status: + $service_keys = array('host_name', 'service_description', 'has_been_checked', 'check_execution_time', 'check_latency', 'current_state', 'state_type', 'last_state_change', 'last_time_ok', 'last_time_warning', 'last_time_unknown', 'last_time_critical', 'plugin_output', 'last_check', 'notifications_enabled', 'active_checks_enabled', 'passive_checks_enabled', 'problem_has_been_acknowledged', 'acknowledgement_type', 'last_update', 'is_flapping'); + +*/ + + +?>