-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.py
More file actions
executable file
·168 lines (138 loc) · 4.23 KB
/
memory.py
File metadata and controls
executable file
·168 lines (138 loc) · 4.23 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import sys
import signal
import time
import json
import telnetlib
import requests
import configparser
from parse import parse
class tn_session:
def __init__ (self, ip, uname, passw):
self.ip = ip
self.uname = uname
self.passw = passw
self.instance = telnetlib.Telnet(ip)
self.file = None
self.json_array = []
class mem_unit:
def __init__(self):
self.peak = 0
self.size = 0
self.lck = 0
self.hwm = 0
self.rss = 0
self.data = 0
self.stk = 0
self.exe = 0
self.lib = 0
self.pte = 0
self.swap = 0
def dump(self):
js = '{'
js += '"timestamp":"{}",'.format(time.strftime('%H:%M:%S'))
js += '"peak":{},'.format(self.peak)
js += '"size":{},'.format(self.size)
js += '"lck":{},'.format(self.lck)
js += '"hwm":{},'.format(self.hwm)
js += '"rss":{},'.format(self.rss)
js += '"data":{},'.format(self.data)
js += '"stk":{},'.format(self.stk)
js += '"exe":{},'.format(self.exe)
js += '"lib":{},'.format(self.lib)
js += '"pte":{},'.format(self.pte)
js += '"swap":{}'.format(self.swap)
js += '}'
return json.loads(js)
def parse(self, resp):
for i in range(len(resp) - 1):
val = parse('{}:{} kB', resp[i])
res = int(str.strip(str(val[1])))
if 'Peak' in val[0]:
unit.peak = res
elif 'Size' in val[0]:
unit.size = res
elif 'Lck' in val[0]:
unit.lck = res
elif 'HWM' in val[0]:
unit.hwm = res
elif 'RSS' in val[0]:
unit.rss = res
elif 'Data' in val[0]:
unit.data = res
elif 'Stk' in val[0]:
unit.stk = res
elif 'Exe' in val[0]:
unit.exe = res
elif 'Lib' in val[0]:
unit.lib = res
elif 'PTE' in val[0]:
unit.pte = res
elif 'Swap' in val[0]:
unit.swap = res
def append_to_file(self, js, s):
print(json.dumps(js))
s.file = open("{}.json".format(s.ip), "w+")
s.json_array.append(json.dumps(js).replace('\'', ''))
s.file.write('[')
for line in s.json_array[:-1]:
s.file.write('{},'.format(line))
s.file.write(s.json_array[-1])
s.file.write(']')
s.file.close()
def telnet_login(s):
inst = s.instance
if s.passw != '':
inst.read_until(b"Login: ")
inst.write(s.uname.encode('ascii') + b"\n")
inst.read_until(b"Password: ")
inst.write(s.passw.encode('ascii') + b"\n")
inst.read_until(b" > ")
inst.write("sh".encode('ascii') + b"\n")
else:
inst.read_until(b"login: ")
inst.write(s.uname.encode('ascii') + b"\n")
inst.read_until(b"# ")
return
def telnet_exec(s, cmd):
inst = s.instance
inst.write(cmd.encode('ascii') + b"\n")
out = inst.read_until(b"# ")
return out.decode('ascii')
def get_config(cfile):
config = []
try:
parser = configparser.ConfigParser()
parser.read(cfile)
except configparser.ParsingError:
print('Could not parse config')
return None
for elem in parser.sections():
dev = {}
for name, value in parser.items(elem):
dev[name] = value
config.append(dev)
return config
if __name__ == '__main__':
command = 'PID=$(pidof {}) && cat /proc/$PID/status | grep Vm'.format(sys.argv[1])
sessions = []
config = get_config("config.ini")
for node in config:
sessions.append(tn_session(
"{}.local".format(node['mac']),
node['user'],
node['passw']))
for s in sessions:
telnet_login(s)
while True:
for s in sessions:
res = telnet_exec(s, command).replace('\r', '').split('\n')
res = res[1:-1]
unit = mem_unit()
unit.parse(res)
js = unit.dump()
unit.append_to_file(js, s)
time.sleep(1)
for s in sessions:
s.instance.close()