python監(jiān)控網(wǎng)卡流量并使用graphite繪圖的示例
更新時間:2014年04月27日 10:48:41 作者:
這篇文章主要介紹了python監(jiān)控網(wǎng)卡流量并使用graphite繪圖的示例,需要的朋友可以參考下
復(fù)制代碼 代碼如下:
#!/usr/bin/env python
import sys,time
from socket import socket
def read_interface(in_file):
with file(in_file) as f:
return f.readlines()[2:]
def set_interface(inter_msg):
dic={}
for i in xrange(len(inter_msg)):
dic[inter_msg[i].split(":")[0].strip()]={"in":inter_msg[i].split(":")[1].strip().split()[0],"out":inter_msg[i].split(":")[1].strip().split()[8]}
return dic
CARBON_SERVER = '127.0.0.1'
CARBON_PORT = 2003
delay = 3
sock = socket()
try:
sock.connect( (CARBON_SERVER,CARBON_PORT) )
except:
print "Couldn't connect to %(server)s on port %(port)d, is carbon-agent.py running?" % { 'server':CARBON_SERVER, 'port':CARBON_PORT }
sys.exit(1)
while True:
now = int( time.time() )
lines=[]
int1=read_interface("/proc/net/dev")
inter_dic1=set_interface(int1)
time.sleep(1)
int2=read_interface("/proc/net/dev")
inter_dic2=set_interface(int2)
print int(inter_dic2[inter_dic2.keys()[1]]["in"]),int(inter_dic1[inter_dic1.keys()[1]]["in"])
for i in xrange(len(inter_dic1.keys())):
lines.append("interface.%s_in %s %d" % (inter_dic1.keys()[i],int(inter_dic2[inter_dic2.keys()[i]]["in"])-int(inter_dic1[inter_dic1.keys()[i]]["in"]),now))
lines.append("interface.%s_out %s %d" % (inter_dic1.keys()[i],int(inter_dic2[inter_dic2.keys()[i]]["out"])-int(inter_dic1[inter_dic1.keys()[i]]["out"]),now))
message = '\n'.join(lines) + '\n'
print "sending message\n"
print '-' * 80
print message
sock.sendall(message)
time.sleep(delay)
您可能感興趣的文章:
- Python 使用指定的網(wǎng)卡發(fā)送HTTP請求的實(shí)例
- python 的 scapy庫,實(shí)現(xiàn)網(wǎng)卡收發(fā)包的例子
- Python選擇網(wǎng)卡發(fā)包及接收數(shù)據(jù)包
- Python簡單獲取網(wǎng)卡名稱及其IP地址的方法【基于psutil模塊】
- Python獲取本機(jī)所有網(wǎng)卡ip,掩碼和廣播地址實(shí)例代碼
- Linux服務(wù)器網(wǎng)卡流量查看方法 shell和Python各一枚
- python使用WMI檢測windows系統(tǒng)信息、硬盤信息、網(wǎng)卡信息的方法
- Python腳本實(shí)現(xiàn)網(wǎng)卡流量監(jiān)控
- python 獲取計(jì)算機(jī)的網(wǎng)卡信息
相關(guān)文章
聊聊通過celery_one避免Celery定時任務(wù)重復(fù)執(zhí)行的問題
Celery Once 也是利用 Redis 加鎖來實(shí)現(xiàn), Celery Once 在 Task 類基礎(chǔ)上實(shí)現(xiàn)了 QueueOnce 類,該類提供了任務(wù)去重的功能,今天通過本文給大家介紹通過celery_one避免Celery定時任務(wù)重復(fù)執(zhí)行的問題,感興趣的朋友一起看看吧2021-10-10
python3實(shí)現(xiàn)釘釘消息推送的方法示例
這篇文章主要介紹了python3實(shí)現(xiàn)釘釘消息推送的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
PyTorch CNN實(shí)戰(zhàn)之MNIST手寫數(shù)字識別示例
本篇文章主要介紹了PyTorch CNN實(shí)戰(zhàn)之MNIST手寫數(shù)字識別示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
Python實(shí)現(xiàn)修改Excel文件的元數(shù)據(jù)
這篇文章將通過使用Python、Openpyxl模塊以及wxPython庫,實(shí)現(xiàn)創(chuàng)建一個GUI界面來輸入元數(shù)據(jù),然后將這些元數(shù)據(jù)與Excel文件一起保存,感興趣的可以了解一下2023-04-04
python神經(jīng)網(wǎng)絡(luò)MobileNetV3?small模型的復(fù)現(xiàn)詳解
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)MobileNetV3?small模型的復(fù)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Python?matplotlib實(shí)現(xiàn)折線圖的繪制
Matplotlib作為Python的2D繪圖庫,它以各種硬拷貝格式和跨平臺的交互式環(huán)境生成出版質(zhì)量級別的圖形。本文將利用Matplotlib庫繪制折線圖,感興趣的可以了解一下2022-03-03
利用Python將每日一句定時推送至微信的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于利用Python將每日一句定時推送至微信的實(shí)現(xiàn)方法,文中通過示例代碼將實(shí)現(xiàn)的步驟一步步介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08

