网卡监控脚本--nagios
#!/usr/bin/python26
#filename:check_traffic.py
#the python script function for monitoring network traffic.
'''
--help
example:
python26 check_traffic.py dev in out checknum
python26 check_traffic.py eth0 30000 30000 checknum
the limit unit is KB
'''
import sys,os,time
import psutil
if len(sys.argv) != 5:
print __doc__
sys.exit(3)
dev=sys.argv[1]
value_in=int(sys.argv[2])
value_out=int(sys.argv[3])
lineNum=sys.argv[4]
tmpFile=dev+"_traffic.log"
unit="KB/s"
###get script pwd
binPwd=os.path.dirname(os.path.realpath(__file__))
os.chdir(binPwd)
###get current network traffic
def getCurrentTraffic(dev):
device=dev
current=psutil.network_io_counters(pernic=True)
time.sleep(1)
current1=psutil.network_io_counters(pernic=True)
c_send=current[device][0]
c_rev=current[device][1]
sc_send=current1[device][0]
sc_rev=current1[device][1]
send=(sc_send-c_send)/1024
rev=(sc_rev-c_rev)/1024
witeLog(send,rev)
return send,rev
###write log file the network traffic
def witeLog(send,rev):
now=time.strftime("%Y-%m-%d %X")
f=file(tmpFile,'a')
f.write("%s,%s,%s\n" % (rev,send,now))
f.close()
def isTraffic():
X=[]
Y=[]
if len(open(tmpFile,'rU').readlines()) < int(lineNum):
print "The check num too less."
sys.exit(0)
lastData=os.popen("cat "+tmpFile+"|tail -n " + lineNum)
for tmp in lastData.readlines():
X.append(int(tmp.split(",")[0].strip('\n')))
Y.append(int(tmp.split(",")[1].strip('\n')))
X.sort()
Y.sort()
return X[0],Y[0]
###check tmpFile size
def isTmpFileSzie():
os.system("touch %s" % tmpFile)
if os.path.getsize(tmpFile)/1024 > 65535:
os.remove(tmpFile)
###check network traffic the value of is normal
def monitor():
getCurrentTraffic(dev)
isCheck=isTraffic()
if isCheck[0] < value_in and isCheck[1] < value_out:
print "network traffic ok, in: %s %s out: %s %s" % (isCheck[0],unit,isCheck[1],unit)
sys.exit(0)
elif isCheck[0] >= value_in or isCheck[1] >= value_out:
print "network traffic warning, in: %s %s out: %s %s" % (isCheck[0],unit,isCheck[1],unit)
sys.exit(1)
else:
print "UNKNOWN."
sys.exit(3)
def main():
isTmpFileSzie()
monitor()
if __name__=='__main__':
main()
网卡监控脚本--nagios的更多相关文章
- centos shell编程6一些工作中实践脚本 nagios监控脚本 自定义zabbix脚本 mysql备份脚本 zabbix错误日志 直接送给bc做计算 gzip innobackupex/Xtrabackup 第四十节课
centos shell编程6一些工作中实践脚本 nagios监控脚本 自定义zabbix脚本 mysql备份脚本 zabbix错误日志 直接送给bc做计算 gzip innobacku ...
- nagios和zabbix自定义监控脚本
一. 自定义nagios监控脚本1. 在客户端上创建脚本/usr/local/nagios/libexec/check_disk.shvim /usr/local/nagios/libexec/ch ...
- [工具开发] keepalived使用nagios监控脚本
最近在做开发和办公环境的高可用,采用的是keepalived:keepalived基于Linux内核支持的LVS,既能实现高可用,又能实现负载均衡,非常实用. keepalived监控服务状态时可以用 ...
- Linux下针对服务器网卡流量和磁盘的监控脚本
1)实时监控网卡流量的通用脚本: [root@ceph-node1 ~]# cat /root/net_monit.sh #!/bin/bash PATH=/bin:/usr/bin:/sbin:/u ...
- Python 监控脚本(硬盘、cpu、内存、网卡、进程)
#磁盘使用率disk = psutil.disk_partitions()for i in disk: print "磁盘:%s 分区格式:%s"%(i.device,i ...
- 自定义nagios监控脚本---磁盘检测
自定义nagios监控脚本---磁盘检测 1. 在客户端上创建脚本/usr/local/nagios/libexec/check_disk.shvim /usr/local/nagios/libexe ...
- nginx+keepalived主辅切换(监控脚本在keepalived.conf中执行)
以前写过一篇,nginx+keepalived 双机互备的文章,写那篇文章的时候没有想过如果apache或者nginx 挂了,而 keepalived 或者 机器没有死,那么主辅是不会切换的,今天就研 ...
- 【不积跬步,无以致千里】五个常用的Linux监控脚本代码
为大家提供五个常用Linux监控脚本(查看主机网卡流量.系统状况监控.监控主机的磁盘空间,当使用空间超过90%就通过发mail来发警告.监控CPU和内存的使用情况.全方位监控主机),有需要的朋友不妨看 ...
- linux系统CPU,内存,磁盘,网络流量监控脚本
前序 1,#cat /proc/stat/ 信息包含了所有CPU活动的信息,该文件中的所有值都是从系统启动开始累积到当前时刻 2,#vmstat –s 或者#vmstat 虚拟内存统计 3, #cat ...
随机推荐
- 【BZOJ4942】[NOI2017]整数(分块)
[BZOJ4942][NOI2017]整数(分块) 题面 BZOJ 洛谷 题解 暴力就是真正的暴力,直接手动模拟进位就好了. 此时复杂度是模拟的复杂度加上单次询问的\(O(1)\). 所以我们需要优化 ...
- java8时间处理实例
实例: package com.javaBase.time; import java.time.Clock; import java.time.LocalDate; import java.time. ...
- 实测搭建jenkins多环境、多分支demo
一.环境以及工具信息 1. 3台服务器信息 jenkins: 192.168.123.163.serverA:192.168.123.130.serverB :139.198.17.241三台机器都是 ...
- 设计模式之(八)组合模式(COMPOSITE)
初始印象 在开发中存在很多整体和部分的关系,这个方式最大的体现就是树形结构.组合模式就是为了更好地解决这类业务场景的问题.先看下组合模式的定义: 将对象组合成树形结构以表示“整体—部分”的层次关系.组 ...
- 【python+selenium学习】Python常见错误之:IndentationError: unexpected indent
初入python+selenium学习之路,总会遇到这样那样的问题.IndentationError: unexpected indent,这个坑我已经踏进数次了,索性记录下来.都知道Python对代 ...
- 手机网页唤醒支付宝APP发送加好友验证
手机网页唤醒支付宝APP发送加好友验证 <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...
- 【MySQL】rds 不支持镜像表/联合表,怎么办?
出于对业务的不了解,往往会让人做出错误的判断 CREATE TABLE `new_tables` ( `customer_id` int(11) NOT NULL AUTO_INCREMENT C ...
- Linux文件服务管理之vsftpd
简介 vsftpd是 "very secure FTP deamon"的缩写,是一个完全免费,开源的ftp服务器软件. 特点 小巧轻快,安全易用,支持虚拟用户.支持带宽限制等功能. ...
- selenium 框架
结构如下: test_project|--logs|---pages |---register_page.py| |---base_page.py|---test_case |- ...
- 大数据技术原理与应用【第五讲】NoSQL数据库:5.4 NoSQL的三大基石
NoSQL的三大基石:cap,Base,最终一致性 5.4.1 cap理论(帽子理论): consistency:一致性availability:可用性partition tolerance: ...