-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbdh
executable file
·244 lines (197 loc) · 8.79 KB
/
bdh
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#! /bin/bash
#set alarm thresholds here:
Def_major="70";
Def_critical="85";
Def_urgent="95";
#File that stores the PID program
prFile=".bdhpid";
#Put your incoming Slack app webhook URL here:
SlackWebHook="INSERT YOUR WEBHOOK URL HERE"
usage()
{
echo -e "Usage: $0 [ -g | -m | -k ] [ -d Interval in seconds ] [-s]"
echo -e "\t OPTIONS:"
echo -e "\t -g or -G show all FS as Gb values."
echo -e "\t -m or -M show all FS as Mb values."
echo -e "\t -k or -K show all FS as Kb values."
echo -e "\t -d or -D [INTERVAL IN SECONDS] - It will start the BDH as a deamon process sending alert messages to SLACK always an FS threshold usage has reached. (Need WEBHOOK APP instaled on SLACK)"
echo -e "\t -s or -S - Stop bdh daemon process"
exit 2
}
if [ -Z $1 ]; then
usage
fi
log ()
{
#Change here logdir (log will be generated only in daemon mode).
logdir="/tmp";
fs=$1;
used=$2;
free=$3;
percent=$4;
logType=$5;
PRG_PID=$6;
other=$7;
timeStamp=`date +%D-%H:%M:%S`;
logTimeStamp=`date +%m%d%y`;
if [ ! -d $logdir ];then
echo "$logdir does not exists! Impossible to write file $logdir/bdh_$logTimeStamp.log";
exit 1;
fi
if [[ $logType != "START" && $logType != "STOP" ]]; then
echo $logType " | " $timeStamp" | FS:" $fs "| Used:" $used "kb | Free:" $free "kb | "$percent"%" >> $logdir/bdh_$logTimeStamp.log
elif [[ $logType == "START" ]]; then
echo "$logType | $timeStamp | PID: $PRG_PID INTERVAL: $other | logFile: $logdir/bdh_$logTimeStamp.log" >> $logdir/bdh_$logTimeStamp.log;
elif [[ $logType == "STOP" ]];then
echo "$logType | $timeStamp | Process stopped by $other" >> $logdir/bdh_$logTimeStamp.log;
fi
}
alert()
{
url=$1;
messageType=$2;
FS=$3;
used=$4;
free=$5;
icon=$6;
timeStamp=`date +%D-%H:%M:%S`;
msg="$messageType: $icon FS $FS has only $free Kb free ( $used% used ) - $timeStamp";
json="{\"text\":\"$msg\"}";
#curl -X POST -H 'Content-type: application/json' --data '{"text":\"$msg\"}' $url
curl -s -d "payload=$json" "$url" 1>/dev/null;
}
while getopts d:D:gGmMkKhHsS OPTION; do
case $OPTION in
g | G )
Filesystem=`df -Pk | grep -v ^Filesystem | sed "s#%##g"`;
for FS in "$Filesystem"; do
echo "$FS" | awk -v THR_urg="$Def_urgent" -v THR_cri="$Def_critical" -v THR_maj="$Def_major" 'BEGIN {
format_header="%-40s %-20s %-20s %-15s %-8s %-8s\n"
use_format="";
metricType="Gb";
printf(format_header, "MOUNT POINT", "TOTAL", "USED", "AVAILABLE", "IN USE", "FS");
}
{
total=$2/1024/1024;
used=$3/1024/1024;
available=$4/1024;
F_total=sprintf("%.3f", total);
F_used=sprintf("%.3f", used);
F_available=sprintf("%.3f", available);
format="%-40s %-20s %-20s %-15s %-8s %-8s\n";
urgent="\033[5;31;1m" format "\033[0m";
critical="\033[31;1m" format "\033[0m";
major="\033[33;1m" format "\033[0m";
normal="\033[37;1m" format "\033[0m";
low="\033[32;1m" format "\033[0m";
if ($5 >= THR_maj && $5 < THR_cri) { use_format=major; }
else if ( $5 >= THR_cri && $5 <= THR_urg) { use_format=critical;}
else if ( $5 >= THR_urg ) {use_format=urgent;}
else {use_format=normal;}
printf(use_format, $1, F_total"("metricType")", F_used "("metricType")", F_available"("metricType")", $5"%", $6)
}'
done
;;
m | M )
Filesystem=`df -Pk | grep -v ^Filesystem | sed "s#%##g"`;
for FS in "$Filesystem"; do
echo "$FS" | awk -v THR_urg="$Def_urgent" -v THR_cri="$Def_critical" -v THR_maj="$Def_major" 'BEGIN {
format_header="%-40s %-20s %-20s %-15s %-8s %-8s\n"
use_format="";
metricType="Mb"
printf(format_header, "MOUNT POINT", "TOTAL", "USED", "AVAILABLE", "IN USE", "FS");
}
{
total=$2/1024;
used=$3/1024;
available=$4/1024;
F_total=sprintf("%.2f", total);
F_used=sprintf("%.2f", used);
F_available=sprintf("%.2f", available);
format="%-40s %-20s %-20s %-15s %-8s %-8s\n";
urgent="\033[5;31;1m" format "\033[0m";
critical="\033[31;1m" format "\033[0m";
major="\033[33;1m" format "\033[0m";
normal="\033[37;1m" format "\033[0m";
low="\033[32;1m" format "\033[0m";
if ($5 >= THR_maj && $5 < THR_cri) { use_format=major; }
else if ( $5 >= THR_cri && $5 <= THR_urg) { use_format=critical;}
else if ( $5 >= THR_urg ) {use_format=urgent;}
else {use_format=normal;}
printf(use_format, $1, F_total"("metricType")", F_used "("metricType")", F_available"("metricType")", $5"%", $6)
}'
done
;;
k | K )
Filesystem=`df -Pk | grep -v ^Filesystem | sed "s#%##g"`;
for FS in "$Filesystem"; do
echo "$FS" | awk -v THR_urg="$Def_urgent" -v THR_cri="$Def_critical" -v THR_maj="$Def_major" 'BEGIN {
format_header="%-40s %-20s %-20s %-15s %-8s %-8s\n"
use_format="";
metricType="Kb"
printf(format_header, "MOUNT POINT", "TOTAL", "USED", "AVAILABLE", "IN USE", "FS");
}
{
total=$2;
used=$3;
available=$4;
F_total=sprintf("%.0f", total);
F_used=sprintf("%.0f", used);
F_available=sprintf("%.0f", available);
format="%-40s %-20s %-20s %-15s %-8s %-8s\n";
urgent="\033[5;31;1m" format "\033[0m";
critical="\033[31;1m" format "\033[0m";
major="\033[33;1m" format "\033[0m";
normal="\033[37;1m" format "\033[0m";
low="\033[32;1m" format "\033[0m";
if ($5 >= THR_maj && $5 < THR_cri) { use_format=major; }
else if ( $5 >= THR_cri && $5 <= THR_urg) { use_format=critical;}
else if ( $5 >= THR_urg ) {use_format=urgent;}
else {use_format=normal;}
printf(use_format, $1, F_total"("metricType")", F_used "("metricType")", F_available"("metricType")", $5"%", $6)
}'
done
;;
d | D )
wait=$OPTARG;
if [ -f $prFile ]; then
oldPrFile=`cat $prFile`;
pkill $oldPrFile 2>/dev/null;
fi
while true; do
Filesystem=`df -Pk | grep -v ^Filesystem | sed "s#%##g"`;
while read FS; do
FS_name=`echo $FS | awk '{ print $1 }'`;
used=`echo $FS | awk '{ print $3 }'`;
available=`echo $FS | awk '{ print $4 }'`;
percent=`echo $FS | awk '{ print $5 }'`;
if [[ $percent -ge $Def_urgent ]]; then
alert $SlackWebHook "Urgent" $FS_name $percent $available ":name_badge:";
log $FS_name $used $available $percent "URGENT";
elif [[ $percent -ge $Def_critical ]]; then
alert $SlackWebHook "Critical" $FS_name $percent $available ":bomb:";
log $FS_name $used $available $percent "CRITICAL";
else
log $FS_name $used $available $percent "INFO";
fi
done <<< "$Filesystem"
sleep $wait;
done &
prPID=$!
echo $prPID > $prFile;
log "" "" "" "" "START" "$prPID" "$wait";
;;
s | S )
log "" "" "" "" "STOP" "" "`whoami`";
pkill -F $prFile; 2>/dev/null
rm -f $prFile;
exit 0;
;;
h | H )
usage
;;
\? )
usage
;;
esac
done