]> git.sur5r.net Git - bacula/bacula/blob - bacula/examples/reports/check_bacula_pools.sh
kes Fix optional files in Makefile.in of src/filed that caused
[bacula/bacula] / bacula / examples / reports / check_bacula_pools.sh
1 #! /bin/sh
2 # Author : Ludovic Strappazon. l.strappazon@gmail.com
3 # Copyright : Kern Sibbald 
4 # Any comment, advice or enhancement are welcome :-)
5
6 PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
7 MYSQL="/usr/bin/mysql -u bacula --password=cW8s9_aH"
8 TMP=/tmp
9 BACULA=/usr/local/bacula
10
11 PROGNAME=`basename $0`
12 PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
13 STATUS=""
14
15 . $PROGPATH/utils.sh
16
17 print_usage() {
18         echo "Usage: $PROGNAME -P <pool> -M <media-type> -w <warning threshold> -c <critical threshold> [-S]"
19 }
20
21 print_help() {
22         echo ""
23         print_usage
24         echo ""
25         echo "This plugin checks the space available in the pool against the space required for the next scheduled backups"
26         echo "Example : $PROGNAME -P default -M LTO -w 20 -c 10 will check the default pool, return OK if (available space) > 1,20*(required space), WARNING if 1,20*(required space) > (available space) > 1,10*(required space), and CRITICAL else."
27         echo ""
28         echo "With the -S option, it will check the pool named Scratch and return WARNING instead of CRITICAL if the Scratch pool can save the situation."
29         echo "Example : $PROGNAME -P default -M LTO -w 20 -c 10 -S will check the default pool, return OK if (available space) > 1,20*(required space), WARNING if 1,20*(required space) > (available space) > 1,10*(required space) or if (available space in default and Scratch) > 1,10*(required space) > (available space in default), and CRITICAL else."
30         echo ""
31         echo "The evaluation of the space required is done by adding the biggest backups of the same level than the scheduled jobs"
32         echo "The available space is evaluated by the number of out of retention tapes and the average VolBytes of these Full tapes"
33         echo ""
34         echo "The Information Status are : \"Required, Available, Volume Errors\" and \"Will use Scratch pool\" if necessary."
35         echo ""
36         echo "I think this plugin should be used in passive mode, and ran by a RunAfterJob"
37         exit 3
38 }
39
40 NB_ARGS=$#
41 SCRATCH=0
42 while getopts :P:M:w:c:hS OPTION
43 do
44   case $OPTION in
45     P) POOL="$OPTARG"
46        ;;
47     M) MEDIA_TYPE="$OPTARG"
48        ;;
49     S) SCRATCH=1
50        ;;
51     w) WARNING="$OPTARG"
52        ;;
53     c) CRITICAL="$OPTARG"
54        ;;
55     h) print_help
56        exit 3
57        ;;
58     *) print_usage
59        exit 3
60        ;;
61   esac
62 done
63 shift $(($OPTIND - 1))
64
65 if [ "$NB_ARGS" -ne 8 -a "$NB_ARGS" -ne 9 ]; then
66         print_revision $PROGNAME 13/06/2006
67         print_usage
68         exit 3
69 fi
70
71 LAST_CHECK=`ps -ef | grep check_ba[Cc]ula_pools.sh | awk {'print $5'} | uniq | wc -l`
72 if [ "$LAST_CHECK" -gt 1 ]; then
73         echo "The last check was not complete, you should increase the check_period."
74         exit 3
75 fi
76
77 NB_VOLUMES=`$MYSQL << EOF
78 USE bacula
79 SELECT COUNT(MediaId) from Media, Pool where Media.PoolId=Pool.PoolId and Pool.Name="$POOL" AND Inchanger = "1";
80 EOF`
81
82 NB_VOLUMES=`echo $NB_VOLUMES | cut -f 2 -d ' '`
83
84 echo "st
85 1
86 q" | $BACULA/etc/bconsole | sed -n /Scheduled/,/Running/p | grep Backup | tr -s [:blank:] | tr '[:blank:]' '@' > ${TMP}/Scheduled.txt
87
88 CAPA_REQUIRED=0
89 for LINE in `cat ${TMP}/Scheduled.txt`
90 do
91   SCHEDULED_JOB=`echo $LINE | awk -F@ '{print $6}'`
92   LEVEL=`echo $LINE | awk -F@ '{print $1}' | cut -c 1`
93
94 MAX_VOLUME_JOB_FOR_LEVEL=`$MYSQL << EOF
95 USE bacula
96 SELECT MAX(JobBytes) from Job, Pool where Level="$LEVEL" AND Job.Name="$SCHEDULED_JOB" AND Job.PoolId=Pool.PoolId AND Pool.Name="$POOL";
97 EOF
98 `
99 MAX_VOLUME_JOB_FOR_LEVEL=`echo $MAX_VOLUME_JOB_FOR_LEVEL | cut -f 2 -d ' ' `
100
101 CAPA_REQUIRED=$[CAPA_REQUIRED+MAX_VOLUME_JOB_FOR_LEVEL]
102 done
103
104 rm ${TMP}/Scheduled.txt
105
106
107 if [ $NB_VOLUMES -gt 0 ]
108 then
109
110   NB_VOLUMES_OUT_OF_RETENTION=`$MYSQL << EOF
111 USE bacula
112 SELECT COUNT(MediaId) from Media, Pool where Media.PoolId=Pool.PoolId and Pool.Name="$POOL" AND LastWritten <> "0000-00-00 00:00:00" AND UNIX_TIMESTAMP()-UNIX_TIMESTAMP(LastWritten)>Media.VolRetention AND Inchanger = "1";
113 EOF
114 `
115   NB_VOLUMES_OUT_OF_RETENTION=`echo $NB_VOLUMES_OUT_OF_RETENTION | cut -f 2 -d ' '`
116
117 NB_VOLUMES_ERROR=`$MYSQL << EOF
118 USE bacula
119 SELECT COUNT(MediaId) from Media, Pool where Media.PoolId=Pool.PoolId and Pool.Name="$POOL" AND VolStatus="Error" AND Inchanger = "1";
120 EOF
121 `
122 NB_VOLUMES_ERROR=`echo $NB_VOLUMES_ERROR | cut -f 2 -d ' '`
123
124 AVERAGE_CAPA_VOLUME=`$MYSQL << EOF
125 USE bacula
126 SELECT SUM(VolBytes)/COUNT(MediaId) FROM Media where VolStatus="Full" AND MediaType="$MEDIA_TYPE";
127 EOF
128 `
129 AVERAGE_CAPA_VOLUME=`echo $AVERAGE_CAPA_VOLUME | cut -f 2 -d ' ' | cut -f 1 -d '.'`
130
131 CAPA_VOLUMES_APPEND=`$MYSQL << EOF
132 USE bacula
133 SELECT SUM("$AVERAGE_CAPA_VOLUME"-VolBytes) from Media, Pool where Media.PoolId=Pool.PoolId and Pool.Name="$POOL" AND (VolStatus = "Append" OR VolStatus = "Recycle" OR VolStatus = "Purge") AND Inchanger = "1" AND MediaType="$MEDIA_TYPE";
134 EOF
135 `
136 CAPA_VOLUMES_APPEND=`echo $CAPA_VOLUMES_APPEND | cut -f 2 -d ' '`
137
138 if [ $SCRATCH -eq 1 ]
139 then
140 CAPA_VOLUMES_SCRATCH=`$MYSQL << EOF
141 USE bacula
142 SELECT SUM("$AVERAGE_CAPA_VOLUME"-VolBytes) from Media, Pool where Media.PoolId=Pool.PoolId and Pool.Name="Scratch" AND VolStatus = "Append" AND Inchanger = "1" AND MediaType="$MEDIA_TYPE";
143 EOF
144 `
145 CAPA_VOLUMES_SCRATCH=`echo $CAPA_VOLUMES_SCRATCH | cut -f 2 -d ' '`
146 else 
147 CAPA_VOLUMES_SCRATCH=0
148 fi
149
150 CAPA_WARNING=`echo $[(WARNING+100)*CAPA_REQUIRED]/100 | bc | cut -f 1 -d '.'`
151 CAPA_CRITICAL=`echo $[(CRITICAL+100)*CAPA_REQUIRED]/100 | bc | cut -f 1 -d '.'`
152 CAPA_DISP=$[NB_VOLUMES_OUT_OF_RETENTION*AVERAGE_CAPA_VOLUME+CAPA_VOLUMES_APPEND]
153 CAPA_DISP_INCLUDING_SCRATCH=$[CAPA_DISP+CAPA_VOLUMES_SCRATCH]
154
155 MESSAGE="Required : $[CAPA_REQUIRED/1000000000] Go, available : $[CAPA_DISP/1000000000] Go, Volumes Error : $NB_VOLUMES_ERROR"
156
157 if [ "$CAPA_DISP" -gt $CAPA_WARNING ]; then
158    echo $MESSAGE
159    exit 0
160 elif [ "$CAPA_DISP" -gt $CAPA_CRITICAL ];then
161    echo $MESSAGE
162    exit 1
163 elif [ "$CAPA_DISP_INCLUDING_SCRATCH" -gt $CAPA_CRITICAL ];then
164    MESSAGE="${MESSAGE}. Will use Scratch Pool !"
165    echo $MESSAGE
166    exit 1
167 else
168    exit 2
169 fi
170 exit 3
171
172 else
173   echo "No volume in pool ${POOL}"
174   if [ "$CAPA_REQUIRED" -gt 0 ] 
175      then exit 2
176      else exit 0
177   fi
178 fi
179