-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.md
82 lines (71 loc) · 3.01 KB
/
README.md
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
#!/bin/bash
chat_id="10667xxxxx" #### Nhập ID Telegram của bạn
token="5463311202:AAELZ-2u2eU1xxxxxxxxx" #### Nhập Token Telegram của bạn
interface="eth0" ### Nhập tên card mạng server của bạn
time_report=("6:00" "12:00" "17:00" "20:30") ### Nhập khung giờ muốn nhận thông báo
mem_threshold=90; cpu_threshold=90; disk_threshold=90; inode_threshold=90; #Percent
bwin_threshold=80; bwout_threshold=80; #MB
my_text="" #String contain alert
ip_sv=$(ip a | grep -w inet | grep -Ev "103.xxx.xxx.xxx" | awk '{print $2}' | head -n 1 | awk -F "/" '{print $1}')
#Detect resource usage
cpu_usage=$(mpstat -P ALL | grep all | awk '{print $4}' | sed "s/\,/\./g")
mem_usage=$(free -m | awk '/Mem:/ { printf("%3.1f", $3/$2*100) }')
disk_usage=$(df -h / | awk '/\// {print $(NF-1)}' | sed "s/%//g")
inode_usage=$(df -ih / | awk '{print $5}' | sed "s/%//g" | tail -n 1)
bw=$(vnstat -i "$interface" -tr 3)
bw_incoming=$(echo "$bw" | grep rx | awk '{printf "%s %s\t PPS: %s %s", $2, $3, $4, $5}')
bw_outcoming=$(echo "$bw" | grep tx | awk '{printf "%s %s\t PPS: %s %s", $2, $3, $4, $5}')
function telegram_send(){
curl -X POST "https://api.telegram.org/bot"$token"/sendMessage" -d "chat_id="${chat_id}"&text=${my_text}"
}
function float_ge() {
perl -e "{if("$1">="$2"){print 1} else {print 0}}"
}
function auto_check() {
bw_in=$(echo "${bw_incoming}" | grep "Mbit" | awk '{print $1}')
bw_out=$(echo "${bw_outcoming}" | grep "Mbit" | awk '{print $1}')
if \
[[ $(float_ge "${cpu_usage}" "${cpu_threshold}") == 1 ]] || \
[[ $(float_ge "${mem_usage}" "${mem_threshold}") == 1 ]] || \
[[ $(float_ge "${disk_usage}" "${disk_threshold}") == 1 ]] || \
[[ $(float_ge "${inode_usage}" "${inode_threshold}") == 1 ]] || \
[[ ! -z "${bw_in}" && $(float_ge "${bw_in}" "${bwin_threshold}") == 1 ]] || \
[[ ! -z "${bw_out}" && $(float_ge "${bw_out}" "${bwout_threshold}") == 1 ]]; then
my_text=$(echo -e "⛔️Problem: "sv.hosting-1" #### Bạn có thể sửa nội dung thông báo Problem
CPU usage: "${cpu_usage}"%
Memory usage: "${mem_usage}"%
Disk usage: "${disk_usage}"%
Inode usage: "${inode_usage}"%
Bandwith usage:
➡️ In: "${bw_incoming}"
⬅️ Out: "${bw_outcoming}"
")
fi
telegram_send
}
function daily_report(){
now=$(date "+%H:%M")
for i in "${time_report[@]}"
do
if [[ "$now" == "$i" ]];
then
my_text=$(echo -e "✅ Resolved: "sv.hosting-1" #### Bạn có thể sửa nội dung thông báo Resolved
CPU usage: "${cpu_usage}"%
Memory usage: "${mem_usage}"%
Disk usage: "${disk_usage}"%
Inode usage: "${disk_usage}"%
Bandwith usage:
➡️ In: "${bw_incoming}"
⬅️ Out: "${bw_outcoming}"
")
telegram_send
fi
done
unset cpu_usage; unset cpu_usage; unset disk_usage; unset bw_in; unset bw_out;
unset inode_usage; unset bw; unset bw_incoming; unset bw_outcoming;
}
function main(){
auto_check
daily_report
}
main