python3監(jiān)控CentOS磁盤空間腳本
Python腳本監(jiān)控CentOS磁盤空間,任何一個(gè)分區(qū)空間使用大于80%即發(fā)郵件給到指定郵箱。
monitor.py
#-*- coding: utf-8 -*-
import socket
import subprocess
import smtplib
from email.mime.text import MIMEText
sender="xxx.xx@xxx.com"
receiver= ["xxx.xx@xxx.com"]
smtpHost="10.134.xxx.xxx"
smtpPort="587"
def get_ip():
hostname = socket.getfqdn(socket.gethostname())
ip = socket.gethostbyname(hostname)
return ip
def send_mail(receiver,subject,content):
ip = get_ip()
msg = MIMEText(content,_subtype='plain',_charset='utf-8')
msg['Subject'] = subject
msg['From'] = 'CLOUD SERVER ' + ip
msg['To'] = ",".join(receiver)
try:
smtp = smtplib.SMTP(smtpHost,smtpPort)
#smtp.set_debuglevel(1)
smtp.docmd("HELO Server")
smtp.ehlo("ismetoad")
smtp.starttls()
smtp.helo("ismetoad")
smtp.sendmail(sender,receiver,msg.as_string())
smtp.close()
except Exception as error:
print(error)
def run_cmd(cmd):
process = subprocess.Popen(cmd,shell=True, stdout=subprocess.PIPE,stderr=subprocess.PIPE)
result_f,error_f = process.stdout,process.stderr
errors = error_f.read()
if errors:
pass
result = result_f.read().decode()
if result_f:
result_f.close()
if error_f:
error_f.close()
return result
def disk_check():
subject = ''
result = run_cmd(cmd)
content = '[root@vm-vc02-SR910 ~]# ' + cmd + '\n' + result
result = result.split('\n')
for line in result:
if 'G ' in line or 'M ' in line:
line = line.split()
for i in line:
if '%' in i and int(i.strip('%')) > 80:
subject = '[WARNING] SERVER FILESYSTEM USE% OVER ' + i + ', PLEASE CHECK!'
if subject:
send_mail(receiver,subject,content)
print('email sended')
else:
print('Everything is ok, keep on monitor.')
if __name__ == '__main__':
cmd = 'df -h'
disk_check()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python通過get,post方式發(fā)送http請(qǐng)求和接收http響應(yīng)的方法
這篇文章主要介紹了python通過get,post方式發(fā)送http請(qǐng)求和接收http響應(yīng)的方法,涉及Python使用urllib模塊與urllib2模塊實(shí)現(xiàn)get與post發(fā)送數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下2015-05-05
Python Arrow處理時(shí)間數(shù)據(jù)使用詳解(標(biāo)準(zhǔn)庫之外另一種選擇)
這篇文章主要介紹了Python標(biāo)準(zhǔn)庫之外Arrow處理時(shí)間數(shù)據(jù)的另一種選擇使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
python3.7環(huán)境下sanic-ext未生效踩坑解析
這篇文章主要為大家介紹了python3.7環(huán)境下sanic-ext未生效踩坑解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
python實(shí)現(xiàn)二級(jí)登陸菜單及安裝過程
這篇文章主要介紹了python實(shí)現(xiàn)二級(jí)登陸菜單及安裝過程,,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
From CSV to SQLite3 by python 導(dǎo)入csv到sqlite實(shí)例
今天小編就為大家分享一篇From CSV to SQLite3 by python 導(dǎo)入csv到sqlite實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02
PyTorch預(yù)訓(xùn)練的實(shí)現(xiàn)
這篇文章主要介紹了PyTorch預(yù)訓(xùn)練的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
攻擊者是如何將PHP Phar包偽裝成圖像以繞過文件類型檢測(cè)的(推薦)
這篇文章主要介紹了攻擊者是如何將PHP Phar包偽裝成圖像以繞過文件類型檢測(cè)的,需要的朋友可以參考下2018-10-10

