-
Notifications
You must be signed in to change notification settings - Fork 2
/
check-filesize.sh
executable file
·157 lines (137 loc) · 4.92 KB
/
check-filesize.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/bash
# --------------------------------------------------------------------
# **** BEGIN LICENSE BLOCK *****
#
# Version: MPL 1.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is echocat management.
#
# The Initial Developer of the Original Code is Daniel Werdermann.
# Portions created by the Initial Developer are Copyright (C) 2011
# the Initial Developer. All Rights Reserved.
#
# **** END LICENSE BLOCK *****
# --------------------------------------------------------------------
# https://source.echocat.org/browse/management/monitoring/nagios/plugins/check_filesize
# --------------------------------------------------------------------
# Check if a file size is smaller then given parameters.
#
# @author: Daniel Werdermann / [email protected]
# @version: 1.1
# @date: 2011-11-04 16:19:42Z
#
# changes 1.1
# - add licence information
# --------------------------------------------------------------------
#
# --------------------------------------------------------------------
# configuration
# --------------------------------------------------------------------
PROGNAME=$(basename $0)
WARN_MESG=()
CRIT_MESG=()
LOGGER="/bin/logger -i -p kern.warn -t"
export PATH="/bin:/usr/local/bin:/sbin:/usr/bin:/usr/sbin"
LIBEXEC="/opt/nagios/libexec /opt/nagios-3/libexec"
for i in ${LIBEXEC};do
[ -r ${i} ] && . ${i}/utils.sh && break
done
if [ -z "$STATE_OK" ];then
echo "nagios utils.sh not found" &>/dev/stderr
exit 1
fi
# --------------------------------------------------------------------
# --------------------------------------------------------------------
# functions
# --------------------------------------------------------------------
function log() {
$LOGGER ${PROGNAME} "$@";
}
function usage() {
echo "Usage: $PROGNAME -w BYTESIZE -c BYTESIZE FILE [FILE2 FILE3 ...]"
echo "Usage: $PROGNAME -h,--help"
echo "Options:"
echo " FILES Check this file(s)"
echo " -w Bytes Warn if filesize greater then this"
echo " -c Bytes Critical filesize greater this"
}
function print_help() {
echo ""
usage
echo ""
echo "Check if filesize is smaller then given parameters"
echo ""
echo "This plugin is NOT developped by the Nagios Plugin group."
echo "Please do not e-mail them for support on this plugin, since"
echo "they won't know what you're talking about."
echo ""
echo "For contact info, read the plugin itself..."
}
# --------------------------------------------------------------------
# startup checks
# --------------------------------------------------------------------
if [ $# -eq 0 ]; then
usage
exit $STATE_CRITICAL
fi
while [ "$1" != "" ]
do
case "$1" in
--help) print_help; exit $STATE_OK;;
-h) print_help; exit $STATE_OK;;
-w) WARN=$2; shift 2;;
-c) CRIT=$2; shift 2;;
/*) FILES="${FILES} $1"; shift;;
*) usage; exit $STATE_UNKNOWN;;
esac
done
if [ $WARN -gt $CRIT ] ; then
log "UNKNOWN: warn value is greater then crit value"
exit $STATE_UNKNOWN
fi
# --------------------------------------------------------------------
# now we check if ...
# 1) ... file exists
# 2) ... has the correct filesize
# --------------------------------------------------------------------
for FILE in ${FILES} ; do
if [ ! -f $FILE ]; then
log "CRITICAL: $FILE don't exists"
CRIT_MESG[${#CRIT_MESG[*]}]="${FILE} don't exists"
fi
SIZE=$(stat -c %s $FILE)
if [ $SIZE -gt $CRIT ] ; then
log "CRITICAL: ${FILE} has size ${SIZE} Byte. Critical at ${CRIT}."
CRIT_MESG[${#CRIT_MESG[*]}]="${FILE} has size ${SIZE} Byte. Critical at ${CRIT}."
elif [ $SIZE -gt $WARN ] ; then
log "WARN: ${FILE} has size ${SIZE} Byte. Warn at ${CRIT}."
WARN_MESG[${#WARN_MESG[*]}]="${FILE} has size ${SIZE} Byte. Warn at ${WARN}."
fi
done
if [ ${#CRIT_MESG[*]} -ne 0 ]; then
echo -n "CRITICAL: "
for element in "${CRIT_MESG[@]}"; do
echo -n ${element}" ; "
done
echo
exit $STATE_CRITICAL
elif [ ${#WARN_MESG[*]} -ne 0 ]; then
echo -n "WARN: "
for element in "${WARN_MESG[@]}"; do
echo -n ${element}" ; "
done
echo
exit $STATE_WARNING
fi
echo "OK: filesize of ${FILES} is smaller then ${WARN} Bytes."
exit $STATE_OK