-
Notifications
You must be signed in to change notification settings - Fork 5
/
sys_stat
executable file
·129 lines (111 loc) · 3.25 KB
/
sys_stat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/python
#
# This simple tool will augment the output from vmstat with line counts from
# log files and network interface counters.
#
import os
import signal
import subprocess
import sys
import time
def cl(li):
pos = li[0].tell()
li[0].seek(pos)
lines = len(li[0].readlines())
if os.stat(li[2]).st_size < pos:
fd[0] = open(li[2], 'r')
lines += len(li[0].readlines())
return str(lines)
def pad(title, data=None):
return (' %%%ds' % len(title)) % ((data is None) and title or data)
def yank_arg(name, args):
idx = args.index(name)
arg = args[idx+1]
args[idx:idx+2] = []
return arg
class Vmstat(object):
def __init__(self, args):
self.output = None
self.outfile = None
if '-O' in args:
self.outfile = yank_arg('-O', args)
self.Reopen()
signal.signal(signal.SIGHUP, self.Interrupt)
else:
self.output = sys.stdout
self.logfiles = []
while '-F' in args:
lid, lname = yank_arg('-F', args).split(':')
linfo = [open(lname, 'r'), lid, lname]
self.logfiles.append(linfo)
cl(linfo)
self.interfaces = []
self.traffic = {}
while '-I' in args:
iface = yank_arg('-I', args)
self.interfaces.append(iface)
self.vmstat_cmd = ['vmstat']
self.vmstat_cmd.extend(args)
def Interrupt(self, a=None, b=None):
raise IOError('Interrupted')
def Reopen(self):
if self.outfile:
self.output = open(self.outfile, 'a', buffering=0)
def Traffic(self, iface):
for line in open('/proc/net/dev', 'r'):
if iface in line:
data = line[line.index(':')+1:].split()
rcvd, xmit = int(data[0]), int(data[8])
if iface in self.traffic:
orcv, oxmt = self.traffic.get(iface, (0, 0))
self.traffic[iface] = (rcvd, xmit)
return (rcvd-orcv, xmit-oxmt)
else:
self.traffic[iface] = (rcvd, xmit)
return (0, 0)
return (0, 0)
def Run(self):
got_data = 1
while got_data:
line = 'first'
got_data = 0
self.vmstat = subprocess.Popen(self.vmstat_cmd, bufsize=1,
stdout=subprocess.PIPE)
while line:
try:
line = self.vmstat.stdout.readline().strip()
if not line:
break
if '----' in line:
continue
line += pad(time.asctime())
if 'swpd' in line:
if self.logfiles:
line += ' '.join([pad(l[1]) for l in self.logfiles])
for iface in self.interfaces:
line += pad('%s_i' % iface)
line += pad('%s_o' % iface)
else:
if self.logfiles:
line += ' '.join([pad(l[1], cl(l)) for l in self.logfiles])
for iface in self.interfaces:
r, w = self.Traffic(iface)
line += pad('%s_i' % iface, r)
line += pad('%s_o' % iface, w)
self.output.write('%s\n' % line)
got_data += 1
except (OSError, IOError):
self.Reopen()
break
try:
self.vmstat.kill()
except:
pass
if 0 == self.vmstat.wait():
break
if __name__ == '__main__':
try:
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
vms = Vmstat(sys.argv[1:]).Run()
except:
sys.exit(1)