From 55c60873a76c0e95737785a5d78516f9beef7f33 Mon Sep 17 00:00:00 2001 From: Antoine Beaupre Date: Tue, 13 Jan 2009 16:08:28 -0500 Subject: rewrite nagios check scripts in perl --- files/checkbackups.pl | 101 ++++++++++++++++++++++++++++++++++++++++++++++ manifests/server.pp | 4 +- templates/checkbackups.sh | 73 --------------------------------- 3 files changed, 103 insertions(+), 75 deletions(-) create mode 100755 files/checkbackups.pl delete mode 100755 templates/checkbackups.sh diff --git a/files/checkbackups.pl b/files/checkbackups.pl new file mode 100755 index 0000000..24632d1 --- /dev/null +++ b/files/checkbackups.pl @@ -0,0 +1,101 @@ +#!/usr/bin/perl -w + +# This script is designed to check a backup directory populated with +# subdirectories named after hosts, within which there are backups of various +# types. +# +# Example: +# /home/backup: +# foo.example.com +# +# foo.example.com: +# rdiff-backup .ssh +# +# rdiff-backup: +# root home rdiff-backup-data usr var +# +# There are heuristics to determine the backup type. Currently, the following +# types are supported: +# +# rdiff-backup: assumes there is a rdiff-backup/rdiff-backup-data/backup.log file +# duplicity: assumes there is a dup subdirectory, checks the latest file +# dump files: assumes there is a dump subdirectory, checks the latest file +# +# This script returns output suitable for send_nsca to send the results to +# nagios and should therefore be used like this: +# +# checkbackups.sh | send_nsca -H nagios.example.com + +use Getopt::Std; + +# XXX: taken from utils.sh from nagios-plugins-basic +my $STATE_OK=0; +my $STATE_WARNING=1; +my $STATE_CRITICAL=2; +my $STATE_UNKNOWN=3; +my $STATE_DEPENDENT=4; + +our $opt_d = "/backup"; +our $opt_c = 48 * 60 * 60; +our $opt_w = 24 * 60 * 60; + +if (!getopts('d:c:w:')) { + print < ] [ -c ] [ -w ] +EOF + ; + exit(); +} + +my $backupdir= $opt_d; +my $crit = $opt_c; +my $warn = $opt_w; + +# XXX: this should be a complete backup registry instead +my @hosts=qx{ls $backupdir}; + +chdir($backupdir); +foreach my $host (@hosts) { + chomp($host); + my $flag=""; + my $type="unknown"; + if (-d $host) { + # guess the backup type and find a proper stamp file to compare + # XXX: this doesn't check if the backup was actually successful + # XXX: the backup type should be part of the machine registry + if (-d "$host/rdiff-backup") { + $flag="$host/rdiff-backup/rdiff-backup-data/backup.log"; + $type="rdiff"; + } elsif (-d "$host/dump") { + $flag="$host/dump/" . `ls -tr $host/dump | tail -1`; + chomp($flag); + $type="dump"; + } elsif (-d "$host/dup") { + $flag="$host/dup"; + $type="duplicity"; + } else { + printf "$host\tbackups\t$STATE_UNKNOWN\tunknown system\n"; + next; + } + my @stats = stat($flag); + if (not @stats) { + printf "$host\tbackups\t$STATE_UNKNOWN\tcannot stat flag $flag\n"; + next; + } + my $t = time(); + my $delta = $t - $stats[9]; + my $state = $STATE_UNKNOWN; + if ($delta > $crit) { + $state = $STATE_CRITICAL; + } elsif ($delta > $warn) { + $state = $STATE_WARNING; + } elsif ($delta >= 0) { + $state = $STATE_OK; + } + print "$host\t"; + print "backups\t$state"; + print "\t$delta seconds old\n"; + } else { + printf "$host\tbackups\t$STATE_UNKNOWN\tno directory\n"; + } +} diff --git a/manifests/server.pp b/manifests/server.pp index 4284ffa..9b08b22 100644 --- a/manifests/server.pp +++ b/manifests/server.pp @@ -25,12 +25,12 @@ class backupninja::server { file { "/usr/local/bin/checkbackups": ensure => "present", - content => template("backupninja/checkbackups.sh"), + source => "puppet://$servername/backupninja/checkbackups.pl", mode => 0755, owner => root, group => root, } cron { checkbackups: - command => "/usr/local/bin/checkbackups.sh | /usr/sbin/send_nsca -H nagios.koumbit.net -c /etc/send_nsca.cfg", + command => "/usr/local/bin/checkbackups -d $real_backupdir | /usr/sbin/send_nsca -H nagios.koumbit.net -c /etc/send_nsca.cfg | grep -v 'sent to host successfully'", user => "root", hour => "8-23", minute => 59, diff --git a/templates/checkbackups.sh b/templates/checkbackups.sh deleted file mode 100755 index 8d143a3..0000000 --- a/templates/checkbackups.sh +++ /dev/null @@ -1,73 +0,0 @@ -#!/bin/sh - -# This script is designed to check a backup directory populated with -# subdirectories named after hosts, within which there are backups of various -# types. -# -# Example: -# /home/backup: -# foo.example.com -# -# foo.example.com: -# rdiff-backup .ssh -# -# rdiff-backup: -# root home rdiff-backup-data usr var -# -# There are heuristics to determine the backup type. Currently, the following -# types are supported: -# -# rdiff-backup: assumes there is a rdiff-backup/rdiff-backup-data/backup.log file -# duplicity: assumes there is a dup subdirectory, checks the latest file -# dump files: assumes there is a dump subdirectory, checks the latest file -# -# This script returns output suitable for send_nsca to send the results to -# nagios and should therefore be used like this: -# -# checkbackups.sh | send_nsca -H nagios.example.com - -# XXX: taken from utils.sh from nagios-plugins-basic -STATE_OK=0 -STATE_WARNING=1 -STATE_CRITICAL=2 -STATE_UNKNOWN=3 -STATE_DEPENDENT=4 - -backupdir="<%= real_backupdir -%>" - -# XXX: this should be a complete backup registry instead -hosts=`ls $backupdir` -stampfile=$backupdir/.stamp - -cd $backupdir -for host in $hosts; do - flag="" - type="unknown" - if [ -d $host ]; then - # guess the backup type and find a proper stamp file to compare - # XXX: this doesn't check if the backup was actually successful - # XXX: the backup type should be part of the machine registry - if [ -d $host/rdiff-backup ]; then - flag=$host/rdiff-backup/rdiff-backup-data/backup.log - type="rdiff" - elif [ -d $host/dump ]; then - flag="$host/dump/`ls -tr $host/dump | tail -1`" - type="dump" - elif [ -d $host/dup ]; then - flag="$host/dup/`ls -tr $host/dup | tail -1`" - type="duplicity" - else - printf "$host\tbackups\t$STATE_UNKNOWN\tUNKNOWN unknown system\n" - continue - fi - touch -t `date +%Y%m%d0000.00` $stampfile - date=`ls -l $flag | awk '{print $6 " " $7}'` - if [ $flag -nt $stampfile ]; then - printf "$host\tbackups\t$STATE_OK\tOK timestamp $date\n" - else - printf "$host\tbackups\t$STATE_CRITICAL\tCRITICAL timestamp $date\n" - fi - else - printf "$host\tbackups\t$STATE_UNKNOWN\tUNKNOWN timestamp no directory\n" - fi -done -- cgit v1.2.3