-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcheck_forkrate.sh
executable file
·105 lines (93 loc) · 3.01 KB
/
check_forkrate.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
#!/bin/bash
# Copyright bitly, Aug 2011
# written by Jehiah Czebotar
DATAFILE="/var/tmp/nagios/nagios_check_forkrate.dat"
VALID_INTERVAL=600
NAGIOS_USER=${SUDO_USER:-$(whoami)}
if ! [ -d "$(dirname "$DATAFILE")" ]; then
install -g "$NAGIOS_USER" -o "$NAGIOS_USER" -m 750 -d "$(dirname "$DATAFILE")"
fi
# :COMMENT:maethor:20210121: Temporaire
if [ -f "${DATAFILE/nagios\//}" ] && [ ! -f "$DATAFILE" ]; then
mv ${DATAFILE/nagios\//} "$DATAFILE"
fi
if [ -f "$DATAFILE" ] && [ ! -O "$DATAFILE" ]; then
echo "UNKNOWN: $DATAFILE is not owned by $USER"
exit 3
fi
OK=0
WARNING=1
CRITICAL=2
UNKNOWN=-1
function usage()
{
echo "usage: $0 --warn=<int> --critical=<int>"
echo "this script checks the rate processes are created"
echo "and alerts when it goes above a certain threshold"
echo "it saves the value from each run in $DATAFILE"
echo "and computes a delta on the next run. It will ignore"
echo "any values that are older than --valid-interval=$VALID_INTERVAL (seconds)"
echo "warn and critical values are in # of new processes per second"
}
while [ "$1" != "" ]; do
PARAM=$(echo "$1" | awk -F= '{print $1}')
VALUE=$(echo "$1" | awk -F= '{print $2}')
case $PARAM in
-w | --warn)
WARN_THRESHOLD=$VALUE
;;
-c | --critical)
CRITICAL_THRESHOLD=$VALUE
;;
--valid-interval)
VALID_INTERVAL=$VALUE
;;
-h | --help)
usage
exit 0;
;;
esac
shift
done
if [ -z "$WARN_THRESHOLD" ] || [ -z "$CRITICAL_THRESHOLD" ]; then
echo "error: --warn and --critical parameters are required"
exit $UNKNOWN
fi
if [[ $WARN_THRESHOLD -ge $CRITICAL_THRESHOLD ]]; then
echo "error: --warn ($WARN_THRESHOLD) can't be greater than --critical ($CRITICAL_THRESHOLD)"
exit $UNKNOWN
fi
NOW=$(date +%s)
min_valid_ts=$((NOW - VALID_INTERVAL))
current_process_count=$(awk '/processes/ {print $2}' /proc/stat)
if [ ! -f $DATAFILE ]; then
mkdir -p "$(dirname $DATAFILE)"
echo -e "$NOW\t$current_process_count" > $DATAFILE
echo "Missing $DATAFILE; creating"
exit $UNKNOWN
fi
# now compare this to previous
mv $DATAFILE{,.previous}
while read -r ts process_count; do
if [[ $ts -lt $min_valid_ts ]]; then
continue
fi
if [[ $ts -ge $NOW ]]; then
# we can't use data from the same second
continue
fi
# calculate the rate
process_delta=$((current_process_count - process_count))
ts_delta=$((NOW - ts))
current_fork_rate=$(echo "$process_delta / $ts_delta" | bc)
echo -e "$ts\t$process_count" >> $DATAFILE
done < $DATAFILE.previous
echo -e "$NOW\t$current_process_count" >> $DATAFILE
echo "fork rate is $current_fork_rate processes/second (based on the last $ts_delta seconds) | rate=${current_fork_rate}processes/s;${WARN_THRESHOLD};${CRITICAL_THRESHOLD};;"
if [[ $current_fork_rate -ge $CRITICAL_THRESHOLD ]]; then
exit $CRITICAL
fi
if [[ $current_fork_rate -ge $WARN_THRESHOLD ]]; then
exit $WARNING
fi
exit $OK