码迷,mamicode.com
首页 > 系统相关 > 详细

shell脚本

时间:2016-09-06 23:41:14      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:shell


1.磁盘剩余空间不足80%

#!/bin/bash

#Usage:if the disk use%>80%,give an alarm

#Author:chengyanli

#Date:2016/7/14


IP=`ip addr show|sed ‘8!d‘|awk ‘{print $2}‘|cut -f 1 -d ‘/‘` #local ip

USER=742019722@qq.com #mail

DISKUSED=`df -h |awk ‘{print $5}‘|sed ‘2!d‘|awk -F ‘%‘ ‘{print $1}‘` #The use of the disk

NUM=80 #The standard number

if [ "$DISKUSED" -gt "$NUM" ] ;then

echo -e "Notice:${IP} disk use>80%,plz do something:\n\n`df -h`\n\n`date +%Y-%m-%d`"|mail -s ‘WARNING‘ $USER

else

echo $IP disk is ok

fi



2.cpu及内存、虚拟内存查看并报警

cat 2_cpu+mem.sh

#!/bin/bash

#Usage:if the cpu or mem or virtmem use% > 80%,give an alarm

#Author:chengyanli

#Date:2016/7/14


IP=`ip addr show|sed ‘8!d‘|awk ‘{print $2}‘|cut -f 1 -d ‘/‘`

USER=742019722@qq.com


#The use of cpu,mem and virtmem

cpu=`top -n 1 | grep Cpu | awk ‘{print $5}‘ |awk -F ‘.‘ ‘{print $1}‘`

mem=`free -m | sed ‘2!d‘ | awk -F ‘ ‘ ‘{print $3/$2*100}‘ | awk -F ‘.‘ ‘{print $1}‘`

virtmem=`free -m|sed ‘4!d‘|awk ‘{print $3/$2*100}‘|awk -F ‘.‘ ‘{print $1}‘`


#This is about cpu

if [ "$cpu" -le 20 ]; then

echo "Notice:${IP} cpu use > 80%,plz do something "| mail -s ‘WARNING‘ $USER

fi


#This is about mem

if [ "$mem" -ge 80 ]; then

echo "Notice:${IP} mem use > 80%,plz do something "| mail -s ‘WARNING‘ $USER

fi


#This is about virtmem

if [ "$virtmem" -ge 80 ]; then

echo "Notice:${IP} virtmem use > 80%,plz do something "| mail -s ‘WARNING‘ $USER

fi


3.进程丢失报警 

cat 3.4_procs.sh

#!/bin/bash

#Usage:if the server process lost,give an alarm

#Author:chengyanli

#Date:2016/7/14


IP=`ip addr show|sed ‘8!d‘|awk ‘{print $2}‘|cut -f 1 -d ‘/‘`

USER=742019722@qq.com


pgrep server > /dev/null 2>&1

if [ $? != 0 ]; then

echo "Notice:${IP} server process lost"|mail -s ‘WARNING‘ $USER

elif [ $? = 0 ];then

echo ‘The server process is ok‘

fi


4.磁盘监控及报警 

cat 5_disk.sh

#!/bin/bash

#Usage:if disk is not ok ,give an alarm

#Author:chengyanli

#Date:2016/07/19


IP=`ip addr show|sed ‘8!d‘|awk ‘{print $2}‘|cut -f 1 -d ‘/‘`

USER=742019722@qq.com


#all disk in localhost

disklist=`ls -l /dev|grep -E ‘\bhd[a-z]$|\bsd[a-z]$‘|awk ‘{print $10}‘`


#check disk is ok,if not then give an alarm

for i in $disklist

do

smartctl -s on -a $disklist|grep OK

if [ $? != 0 ]; then

echo -e "Notice:${IP} $disklist status is not ok:\n\n`smartctl -a $disklist`\n\n`date +%Y-%m-%d`"|mail -s ‘WARNING‘ $USER

fi

done


5.监控ssh的连通性

 cat 6_ssh.sh

#!/bin/bash

#Usage:Juage ssh is ok or not

#Author:chengyanli

#Date:2016/7/18


#This is the module check remote 22

for ip in `cat /mnt/ip_only`

do

nc -z -v -n $ip 22 >/dev/null 2>&1

if [ $? = 0 ];then

echo "${ip}:ssh is ok"

else

echo "{$ip}:ssh is not ok"

fi

done


6.监控crontab正常性

cat 7_crontab.sh

#!/bin/bash

#Usage:judge crontab is normal

#Author:chengyanli

#Date:2016/07/14


#start crontd

/etc/init.d/crond status|grep running /dev/null 2>&1

if [ $? != 0 ]; then

/etc/init.d/crond start >/dev/null 2>&1

fi


#check crontab is ok

/bin/rm -f /tmp/test_cron.txt

echo "*/1 * * * * /bin/touch /tmp/test_cron.txt" > /var/spool/cron/root

sleep 60

if [ -f /tmp/test_cron.txt ]; then

echo ‘the crontab is ok‘

else

echo ‘the crontab is not ok‘

fi


7.home分区不可写

cat 11_home.sh

#!/bin/bash

#Usage:judge the /home is writable or not

#Author:hengyanli

#Date:2016/7/15


#If the file exists,delete it

if [ -f "file" ]; then

rm -f file

fi


#touch file to check home can write

touch /home/file

if [ $? != 0 ]; then

echo ‘The /home can not write‘

else

echo ‘The /home can write‘

fi


本文出自 “真水无香” 博客,请务必保留此出处http://chengyanli.blog.51cto.com/11399167/1846781

shell脚本

标签:shell

原文地址:http://chengyanli.blog.51cto.com/11399167/1846781

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!