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
| [iyunv@lb01 extra]# cd /server/scripts/
[iyunv@lb01 scripts]# vi nginx_check.sh
#!/bin/bash
# oldboy training 21 zhangyao
# Defined variables
NginxDir=/application/nginx
ExtraPath=$NginxDir/conf/extra
ScriptDir=/server/scripts
StatusLog=$ScriptDir/status.log
StatusHtml=$NginxDir/html/status/status.html
StatusHtmlOri=$NginxDir/html/status/status.html.ori
# Judge some files
[ -d $NginxDir ] ||exit 1
[ -d $ScriptDir ] ||mkdir -p $ScriptDir
[ -f $StatusLog ] ||touch $StatusLog
[ -f $StatusHtml ] ||touch $StatusHtml
# Defined Check URL Functions
function check_url(){
status=`curl -s $2/check.html`
if [ "$status" == "OK" ]
then
echo "$1 $2 up" >>$StatusLog
else
echo "$1 $2 down" >>$StatusLog
fi
}
# Defined List URL and Check Functions
function check(){
>$StatusLog
cd $ExtraPath
for file in `ls` #首先遍历extra目录下的所有文件,然后遍历每个文件的IP行,将参数传给check_url
do
url=(`awk -F "[ ]+" '/server/ {print $3}' $file`)
for i in ${url}
do
check_url $file $i
done
done
}
# Defined Html Table Format Functions
function table(){ #将表格的一行语句累加后一次性插入html文件
char="<tr bgcolor="$1"><th>$2</th><th>$3</th><th>$4</th><th>$5</th></tr>"
sum="$sum""$char"
}
function html(){
Index=1 #表格最左侧的一列,初始值为1
flag=0
sum="" #行语句初始值null
/bin/cp $StatusHtmlOri $StatusHtml #将status html文件初始化
while read line #一行行读入$StatusLog文件,格式为dynamic_pools 10.0.0.6:80 up
do
array_line=($line)
if [ "${array_line[2]}" == "up" ]
then
table "#90EE90" $Index ${array_line} #将颜色参数、index值及其他参数传给table函数
else
table "#FF0000" $Index ${array_line}
((flag++)) #down情况下flag会计数
fi
((Index++))
done<$StatusLog
[ $flag -eq 0 ] ||\ #如果flag不为0,肯定有down机器,增加一个语音报警的功能,仿照zabbix
sum=$sum"<audio id="clickSound" autoplay="autoplay"><source src="warning.mp3" type="audio/mpeg"></audio>"
sed -i "/C0C0C0/a $sum" $StatusHtml #将sum语句插入html文件
}
# Defined Main Functions
function main(){
while true
do
check
html
sleep 5
done
}
main
|