Chinaunix首页 | 论坛 | 博客
  • 博客访问: 47330
  • 博文数量: 61
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 360
  • 用 户 组: 普通用户
  • 注册时间: 2015-01-05 15:04
文章分类
文章存档

2015年(61)

我的朋友

分类: LINUX

2015-01-26 16:48:05

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

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

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

Bash代码:  

#!/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运行状态监控,没有正常运行则重新启动服务,重启失败发送邮件通知

Bash代码: 

#! /bin/bash  

#MySQL running这个字符串根据数据库版本正常运行时status显示的信息确定  

/sbin/service  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%时,邮件通知

Bash代码:  

#!/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/.rc,在开始部分加入下面的代码,:wq退出保存即可!

Xml代码:  

set from=test@163.com  

set smtmtp=smtp.163.com  

set smtp-auth-user=test  

set smtp-auth-password=test123  

 

  • 本文来自:
阅读(196) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~