Documente Academic
Documente Profesional
Documente Cultură
From : http://www.stardothosting.com
Working at many different organisations over the past 10 years, I have been
involved in the implementation and maintenance of many different monitoring
implementations. These include commercial and open source implementations, such as
:
- Nagios
- IP Monitor
- Uptime
- OpenNMS
- Zabbix
Although Nagios may not be the most scalable or dynamic solution, for some
organisations that perhaps have 1-100 servers, Nagios may be the best solution.
Additionally, the ability to write custom plugins, as well as the inherent SSL /
TLS encryption of the NRPE checks, it may be the most viable. There are pro’s and
con’s for each solution out there, and it is completely dependant on the skill
level, nature of environment and available time for management / maintenance.
During the course of utilising Nagios, we noticed that one of the most time
consuming tasks was maintaining the flat file configuration for adding, removing
and modifying hosts within Nagios.
As a result, it was decided to write a quick Perl based script to manage the day-
to-day tasks of adding and removing hosts within Nagios. When all is said and
done, it really does save ALOT of time. This script can be integrated with
existing control based management situations or other automation scripts /
solutions where command line options and external scripting / plugins are
possible. This way, you can encompass a more rounded, standardised and reliable
way of managing your systems in Nagios.
In order for the script to work, you need to have 3 types of servers :
- Windows
- Unix/Linux
- VPS (Virtual Private Server)
Please take a look at the script, hopefully it will help make your life a little
easier! ;)
#!/usr/bin/perl
###############################################################################
# Star Dot Hosting : www.stardothosting.com
# Nagios Config Manager
# Description: This program will add/remove entries from nagios.
# The files will be backed up in a archive before any changes are made.
###############################################################################
# Perl Libraries
use File::Copy;
use Switch;
###############################################################################
# Variables
###############################################################################
# Nagios file handlers
my $host_file = "/usr/local/nagios/etc/objects/hosts.cfg";
my $group_file = "/usr/local/nagios/etc/objects/hostgroups.cfg";
my $services_file = "/usr/local/nagios/etc/objects/services.cfg";
my $unixmatch = "sdh-unix" ;
my $windowsmatch = "sdh-windows";
my $vpsmatch = "vps-server";
my $date = `date "+%d%m%y-%H%M%S"`;
###############################################################################
# Verify Arguments
if ((!$ARGV[0]) || (!$ARGV[1])) {
&usage;
}
###############################################################################
# The Main Program control statement.
###############################################################################
switch ($ARGV[1]) {
###############################################################################
# Subroutines
###############################################################################
###############################################################################
## sub backup - Backs up the nagios config files that are to be modified
###############################################################################
sub backup {
###############################################################################
## sub openFile($filename) - returns the file to a buffer for parsing
###############################################################################
sub openFile {
my $blob;
my $file = shift;
open (F, "< $file") or die "Can't open $file : $!";
while( ) {
$blob .= $_;
}
close(F);
return $blob;
###############################################################################
###############################################################################
###############################################################################
## sub delete - Deletes the servername from the config files.
###############################################################################
sub delete {
&backup; # Backup the files before we do anything to them.
&delete_host;
&delete_hostgroup;
&delete_services;
&checkNagios;
}
###############################################################################
## sub delete_host - deletes the host entry from hosts.cfg
###############################################################################
sub delete_host {
my $host_str = &openFile($host_file);
my $pattern=$ARGV[0]; # The parser doesn't like the array so we just
pass it to a variable.
} else { die "Could not find and entry for $ARGV[0] in hosts.cfg\n"; };
}
###############################################################################
## sub delete_hostgroup - deletes the hostgroup entry
###############################################################################
sub delete_hostgroup {
my $hostgrp_str = &openFile($group_file);
my $pattern=$ARGV[0]; # The parser doesn't like the array so we just
pass it to a variable.
} else {
die "Could not find and entry for $ARGV[0] in hostgroups.cfg\n";
}
open(HGF, ">/tmp/hostgroups.cfg.tmp") || die "Cannot open
/tmp/hostgroups.cfg.tmp";
print HGF $hostgrp_str;
close(HGF);
}
###############################################################################
## sub delete_services - delete the serivices.cfg entry
###############################################################################
sub delete_services {
my $services_str= &openFile($services_file);
my $pattern=$ARGV[0]; # The parser doesn't like the array so we just
pass it to a variable.
###############################################################################
## sub checkNagios - checks nagios for errors and rolesback if so.
###############################################################################
sub checkNagios {
} else {
print "Nagios config reports success, restarting nagios\n";
my $restart = `/etc/init.d/nagios reload`;
print $restart;
}
###############################################################################
## sub addEntry - adds the unix or windows host entry.
###############################################################################
sub addEntry {
my $type = shift;
my $pattern = $ARGV[0];
my $host_str = &openFile($host_file);
my $hostgrp_str = &openFile($group_file);
my $services_str= &openFile($services_file);
switch ($type) {
}
###############################################################################
## sub usage - prints the usage when things don't add up from args
###############################################################################
sub usage{
print "Usage: /usr/local/bin/nagios-add.pl \n\n";
print "Optional Flags:\n";
print "\td delete a server\n";
print "\tw add a windows server\n";
print "\tx add a unix server\n";
print "\tv add a VPS server\n\n";
print "eg delete:\t./usr/local/bin/nagios-add.pl sdh-server12 d\n";
print "eg add:\t\t./usr/local/bin/nagios-add.pl sdh-server12 x \"sdh-
server12 sdh-server12.stardothosting.com MemID:155\" 192.168.111.10\n";
exit 0;
}