linux下几个简易的系统监控脚本

    公司没有专门的系统管理员,因此一些服务器安全措施也得我们程序员自己去做,对Linux服务器了解不是很多,查了些资料,下面是自己写的几个简易的服务器监控脚本,希望路过的仙人指点指点,进一步修正完善!

    1.服务器登陆用户监控,登陆用户超过两个时发邮件通知,使用139邮箱接收,方便短信通知。

       

#!/bin/bash
IP=`ifconfig eth0 | grep "inet addr"|awk '{print $2}'|cut -f 2 -d ":"`
users=`uptime|awk '{print $6}'`
#if[$users -ge 2]
if [ $users -ge 2 ]
then
  echo "$IP server login users is more than 2"|mail -s "warning:" ****@139.com
fi

    2.MySQL运行状态监控,没有正常运行则重新启动服务,重启失败发送邮件通知

   

#! /bin/bash
#MySQL running这个字符串根据数据库版本正常运行时status显示的信息确定
/sbin/service mysql status | grep "MySQL running" > /dev/null

if [ $? -eq 0 ]
then
        #状态正常检查3306端口是否正常监听
        netstat -ntp | grep 3306 > /dev/null
        if [ $? -ne 0 ]
        then
                /sbin/service mysql restart
                sleep 3
                /sbin/service mysql status | grep " MySQL running" > /dev/null
                if [ $? -ne 0 ]
                then
                        echo "mysql service has stoped ,Automatic startup failure, please start it manually!" | mail -s "mysql is not running" ***@139.com
                 fi

        fi
else
        /sbin/service mysql start
        sleep 2;
        /sbin/service mysql status | grep "MySQL running" > /dev/null
        if [ $? -ne 0 ]
        then
                echo "mysql service has stoped ,Automatic startup failure, please start it manually!" | mail -s "mysql is not running" ***@139.com
        fi
fi

    3.硬盘空间使用状况监控,当有分区空间使用超过80%时,邮件通知

    

#!/bin/bash
#set -x
checkLog=/var/log/check-space.log
fullFlag=0
df -h > $checkLog
percent_list=$(cat $checkLog  | awk '{print $5}' | grep -Eo "[0-9]+")
for num in $percent_list
do
    if [ $num -ge 80 ]; then
        fullFlag=1
    fi
done

if [ $fullFlag -eq 1 ]; then
    echo "$(hostname): used disk space is more than 80%"|mail -s "warning:disk space is not enough" ***@139.com
fi

 ps:邮件通过配置mail使用外部SMTP服务器发送,这里使用163的SMTP服务器。vi 编辑/etc/mail.rc,在开始部分加入下面的代码,:wq退出保存即可!

set [email protected]
set smtp=smtp.163.com
set smtp-auth-user=test
set smtp-auth-password=test123

猜你喜欢

转载自focus-zhong.iteye.com/blog/1838952
今日推荐