shell腳本-監控系統資源並通過短信報警

公司採用nagios監控,通過nagios client將需要的數據收集並傳給nagios服務器,目前所遇到的問題是我們公司部分機器是再其他機房,例如:東航,是不允許安裝客戶端並訪問外網的,爲了更好的檢測服務器狀態,遇到問題可以發短信和郵件的方式通知運維人員進行處理,與開發人員協商開放了短信、郵件兩個接口,通過腳本的方式將監控服務器狀態,遇到故障後通過腳本方式發送報警。 


目標分析:


  所需的監控資源:

       1、登陸用戶數

       2、CPU負載

       3、服務探測

       4、硬盤空間(根分區、應用分區、備份分區)

       5、內存資源

  短信接口、郵件接口


       格式上傳至附件


腳本:

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
#!/bin/bash 
#監控用戶登錄 
Usermonitor ()  { 
LoginUser=`uptime | awk'{print $6}'
if[ $LoginUser -ge2 ] 
then
Critical="當前系統登錄用戶人數超過1人,具體人數爲:$LoginUser 個,請確認操作者人數。"
status=0 
else
echo"loginuser ok"
status=1 
fi
#監控內存 
MemMonitor () { 
MemTotal=`free-m | grepMem | awk-F: '{print $2}'awk'{print $1}'
MemFree=`free-m | grepcache | awkNR==2 | awk'{print $4}'
MemFreeB=`awk'BEGIN{printf "%.2f%\n",'$MemFree/$MemTotal\*100'}'
MemFreeS=`awk'BEGIN{printf "%.f",'$MemFree/$MemTotal\*100'}'
if[ $MemFreeS -lt  10 ] 
then
Critical="系統可用內存小於10%,實際可用內存爲:$MemFreeB ,請處理。"
status=0 
elif[ $MemFreeS -lt 20 ] 
then
Warning="系統可用內存小於20%,實際可用內存爲:$MemFreeB ,請查看。"
WarningT="內存報警"
status=1 
else
echo"Mem OK"
status=2 
fi
#監控分區空間大小 
DiskMonitorG () { 
#根分區 
DiskGB=`df-h | awkNR==2 | awk'{print $5}'
DiskGS=`df-h | awkNR==2 | awk'{print $5}'awk-F% '{print $1}'
if[ $DiskGS -gt 90 ] 
then
Critical="根分區使用率超過90%,實際已使用 $DiskGB ,請處理。"
status=0 
elif[ $DiskGS -gt 80 -a $DiskGS -lt 90 ] 
then
Warning="根分區使用率超過80%,實際已使用 $DiskGB , 請查看。"
WarningT="根分區報警"
status=1 
else
echo"DiskGB Ok"
status=2 
fi
DiskMonitorA () { 
#應用分區 
ApplyB=`df-h | awkNR==4 | awk'{print $5}'
ApplyS=`df-h | awkNR==4 | awk'{print $5}'awk-F% '{print $1}'
if[ $ApplyS -gt 90 ] 
then
Critical="應用分區使用率超過90%,實際已使用 $ApplyB ,請處理."
status=0 
elif[ $ApplyS -gt 80 -a $ApplyS -lt 90 ] 
then
Warning="應用分區使用率超過80%,實際已使用 $ApplyB ,請查看。"
WarningT="應用分區報警"
status=1 
else
echo"Apply ok"
status=2 
fi
#監控CPU負載 
CPULoad () { 
CPULoad1=`uptime | awk'{print $10}'awk-F. '{print $1}'
CPULoad2=`uptime` 
if[ $CPULoad1 -gt 5 ] 
then
Critical="CPU負載過高,請即使處理。 $CPULoad2 "
status=0 
elif[ $CPULoad1 -gt 3 -a $CPULoad1 -lt 5 ] 
then
Warning="CPU負載警告, $Warning "
WarningT="CPU負載報警"
status=1 
else
echo"CPU OK"
status=2 
fi
#監控服務狀態 
ServerMonitor () { 
#服務狀態監控 
timeout=10 
makfails=2 
fails=0 
success=0 
whiletrue
do
/usr/bin/wget--timeout=$timeout --tries=1 http://192.168.20.84/ -q -O /dev/null
if[ $? -ne0 ] 
then
letfails=fails+1 
success=0 
else
fails=0 
letsuccess=1 
fi
if[ $success -ge1 ] 
then
exit
fi
if[ $fails -ge1 ] 
then
Critical="TMS應用服務出現故障,請緊急處理!"
echo$Critical | mutt -s "服務down"[email protected] 
exit-1 
fi
done
#發送報警短信、報警郵件 
forinUsermonitor MemMonitor DiskMonitorG DiskMonitorA CPULoad ServerMonitor 
do
$n 
if[ $status -eq0 ] 
then
curl "http://172.20.36.118/app/tms.do?tranCode=TM0311&content=$Critical"
elif[ $status -eq1 ] 
then
curl "http://172.20.36.118/app/tms.do?tranCode=TM0310&title=$WarningT&content=Warning"
else
echo"ok"
fi
done
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章