shell脚本实例-系统监控

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
上一篇:springboot+springdatajpa:javax


下一篇:机器学习与Tensorflow(5)——循环神经网络、长短时记忆网络