IAccess for Nagios Support Page
Welcome to the support page for the iphone app IAccess for Nagios
Starting with version 1.1 of IAccess for Nagios, it is possibly to integrate external Nagios data into the app.
There are two ways to do this:
1. Poll device-specific messages from an external host
and/or
2. Poll the Nagios server state (for up to 4 Nagios systems)
Both methods 1. and 2. are optional.
If you want to use either of these methods, you need to setup a script for each method, ususally on your Nagios server.
Here is a very short HOWTO and examples on what these scripts could look like and how this could be done.
Please note that the php scripts in the examples below are just that, examples of how it can be done. It is up to you to implement them in your favorite programming language, and to do it in a safe manner.
Example 1- polling messages from the iPhone
Please note, https access for polling the messages, requires a valid SSL certificate on the server.
Step 1: Enter the URL to your script in IAccess for Nagios
Enter the URL under "Optional URL to poll device messages" in the IAccess settings
Use this syntax:
your-nagios-host-name-and-domain/getmsg.php
(getmsg.php being the name of your server-side script of course)
In case you intend to use authentication, simply prepend your username and password like this:
your-nagios-host-name-and-domain/getmsg.php
Step 2: Prepare the corresponding script on the server
IAccess calls the script with the iPhones uniqueIdentifier, aka deviceID as a parameter. Using this identifier, you can for example poll up to 10 device messages from a mysql database and return them to the app.
Please note that the parameter passed to the script was called "devicetoken" in Version 1.1 of IAccess. This has been changed to "deviceID" in Version 1.2 of IAccess.
<?php
// example getmsg.php script.
// script is called with the deviceID as parameter
$uniqueIdentifier = $_GET['deviceID'];
if ($uniqueIdentifier==NULL) { die ("");}
$dbusername="dbuser";
$dbpassword="dbpassword";
$database="my_database";
$dbhandle = mysql_connect("localhost", $dbusername, $dbpassword);
if ($dbhandle==NULL) {
echo date('Y-m-d H:i - ') . "Connection Failure to mysql database";
exit;
}
$result=mysql_select_db($database);
if ($result==NULL) {
echo date('Y-m-d H:i - ') . ("mysql database not found.");
exit;
}
$query="select message from messages where uniqueIdentifier='$uniqueIdentifier' order by timestamp desc limit 10";
$result=mysql_query($query);
$num = mysql_numrows($result);
if (!$num)
{
// no messages found
print "No archived messages for this device $$";
}
else
{
for($i = 0; $i < $num ; $i++)
{
$message=mysql_result($result,$i,"message");
$line = preg_replace('/{"aps":{"alert":"/', "", $message);
$line2 = preg_replace('/".*/', "", $line);
// $$ = seperator
print $line2 . "$$";
}
}?>
Example 2- integrating the Nagios server state
Please note, https access for polling the Nagios server state, requires a valid SSL certificate on the server
Step 1: Making the necessary changes in IAccess for Nagios
Enable "Poll this Nagios" in the Nagios host settings for the Nagios server you wish to get the information from.
Enter a custom URL in case your script is not called "nagstat_IA.php".
Step 2: Prepare the script on each server, which you wish to integrate
Here is an example of what such a script could look like.
Please note, this example script requires that ndo2db and a mysql database is installed on the Nagios server.
<pre><?php
// nagstat_IA.php example script
$hosts_status=0;
$services_status=0;
$overall_status=0;
$text_status="";
$dbusername="nagios";
$dbpassword="nagiospassword";
$database="nagios";
$dbhandle = mysql_connect("localhost", $dbusername, $dbpassword);
if ($dbhandle==NULL) {
echo date('Y-m-d H:i - ') . "Connection Failure to mysql database";
exit;
}
$result=mysql_select_db($database);
if ($result==NULL) {
echo date('Y-m-d H:i - ') . ("mysql database not found.");
exit;
}
$query="select count(current_state) result from nagios_hoststatus where current_state=0";
$result=mysql_query($query);
$hosts_up=mysql_result($result,0,"result");
$query="select count(current_state) result from nagios_hoststatus where current_state=1";
$result=mysql_query($query);
$hosts_down=mysql_result($result,0,"result");
if ($hosts_down>0) {
$hosts_status=2;
}
$query="select count(current_state) result from nagios_hoststatus where current_state=2";
$result=mysql_query($query);
$hosts_unreachable=mysql_result($result,0,"result");
if ($hosts_unreachable>0) {
$hosts_status=2;
}
$query="select count(current_state) result from nagios_servicestatus where current_state=0";
$result=mysql_query($query);
$services_up=mysql_result($result,0,"result");
$query="select count(current_state) result from nagios_servicestatus where current_state=3";
$result=mysql_query($query);
$services_unknown=mysql_result($result,0,"result");
if ($services_unknown>0) {
$services_status=3;
}
$query="select count(current_state) result from nagios_servicestatus where current_state=1";
$result=mysql_query($query);
$services_warning=mysql_result($result,0,"result");
if ($services_warning>0) {
$services_status=2;
}
$query="select count(current_state) result from nagios_servicestatus where current_state=2";
$result=mysql_query($query);
$services_critical=mysql_result($result,0,"result");
if ($services_critical>0) {
$services_status=1;
}
if ($hosts_status==3 || $services_status==3) $overall_status=3;
if ($hosts_status==2 || $services_status==2) $overall_status=2;
if ($hosts_status==1 || $services_status==1) $overall_status=1;
if ($overall_status==0) $text_status="G";
if ($overall_status==1) $text_status="R";
if ($overall_status==2) $text_status="Y";
if ($overall_status==3) $text_status="U";
// final text output of Nagios status
print $text_status . " - H: " . $hosts_up . "up " . $hosts_down . "d " . $hosts_unreachable . "un " . "S: " . $services_up . "up " . $services_critical . "c " . $services_warning . "w " . $services_unknown . "un";
?>
</pre>

asion-it.de