speedometer指令 - Linux

Table of Contents

※ 引述《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/python@2/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,剛剛跑一下可以運作。


--

All Comments

Olga avatarOlga2018-04-15
真的太感謝您了!!!
Barb Cronin avatarBarb Cronin2018-04-19
James avatarJames2018-04-22
比較意外的是改很快一直沒有人提交新的更新給作者蠻可
惜的,後續也來提交看看不過可能得順便處理py3的問題
Blanche avatarBlanche2018-04-26
fork 版本可以請測試用看看,py3 也可以支援了: