PFZheng / psutil

Automatically exported from code.google.com/p/psutil
Other
0 stars 0 forks source link

psutil.network_io_counters returns incorrect results #248

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. import psutil
2. psutil.network_io_counters(True)
3.

What is the expected output?
{'lo': iostat(bytes_sent=485737, bytes_recv=73461834, packets_sent=0, 
packets_recv=0), 'eth0': iostat(bytes_sent=802707, bytes_recv=357763247, 
packets_sent=0, packets_recv=0)}

What do you see instead?
{'lo:6221278': iostat(bytes_sent=447754, bytes_recv=447754, packets_sent=0, 
packets_recv=0), 'eth0:52841330': iostat(bytes_sent=960063, bytes_recv=970736, 
packets_sent=28, packets_recv=0)}

What version of psutil are you using? What Python version?
0.4.1

On what operating system? Is it 32bit or 64bit version?
Linux, both 32bit and 64bit affected.

Please provide any additional information below.

The bug is caused by the fact that the device name and recieved bytes are not 
separated by a space instead by ":" so split does not split them.

[root@ms ~]# cat /proc/net/dev
Inter-|   Receive                                                |  Transmit
 face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
    lo:78509297  522704    0    0    0     0          0         0 78509297  522704    0    0    0     0       0          0
  eth0:383271109  600856    0    0    0     0          0         0 954496903  876015    0    0    0     0       0          0

I have attached a patch that fixes the bug

Original issue reported on code.google.com by andrew.c...@gmail.com on 30 Jan 2012 at 11:38

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks, fixed in r1255.

Original comment by g.rodola on 30 Jan 2012 at 12:14

GoogleCodeExporter commented 9 years ago

Original comment by g.rodola on 30 Jan 2012 at 12:18

GoogleCodeExporter commented 9 years ago
Thanks for your prompt attention to the issue. And thanks for your work on 
psutil it is a life saver.

Original comment by andrew.c...@gmail.com on 30 Jan 2012 at 2:05

GoogleCodeExporter commented 9 years ago
I am attaching a change to the logic in the path. I noticed a case where there 
is both space (as well as not space) between the name and the stats on the line.

Logic should be:
1) split the line by ":" to get the device and the remaining values.
2) strip() any spaces on the device
3) split() the remaining items on the line to get sent and received values.

# cat /proc/net/dev
Inter-|   Receive                                                |  Transmit
 face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
    lo:  523965    1544    0    0    0     0          0         0   523965    1544    0    0    0     0       0          0
  eth0:699498750  762615    0 1650    0     0          0         0 69956705  301025    0    0    0     0       0          0

###################

    for line in lines[2:]:
        name, remainder = line.split(':')
        name = name.strip()
        fields = remainder.split()
        bytes_recv = int(fields[0])
        packets_recv = int(fields[1])
        bytes_sent = int(fields[8])
        packets_sent = int(fields[9])

Original comment by jklingin...@gmail.com on 4 May 2012 at 4:43

GoogleCodeExporter commented 9 years ago
AFAICT the current logic covers both cases, this one:

"eth0:699498750  762615    0 1..."

...and this one:

"eth0: 699498750  762615    0 1..."

Are you proposing this change because you noticed that the first number 
(699498750) gets truncated or something?

Original comment by g.rodola on 4 May 2012 at 10:03

GoogleCodeExporter commented 9 years ago
I pulled _pslinux.py from the trunk. It resolved the issue I was seeing. I was 
seeing my issue from the patch attached to issue. Sorry for the confusion.

Original comment by jklingin...@gmail.com on 4 May 2012 at 11:39

GoogleCodeExporter commented 9 years ago
Issue 263 has been merged into this issue.

Original comment by g.rodola on 9 May 2012 at 5:29

GoogleCodeExporter commented 9 years ago
0.5.0 is finally out. Closing out as fixed.

Original comment by g.rodola on 27 Jun 2012 at 6:54

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Updated csets after the SVN -> Mercurial migration:
r1255 == revision cd7a6abcdb20

Original comment by g.rodola on 2 Mar 2013 at 12:06