Re: [問題] speedometer指令

作者: kenduest (小州)   2018-04-12 13:08:20
※ 引述《smile2359630 (。。。)》之銘言:
: 小弟剛步入linux系統沒多久而已,目前遇到一個問題
: 我想在macbook上使用speedometer這個網路監測軟體,想說都是以unix下去發展出來的OS,
: 執行上照理說可行的,但目前遇到下面問題
: speedometer -rx eth0 時顯示以下
: Traceback (most recent call last):
: File "/usr/local/bin/speedometer", line 1093, in <module>
: console()
: File "/usr/local/bin/speedometer", line 797, in console
: wait_all(cols)
: File "/usr/local/bin/speedometer", line 1088, in wait_all
: tap.wait_creation()
: File "/usr/local/bin/speedometer", line 856, in wait_creation
: if self.feed() is None:
: File "/usr/local/bin/speedometer", line 610, in networkfn
: f = open('/proc/net/dev')
: IOError: [Errno 2] No such file or directory: '/proc/net/dev'
這個程式寫法是抓 linux 特有的 /proc/net/dev 分析內容,所以 linux 適合使用,
mac 沒該檔案所以運作失敗。
要改寫支援 mac 也很簡單,剛看一下大約這樣子
/usr/local/bin/speedometer.py 檔案打開,幾個地方要改:
1. 找一下一開始 import 部分語法
#!/usr/local/opt/[email protected]/bin/python2.7
# speedometer.py
# Copyright (C) 2001-2011 Ian Ward
#
# This module is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This module is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
__version__ = "2.8"
import time
import sys
import os
import string
import math
import re
在 import re 之後,多加一行:
import psutil
2. 下面這段要改:
def network_feed(device,rxtx):
"""network_feed(device,rxtx) -> function that returns given device stream speed
rxtx is "RX" or "TX"
"""
assert rxtx in ["RX","TX"]
r = re.compile(r"^\s*" + re.escape(device) + r":(.*)$", re.MULTILINE)
def networkfn(devre=r,rxtx=rxtx):
f = open('/proc/net/dev')
dev_lines = f.read()
f.close()
match = devre.search(dev_lines)
if not match:
return None
parts = match.group(1).split()
if rxtx == 'RX':
return long(parts[0])
else:
return long(parts[8])
return networkfn
改成:
def network_feed(device,rxtx):
"""network_feed(device,rxtx) -> function that returns given device stream speed
rxtx is "RX" or "TX"
"""
assert rxtx in ["RX","TX"]
r = re.compile(r"^\s*" + re.escape(device) + r":(.*)$", re.MULTILINE)
def networkfn(devre=r,rxtx=rxtx):
if rxtx == 'RX':
return psutil.net_io_counters(pernic=True)[device].bytes_recv
else:
return psutil.net_io_counters(pernic=True)[device].bytes_sent
return networkfn
主要是 networkfn() 裡面改改就好。
最後系統用 pip install psutil 安裝上該 python module,剛剛跑一下可以運作。
作者: smile2359630 (TYW)   2018-04-12 13:30:00
真的太感謝您了!!!
作者: ddtsatan   2018-04-12 20:51:00
作者: kenduest (小州)   2018-04-14 16:44:00
比較意外的是改很快一直沒有人提交新的更新給作者蠻可惜的,後續也來提交看看不過可能得順便處理py3的問題fork 版本可以請測試用看看,py3 也可以支援了:https://github.com/kenduest/speedometer

Links booklink

Contact Us: admin [ a t ] ucptt.com