Here's a plugin I wrote for Nagios to check directories where some scripts regularly store backup files. You may specify a directory to scan, an optional pattern for the filename and the minimal age and size you expect for the latest file found in that directory and matching the pattern. The script can output warnings and critical alerts with different thresholds.

/root/bin//nagios-check-backup.sh :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
 
# Written by Alexis Bezverkhyy <alexis@grapsus.net> in july 2010
# This is free and unencumbered software released into the public domain.
# For more information, please refer to <http://unlicense.org/>
 
 
function PRINT_USAGE(){
  echo "This Nagios plugin checks backup folders :
  -d DIRECTORY  the directory to search for backup files
  -p PATTERN  an optionnal pattern for backup files
  -t HOURS  maximal age in hours for the latest backup before a warning is issued
  -T HOURS  maximal age in hours for the latest backup before a critical alert is issued
  -s KBYTES maximal size in kilo bytes for the latest backup before a warning is issued
  -S KBYTES maximal size in kilo bytes for the latest backup before a critical alert is issued
  -h    prints out this help
You must at least specify a directory and a minimal size or a minimal age."
  exit 0
}
 
WTIME=0;CTIME=0;WSIZE=0;CSIZE=0;DIR='';PATTERN=''
declare -i CTIME 
declare -i WTIME
declare -i CSIZE
declare -i WSIZE
while true ; do
  getopts 't:T:s:S:d:p:h' OPT 
  if [ "$OPT" = '?' ] ; then break; fi; 
  case "$OPT" in
    "t") WTIME="$OPTARG";;
    "T") CTIME="$OPTARG";;
    "s") WSIZE="$OPTARG";;
    "S") CSIZE="$OPTARG";;
    "d") DIR="$OPTARG";;
    "p") PATTERN="$OPTARG";;
    "h") PRINT_USAGE;;
  esac
done
 
if [ -z "$DIR" -o '(' "$WTIME" = '0' -a "$CTIME" = '0'\
 -a "$WSIZE" = '0' -a "$CSIZE" = '0' ')' ] ; then
  PRINT_USAGE
fi
 
LASTFILE=$(ls -lt --time-style=+%s "$DIR" | grep -v "^total " | grep "$PATTERN"\
 | head -n 1 | sed 's/\s\+/ /g')
if [ -z "$LASTFILE" ] ; then
  echo "CRITICAL - no backup found in $DIR" 
  exit 2
fi
 
TIMESTAMP=$(cut -d ' ' -f 6 <<< "$LASTFILE")
BYTES=$(cut -d ' ' -f 5 <<< "$LASTFILE")
let "SIZE = $BYTES / 1024"
FILENAME=$(cut -d ' ' -f 7 <<< "$LASTFILE")
let "AGE = ( $(date +%s) - $TIMESTAMP ) / 3600"
 
if [ "$CTIME" -gt 0 -a "$AGE" -gt "$CTIME" ] ; then
  echo "CRITICAL - $FILENAME is out of date ($AGE hours old)" 
  exit 2
fi
 
if [ "$WTIME" -gt 0 -a "$AGE" -gt "$WTIME" ] ; then
  echo "WARNING - $FILENAME is out of date ($AGE hours old)"  
  exit 1
fi
 
if [ "$CSIZE" -gt 0 -a "$SIZE" -lt "$CSIZE" ] ; then
  echo "CRITICAL - $FILENAME is too small ($SIZE kb)" 
  exit 2
fi
 
if [ "$WSIZE" -gt 0 -a "$SIZE" -lt "$WSIZE" ] ; then
  echo "WARNING - $FILENAME is too small ($SIZE kb)"  
  exit 1
fi
 
echo "OK - $FILENAME ($AGE hours old, $SIZE kb)"
exit 0

Here is a sample configuration for Nagios to use my script. check_backup checks a regular folder with compressed backups and check_sync checks a directory that is ought to be synchronized, therefore only age is checked and not the size.

commands.cfg :

1
2
3
4
5
6
7
8
9
10
define command {
  command_name check_backup
  command_line /root/bin/nagios-check-backup.sh -d $ARG1$ -p $ARG2$ -t $ARG3$ \
   -T $ARG4$ -s $ARG5$ -S $ARG6$
}
 
define command {
  command_name check_sync
  command_line /root/bin/nagios-check-backup.sh -d $ARG1$ -t $ARG3$ -T $ARG4$
}

foo.cfg :

1
2
3
4
5
6
7
8
9
10
11
12
13
define service{
        use                             generic-service
        host_name                       bar
        service_description             BAK-FOO-CONF
        check_command                   check_backup!/media/backup/foo/conf/!conf!30!60!2000!1000
}
 
define service{
        use                             generic-service
        host_name                       bar
        service_description             BAK-FOO-WWW
        check_command                   check_sync!/media/backup/foo/www/!30!60
}