Initial commit

This commit is contained in:
Will Bradley 2011-12-11 03:59:58 -07:00
commit 5462099c20
2 changed files with 600 additions and 0 deletions

182
ArduinoNagiosDisplay.pde Normal file
View File

@ -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 <WiServer.h>
#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<len;i++) {
if(data[i] == '$')
{
for(int rows=0;rows<5;rows++){
int rowData = 0;
for(int cols=0;cols<8;cols++){
i++;
char thisData = data[i];
// a 0 in Nagios means the LED should be on
if(thisData == '0') {
// the absolute value of cols-7 is used so binary 10000000 shows up as 128 instead of 1 (left light on, not right)
rowData += (1<<abs(cols-7));
Serial.print((1<<abs(cols-7)));
Serial.print("+");
}
if(thisData == '1') {
// any other value means the LED should be off
Serial.print("0");
Serial.print("+");
}
if(thisData == '2') {
Serial.print("0");
Serial.print("+");
}
matrix += thisData;
}
shiftOut(dataPin, clockPin, MSBFIRST, rowData); // write to LEDs
Serial.print("=");
Serial.println(rowData);
}
Serial.println('*');
Serial.println(matrix);
digitalWrite(latchPin, HIGH); // stop writing to LEDs
}
}
}
void setup() {
// Initialize WiServer (we'll pass NULL for the page serving function since we don't need to serve web pages)
WiServer.init(NULL);
// Enable Serial output and ask WiServer to generate log messages (optional)
Serial.begin(57600);
WiServer.enableVerboseMode(true);
// Have the processData function called when data is returned by the server
getData.setReturnFunc(printData);
//set pins to output so you can control the shift register
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
// flash the display and clear everything upon startup
displayData(0);
displayData(0);
displayData(0);
displayData(0);
displayData(0);
delay(100);
displayData(255);
displayData(255);
displayData(255);
displayData(255);
displayData(255);
delay(100);
displayData(0);
displayData(0);
displayData(0);
displayData(0);
displayData(0);
}
// Time (in millis) when the data should be retrieved
long updateTime = 0;
void loop(){
// Check if it's time to get an update
if (millis() >= updateTime) {
getData.submit();
// Get an update every 30 seconds
updateTime += 1000 * 30;
}
// Run WiServer
WiServer.server_task();
delay(10);
}

418
nag.php Normal file
View File

@ -0,0 +1,418 @@
<?php
// nag.php by Will Bradley, www.zyphon.com/arduino-nagios
// Script to parse Nagios status.dat and present it in simplified HTML.
// usage: nag.php for human view, nag.php?minimal=1 for computer view.
//
// Based on statusXML.php by Jason Antman
//
// +----------------------------------------------------------------------+
// | PHP EMS Tools http://www.php-ems-tools.com |
// +----------------------------------------------------------------------+
// | Copyright (c) 2006, 2007 Jason Antman. |
// | |
// | This program is free software; you can redistribute it and/or modify |
// | it under the terms of the GNU General Public License as published by |
// | the Free Software Foundation; either version 3 of the License, or |
// | (at your option) any later version. |
// | |
// | This program is distributed in the hope that it will be useful, |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
// | GNU General Public License for more details. |
// | |
// | You should have received a copy of the GNU General Public License |
// | along with this program; if not, write to: |
// | |
// | Free Software Foundation, Inc. |
// | 59 Temple Place - Suite 330 |
// | Boston, MA 02111-1307, USA. |
// +----------------------------------------------------------------------+
// | Authors: Jason Antman <jason@jasonantman.com> |
// +----------------------------------------------------------------------+
// $Id: nag.php,v 1.0 2011/12/11 03:76:14 wbradley Exp $
// $Source: ArduinoNagiosDisplay/nag.php,v $
// change the statusFile to the location of status.dat on your Nagios system
$statusFile = "/var/cache/nagios3/status.dat";
$nag_version = getFileVersion($statusFile); // returns integer 2 or 3
$created_ts = 0;
$debug = false;
if($nag_version == 3)
{
$data = getData3($statusFile); // returns an array
}
else
{
$data = getData2($statusFile); // returns an array
}
$hosts = $data['hosts'];
$services = $data['services'];
$program = "";
if(array_key_exists("program", $data))
{
$program = $data['program'];
}
echo outputHTML($hosts, $services, $program);
function outputHTML($hosts, $services, $program)
{
if($_GET["minimal"] == "1") {
$minimal = true;
}
$ret = "";
// begin outputting XML
//header("Content-type: text/xml");
if($minimal){
$ret .= "$";
}
else {
$ret .= "<html>"."\n";
$ret .= '<head><style type="text/css">body {font:15px arial,sans-serif; } dt { font-weight: bold; float: left; margin-right: 1em; }</style></head>'."\n";
$ret .= "<body>"."\n";
}
// program status
/*
if($program != "")
{
$ret .= '<programStatus>'."\n";
foreach($program as $key => $val)
{
$key = xmlname($key);
$val = xmlstring($val);
$ret .= ' <'.$key.'>'.$val.'</'.$key.'>'."\n";
}
$ret .= '</programStatus>'."\n";
}
*/
// hosts
foreach($hosts as $hostName => $hostArray)
{
if($minimal) {
$ret .= $hostArray['current_state'];
}
else {
$ret .= "<dt>".$hostArray['host_name']."</dt>";
$ret .= "<dd>".$hostArray['current_state']."</dd>";
}
/*
$current_state = $hostArray['current_state'];
$ret .= ' <host>'."\n";
foreach($hostArray as $key => $val)
{
$key = xmlname($key);
$val = xmlstring($val);
$ret .= ' <'.$key.'>'.$val.'</'.$key.'>'."\n";
}
$ret .= ' </host>'."\n";
*/
}
// loop through the services
foreach ($services as $hostName => $service)
{
foreach ($service as $serviceDesc => $serviceArray)
{
if($minimal) {
$ret .= $serviceArray['current_state'];
}
else {
$ret .= "<dt>".$serviceArray['host_name']." ".$serviceArray['service_description']."</dt>";
$ret .= "<dd>".$serviceArray['current_state']."</dd>";
}
}
}
if(!$minimal){
$ret .= "</body></html>";
}
return $ret;
}
// replace reserved characters in key for name
function xmlname($s)
{
$s = str_replace("<", "&lt;", $s);
$s = str_replace(">", "&gt;", $s);
$s = str_replace("&", "&amp;", $s);
return $s;
}
function xmlstring($s)
{
$s = str_replace("<", "&lt;", $s);
$s = str_replace(">", "&gt;", $s);
$s = str_replace("&", "&amp;", $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');
*/
?>