通过shell监控URL

Jenkins Job(Tomcat_check)

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
#!/bin/bash
######
#used for jenkins job:Tomcat_check
######

ATTEMPTS=20
FAILED=0
file=$JENKINS_HOME/jobs/$JOB_NAME/builds/$BUILD_NUMBER/check
log=$JENKINS_HOME/jobs/$JOB_NAME/builds/$BUILD_NUMBER/log

check(){
echo \<h3\> `date` >> $file
#check函数必须指定参数(URL),否则退出函数(检测结束)
while [ $# != 0 ]
do
env=`grep "$1" /data/ztjy/$NAME/url | grep -v "^#" | awk '{print $1}'`
IP=`echo $1 | awk -F "/" '{print $3}' |awk -F ':' '{print $1}'`
curl -skf -m 1 $1 > /dev/null

#如果返回正常,则显示对应的版本号
if [ $? -eq 0 ];then
ver=`curl -skf -m 1 $1 |grep -i "v" |head -1|dos2unix`
echo \<br\> $env: \<a href=$1\>$IP\<\/a\> ++ $ver >> $file
echo $1 is OK! >> $log
FAILED=0
shift
continue

#如果返回异常,则标注版本号为ERROR并以红色显示
else
FAILED=$(($FAILED + 1))
TIMES=$(($ATTEMPTS - $FAILED + 1))
echo "Waiting for check the URL: $1, retrying after 10 seconds for $TIMES times..." >> $log
sleep 10
if [ $ATTEMPTS -gt $FAILED ];then
continue
else
echo "Failed to check the web-$1" >> $log
fi

echo \<br\> $env: \<a href=$1\>$IP\<\/a\> ++ \<font color='Red'\>Error\<\/font\> >> $file
FAILED=0
shift
continue
fi
done
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
######
#used for jenkins job:Tomcat_check
######

NAME=$1

. /data/ztjy/functions
#定义URL作为参数,可循环对多个url进行检测
args=`cat /data/ztjy/$NAME/url | grep -v "^#" | awk '{print $2}'`
check $args

Maxver=`awk -F "+" '{print $3}' $file | grep -i v | sort |tail -1`
Minver=`awk -F "+" '{print $3}' $file | grep -i v | sort |head -1`
#大版本号则显示为橙色,小版本号则显示为绿色
sed -i "s/$Maxver/ <font color='Orange'>$Maxver<\/font>/g" $file
sed -i "s/$Minver/ <font color='SeaGreen'>$Minver<\/font>/g" $file

执行以上脚本时需要指定预检测的服务/data/ztjy/check.sh $Service


URL check

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
DIR=/data/Version
#列出所有服务
NAME=`ls -l /data/ztjy |grep ^d|awk '{print $9}'`
#定义check函数,用于通过curl循环检测指定服务的所有URL
check(){
> $DIR/$i.htm
echo "<h1> $i </h1>" >> $DIR/$i.htm
echo "<h3/> `date`" >> $DIR/$i.htm
while [ $# != 0 ]
do
env=`grep "$1" /data/ztjy/$i/url | grep -v "^#"| awk '{print $1}'`
IP=`echo $1 | awk -F "/" '{print $3}' |awk -F ':' '{print $1}'`
ver=`curl -skf -m 5 $1 |grep -i "v" |head -1|dos2unix|sed '1 s/^\xef\xbb\xbf//'`
curl -skf -m 5 $1 > /dev/null
if [ $? -eq 0 ];then
echo \<br\> $env: \<a href=$1 target="_blank" \>$IP\<\/a\> ++ $ver >> $DIR/$i.htm
echo $1 is OK !
shift
continue
else
echo \<br\> $env: \<a href=$1\>$IP\<\/a\> ++ \<font color='Red'\>Error\<\/font\> >> $DIR/$i.htm
echo -e "\033[31m$1 is Failue !\033[0m"
shift
continue
fi
done
}

#定义main函数,用于调用check函数
main(){
for i in $NAME
do
args=`cat /data/ztjy/$i/url | grep -v "^#" | awk '{print $2}'`
echo -e "\033[32 ========================= $i ========================= \033[0m"
check $args
Maxver=`awk -F "+" '{print $3}' $DIR/$i.htm | grep -i v | sort |tail -1`
Minver=`awk -F "+" '{print $3}' $DIR/$i.htm | grep -i v | sort |head -1`
sed -i "s/$Maxver/ <font color='Orange'>$Maxver<\/font>/g" $DIR/$i.htm
sed -i "s/$Minver/ <font color='SeaGreen'>$Minver<\/font>/g" $DIR/$i.htm
#/bin/cp $DIR/$i.htm $DIR/$i.html
done
}

#脚本执行入口
if [ $# -eq 1 ];then
echo $NAME |grep $1 > /dev/null
if [ $? -ne 0 ];then
echo "args error"
else
NAME=$1
main
fi
else
main
fi

执行以上脚本时可指定预检测的服务/data/version.sh $Service,不指定则检测所有服务

Version check


由于URL检测结果同时保存为html文件,故可以将其合并一起做成web页面进行监控,且同时可以显示所有服务中每个节点对应的版本号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<head>
<title>掌通家园Version</title>
<!-- 定时刷新页面 -->
<meta http-equiv="refresh" content="30">
</head>
<!-- 定义当前时间 -->
<script type="text/javascript">var myDate = new Date();document.write(myDate.toLocaleString())</script><br/>
<table width="100%" border="1" cellpadding="2" cellspacing="1" style="table-layout:fixed;">
<tbody>
<tr>
<td><iframe frameborder=no height=300 width=500 src="http://10.1.10.62/Version/51110.htm"></iframe><br/></td>
<td><iframe frameborder=no height=300 width=500 src="http://10.1.10.62/Version/51110-web.htm"></iframe><br/></td>
<td><iframe frameborder=no height=300 width=500 src="http://10.1.10.62/Version/Admanage.htm"></iframe><br/></td>
<td><iframe frameborder=no height=300 width=500 src="http://10.1.10.62/Version/AfficheServer.htm"></iframe><br/></td>
</tr>
<tr>
<td><iframe frameborder=no height=450 width=500 src="http://10.1.10.62/Version/AgentServer.htm"></iframe><br/></td>
<td><iframe frameborder=no height=450 width=500 src="http://10.1.10.62/Version/AMS.htm"></iframe><br/></td>
<td><iframe frameborder=no height=450 width=500 src="http://10.1.10.62/Version/AuthAdmin.htm"></iframe><br/></td>
<td><iframe frameborder=no height=450 width=500 src="http://10.1.10.62/Version/AuthServer.htm"></iframe><br/></td>
</tr>
...
</tbody></table>

坚持原创技术分享,您的支持将鼓励我继续创作!
0%