码迷,mamicode.com
首页 > 其他好文 > 详细

简单监控liunx中cpu、内存、磁盘及发送邮件参考

时间:2018-11-24 14:41:33      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:简单监控   span   发送邮件   .exe   linux中   date   df -h   echo   stdout   

 shell脚本

vim jk.sh  #命名脚本名
 
#!/bin/bash
time=`date "+%Y-%m-%d %H:%M:%S"`      #定义时间
echo "$time"
echo  "     警告!!!!!!"
 
cpu_info(){
cpu_free=`top -i -c -bn 1| grep Cpu | awk -F ":" ‘{print $2}‘ | awk -F "," ‘{print $4}‘ | awk -F " " ‘{print $1}‘| awk -F "." ‘{print $1}‘`  #取cpu空闲值
echo "cpu剩余量:" $cpu_free"%"
}
 
mem_info(){
mem_used=` free -h | grep Mem | awk -F ":" ‘{print $2}‘ | awk -F " " ‘{print $6}‘`   #取可用内存值
echo "---mem:"
echo "可用内存为:"$mem_used
}
 
disk_info(){
disk_total=`df -h | grep cent | awk -F " " ‘{print $2}‘`          #取磁盘总量
disk_used=`df -h | grep cent | awk -F " " ‘{print $3}‘`         #取磁盘使用量
disk_per=`df -h | grep cent | awk -F " " ‘{print $5}‘`        #取磁盘使用量率
echo "---disk:"
echo "磁盘总量:"$disk_total
echo "磁盘使用量:"$disk_used
echo "磁盘使用率:"$disk_per
}
 
mem=` free -h | grep Mem | awk -F ":" ‘{print $2}‘ | awk -F " " ‘{print $6}‘|sed s/M//g`  #把使用内存中的去M掉便于比较
disk=`df -h | grep cent | awk -F " " ‘{print $5}‘|sed s/%//g`   #把硬盘使用率中的百分号去掉便于比较
war_cpu=10   #定义cpu阈值值   
war_mem=100 #定义可用内存阈值100M
war_disk=90   #定义磁盘使用率阈值
 
main(){
if [ $cpu -lt $war_cpu  ] || [ $mem -lt $war_mem ] ||[ $disk -gt $war_disk ]  
 #cpu剩余空间小于10   可用内存小于100M  磁盘使用率大于90
then
echo "---cpu:"  
cpu_info
mem_info
disk_info
echo ".........等待处理........."
 
else
echo "-------------"
echo "Server‘s fine"
echo "-------------"
 
fi
}
main
                                                                                  

用python实现发邮件

#导入模块
import paramiko
import smtplib
from email.mime.text import MIMEText
from email.header import Header
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 
def send_mail(message):
    sender = ‘aa1052995312@163.com
    receiver = [‘ruri9999@163.com‘]
    subject = ‘报警‘
    username = ‘aa1052995312@163.com
    password = ‘123456‘
    msg = MIMEText(message, ‘plain‘, ‘utf-8‘)
    msg[‘Subject‘] = Header(subject, ‘utf-8‘)
    msg[‘From‘] = ‘warning<aa1052995312@163.com>‘
    msg[‘To‘] = "ruri9999@163.com"
    smtp = smtplib.SMTP()
    smtp.connect(‘smtp.163.com‘)
    smtp.login(username, password)
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()
 
def server():
    ssh.connect("192.168.88.31", 22, ‘root‘, ‘123‘, timeout=3)  #连接192.168.88.31 端口22 用户root 密码123
    stdin, stdout, stderr = ssh.exec_command(‘./jk.sh ‘ )   #在linux中执行./jk.sh
    msg = stdout.read().decode(‘utf_8‘)
    war = "警告"
    while True:
        if war in msg:
            send_mail(msg)
            print(‘发送成功!!!‘)
            break
        else:
            print("server‘s find.")
            break
server()
 
 

简单监控liunx中cpu、内存、磁盘及发送邮件参考

标签:简单监控   span   发送邮件   .exe   linux中   date   df -h   echo   stdout   

原文地址:https://www.cnblogs.com/heiguu/p/10011369.html

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