linux下利用curl监控网页shell脚本
smail() {
mail -s "$1" gjw_apparitor@gmail.com <<EOF
$1
$2
====
report time: `date +"%F %T"`
current user: `whoami`
shell script: `echo $0`
====
EOF
}
ssms() {
/usr/local/feixin/fetion --mobile=150000000 --pwd=******** --to=13810000000 --msg-gb="fx $1"
}
cd /home/maintain/gaojianwei/Script/
File=Monitor_IP.txt
sed -i /.*/d Curl_Out.txt
sed -i /.*/d Curl_Out_1.txt
sed -e '/^#/d;/^$/d' ${File} | while read Ip Port URL
do
/usr/bin/curl
--connect-timeout 8 --max-time 12 -o /dev/null -s -w
%{time_total}:%{size_download}:%{http_code} http://${URL} -x
${Ip}:${Port} >> Curl_Out.txt
echo ":${Ip}:${URL}" >> Curl_Out.txt
done
awk
-F":"
'{if(($1*1000<8000)&&($2>0)&&($3=="200"||$3=="301"||$30=="302"||$3=="401"))
{} else {print $0 >> "Curl_Out_1.txt"}}' Curl_Out.txt
if [ -s Curl_Out_1.txt ];then
Warning="`awk '{printf("%s#",$0)}' Curl_Out_1.txt`"
ssms ${Warning}
smail CURL_Monitor ${Warning}
fi
|
curl是一个命令行下的http下载工具,类似wget。与wget相似,它也可以通过发送指定的http header到服务器来判断服务的状态。
这里介绍一个使用curl监控页面可用性的方法。
可以使用下面的命令,来采集页面的状态码。如果这条命令返回结果为200,说明服务正常。如果返回的是其他的页面,说明异常。
curl -o /dev/null -s -w %{http_code} http://zys.8800.org/
-o 参数,是把下载的所有内容都重定向到/dev/null,-s命令,是屏蔽了curl本身的输出,而-w参数,是根据我们自己的需要,自定义了curl的输出格式。
使用这条命令,再配合邮件和短信,就可以实现对页面的可用性监控。将这个程序部署在全国各地的机器上,就可以对cdn网络进行可用性监控。
|
curl只返回服务器响应状态,不返回内容,返回200是正常的,其它的不正常,简单的命令如下:
[coomix@localhost ~]$ echo `curl -o /dev/null -s -m 10 --connect-timeout 10 -w %{http_code} "http://www.coomix.net/index.jsp"`
200
[coomix@localhost ~]$ echo `curl -o /dev/null -s -m 10 --connect-timeout 10 -w %{http_code} "http://www.coomix.net/index5.jsp"`
404
====================================================
监控机器列表文件:
server.list
server1
server2
server3
建立监控脚本: webstatus.sh
#!/bin/sh
monitor_dir=/home/admin/monitor/
if [ ! -d $monitor_dir ]; then
mkdir $monitor_dir
fi
cd $monitor_dir
web_stat_log=web.status
if [ ! -f $web_stat_log ]; then
touch $web_stat_log
fi
server_list_file=server.list
if [ ! -f $server_list_file ]; then
echo "`date '+%Y-%m-%d %H:%M:%S'` ERROR:$server_list_file NOT exists!" >>$web_stat_log
exit 1
fi
#total=`wc -l $server_list_file|awk '{print $1}'`
for website in `cat $server_list_file`
do
url="http://$website/app.htm"
server_status_code=`curl -o /dev/null -s -m 10 --connect-timeout 10 -w %{http_code} "$url"`
if [ "$server_status_code" = "200" ]; then
echo "`date '+%Y-%m-%d %H:%M:%S'` visit $website status code 200 OK" >>$web_stat_log
else
echo "`date '+%Y-%m-%d %H:%M:%S'` visit $website error!!! server can't
connect at 10s or stop response at 10 s, send alerm sms ..."
>>$web_stat_log
echo "!app alarm @136xxxxxxxx server:$website can't connect at 10s or
stop response at 10s ..." | nc smsserver port &
fi
done
exit 0
主要是利用 curl -o /dev/null -s -m 10 --connect-timeout 10 -w %{http_code} "$url" 返回状态码是否200,如果10s没有返回200状态码,则发警报
最后让linux 定时执行脚本:
crontab -e
*/10 * * * * /home/admin/app/bin/webstatus.sh
这样每隔10分钟就会执行一次
这个是另外一种脚本写法:
#!/bin/bash
while read URL
do
echo `date`
result=`curl -o /dev/null -s -m 10 --connect-timeout 10 -w %{http_code} $URL`
test=`echo $result`
if [[ "$test" = "200" ]]
then
echo "$URL is ok"
else
echo "test err"
/usr/sbin/sendmail -t << EOF
From:SD-Detect
To:13918888888@139.com,13800000000@139.com
Subject:Detected $URL
------------------------------
${URL} is err!!
------------------------------
EOF
fi
done < /root/jiankong/httplist.txt
linux下利用curl监控网页shell脚本的更多相关文章
- curl命令,curl实现post,curl监控网页shell脚本,curl多进程实现并控制进程数,
cURL > Docs > Tutorial: http://curl.haxx.se/docs/httpscripting.html 下载单个文件,默认将输出打印到标准输出中(STDO ...
- linux下利用curl监控web应用状态
监控机器列表文件: server.list 建立监控脚本: webstatus.sh #!/bin/sh monitor_dir=/home/admin/monitor/ #Log记 ...
- 如何在linux下编写一个简单的Shell脚本程序
在了解了linux终端和其搭配的基本Shell(默认为bash)的基础下,我们就可以在终端中用vi/vim编辑器编写一个shell的脚本程序了 Shell既为一种命令解释解释工具,又是一种脚本编程语言 ...
- Linux下备份MySQL数据库的Shell脚本
数据库每天都想备份,手动备份太麻烦而又容易忘记,所以写了一个自动备份MySQL数据库的脚本,加入定时计划中,每天自运运行. 创建Shell脚本代码如下,命名为mysql_dump.sh #!/bin/ ...
- linux下利用JMX监控Tomcat
利用JMX监控Tomcat,就是相当于部署在tomcat上的应用作为服务端,也就是被管理资源的对象.然后通过程序或者jconsole远程连接到该应用上来.远程连接需要服务器端提供ip和port.如果需 ...
- Linux下C程序插入执行shell脚本
1.system(执行shell命令) 相关函数 fork,execve,waitpid,popen表头文件 #include<stdlib.h>定义函数 int system(const ...
- Linux下查看所有用户(shell脚本获取)
在Linux系统中,使用者账号管理最重要的两个文件是/etc/password和/etc/shadow.在/etc/password文件中,每一行都代表一个账号,但是有很多账号是系统账号.比如:b ...
- Linux下的IO监控与分析
Linux下的IO监控与分析 近期要在公司内部做个Linux IO方面的培训, 整理下手头的资料给大家分享下 各种IO监视工具在Linux IO 体系结构中的位置 源自 Linux Performan ...
- php利用root权限执行shell脚本 (转)
转一篇博客,之前搞这个东西搞了好久,结果今天晚上看到了一篇救命博客,瞬间开心了...转载转载 利用sudo来赋予Apache的用户root的执行权限,下面记录一下: 利用PHP利用root权限执行sh ...
随机推荐
- hdu 1166:敌兵布阵(树状数组 / 线段树,入门练习题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- 【现代程序设计】homework-04
题目要求: 第四次作业,构造一个方阵将指定单词填入 stage 1:每个单词只出现1次,且八个方向各至少有两个单词 stage 2:矩阵长宽相等 stage 3:方阵的四个角都要参与单词的构建 算法思 ...
- codeforces Gym 100187J J. Deck Shuffling dfs
J. Deck Shuffling Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...
- android 定时器的实现
在Android上常用的定时器有两种,一种是Java.util.Timer,一种就是系统的AlarmService了. 实验1:使用Java.util.Timer. 在onStart()创创建Time ...
- contentprovider的学习实例总结
工作中遇到了contentprovider数据共享机制,下面来总结一下: 一.ContentProvider简介 当应用继承ContentProvider类,并重写该类用于提供数据和存储数 ...
- 介绍开发Android手持终端PDA盘点APP软件
介绍开发Android手持终端PDA盘点APP软件 软件需要自动识别我导入的TXT格式或者excl格式的盘点表,然后自动生成一个复盘数据,做AB比对,界面上需要显示的有总数量,单品数量,条码,编码,商 ...
- LightOJ1079 Just another Robbery(DP)
题目大概是抢劫银行,每家银行都有一个收益和一个被抓的概率,求在被抓概率小于P的条件下能获得的最大收益. dp[i][j]表示抢劫前i家银行收益为j被抓的最小概率 dp[0][0]=0 dp[i][j] ...
- lua if
function fact(n) then else ) end end print("输入一个数") a=io.read("*number") print(f ...
- 游戏 scrollView
using UnityEngine; using System.Collections; public class LLL : MonoBehaviour { Vector2 scrollPositi ...
- BZOJ1807 : [Ioi2007]Pairs 彼此能听得见的动物对数
一维的情况: 排序后维护一个单调指针即可,时间复杂度$O(n\log n)$. 二维的情况: 旋转坐标系后转化为二维数点问题,扫描线+树状数组维护即可,时间复杂度$O(n\log n)$. 三维的情况 ...