Re: [問題] speedometer指令
※ 引述《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,剛剛跑一下可以運作。
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.117.155.17
※ 文章網址: https://www.ptt.cc/bbs/Linux/M.1523509703.A.842.html
推
04/12 13:30,
7年前
, 1F
04/12 13:30, 1F
推
04/12 20:51,
7年前
, 2F
04/12 20:51, 2F
→
04/14 16:44,
7年前
, 3F
04/14 16:44, 3F
→
04/14 16:44,
7年前
, 4F
04/14 16:44, 4F
→
04/15 01:37,
7年前
, 5F
04/15 01:37, 5F
→
04/15 01:38,
7年前
, 6F
04/15 01:38, 6F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
Linux 近期熱門文章
21
56
PTT數位生活區 即時熱門文章