python icmp\dns\http监控网络各个节点状态,并记录日志
配置文件如下:支持多节点:
{
"dns":[{"domainname":"www.baidu.com","dnsserver":"114.114.114.114"}],
"icmp":[{"ip_address":"114.114.114.114"},{"ip_address":"172.17.1.1"},{"ip_address":"192.168.101.1"},{"ip_address":"192.168.9.1"}],
"http":[{"url":"www.baidu.com","title":"百度"}]
}
# 读取配置文件,配置文件,每个监控类型允许多个监控节点,函数返回包含多个节点的列表
def read_config():
try:
file = open("config.json")
config = file.read()
file.close()
config = json.loads(config) http = config["http"]
icmp = config["icmp"]
dns = config["dns"] return http,icmp,dns
except Exception:
print("read config error")
write_logs("read config error")
return None
# 用来写result结果,只有网络异常的时候才会输出
def write_resules(filename,results):
try :
file = open(filename,"a")
file.write(time.asctime(time.localtime(time.time())) + " : " + results + "\n")
file.close()
except Exception:
print(time.asctime(time.localtime(time.time())) + "write results is error")
# 写日志文件,方便在出现错误的时候判断错误原因,只有在出错的时候才会输出
def write_logs(logs):
try :
file = open("logs.txt","a")
file.write(time.asctime(time.localtime(time.time())) + " : " + logs + "\n")
file.close()
except Exception:
print(time.asctime(time.localtime(time.time())) + "write log is error")
# 调用requests发送数据包
def send_http_packet(url):
# requests.packages.urllib3.disable_warnings()
user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
headers = {'User-Agent': user_agent}
url = "http://" + url
try:
response = requests.get(url, headers)
response_html = response.content.decode()
return response_html
except Exception:
print('send http packet error,error')
write_logs('send http packet error,error')
return None
# 使用bs4解析获取到的数据,并和给出的网页标题对比
def check_http(url,title):
response_html = send_http_packet(url)
if response_html != None and title != None :
soup = bs4.BeautifulSoup(response_html, 'lxml')
html_title = soup.title.text
if title in html_title:
return True
else:
write_logs("title not same ,web title is : " + html_title)
return False
else:
print('html or title is None')
return False
# 发送数ping数据包
def send_icmp_packet(ip_address):
try:
response_packet = sr1(IP(dst=ip_address)/ICMP(),timeout=2,verbose = False)
return response_packet
except Exception:
print('send icmp packet error,error')
write_logs('send icmp packet error,error')
return None
# 检查icmp数据包有没有正常恢复
def check_icmp(ip_address):
response_packet = send_icmp_packet(ip_address)
if response_packet != None:
return True
else:
return False
# 发送DNS请求
def send_dns_packet(domainname,dnsserver):
i = IP(dst=dnsserver)
u = UDP(dport = 53)
d = DNS(rd=1)
d.qd = DNSQR(qname = domainname , qtype = "A" ,qclass = "IN")
dns_request_packet = i/u/d try:
dns_response_packet = sr1(dns_request_packet,timeout=2,verbose = False)
return dns_response_packet
except Exception:
print("send dns packet error ")
write_logs('send icmp packet error,error')
return None
# 检查DNS请求是否正常返回
def check_dns(domainname,dnsserver):
dns_response_packet = send_dns_packet(domainname,dnsserver) if dns_response_packet != None:
return True
else:
return False
# 为了方便多线程调用,每个监控类型都设置一个启动程序
def start_http(url, title):
while True:
if check_http(url, title) :
print("http True : " + url + "," + title)
else:
print("http False : " + url + "," + title)
write_resules("http_results.txt","http False : " + url + "," + title)
time.sleep(2)
# 为了方便多线程调用,每个监控类型都设置一个启动程序
def start_icmp(ip_address):
while True:
if check_icmp(ip_address) :
print("icmp True : " + ip_address)
else:
print("icmp False : " + ip_address)
write_resules("icmp_results.txt","icmp False : " + ip_address)
time.sleep(2)
# 为了方便多线程调用,每个监控类型都设置一个启动程序
def start_dns(domainname,dnsserver):
while True:
if check_dns(domainname,dnsserver) :
print("dns True : " + domainname + "," + dnsserver)
else:
print("dns False : " + domainname + "," + dnsserver)
write_resules("dns_results.txt","dns False : " + domainname + "," +dnsserver)
time.sleep(2)
# 主函数
def main():
http,icmp,dns= read_config() # 循环取出http监控节点,每个节点启动一个线程
for h in http:
url = h["url"]
title = h["title"]
moniter_http = threading.Thread(target=start_http,args=(url, title))
moniter_http.start() # 循环取出icmp监控节点,每个节点启动一个线程
for i in icmp:
ip_address = i["ip_address"]
moniter_icmp = threading.Thread(target=start_icmp,args=(ip_address,))
moniter_icmp.start() # 循环取出dns监控节点,每个节点启动一个线程
for d in dns:
domainname = d["domainname"]
dnsserver = d["dnsserver"]
moniter_dns = threading.Thread(target=start_dns,args=(domainname,dnsserver))
moniter_dns.start()
if __name__ == '__main__':
main()
python icmp\dns\http监控网络各个节点状态,并记录日志的更多相关文章
- 运用Ntop监控网络流量(视频Demo)
运用Ntop监控网络流量 ____网络流量反映了网络的运行状态,是判别网络运行是否正常的关键数据,在实际的网络中,如果对网络流量控制得不好或发生网络拥塞,将会导致网络吞吐量下降.网络性能降低.通过流量 ...
- Ntop监控网络流量
运用Ntop监控网络流量 ____ 网络流量反映了网络的运行状态,是判别网络运行是否正常的关键数据,在实际的网络中,如果对网络流量控制得不好或发生网络拥塞,将会导致网络吞吐量下降. 网络性能降低.通过 ...
- Linux服务器上监控网络带宽的18个常用命令nload, iftop,iptraf-ng, nethogs, vnstat. nagios,运用Ntop监控网络流量
Linux服务器上监控网络带宽的18个常用命令 本文介绍了一些可以用来监控网络使用情况的Linux命令行工具.这些工具可以监控通过网络接口传输的数据,并测量目前哪些数据所传输的速度.入站流量和出站流量 ...
- C# 利用性能计数器监控网络状态
本例是利用C#中的性能计数器(PerformanceCounter)监控网络的状态.并能够直观的展现出来 涉及到的知识点: PerformanceCounter,表示 Windows NT 性能计数器 ...
- python基础教程总结13——网络编程,
1.网络设计模块 1.1 socket模块 根据连接启动的方式以及本地套接字要连接的目标,套接字之间的连接过程可以分为三个步骤:服务器监听,客户端请求,连接确认. 1)服务器监听:是服务器端套接 ...
- 用python进行服务器的监控
用python进行服务器的监控 在linux服务器中,一切皆为文件,就是说,服务器运行的个中信息,其实是可以从某些文件中查询得到的:百度后,你会知道,在Linux系统中,有一个/proc的虚拟文件系统 ...
- 【python之路35】网络编程之socket相关
Socket socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. sock ...
- Python全栈【Socket网络编程】
Python全栈[socket网络编程] 本章内容: Socket 基于TCP的套接字 基于UDP的套接字 TCP粘包 SocketServer 模块(ThreadingTCPServer源码剖析) ...
- zabbix监控网络的出入口流量
首先我们登录到zabbix 点击配置---->模板-->Template OS Linux 下的监控项 点击右上角的添加监控项目 我们的服务器是在Ucloud上的,我们的网卡名称为eth0 ...
随机推荐
- PHP获取远程http或ftp文件的md5值
PHP获取本地文件的md5值: md5_file("/path/to/file.png"); PHP获取远程http文件的md5值: md5_file("https:// ...
- Ubuntu 18.04.1 LTS + kolla-ansible 部署 openstack Rocky all-in-one 环境
1. kolla 项目介绍 简介 kolla 的使命是为 openstack 云平台提供生产级别的.开箱即用的自动化部署能力. kolla 要实现 openetack 部署分为两步,第一步是制作 do ...
- shell编写小技巧整理
1. if和else语句可以进行嵌套.if的条件判断部分可能会变得很长,可以使用逻辑运算符将它变得简洁一些. [ condition ] && action :如果condition为 ...
- SignalR使用笔记
最近项目要求添加一个给用户发送消息的功能,就决定使用SignalR.翻到了以前学习SignalR的学习笔记,基本是官方文档的简版整理,便于快速阅览和实现. 1. nuget添加signalr引用: a ...
- 【TensorFlow篇】--DNN初始和应用
一.前述 ANN人工神经网络有两个或两个以上隐藏层,称为DNN 只有一个隐藏层是多层感知机 没有隐藏层是感知机 二.反向传播应用举例 举例: 正向传播,反向传播是一次迭代, 正向传播:在开始的每一层上 ...
- java~springboot~ibatis数组in查询的实现
在ibatis的xml文件里,我们去写sql语句,对应mapper类的方法,这些sql语句与控制台上没什么两样,但在有些功能上需要注意,如where in这种从数组里查询符合条件的集合里,需要在xml ...
- Java将数据按列写入Excel并设置格式(字体、背景色、自动列宽、对齐方式等)
本文使用jxl.jar工具类库将数据按列写入Excel并设置格式(字体.背景色.自动列宽.对齐方式等). /** * 按列写入Excel并设置格式 * * @param outputUrl * 输出路 ...
- SLAM+语音机器人DIY系列:(一)Linux基础——2.安装Linux发行版ubuntu系统
摘要 由于机器人SLAM.自动导航.语音交互这一系列算法都在机器人操作系统ROS中有很好的支持,所以后续的章节中都会使用ROS来组织构建代码:而ROS又是安装在Linux发行版ubuntu系统之上的, ...
- jQuery(八)、ajax
1.jQuery.ajax(url[, settings]) 通过HTTP请求加载远程数据. 注意:所有的settings选择都可以通过$.ajaxSetup()函数来全局指定. 回调函数 在实际开发 ...
- 【MySQL】MySQL的执行计划及索引优化
我们知道一般图书馆都会建书目索引,可以提高数据检索的效率,降低数据库的IO成本.MySQL在300万条记录左右性能开始逐渐下降,虽然官方文档说500~800w记录,所以大数据量建立索引是非常有必要的. ...