From a1a2df9ad1ae892f415e29f69d4c6ca5843e8c99 Mon Sep 17 00:00:00 2001 From: Marius Halden Date: Sun, 15 Feb 2015 00:59:09 +0100 Subject: Add support for changing nagios settings through a configuration file. --- hosts.conf | 17 ++++++ sitesummary-nodes | 160 ++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 131 insertions(+), 46 deletions(-) create mode 100644 hosts.conf diff --git a/hosts.conf b/hosts.conf new file mode 100644 index 0000000..ede658f --- /dev/null +++ b/hosts.conf @@ -0,0 +1,17 @@ +# +# +# enabled = true +# warn = 10 +# crit = 5 +# +# +# enabled = false +# +# +# warn = "20%" +# crit = "10%" +# +# +# enabled = true +# diff --git a/sitesummary-nodes b/sitesummary-nodes index a889fe7..5a516d9 100755 --- a/sitesummary-nodes +++ b/sitesummary-nodes @@ -10,6 +10,7 @@ use SiteSummary; use Getopt::Std; use Socket; use File::Temp qw(tempfile); +use Config::General qw(ParseConfig); use vars qw($npingwarn $npingwarnp $npingcrit $npingcritp); sub usage { @@ -38,6 +39,8 @@ if ($@ && $@ !~ qr{^Can't locate $conffile}) { my %opts; getopts("hmnw", \%opts) || (usage(), exit(1)); +my $hostcfgfile = "/etc/sitesummary/hosts.conf"; +my %hostcfg = (); my $nagiosignorefile = "/etc/sitesummary/nagiosignore"; my %nagios_ignore; @@ -63,6 +66,10 @@ if ($opts{'h'}) { } close $fh; } + + %hostcfg = ParseConfig($hostcfgfile) + if ( -f $hostcfgfile ); + generate_nagios_config(); } else { print_list(); @@ -248,9 +255,31 @@ EOF print "}\n"; } +sub is_enabled { + my ($host, $service) = @_; + + return 1 unless (defined $hostcfg{$host} && + defined $hostcfg{$host}->{$service} && + defined $hostcfg{$host}->{$service}->{'enabled'} && + $hostcfg{$host}->{$service}->{'enabled'} eq "false"); + + return 0; +} + +sub get_cfg { + my ($host, $service, $setting) = @_; + + return undef unless (defined $hostcfg{$host} && + defined $hostcfg{$host}->{$service} && + defined $hostcfg{$host}->{$service}->{$setting}); + + return $hostcfg{$host}->{$service}->{$setting}; +} + sub generate_nagios_config { my %hosts; my %hostgroup; + for my $hostname (sort keys %hostnames) { my @groups = (); my $hostid = $hostnames{$hostname}; @@ -344,12 +373,14 @@ sub generate_nagios_config { unless (exists $hosts{$hostname}); $hosts{$hostname} = $address; - # first, check ping to see if the other checks should be performed - print_nagios_service_check(0, $hostname, "ping", - "check_ping", - sprintf("%.1f,%d%%!%.1f,%d%%", - $npingwarn, $npingwarnp, - $npingcrit, $npingcritp)); + if (is_enabled($hostname, "ping")) { + # first, check ping to see if the other checks should be performed + print_nagios_service_check(0, $hostname, "ping", + "check_ping", + sprintf("%.1f,%d%%!%.1f,%d%%", + $npingwarn, $npingwarnp, + $npingcrit, $npingcritp)); + } my %tcpservices = ( @@ -363,6 +394,8 @@ sub generate_nagios_config { is_pkg_installed($hostid, $tcpservices{$port}->{package})); my $servicename = $tcpservices{$port}->{name}; + next unless (is_enabled($hostname, $servicename)); + print_nagios_service_check(0, $hostname, $servicename, "check_tcp", $port); } @@ -370,15 +403,17 @@ sub generate_nagios_config { # Check SSH server print_nagios_service_check(0, $hostname, "ssh", "check_ssh") - if is_pkg_installed($hostid, "openssh-server"); + if is_pkg_installed($hostid, "openssh-server") && is_enabled($hostname, "ssh"); print_nagios_service_check(0, $hostname, "http", "check_http") - if (is_pkg_installed($hostid, "apache") || - is_pkg_installed($hostid, "apache2")); + if ((is_pkg_installed($hostid, "apache") || + is_pkg_installed($hostid, "apache2")) && + (is_enabled($hostname, 'apache') || + is_enabled($hostname, 'apache2'))); # Check XFS port only if we can see that it is listening on TCP - if (is_pkg_installed($hostid, "xfs")) { + if (is_pkg_installed($hostid, "xfs") && is_enabled($hostname, "xfs")) { my $path = get_filepath_current($hostid, "/system/x11-fs-config"); if ( -e $path ) { my $tcp = 1; @@ -398,7 +433,8 @@ sub generate_nagios_config { print_nagios_service_check(0, $hostname, "cups queue", "check_cups_queue") if (is_pkg_installed($hostid, "cups") && - is_pkg_installed($hostid, "cups-client")); + is_pkg_installed($hostid, "cups-client") && + is_enabled($hostname, "cups")); # The rest of the checks only work if NRPE is installed and configured next unless ((!$remote && $nagiosclient) @@ -408,23 +444,28 @@ sub generate_nagios_config { # Check for APT upgrades print_nagios_service_check($remote, $hostname, "apt-updates", - "check_apt"); + "check_apt") + if (is_enabled($hostname, "apt-updates")); # Check if a kernel reboot is needed print_nagios_service_check($remote, $hostname, "kernel status", - "check_kernel_status"); + "check_kernel_status") + if (is_enabled($hostname, "kernel-status"));; # Detect bad DNS servers print_nagios_service_check($remote, $hostname, "/etc/resolv.conf", - "check_etc_resolv"); + "check_etc_resolv") + if (is_enabled($hostname, "resolvconf")); # Detect hosts entries not matching DNS entries print_nagios_service_check($remote, $hostname, "/etc/hosts", - "check_etc_hosts"); + "check_etc_hosts") + if (is_enabled($hostname, "hosts")); # Detect a shutdown in progress print_nagios_service_check($remote, $hostname, "shutdown status", - "check_shutdown"); + "check_shutdown") + if (is_enabled($hostname, "shutdown")); # The check_dhcp test do not seem to work. Check for the process existence. @@ -433,29 +474,34 @@ sub generate_nagios_config { # if is_pkg_installed($hostid, "dhcp3-server"); print_nagios_service_check($remote, $hostname, "process - dhcpd3", "check_procs_dhcpd3", "1:15!1:25") - if (is_pkg_installed($hostid, "dhcp3-server")); + if (is_pkg_installed($hostid, "dhcp3-server") && + is_enabled($hostname, "dhcpd")); print_nagios_service_check($remote, $hostname, "process - dhcpd", "check_procs_dhcpd", "1:15!1:25") - if (is_pkg_installed($hostid, "isc-dhcp-server")); + if (is_pkg_installed($hostid, "isc-dhcp-server") && + is_enabled($hostname, "dhcpd")); # Check DNS server print_nagios_service_check($remote, $hostname, "dns", "check_dns", $localhostname) - if (is_pkg_installed($hostid, "pdns-server") || - is_pkg_installed($hostid, "bind9")); + if ((is_pkg_installed($hostid, "pdns-server") || + is_pkg_installed($hostid, "bind9")) && + is_enabled($hostname, "dns")); # Check FTP server print_nagios_service_check($remote, $hostname, "ftp", "check_ftp") - if is_pkg_installed($hostid, "proftpd"); + if is_pkg_installed($hostid, "proftpd") && + is_enabled($hostname, "ftp"); # Check IMAPS server print_nagios_service_check($remote, $hostname, "imaps", "check_imaps") - if is_pkg_installed($hostid, "courier-imap-ssl"); + if is_pkg_installed($hostid, "courier-imap-ssl") && + is_enabled($hostname, "imaps"); # check software raid status if any is active - if ( -e get_filepath_current($hostid, "/system/mdstat")) { + if ( -e get_filepath_current($hostid, "/system/mdstat") && is_enabled($hostname, "mdadm")) { my $fh; if (open($fh, get_filepath_current($hostid, "/system/mdstat")) && grep(/^md\d+ :/, <$fh>)) { @@ -468,19 +514,26 @@ sub generate_nagios_config { # Check NFS server print_nagios_service_check($remote, $hostname, "nfs", "check_nfs") - if is_pkg_installed($hostid, "nfs-kernel-server"); + if is_pkg_installed($hostid, "nfs-kernel-server") && + is_enabled($hostname, "nfs"); print_nagios_service_check($remote, $hostname, "smtp", "check_smtp") - if (is_pkg_installed($hostid, "exim4-daemon-heavy") || - is_pkg_installed($hostid, "exim4-daemon-light")); + if ((is_pkg_installed($hostid, "exim4-daemon-heavy") || + is_pkg_installed($hostid, "exim4-daemon-light")) && + is_enabled($hostname, "smtp")); # These need argument passing (as in dont_blame_nrpe=1) next unless ((!$remote && $nagiosclient) || ($remote && "args" eq $nrpestatus)); - print_nagios_service_check($remote, $hostname, "swap", - "check_swap", "10%!5%"); + if (is_enabled($hostname, "swap")) { + my $warn = get_cfg($hostname, 'swap', 'warn') || "10%"; + my $crit = get_cfg($hostname, 'swap', 'crit') || "5%"; + print_nagios_service_check($remote, $hostname, "swap", + "check_swap", "$warn!$crit"); + } + my $proclimit = "500!1000"; # Raise process count limits for thin clients, as # lots of users can log into these machines and run their @@ -488,13 +541,21 @@ sub generate_nagios_config { if (is_pkg_installed($hostid, "ltsp-server")) { $proclimit = "1500!2500"; } - print_nagios_service_check($remote, $hostname, "processes total", - "check_procs", $proclimit); - print_nagios_service_check($remote, $hostname, "processes zombie", - "check_procs_zombie", "20!100"); - # Check unix load - print_nagios_service_check($remote, $hostname, "load as in top", - "check_load", "75,75,75!90,90,90"); + if (is_enabled($hostname, "proc-total")) { + print_nagios_service_check($remote, $hostname, "processes total", + "check_procs", $proclimit); + } + if (is_enabled($hostname, "proc-zombie")) { + print_nagios_service_check($remote, $hostname, "processes zombie", + "check_procs_zombie", "20!100"); + } + if (is_enabled($hostname, "load-avg")) { + my $warn = get_cfg($hostname, 'load-avg', 'warn') || "75,75,75"; + my $crit = get_cfg($hostname, 'load-avg', 'crit') || "90,90,90"; + # Check unix load + print_nagios_service_check($remote, $hostname, "load as in top", + "check_load", "$crit!$warn"); + } # check disk free space my $path = get_filepath_current($hostid, "/system/procmounts"); @@ -541,12 +602,15 @@ sub generate_nagios_config { $fs eq "usbfs"); $checked{$device} = 1; - my $warn = 10; - my $crit = 5; - print_nagios_service_check($remote, $hostname, - "disk $partition", - "check_disk", - "$warn%!$crit%!$partition"); + + if (is_enabled($hostname, "disk-$partition")) { + my $warn = get_cfg($hostname, "disk-$partition", 'warn') || 10; + my $crit = get_cfg($hostname, "disk-$partition", 'crit') || 5; + print_nagios_service_check($remote, $hostname, + "disk $partition", + "check_disk", + "$warn%!$crit%!$partition"); + } } } @@ -558,22 +622,26 @@ sub generate_nagios_config { # nagios-plugins-basic print_nagios_service_check(0, $hostname, "ldap root DSE", "check_ldap_root") - if is_pkg_installed($hostid, "slapd"); + if is_pkg_installed($hostid, "slapd") && + is_enabled($hostname, "ldap"); # Check Squid web proxy print_nagios_service_check($remote, $hostname, "squid", "check_squid", "3128!http://www") - if is_pkg_installed($hostid, "squid"); + if is_pkg_installed($hostid, "squid") && + is_enabled($hostname, "squid"); print_nagios_service_check($remote, $hostname, "ntp time server", "check_ntp", "-H!localhost") - if (is_pkg_installed($hostid, "ntp") || - is_pkg_installed($hostid, "ntp-server")); + if ((is_pkg_installed($hostid, "ntp") || + is_pkg_installed($hostid, "ntp-server")) && + is_enabled($hostname, "ntp")); # Detect if cron no longer is running print_nagios_service_check($remote, $hostname, "process - cron", "check_procs_cron", "1:15!1:25") - if (is_pkg_installed($hostid, "cron")); + if (is_pkg_installed($hostid, "cron") && + is_enabled($hostname, "cron")); } if (%hostgroup) { -- cgit v1.2.3