shell脚本监控网站并实现邮件、短信报警
shell进程监控脚本(发送邮件报警)
Shell脚本监控服务器在线状态和邮件报警的方法

http://www.jbxue.com/jb/shell/

11.

#!/bin/bash
a="/tmp/test.log"
while [[ -f $a ]];
do
sleep 1;
size=`ls -lrt $a | cut -d " " -f 5`
echo $size
if [ $size -ge 1024 ];
then
logrotate /etc/logrotate.conf;
fi
done a="/mnt/fileserver/daq/check"
if [ ! -f $a ]
do
echo "nfs server is donw"|mail -v -s "nfs" createyuan@sohu.com createyuan1@163.com 403185951@qq.com
fi http://blog.csdn.net/gujing001/article/details/7110589 Shell处理字符串常用方法

10.

请输入要连接的主机

    #!/bin/bash
#written by wubo
#blog:blog.csdn.net/wbls615117
while :
do
echo "请输入你要进行的操作:"
select var in "edit file" "view ip" "delete file" "change directory" "exit" "view directory"
do
break
done
case $var in
"edit file")
echo -n "please input edit file:"
read file
vim $file
echo '编辑文件成功'
;;
"view ip")
echo -n "please input device name:"
read file
ifconfig $file
echo '显示IP地址成功'
;;
"delete file")
echo -n "please input delete file:"
read file
rm -rf $file
echo '成功删除文件'
;;
"change directory")
echo -n "please input change directory:"
read file
cd $file
echo "当前目录为:$(pwd)"
;;
"view directory")
echo -n "please input a directory:"
read file
ls $file
echo "目录浏览成功"
;;
"exit")
break
echo '退出成功'
;;
*)
break
echo '退出成功'
;;
esac
done

9.

#!/bin/bash
IP=`ifconfig eth0 | grep "inet addr" | cut -f -d ":" | cut -f -d " "`
tomcat_dir="/opt/apache-tomcat-7.0.8"
mysql_dir="/usr/local/mysql/bin/mysqld_safe"
vsftp_dir="/usr/sbin/vsftpd"
ssh_dir="/usr/sbin/sshd"
for dir in $tomcat_dir $mysql_dir $vsftp_dir $ssh_dir
do
process_count=$(ps -ef | grep "$dir" | grep -v grep | wc -l)
for service in tomcat mysql vsftp ssh
do
echo "$dir" |grep -q "$service"
if [ $? -eq ]
then
if [ $process_count -eq ]
then
echo "$service is down at $(date +%Y%m%d%H:%M:%S)" >>/usr/monitor/process/process_$(date +%Y%m%d).log
echo "$service is down at $(date +%Y%m%d%H:%M:%S)" | mail -s "$IP服务器 $service服务关闭告警" XXXX@qq.com
else
echo "$service is running at $(date +%Y%m%d%H:%M:%S)" >>/usr/monitor/process/process_$(date +%Y%m%d).log
fi
else
continue
fi
done
done

8.监控日志特定内容

# cat cpu_bug_monitor.sh
#!/bin/bash
grep "BUG: soft lockup - CPU#" /var/log/messages
if [ $? -eq ] ; then
counter=`grep "BUG: soft lockup - CPU#" /var/log/messages | wc -l `
echo "`date` ## CPU BUG: $counter times" | mutt -s "CPU BUG" ****@.com
echo "`date` ## CPU BUG: $counter times" >> /tmp/CPU_BUG_STATUS
else
echo "`date` ## Check CPU BUG normal" >> /tmp/CPU_BUG_STATUS
fi 一旦发现日志中出现"BUG: soft lockup - CPU#"将统计次数并发送到1397710****@.com邮箱。如果正常就记录检查时间和结果到/tmp/CPU_BUG_STATUS。

7.base64bash实现

base64Table=(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z           + /);

function str2binary() {
idx=;
for((i=; i<${#str}; i++)); do
dividend=$(printf "%d" "'${str:i:1}");
for((j=;j<;j++)); do
let idx=*i+-j;
let bin[$idx]=$dividend%;
dividend=$dividend/;
done;
done;
let idx=${#str}*;
for((i=; i<appendEqualCnt*; i++)); do
let bin[$idx]=;
let idx++;
done;
}
function calcBase64() {
for((i=; i<${#bin[*]}/; i++)); do
sum=;
for((j=; j<; j++)); do
let idx=i*+j;
let n=--j;
let sum=sum+${bin[$idx]}***n;
done;
echo -n ${base64Table[$sum]};
done
} declare -a bin
function base64Encode() {
read -p "please enter ASCII string:" str;
let appendZero=${#str}*%;
let bits=${#str}*;
appendEqualCnt=;
if [[ $appendZero -ne ]]; then
let appendEqualCnt=(-$appendZero)/;
fi
str2binary;
calcBase64;
if [[ $appendEqualCnt -eq ]]; then
echo -n "==";
elif [[ $appendEqualCnt -eq ]]; then
echo -n "=";
fi
echo; }

6.颜色码表

[root@-shiyan prog]# cat color
## the test text
T='samples'echo
echo " default 40m 41m 42m 43m 44m 45m 46m 47m"
## FG 为前景(foreground)色, BG 为背景(background)色
for FGs in ' m' ' 1m' ' 30m' '1;30m' ' 31m' '1;31m' ' 32m' '1;32m' ' 33m' '1;33m' ' 34m' '1;34m' ' 35m' '1;35m' ' 36m' '1;36m' ' 37m' '1;37m'
do
FG=$(echo $FGs|tr -d ' ')
echo -en " $FGs \033[$FG $T "
for BG in 40m 41m 42m 43m 44m 45m 46m 47m;
do
echo -en " \033[$FG\033[$BG $T \033[0m"
done
echo
done
echo

5.批量检测url地址是否可以访问的两种写法,for与while

训练点
1.错误时记录到文件中并且同时在控制台输出
2.从文件中循环读入参数,正确错误都记录到文件中,并且在错误时发邮件。 ####输入1:
[root@-shiyan prog]# cat web
#!/bin/bash
monitor_dir=/tmp/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!" |tee -a $web_stat_log
exit
fi for website in `cat $server_list_file`
do
url="http://$website"
server_status_code=`curl -o /dev/null -s -m --connect-timeout -w %{http_code} "$url"`
if [ "$server_status_code" = "" ]
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 ####输入2:
[root@-shiyan prog]# cat server.list
www..com
www..com
www..com
www..com
www..net
www..net
www..org
www..org
www..cn ####输出:
[root@-shiyan prog]# cat /tmp/monitor/web.status
-- :: ERROR:server.list NOT exists!
-- :: visit www..com error!!! server can't connect at 10s or stop response at 10 s, send alerm sms ...
-- :: visit www..com error!!! server can't connect at 10s or stop response at 10 s, send alerm sms ...
-- :: visit www..com error!!! server can't connect at 10s or stop response at 10 s, send alerm sms ...
-- :: visit www..com error!!! server can't connect at 10s or stop response at 10 s, send alerm sms ...
-- :: visit www..net error!!! server can't connect at 10s or stop response at 10 s, send alerm sms ...
-- :: visit www..net error!!! server can't connect at 10s or stop response at 10 s, send alerm sms ...
-- :: visit www..org status code OK
-- :: visit www..org status code OK
-- :: visit www..cn status code OK ####另外一种写法
####输入:
[root@-shiyan prog]# cat web1
#!/bin/bash
while read URL
do
echo `date`
result=`curl -o /dev/null -s -m --connect-timeout -w %{http_code} $URL`
test=`echo $result`
if [[ "$test" = "" ]]
then
echo "$URL is ok"
else
echo "$URL is err"
#/usr/sbin/sendmail -t << EOF
#From:SD-Detect
#To:@.com,@.com
#Subject:Detected $URL
#------------------------------
#${URL} is err!!
#------------------------------
#EOF
fi
done < /root/sh/prog/server.list [root@-shiyan prog]# bash web1
Tue Feb :: CST
www..com is err
Tue Feb :: CST
www..com is err
Tue Feb :: CST
www..com is err
Tue Feb :: CST
www..com is err
Tue Feb :: CST
www..net is err
Tue Feb :: CST
www..net is err
Tue Feb :: CST
www..org is ok
Tue Feb :: CST
www..org is ok
Tue Feb :: CST
www..cn is ok

4.

训练点:
.从格式化的输入文件中将字段分别分配给read的三个变量
.awk的与或判断
.如果错误日志有内容,则将错误内容定义为一个变量,然后传递给邮件函数和短信函数以便通知
.首次执行时,因为没有Curl_Out.txt与Curl_Out_1.txt文件,会出错,所以第一次先注释掉,然后再打开,就会每次都将以前的内容删掉,重新记录。 ####输入1:
[root@-shiyan monitor]# cat aa
#!/bin/bash
smail() {
mail -s "$1" createyuan@sohu.com <<EOF
$
$
====
report time: `date +"%F %T"`
shell script: `echo $`
current user: `whoami`
====
EOF
} ssms() {
/usr/local/feixin/fetion --mobile= --pwd=******** --to= --msg-gb="fx $1"
} cd /tmp/monitor
File=server.list
#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 --max-time -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"||$3=="302"||$3=="401")) {} else {print $0 >> "Curl_Out_1.txt"}}' Curl_Out.txt if [ -s Curl_Out_1.txt ]
then
Warning="`awk '{printf("%s\n",$0)}' Curl_Out_1.txt`"
#ssms ${Warning}
smail CURL_Monitor ${Warning}
fi
输入2:
[root@-shiyan monitor]# cat server.list
192.168.2.2 192.168.2.2
192.168.2.84 192.168.2.84/monitor
192.168.2.222 192.168.2.222
192.168.2.225 192.168.2.225
[root@-shiyan monitor]# ls
aa server.list 输出1:到文件中
[root@-shiyan monitor]# bash aa
[root@-shiyan monitor]# ls
aa Curl_Out_1.txt Curl_Out.txt server.list
[root@-shiyan monitor]# cat Curl_Out.txt
0.044:::192.168.2.2:192.168.2.2
0.004:::192.168.2.84:192.168.2.84/monitor
0.050:::192.168.2.222:192.168.2.222
0.027:::192.168.2.225:192.168.2.225
[root@-shiyan monitor]# cat Curl_Out_1.txt
0.004:::192.168.2.84:192.168.2.84/monitor
输出2:到邮件中,以下是内容
CURL_Monitor
0.004:::192.168.2.84:192.168.2.84/monitor
====
report time: -- ::
shell script: aa
current user: root
====

3.

2.监控磁盘并发邮件

####第一步安装mail客户端,写邮件地址,写脚本
[root@-shiyan ~]# vi disk
#!/bin/bash
yum install mail
mailaddr=createyuan1@.com
smtpserver=smtp..com
user=createyuan1
passwd=*******
cat <<EOF >/etc/mail.rc
set from=$mailaddr
set smtp=$smtpserver
set smtp-auth=login
set smtp-auth-user=$user
set smtp-auth-password=$passwd
EOF space=`df|sed -n '/\/$/p'|gawk '{print $5}'|sed 's/%//'`
if [ $space -ge ]
then
echo "disk is $space" >/tmp/test
mail -v -s "testse" createyuan@sohu.com < /tmp/test
fi ####第二步加入计划任务中执行
[root@-shiyan ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@-shiyan ~]# crontab -l
* * * * bash /root/disk [root@250-shiyan prog]# cat disk1
#!/bin/bash
while sleep 5
  do
    for i in `df -h |sed -n '/\/$/p'|awk '{print $5}'|sed 's/\%//g'`
         do echo $i
         if [ $i -ge 10 ]
         then
         echo "the disk is "
         fi
         done
  done

1.样例

如果是139邮箱还可免费手机短信通知。
注:通过系统直接发送mail容易被拦截,可使用mail连接第三方smtp发送邮件。 #!/bin/bash
for URL in http://www.abc.com http://www.88888.cn
do
#获取http响应代码
HTTP_CODE=`curl -o /dev/null -s -w "%{http_code}" "${URL}"`
#服务器能正常响应,应该返回200的代码
if [ $HTTP_CODE = 200 ]
then
echo "$URL is OK" | /bin/mail -s "Http Check" qq@163.com
# else
# /usr/local/fetion/fetion --mobile=1356440xxxx --pwd 123456 --to=1885151xxxx --msg-utf8="$URL is ERROR; error code is $HTTP_CODE"
fi
done 主要是利用 curl -o /dev/null -s -m 10 --connect-timeout 10 -w %{http_code} "$url" 返回状态码是否200,如果10s没有返回200状态码,则发警报 -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.jbxue.com/index.php"`
200
[coomix@localhost ~]$ echo `curl -o /dev/null -s -m 10 --connect-timeout 10 -w %{http_code} "http://www.jbxue.com/index5.php"`
404

shell脚本实例-系统监控的更多相关文章

  1. shell脚本实例-实现监控tcp的链接状态另一种方式批量创建用户

    Array实现TCP的链接状态 #!/usr/bin/bash declare -A status type=`ss -an | grep :80|awk '{print $2}'` for i in ...

  2. 分享7个shell脚本实例--shell脚本练习必备

    概述 看多shell脚本实例自然就会有shell脚本的编写思路了,所以我一般比较推荐看脚本实例来练习shell脚本.下面分享几个shell脚本实例. 1.监测Nginx访问日志502情况,并做相应动作 ...

  3. 使用shell+python脚本实现系统监控并发送邮件

    1.编辑shell脚本 [root@web03 ~/monitor_scripts]# cat inspect.sh #!/bin/bash # 设置磁盘的阀值 disk_max=90 # 设置监控i ...

  4. shell脚本实例,通向shell脚本大师的必经之路

    概述 读书百遍其义自见,shell脚本也是,只要例子看得多了,自然就知道怎么写了.这里主要整理了20几个例子,因为内容比较多,所以分了几次来做介绍了.下面的实例最好先自己思考怎么去实现,然后再看下实现 ...

  5. linux shell脚本守护进程监控svn服务

    最近搭建的svn服务不知道什么原因服务总是被关闭(如果你不知道怎么搭建svn可以参考linux下搭建svn版本控制软件),因此用shell脚本实现一个守护进程.用于监控svn服务是否启动,如果服务不在 ...

  6. Shell脚本日志关键字监控+告警

    最近小张的爬虫程序越来越多,可当爬虫程序报错,不能及时的发现,从而造成某些重要信息不能及时获取的问题,更有甚者,遭到领导的批评.于是就在想有没有一种方法,当爬取信息报错的时候,可以通过邮件或者短信的方 ...

  7. shell脚本实例

    备注:一些与传递给shell的参数相关的变量:$# 命令行参数的个数$? 调用命令的返回值$$ 当前进程的进程号$! 最后一个后台命令的进程号$0 命令行的第一个参数,也就是命令名$n 命令行的第n个 ...

  8. shell脚本实例一,移动文件夹中大于2000B的文件到另一个文件夹

    shell脚本能帮我们简化linux下的一些工作,现在有个需求,把TMPA文件夹下大于2000B的文件都移动到TMPB下 #! /bin/bash function movefiles() { ` d ...

  9. shell脚本实例一

    一. 什么是shell 脚本时一种解释性语言: shell脚本保存执行动作: 脚本判定命令的执行条件 脚本来实现动作的批量执行.二.如何创建 vim  test.sh     ##shell脚本一般都 ...

随机推荐

  1. python数据分析入门——matplotlib的中文显示问题&最小二乘法

    正在学习<用python做科学计算>,在练习最小二乘法时遇到matplotlib无法显示中文的问题.查资料,感觉动态的加上几条语句是最好,这里贴上全部的代码. # -*- coding: ...

  2. Swift URL Schemes使用

    URL Schemes通常用于分享和第三方登录,但有时需要在html跳至APP,或者APP跳至另外一个APP.这时也需要使用URL Schemes. 一.html跳转至APP eg:html跳转至te ...

  3. 要学JavaScript!进来看吧,反正不花钱!~

    有一段时间没有更新自己的博客了,今天终于可以抽出时间和大家分享一下关于JavaScript了.有很多朋友在问我没有接触过会不会很难,不好理解 其实JavaScript的基本语法和Java的基本语法很相 ...

  4. [转]LUA 学习笔记

    Lua 学习笔记 入门级 一.环境配置 方式一: 1.资源下载http://www.lua.org/download.html 2.用src中的源码创建了一个工程,注释调luac.c中main函数,生 ...

  5. 【转】Fast Entity Component System

    http://entity-systems.wikidot.com/fast-entity-component-system Summary Create a generic System class ...

  6. window.opener调用父窗体方法的用法

    应用实例:        function BindWindowCloss() {            $(window).bind('beforeunload', function () {    ...

  7. 一直纠结中的"底层模板"含义(借鉴)

    无意间看到这个解释,推荐给哪些和我一样迷惑的人!

  8. cookie 和 session 机制

    cookie机制 Cookie实际上是Web服务端与客户端(典型的是浏览器)交互时彼此传递的一部分内容,内容可以是任意的,但要在允许的长度范围之内.客户端会将它保存在本地机器上(如IE便会保存在本地的 ...

  9. Install GD on mac

    Lots of bioinformatics software are based on perl. Some of them, for example, Circos, NGS toolkit... ...

  10. ANTLR3完全参考指南读书笔记[05]

    前言 仅生成给出true/false的识别器是没有多大用处的,自然的就有在识别过程中遇到某一结构时执行一段代码.存储该结构中信息的想法. ANTLR提供了在文法中嵌入属性和动作超级混合“文法”,可以生 ...