Hi,
I am trying to plot netstat (network stats taken from
/proc/net/netstat on linux), the parsed file looks like this:
Tue Jul 28 17:11:39 2009
TcpExt: TcpExt:
SyncookiesSent 35367
SyncookiesRecv 83175
SyncookiesFailed 626981
EmbryonicRsts 2683828
PruneCalled 0
RcvPruned 0
OfoPruned 0
OutOfWindowIcmps 0
LockDroppedIcmps 0
ArpFilter 0
TW 731933844
TWRecycled 2519
TWKilled 0
PAWSPassive 0
PAWSActive 0
PAWSEstab 0
DelayedACKs 260793151
DelayedACKLocked 39078
DelayedACKLost 8463534
ListenOverflows 5702696
ListenDrops 5702696
TCPPrequeued 757095
TCPDirectCopyFromBacklog 20396543
TCPDirectCopyFromPrequeue 41213784
TCPPrequeueDropped 0
TCPHPHits 1980793902
TCPHPHitsToUser 969080
TCPPureAcks 1329017292
TCPHPAcks 1527716572
TCPRenoRecovery 441952
TCPSackRecovery 158939
TCPSACKReneging 590
TCPFACKReorder 2354
TCPSACKReorder 12
TCPRenoReorder 77537
TCPTSReorder 3
TCPFullUndo 364
TCPPartialUndo 2267
TCPDSACKUndo 57659
TCPLossUndo 1377902
TCPLoss 83058
TCPLostRetransmit 65
TCPRenoFailures 597875
I am collecting this information every 3 minutes and want to plot
every indivudal value (like for example TCPLoss) in separate date
graph. Anyhow I have problems setting the date value
This is the exception I get:
Traceback (most recent call last):
File "./graph_last.py", line 86, in <module>
main()
File "./graph_last.py", line 75, in main
ax.plot(counter['date'],counter.__dict__[count])
File "/usr/lib/python2.5/site-packages/matplotlib/axes.py", line 2654, in plot
for line in self._get_lines(*args, **kwargs):
File "/usr/lib/python2.5/site-packages/matplotlib/axes.py", line
397, in _grab_next_args
for seg in self._plot_2_args(remaining, **kwargs):
File "/usr/lib/python2.5/site-packages/matplotlib/axes.py", line
339, in _plot_2_args
func(x[:,j], y[:,j])
File "/usr/lib/python2.5/site-packages/matplotlib/axes.py", line
320, in makeline
axes=self.axes,
File "/usr/lib/python2.5/site-packages/matplotlib/lines.py", line
284, in __init__
self.set_data(xdata, ydata)
File "/usr/lib/python2.5/site-packages/matplotlib/lines.py", line
405, in set_data
self.recache()
File "/usr/lib/python2.5/site-packages/matplotlib/lines.py", line
410, in recache
x = ma.asarray(self.convert_xunits(self._xorig), float)
File "/usr/lib/python2.5/site-packages/numpy/core/ma.py", line 2123,
in asarray
return array(data, dtype=dtype, copy=0)
File "/usr/lib/python2.5/site-packages/numpy/core/ma.py", line 574,
in __init__
self._data = c.astype(tc)
ValueError: invalid literal for float(): Tue Jul 28 17:15:01 2009
This is the script based on the date example:
#!/usr/bin/python
import re
import datetime
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.mlab as mlab
days = mdates.DayLocator()
hours = mdates.HourLocator()
daysFmt = mdates.DateFormatter('%c')
class Mylist(list):
def append(self,x):
if not self.__contains__(x):
return super(Mylist, self).append(x)
else:
raise AttributeError
class Counter():
def __init__(self):
self.names = Mylist()
def __setitem__(self,key,value):
'''
Now try to append the name to the list
'''
try:
self.names.append(key)
except AttributeError:
pass
try:
self.__dict__[key].append(value)
except KeyError:
self.__dict__[key] = list()
def __getitem__(self,key):
'''
Return the list
'''
return list(self.__dict__[key])
def main():
try:
file = open("netstat_output",)
except IOError:
print "There was a problem with the file\n"
counter = Counter()
lastline = re.compile('====================================')
date = re.compile(r"\b\w{3} \w{3} \d{2} \d\d:\d\d:\d\d \d{4}\b")
tcpext = re.compile('Tcpext:',re.IGNORECASE)
lines = file.readlines()
for line in lines:
if (date.search(line)):
counter['date'] = line
elif (lastline.search(line)):
pass
elif (tcpext.search(line)):
pass
else:
currlist = line.split()
counter[currlist[0]] = currlist[1]
for count in counter.names:
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(counter['date'],counter.__dict__[count])
ax.xaxis.set_major_locator(days)
ax.xaxis.set_major_formatter(daysFmt)
ax.xaxis.set_minor_locator(hours)
ax.format_xdata = mdates.DateFormatter('%d-%H')
ax.grid(True)
fig.autofmt_xdate()
plt.show()
if __name__ == "__main__":
main()
Could someone please help me how to pass dates to plot() ?
Thanks,
Jorge