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