Welcome, Guest. Please login or register.

Author Topic: Export for Nagios configuration files from NeDi  (Read 8721 times)

gumba

  • Guest
Export for Nagios configuration files from NeDi
« on: February 17, 2009, 09:12:27 AM »
Following my reply to this topic http://forum.nedi.ch/index.php?topic=291.0, I'd like to share my code for an export script for Nagios config file for NeDi after receiving notes of interest. For anyone using Nagios alongside NeDi to actively monitor network devices, this script might be helpful.

Code: [Select]
<?php
include(
"inc/header.php");
$link DbConnect($dbhost,$dbuser,$dbpass,$dbname);

if ($_POST['mkconf']):
if (!( $_POST['device'] == && $_POST['location'] == && $_POST['exttype'] == "a" || $_POST['exptype'] == "r" && $_POST['expression'] == "")):
switch ($_POST['exptype']):
case "d"
$WHERE1 "name = '".$_POST['device']."'";
break;
case "l"
$WHERE1 "location REGEXP BINARY '^".$_POST['location']."'";
break;
case "r":
$WHERE1 "name REGEXP BINARY '".$_POST['expression']."'";
break;
endswitch;
switch ($_POST['limit']):
case "time":
switch ($_POST['period']):
case "h"
$time 3600 $_POST['time'];
break;
case "d":
$time 86400 $_POST['time'];
break;
case "w":
$time 604800 $_POST['time'];
break;
endswitch;
$WHERE2 "lastseen > ".(time() - $time);
break;
endswitch;
($WHERE1 || $WHERE2) ? ($WHERE " WHERE ".$WHERE1.($WHERE1 && $WHERE2 " AND " NULL).$WHERE2) : $WHERE "";
$query "SELECT name, ip, description FROM devices".$WHERE;
$result DbQuery($query$link);
echo mysql_error();
while ($row DbFetchArray($result)):
//deternime device type
switch (TRUE):
case preg_match('#[C|c]isco|WS-C|AIR-(AP|BR)|[C|c]at#'$row['description']):
if (preg_match('#C[0-9]{4}-K9W7#'$row['description'])):
$type['alias'] = $row['name']." (Cisco Access Point)";
$type['group'] = "switches";
elseif (preg_match('#Version [2-7]\.#'$row['description'])):
$type['alias'] = $row['name']." (Cisco, old firmware)";
$type['group'] = "switches";
else:
$type['alias'] = $row['name']." (Cisco)";
$type['group'] = "switches_cisco";
endif;
break;
case preg_match('#[P|p]ro[C|c]urve#'$row['description']):
$type['alias'] = $row['name']." (HP ProCurve)";
$type['group'] = "switches_hp";
break;
default:
$type['alias'] = $row['name']." (Switch)";
$type['group'] = "switches";
endswitch;
$replace_array = array("&" => "+");
$config "define host {\n";
$config .= " use generic-switch\n";
$config .= " host_name ".strtr($row['name'], $replace_array)."\n";
$config .= " alias ".$type['alias']."\n";
$config .= " address ".long2ip($row['ip'])."\n";
$config .= " hostgroups ".$type['group']."\n";
$config .= " notifications_enabled ".($_POST['en'] ? "1" "0")."\n";
$config .= " }";
if ($_POST['tofile']):
$file fopen("/usr/local/nagios/etc/switches/".$row['name'].".cfg""w");
$bytes_written fputs($file$config);
fclose($file);
endif;
if ($_POST['toscreen']):
$configScreenOut .= " ##################################".str_repeat("#"strlen($row['name']))."##<br>";
$configScreenOut .= " # configuration script for device ".$row['name']." #<br>";
$configScreenOut .= " ##################################".str_repeat("#"strlen($row['name']))."##<br><br>";
$configScreenOut .= str_replace("\n""<br>"$config)."<br><br>";
endif;
endwhile;
elseif ($_POST['exptype'] == "d"):
$ERR "Select a device!";
elseif ($_POST['exptype'] == "l"):
$ERR "Select a location!";
else:
$ERR "No regular expression given!";
endif;
endif;
?>

<h1>Export Nagios configuration scripts</h1>
<br>
<form action="<?= $self ?>.php" method="post">
<table>
<tr class="<?= $modgroup[$self] ?>1">
<th rowspan="2"><img src="img/32/nag.png"></td>
<th>Export configuration script for:</th>
<th>Export options:</th>
<th rowspan="2"><input type="submit" name="mkconf" value="Export"></th>
</tr>
<tr class="<?= $modgroup[$self] ?>1">
<td>
<input type="radio" checked name="exptype" value="d"> Device: <select name="device">
<option value="0" selected>Select a device:</option>
<?php
$result DbQuery("SELECT name FROM devices ORDER BY UPPER(name)"$link);
while ($row DbFetchArray($result)): ?>

<option<?= $row['name'] == $_POST['device'] ? " selected" : NULL ?>><?= $row['name'] ?></option>
<?php
endwhile;
?>

</select><br>
<br>

<input type="radio" name="exptype" value="l"<?= $_POST['exptype'] == "l" ? " checked" : NULL ?>> All devices of location: <select name="location">
<option value="0" selected>Select a location:</option>
<?php
$result DbQuery("SELECT DISTINCT SUBSTRING_INDEX(location, ';', 1) AS region FROM devices WHERE location REGEXP BINARY '[A-Z]{2,3};[A-Za-z]+;[A-Za-z0-9]+;.*' ORDER BY location"$link);
while ($row DbFetchArray($result)):
# $locations[] = $row['region'];
?>

<option><?= $row['region'] == $_POST['location'] ? " selected" : NULL ?><?= $row['region'] ?></option>
<?php
$region_result DbQuery("SELECT DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(location, ';', 2), ';', -1) AS city FROM devices WHERE SUBSTRING_INDEX(location, ';', 1) = '".$row['region']."' ORDER BY location"$link);
while ($region_row DbFetchArray($region_result)):
# $locations[$row['region']][] = $region_row['city'];
?>

<option value="<?= $row['region'] ?>;<?= $region_row['city'] ?>"<?= $row['region'].";".$region_row['city'] == $_POST['location'] ? " selected" : NULL ?>> - <?= $region_row['city'] ?> (<?= $row['region'] ?>)</option>
<?php
$city_result DbQuery("SELECT DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(location, ';', 3), ';', -1) AS building FROM devices WHERE SUBSTRING_INDEX(location, ';', 2) = '".$row['region'].";".$region_row['city']."' ORDER BY location"$link);
while ($city_row DbFetchArray($city_result)):
# $locations[$row['region']][$region_row['city']][] = $city_row['building'];
?>

<option value="<?= $row['region'] ?>;<?= $region_row['city'] ?>;<?= $city_row['building'] ?>"<?= $row['region'].";".$region_row['city'].";".$city_row['building'] == $_POST['location'] ? " selected" : NULL ?>> -- <?= $city_row['building'] ?> (<?= $region_row['city'] ?>, <?= $row['region'] ?>)</option>
<?php
endwhile;
endwhile;
endwhile;
?>

</select><br>
<br>
<input type="radio" name="exptype" value="r"<?= $_POST['exptype'] == "r" ? " checked" : NULL ?>> All devices matching following regular expression:
<input type="text" name="expression" value="<?= $_POST['expression'] ?>"> <input type="submit" name="regexp" value="Show devices">
<br>
<br>
<input type="radio" name="exptype" value="a"> All devices
</td>
<td>
<input type="checkbox" name="tofile" value="print"<?= !$_POST['mkconf'] || $_POST['tofile'] ? " checked" : NULL ?>> Write configuration file to Nagios configuration file directory<br>
<br>
<input type="checkbox" name="toscreen" value="print"<?= !$_POST['mkconf'] || $_POST['toscreen'] ? " checked" : NULL ?>> Show configuration script here<br>
<br>
<input type="checkbox" name="limit" value="time"<?= $_POST['limit'] ? " checked" : NULL ?>> Don't select devices not seen for <input type="text" name="time" size="3" value="<?= $_POST['time'] ?>"> <select name="period">
<option value="h"<?= $_POST['period'] == "h" ? " selected" : NULL ?>>Hours</option>
<option value="d"<?= $_POST['period'] == "d" ? " selected" : NULL ?>>Days</option>
<option value="w"<?= $_POST['period'] == "w" ? " selected" : NULL ?>>Weeks</option>
</select><br>
<br>
<input type="checkbox" name="en" value="enable"<?= $_POST['en'] ? " checked" : NULL ?>> Enable notifications for selected host(s)<br>
</td>
</tr>
</table>
</form>
<?php
if (
$ERR): ?>

<div class="textpad warn"><center><b><?= $ERR ?></b></center></div>
<?php
endif;

if ($_POST['regexp']):
$result DbQuery("SELECT name, ip FROM devices WHERE name REGEXP BINARY '".$_POST['expression']."'"$link);
?>

<div class="textpad devConf">
<?php
if (DbNumRows($result)): ?>

<b>Hosts matching regular expression '<?= $_POST['expression'] ?>':</b><br>
<?php
while ($row DbFetchArray($result)): ?>

<br><?= $row['name'] ?> (<?= long2ip($row['ip']) ?>)
<?php
endwhile;
else: ?>

Regular expression '<?= $_POST['expression'] ?>': No hosts found!
<?php
endif;
?>

</div>
<?php
endif;

if ($_POST['tofile'] && $bytes_written): ?>

<div class="textpad good"><center><b>Configuration scripts written to Nagios configuration file directory.<br>
Changes to Nagios will apply when Nagios server is restarted.</b></center></div>
<?php
endif;

if ($_POST['toscreen'] && $configScreenOut): ?>

<div class="textpad devConf">
<pre>
<?= $configScreenOut ?>
</pre>
</div>
<?php
endif;
?>

<br>
<?php
include(
"inc/footer.php");
?>


The script uses regular expression on the device's description pulled by NeDi to differentiate between Cisco, HP and other switches (more regexps for other vendors might be added). The config file are written /usr/local/nagios/etc/switches/ (may be changed if needed), note that this directory must be writable for the web server for this to work. The host groups 'switches', 'switches_cisco' and 'switches_hp' must exist in Nagios.

Cisco APs and devices with old Cisco firmware are excluded from the switches_cisco host group and added to the more generic switches group, since many services and SNMP attributes from newer Cisco switches are not supported there.

The script is very much adjusted to our needs, so modifications to the script itself and/or the existing Nagios configuration might be needed to fit into your environment. If any help is needed here I'd be happy to assist.

I've placed the script in NeDi as Other-Nagios.php. Don't forget to include the page in nedi.conf.
« Last Edit: February 17, 2009, 09:54:38 AM by gumba »

rickli

  • Administrator
  • Hero Member
  • *****
  • Posts: 2737
    • View Profile
    • NeDi
Re: Export for Nagios configuration files from NeDi
« Reply #1 on: February 17, 2009, 05:15:57 PM »
Good stuff! I put it in the contrib folder with a commented entry in nedi.conf...we'll need to check if it will work with the next release...
Please consider Other-Invoices on your NeDi installation for an annual contribution, tx!
-Remo

gumba

  • Guest
Re: Export for Nagios configuration files from NeDi
« Reply #2 on: February 17, 2009, 05:24:34 PM »
[...]we'll need to check if it will work with the next release...

If it doesn't, feel free to request a fix.

savanna2009

  • Guest
Re: Export for Nagios configuration files from NeDi
« Reply #3 on: December 09, 2009, 06:27:17 PM »
anyone please help to enlighten how to use this Nagios app???

do I need to install additional Nagios app/module or service in Ubuntu before I can use this script or service?


Thanks,
Savanna