#!/usr/bin/perl # addNagHost.pl creates a new Nagios host configuration file via GUI front end. # written 05-may-2009 by fgb # # NB: # 1. You still need to manually add the server(s) to conf.d/hostgroups_nagios2.cfg # in the appropriate hostgroup 'members' list before nagios will monitor them # (and recycle nagios). # 2. Windows servers need agent (service) nsClient++ installed and configured before # the check_nt services defined here will work. use Socket; use Tk; use Tk::DialogBox; my $bDebug = 0; my $sAppName = 'addNagHost'; my $sAuthor = "Frank Brown"; my $sDate = localtime(time); my $sToday = substr($sDate,4); my $sVersion = '0.1'; $sAddress = ''; $sAlias = ''; $sFilename = ''; $sHost = ''; $sIpAddr = ''; $bIis = 0; $bSql = 0; $bWin = 1; $nagiosConfDir = "/etc/nagios2/servers"; $notifyInterval = '30'; $notifyOptions = 'd,u,r'; $notifyPeriod = 'workhours'; $main = MainWindow->new(); $dlg = $main->DialogBox( -title => "Add host to Nagios configuration", -buttons => [ "Add", "Cancel" ]); # add widgets to dialog: $sDescr = $dlg->add("Text", -wrap => "word", -bd => 2, -height => 3)->pack(); $sDescr->insert('end',"\nCreates a Nagios configuration file for an SFD server."); $dlg->add("Label", -text => "Server name:")->pack(); $ecName = $dlg->add("Entry", -width => 35, -validate => 'focusout', -validatecommand => sub { if (length($_[0])) { $sHost = $_[0]; &lookupIpAddress(); }; return 1; }, -takefocus => 1)->pack(); $dlg->add("Label", -text => "Description:")->pack(); $ecAlias = $dlg->add("Entry", -width => 35)->pack(); $dlg->add("Label", -text => "IP address:")->pack(); $ecAddr = $dlg->add("Entry", -width => 35 , -textvariable => \$sAddress)->pack(); $chkWin = $dlg->add("Checkbutton", -text => "Windows", -variable => \$bWin)->pack(); $chkWin->select(); $chkIis = $dlg->add("Checkbutton", -text => "IIS server", -variable => \$bIis)->pack(); $chkSql = $dlg->add("Checkbutton", -text => "SQL server", -variable => \$bSql)->pack(); # add buttons to main window: $main->Button( -text => "Click for form", -command => \&addHost) ->pack(-side => "left"); $main->Button( -text => "Quit", -command => sub { exit } ) ->pack(-side => "left"); MainLoop; # start Tk's event handler loop ### here be subs: sub addHost { my $btn; my $bDone = 0; do { &initForm(); $btn = $dlg->Show; if ($btn eq "Add") { $sHost = $ecName->get; if (defined($sHost) && length($sHost)) { $sAlias = $ecAlias->get; if (! defined($sAlias) || ! length($sAlias)) { $sAlias = $sHost; } # nagios won't accept blank alias $sIpAddr = $ecAddr->get; &genHostFile(); if ($bWin) { &addWindowsServices(); } print "created file $sFilename\n"; print "Be sure to add $sHost to windows-servers in hostgroups_nagios2.cfg!\n"; if ($bWin) { print "Also make sure nsClient++ agent is installed on $sHost\n"; } $bDone = 1; } else { print "missing host name\n" } } elsif ($btn eq "Cancel") { $bDone = 1; } else { print "Unknown button pressed?!"; } } until $bDone; } sub addWindowsServices { my $sTmp = sprintf(">>%s",$sFilename); open(FP,$sTmp) || die "Can't open file $sFilename: $!\n"; seek(FP,0,2); # position FP at EOF for append print FP "# define services for windows server:\n\n"; print FP "define service {\n"; print FP "use\t\t\tgeneric-service\n"; print FP "host_name\t\t$sHost\n"; print FP "service_description\tUptime\n"; print FP "check_command\t\tcheck_nt!UPTIME\n"; print FP "}\n"; print FP "define service {\n"; print FP "use\t\t\tgeneric-service\n"; print FP "host_name\t\t$sHost\n"; print FP "service_description\tCPU\n"; print FP "check_command\t\tcheck_nt!CPULOAD!-l 5,80,90\n"; print FP "}\n"; print FP "define service {\n"; print FP "use\t\t\tgeneric-service\n"; print FP "host_name\t\t$sHost\n"; print FP "service_description\tMemory\n"; print FP "check_command\t\tcheck_nt!MEMUSE!-w 80 -c 90\n"; print FP "}\n"; print FP "define service {\n"; print FP "use\t\t\tgeneric-service\n"; print FP "host_name\t\t$sHost\n"; print FP "service_description\tSystem Disk\n"; print FP "check_command\t\tcheck_nt!USEDDISKSPACE!-l c -w 80 -c 90\n"; print FP "}\n"; if ($bIis) { print FP "define service {\n"; print FP "use\t\t\tgeneric-service\n"; print FP "host_name\t\t$sHost\n"; print FP "service_description\tW3SVC\n"; print FP "check_command\t\tcheck_nt!SERVICESTATE!-d SHOWALL -l W3SVC\n"; print FP "}\n"; } if ($bSql) { print FP "define service {\n"; print FP "use\t\t\tgeneric-service\n"; print FP "host_name\t\t$sHost\n"; print FP "service_description\tSQL\n"; print FP "check_command\t\tcheck_nt!SERVICESTATE!-d SHOWALL -l MSSQLSERVER\n"; print FP "}\n"; } close(FP); } sub genHostFile { my $sBaseFile = "host-$sHost.cfg"; $sFilename = $nagiosConfDir . '/' . $sBaseFile; if ($bDebug) { $sFilename .= '-dbg'; print "dbg: filename = $sFilename\n"; } my $sTmp = sprintf(">%s",$sFilename); open(FP,$sTmp) || die "Can't open file $sFilename: $!\n"; print FP "# $sBaseFile - nagios host config file for $sHost\n# Generated by $sAppName $sVersion $sToday\n\n"; print FP "define host {\n"; print FP "\tuse\t\t\t\tgeneric-host\n"; print FP "\thost_name\t\t\t$sHost\n"; print FP "\talias\t\t\t\t$sAlias\n"; print FP "\taddress\t\t\t\t$sIpAddr\n"; print FP "\tcheck_command\t\t\tcheck-host-alive\n"; print FP "\tmax_check_attempts\t\t5\n"; print FP "\tcheck_period\t\t\t24x7\n"; print FP "\tprocess_perf_data\t\t0\n"; print FP "\tretain_nonstatus_information\t0\n"; print FP "\tcontact_groups\t\t\tadmins\n"; print FP "\tnotification_interval\t\t$notifyInterval\n"; print FP "\tnotification_period\t\t$notifyPeriod\n"; print FP "\tnotification_options\t\t$notifyOptions\n"; print FP "\t}\n"; close(FP); } sub initForm { $ecName->delete(0,'end'); # reset entry fields and checkboxes from last call: $ecAlias->delete(0,'end'); $ecAddr->delete(0,'end'); $chkWin->select(); # default to Windows server $chkIis->deselect(); $chkSql->deselect(); } sub lookupIpAddress { $s = inet_aton($sHost); if (!defined($s)) { print "error: dns lookup failed for $sHost\n"; return 0; } if ($bDebug) { print "dbg: host = $sHost, inet_aton = $s\n"; } $sAddress = inet_ntoa(inet_aton($sHost)); #print "dbg: $sHost addr = $sAddress\n"; } __END__