]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - modules/private/monitoring/plugins/check_bandwidth
Adjust monitoring resources and add bandwidth checks
[perso/Immae/Config/Nix.git] / modules / private / monitoring / plugins / check_bandwidth
1 #!/bin/bash
2
3 # ============================== SUMMARY =====================================
4 #Author : Ken Roulamellah
5 #Date : 19/07/2018
6 #Version : 1.0
7 # Licence : GPL
8 # ===================== INFORMATION ABOUT THIS PLUGIN ========================
9 #
10 # This plugin checks the average RX and TX bandwidth utilisation. It use
11 # kbytes as measure unite.
12 #
13 # ========================== START OF PROGRAM CODE ===========================
14
15 STATE_OK=0
16 STATE_WARNING=1
17 STATE_CRITICAL=2
18 STATE_UNKNOWN=3
19
20 interface=$( ip route | grep default | awk '{print $5}' | head -n1)
21 function print_usage()
22 {
23 echo "Usage :"
24 echo "$0 [ -i=INTERFACE] [ -ct=COUNT ] -w WARNING -c CRITICAL"
25 echo "This script calculate the average bandwith usage."
26 echo "Default values | interface: ${interface}, counter: 10"
27 }
28
29 counter=10
30 warning=-1
31 critical=-1
32
33 sum_rx=0
34 sum_tx=0
35 avg_rx=
36 avg_tx=
37 i=
38
39
40 if [[ $# -lt 4 ]];
41 then
42 echo "Error: Arguments are missing"
43 print_usage
44 exit $STATE_UNKNOWN
45 fi
46
47 while [[ $# -gt 0 ]]; do
48 case "$1" in
49 -i=*)
50 interface="$(cut -d'=' -f2 <<<"$1")"
51 shift
52 ;;
53 -ct=*)
54 counter="$(cut -d'=' -f2 <<<"$1")"
55 shift
56 ;;
57 -w)
58 warning=$2
59 shift 2
60 ;;
61 -c)
62 critical=$2
63 shift 2
64 ;;
65 *)
66 printf "\nError: Invalid option '$1'"
67 print_usage
68 exit $STATE_UNKNOWN
69 ;;
70 esac
71 done
72
73 if [ $warning -lt 0 ] || [ $critical -lt 0 ];
74 then
75 echo "Error: You need to specify a warning and critical treshold"
76 print_usage
77 exit $STATE_UNKNOWN
78 fi
79
80 grep -q "up" /sys/class/net/$interface/operstate || exec echo "$interface: no such device or down"
81
82 read rx <"/sys/class/net/$interface/statistics/rx_bytes"
83 read tx <"/sys/class/net/$interface/statistics/tx_bytes"
84
85 i=$counter
86 while [ $i -gt 0 ]; do
87 sleep 1
88 read newrx <"/sys/class/net/$interface/statistics/rx_bytes"
89 read newtx <"/sys/class/net/$interface/statistics/tx_bytes"
90
91 #echo "old rx :$rx"
92 #echo "new rx :$newrx"
93 rx_cal=$(bc <<< "scale=2; ($newrx-$rx) / 1000")
94 tx_cal=$(bc <<< "scale=2; ($newtx-$tx) / 1000")
95
96 sum_rx=$(bc <<< "scale=2;$sum_rx+$rx_cal")
97 sum_tx=$(bc <<< "scale=2;$sum_tx+$tx_cal")
98
99 #echo "$interface {rx: $rx_cal ko/s, tx: $tx_cal ko/s}"
100 rx=$newrx
101 tx=$newtx
102 ((i --))
103 done
104
105 avg_rx=$(bc <<< "scale=2;$sum_rx/$counter")
106 avg_tx=$(bc <<< "scale=2;$sum_tx/$counter")
107
108 #echo "$avg_rx"
109 #echo "$avg_tx"
110
111
112 if [ $(bc <<< "$avg_rx > $critical || $avg_tx > $critical") -eq 1 ]; then
113 echo "$interface CRITICAL - AVG_RX: $avg_rx kb/s, AVG_TX:
114 $avg_tx kb/s | RX="$avg_rx"kbps;0;0;0; TX="$avg_tx"kbps;0;0;0;"
115 exit $STATE_CRITICAL
116 elif [ $(bc <<< "$avg_rx > $warning || $avg_tx > $warning") -eq 1 ]; then
117 echo "$interface WARNING - AVG_RX: $avg_rx kb/s, AVG_TX: $avg_tx kb/s | RX="$avg_rx"kbps;0;0;0; TX="$avg_tx"kbps;0;0;0;"
118 exit $STATE_WARNING
119 else
120 echo "$interface - OK AVG_RX: $avg_rx kb/s, AVG_TX: $avg_tx kb/s | RX="$avg_rx"kbps;0;0;0; TX="$avg_tx"kbps;0;0;0;"
121 exit $STATE_OK
122 fi
123 exit 3