利用python為運(yùn)維人員寫一個(gè)監(jiān)控腳本
前言:
一直想寫一個(gè)監(jiān)控方面的腳本,然后想到了運(yùn)維這方面的,后來就寫了個(gè)腳本,下面話不多說了,來一起看看詳細(xì)的介紹吧。
準(zhǔn)備:
psutil模塊(基本使用方法可以參考這篇文章:http://www.dhdzp.com/article/65044.htm)
正文:
import os
import time
import re
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import socket
import psutil
while True:
def jianca():
print('[+]Write a port to a file')
querys=os.popen('netstat -an').read()
wsd=open('netstat.txt','w')
wsd.write(querys)
wsd.close()
jianca()
def swsd():
global usd,ow
wsd=open('netstat.txt','r')
swd=wsd.read()
odf=re.findall('(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d):(3389)',swd)
usd=odf[0]
print('[+]Query the IP address of a remote connection')
df=usd[0],usd[1],usd[1],usd[3]
wdst=".".join(df)
ow=wdst+":"+usd[4]
print(usd[0],'.',usd[1],'.',usd[2],'.',usd[3]+":",usd[4])
swsd()
def ipdw():
global wdf,ip,timsd
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
dw=s.connect(('8.8.8.8',80))
ip=s.getsockname()[0]
print('[+]loacl IP:',ip)
wdf=os.popen('tasklist').read()
timsd=time.strftime('%Y-%m-%d',time.localtime(time.time()))
ipdw()
def yunwei():
global cput,cp
cput=psutil.cpu_times()
cp=psutil.disk_io_counters()
yunwei()
def stm():
serder="搜狐郵箱@sina.cn"
revw="收件箱@qq.com"
zhengwen='[+]Query the IP address of a remote connection''{}\n' \
'[+]loacl IP:{}\n' \
'[+]A program running in the background:{}\n' \
'[+]The user / system / idle time of statistical CPU:{}\n' \
'[+]Disk I/O usage{}\n' \
'[+]Last send time:{}\n' .format(ow,ip,wdf,cput,cp,timsd)
msg=MIMEText(zhengwen)
msg['From']=Header('你的搜狐郵箱@sina.cn')
msg['TO']=Header('收件箱@qq.com','utf-8')
sub="實(shí)時(shí)監(jiān)控"
msg['subject']=Header(sub,'utf-8')
try:
smtp=smtplib.SMTP()
smtp.connect('smtp.sina.cn',25)
smtp.login('搜狐郵箱@sina.cn','登錄密碼')
smtp.sendmail(serder,revw,msg.as_string())
print('[+]發(fā)送出')
except Exception as g:
print('[-]發(fā)送失敗,原因:',g)
stm()
time.sleep(3600)
實(shí)現(xiàn)原理:首先獲取端口狀態(tài),然后寫人一個(gè)txt用正則提取出想要的IP和端口。
然后獲取正在后臺(tái)運(yùn)行的程序。在獲取CPU和磁盤I/O的內(nèi)存,然后通過郵箱發(fā)送‘
到指定收件箱。
運(yùn)行截圖:



總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
pytorch?K折交叉驗(yàn)證過程說明及實(shí)現(xiàn)方式
這篇文章主要介紹了pytorch?K折交叉驗(yàn)證過程說明及實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
python優(yōu)化測(cè)試穩(wěn)定性的失敗重試工具pytest-rerunfailures詳解
筆者在執(zhí)行自動(dòng)化測(cè)試用例時(shí),會(huì)發(fā)現(xiàn)有時(shí)候用例失敗并非代碼問題,而是由于服務(wù)正在發(fā)版,導(dǎo)致請(qǐng)求失敗,從而降低了自動(dòng)化用例的穩(wěn)定性,那該如何增加失敗重試機(jī)制呢?帶著問題我們一起探索2023-10-10
Python中定時(shí)器用法詳解之Timer定時(shí)器和schedule庫(kù)
目前所在的項(xiàng)目組需要經(jīng)常執(zhí)行一些定時(shí)任務(wù),于是選擇使用 Python 的定時(shí)器,下面這篇文章主要給大家介紹了關(guān)于Python中定時(shí)器用法詳解之Timer定時(shí)器和schedule庫(kù)的相關(guān)資料,需要的朋友可以參考下2024-02-02
Python如何獲取響應(yīng)體response body
在Python中,我們可以使用多個(gè)庫(kù)來發(fā)送HTTP請(qǐng)求并獲取響應(yīng)體(response body),其中requests就是最常用的庫(kù)之一,下面我們就來看看如何利用requests發(fā)送HTTP GET請(qǐng)求,并獲取響應(yīng)體吧2024-11-11

