-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproc2csv
More file actions
executable file
·83 lines (66 loc) · 1.96 KB
/
proc2csv
File metadata and controls
executable file
·83 lines (66 loc) · 1.96 KB
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
#!/usr/bin/env python
"""Convert logical records from the indicated /proc/net file to a CSV file"""
import sys
import ProcHandlers
if sys.platform == "darwin":
print "MacOS doesn't have a '/proc' filesystem, quitting."
sys.exit(0)
QUALIFY = ""
SEQ = 0
DELIM = "|"
NESTED = 0
HEAD = ""
DEV_SNMP6 = "dev_snmp6"
if len(sys.argv) > 1:
TARGET = sys.argv[1]
else:
TARGET = "/proc/net/tcp"
if len(sys.argv) > 2:
CNAME = sys.argv[2]
else:
CNAME = TARGET
HANDLER = ProcHandlers.GET_HANDLER(CNAME)
ACTIVE = HANDLER(TARGET)
for srec in ACTIVE:
prev_head = HEAD
SEQ += 1
if len(ACTIVE.field) > 0:
NESTED = 1
for key in ACTIVE.field:
if type(ACTIVE.field[key]) != dict:
NESTED = 0
HEAD = DELIM.join(ACTIVE.field)
if HEAD != prev_head:
if SEQ > 1:
print ""
print "{procfile}{delim}{header}".format(procfile=TARGET,
delim=DELIM, header=HEAD)
if NESTED == 1:
for key in ACTIVE.field:
currval = ACTIVE.field[key]
out = ""
for subkey in currval:
out = "{out}{delim}{keyval}".format(out=out,
delim=DELIM, keyval=currval[subkey])
HEAD = DELIM.join(currval)
print ""
print "{seq}{delim}Label{delim}{header}".format(seq=SEQ,
delim=DELIM, header=HEAD)
print "{seq}{delim}{label}{datalist}".format(seq=SEQ,
delim=DELIM, label=key, datalist=out)
else:
out = ""
for key in ACTIVE.field:
out = "{out}{delim}{keyval}".format(out=out, delim=DELIM,
keyval=str(ACTIVE.field[key]))
print "{seq}{datalist}".format(seq=SEQ, datalist=out)
# ---
# pylint: disable=W0702
try:
sys.stdout.close()
except:
pass
try:
sys.stderr.close()
except:
pass