利用python为运维人员写一个监控脚本


Posted in Python onMarch 25, 2018

前言:

一直想写一个监控方面的脚本,然后想到了运维这方面的,后来就写了个脚本,下面话不多说了,来一起看看详细的介绍吧。

准备:

psutil模块(基本使用方法可以参考这篇文章:https://3water.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="实时监控"
 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('[+]发送出')
 except Exception as g:
  print('[-]发送失败,原因:',g)
 stm()
 
 time.sleep(3600)

实现原理:首先获取端口状态,然后写人一个txt用正则提取出想要的IP和端口。

然后获取正在后台运行的程序。在获取CPU和磁盘I/O的内存,然后通过邮箱发送‘

到指定收件箱。

运行截图:

利用python为运维人员写一个监控脚本

利用python为运维人员写一个监控脚本

利用python为运维人员写一个监控脚本

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对三水点靠木的支持。

Python 相关文章推荐
python实现的一只从百度开始不断搜索的小爬虫
Aug 13 Python
浅要分析Python程序与C程序的结合使用
Apr 07 Python
python中sys.argv参数用法实例分析
May 20 Python
Python微信库:itchat的用法详解
Aug 14 Python
Atom的python插件和常用插件说明
Jul 08 Python
python高效过滤出文件夹下指定文件名结尾的文件实例
Oct 21 Python
Python设计模式之享元模式原理与用法实例分析
Jan 11 Python
Python快速转换numpy数组中Nan和Inf的方法实例说明
Feb 21 Python
详解python-图像处理(映射变换)
Mar 22 Python
python 实现矩阵按对角线打印
Nov 29 Python
python框架Django实战商城项目之工程搭建过程图文详解
Mar 09 Python
python ansible自动化运维工具执行流程
Jun 24 Python
python实现数据写入excel表格
Mar 25 #Python
使用requests库制作Python爬虫
Mar 25 #Python
利用Python代码实现数据可视化的5种方法详解
Mar 25 #Python
Python cookbook(数据结构与算法)同时对数据做转换和换算处理操作示例
Mar 23 #Python
教你使用python实现微信每天给女朋友说晚安
Mar 23 #Python
python微信公众号开发简单流程
Mar 23 #Python
python3如何将docx转换成pdf文件
Mar 23 #Python
You might like
php+dbfile开发小型留言本
2006/10/09 PHP
php+mysql实现无限级分类 | 树型显示分类关系
2006/11/19 PHP
php版淘宝网查询商品接口代码示例
2014/06/17 PHP
PHP基于自定义函数实现的汉字转拼音功能实例
2017/09/30 PHP
jQuery创建插件的代码分析
2011/04/14 Javascript
在chrome浏览器中,防止input[text]和textarea在聚焦时出现黄色边框的解决方法
2011/05/24 Javascript
jQuery动态地获取系统时间实现代码
2013/05/24 Javascript
js实现在字符串中提取数字
2013/11/05 Javascript
parentElement,srcElement的使用小结
2014/01/13 Javascript
教你如何自定义百度分享插件以及bshare分享插件的分享按钮
2014/06/20 Javascript
jsonp跨域及实现百度首页联想功能的方法
2018/08/30 Javascript
微信小程序实现电子签名功能
2020/07/29 Javascript
python的id()函数介绍
2013/02/10 Python
PyTorch CNN实战之MNIST手写数字识别示例
2018/05/29 Python
解决Mac安装scrapy失败的问题
2018/06/13 Python
python设定并获取socket超时时间的方法
2019/01/12 Python
python设置代理和添加镜像源的方法
2020/02/14 Python
python实现处理mysql结果输出方式
2020/04/09 Python
Python生成器传参数及返回值原理解析
2020/07/22 Python
解决PyCharm IDE环境下,执行unittest不生成测试报告的问题
2020/09/03 Python
CSS3实战第一波 让我们尽情的圆角吧
2010/08/27 HTML / CSS
详解如何将 Canvas 绘制过程转为视频
2021/01/25 HTML / CSS
西班牙电子产品购物网站:Electronicamente
2018/07/26 全球购物
维多利亚的秘密官方网站:Victoria’s Secret
2018/10/24 全球购物
Notino匈牙利:购买香水和化妆品
2019/04/12 全球购物
英国独特家具和家庭用品购物网站:Cuckooland
2020/08/30 全球购物
文员自我评价怎么写
2013/09/19 职场文书
大学生毕业自我鉴定范文
2013/11/03 职场文书
法制宣传月活动总结
2014/04/29 职场文书
2014年度安全生产目标管理责任书
2014/07/25 职场文书
房屋租赁协议书(标准版)
2014/10/02 职场文书
领导批评与自我批评范文
2014/10/16 职场文书
师范生见习报告范文
2014/11/03 职场文书
房屋分割离婚协议书范本
2014/12/01 职场文书
求职自荐信该如何书写?
2019/06/24 职场文书
Python实现文字pdf转换图片pdf效果
2022/04/03 Python