-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
355 lines (279 loc) · 9.97 KB
/
utils.py
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# from __future__ import division
from datetime import datetime, timedelta
from colorlog import ColoredFormatter
from platform import system as system_name # Returns the system/OS name
from os import system as system_call # Execute a shell command
import socket
import struct
import psutil
import os
import sys
import logging
import logging.handlers
import traceback
import netifaces
import re
__author__ = 'sbertelli'
def totimestamp(dt, epoch=datetime(1970, 1, 1)):
td = dt - epoch
# return td.total_seconds()
return (td.microseconds + (td.seconds + td.days * 86400) * 10**6) / 10**6
def get_logging(script__file__, verbose=False, level=logging.INFO, log_directory=None):
"""
Returns a log facility storing data into the default log directory
:param script__file__: put here __file__ variable from your script
:return: the log facility to be used in your script
"""
if log_directory is None:
# Configure the default logging directory client/log/...
log_directory = os.path.dirname(script__file__)
log = logging.getLogger()
log_file = os.path.join(log_directory, '{}.log'.format(os.path.basename(script__file__)))
file_handler = logging.handlers.TimedRotatingFileHandler(
log_file, when='midnight', interval=1, backupCount=7,
)
formatter = logging.Formatter('%(asctime)s [%(module)s.%(funcName)s](%(lineno)d)-%(levelname)s: %(message)s')
file_handler.setFormatter(formatter)
log.addHandler(file_handler)
if verbose:
formatter = ColoredFormatter(
"%(asctime)s %(log_color)s%(levelname)-8s%(reset)s [%(module)s.%(funcName)s] %(white)s%(message)s",
datefmt=None,
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red',
}
)
shell_handler = logging.StreamHandler(stream=sys.stdout)
shell_handler.setFormatter(formatter)
log.addHandler(shell_handler)
log.setLevel(level)
log.info("Verbosity level: {}".format(level))
return log
def log_error(exception=None):
log = logging.getLogger()
(_type, value, tb) = sys.exc_info()
tblast = traceback.format_tb(tb, limit=None)
traceback.print_tb(tb)
if exception is not None:
log.error("Error: " + exception.__str__())
if len(tblast):
log.error("An error occurred, traceback follows:")
log.error(tblast)
# (_type, value, tb) = sys.exc_info()
# tblast = traceback.extract_tb(tb, limit=None)
#
# if len(tblast):
# log.error("An error occurred, traceback follows:")
# log.error(tblast)
def get_processes():
"""
Retrieve an array containing details about the running processes
:rtype : dict
:return: List of tuples with (pid, command_line)
"""
pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
processes = dict()
for pid in pids:
try:
# Get command line
command = open(os.path.join('/proc', pid, 'cmdline'), 'rb').read()
# Get status description and clean result
status = open(os.path.join('/proc', pid, 'status'), 'rb').read()
status_lines = status.split("\n")
# Create dictionary with all the values retrieved
process_details = dict()
for l in status_lines:
l_array = l.split(":")
if len(l_array) > 1:
text = str(l_array[1])
# Add detail to dictionary
process_details[l_array[0]] = text.replace("\t","")
# Add details to processes dictionary
processes[int(pid)] = process_details
except IOError:
continue
return processes
def get_open_files(process_pid):
"""
Return a tuple (process, symlink, file) with the open files for the given process
:param process_pid: process pid to look for open files list
:return: None on error, list of open files for current process on success (process, symlink, file)
"""
try:
proc_fd_links = os.listdir("/proc/{}/fd".format(process_pid))
except Exception as e:
log_error(e)
print("Error Occurred")
return None
proc_files = []
for filelink in proc_fd_links:
try:
linked_file = os.readlink("/proc/{}/fd/{}".format(process_pid,filelink))
except OSError:
linked_file = None
proc_files.append(
(process_pid,
filelink,
linked_file))
return proc_files
def get_all_open_files():
all_processes = get_processes()
pids = all_processes.keys()
files_list = list()
for p in pids:
process_files = get_open_files(p)
if process_files is not None:
files_list.extend(process_files)
return files_list
def get_mac_address(ifname):
"""
Returns the mac address of the specified interface
:param ifname: String name of the network interface (ex. eth0)
:return: String value of the MAC address for the given interface
"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', ifname[:15]))
return ':'.join(['%02x' % ord(char) for char in info[18:24]])
def get_cpu_usage():
"""
Returns current cpu percent utilization
:return: CPU utilization percentage
"""
return psutil.cpu_percent(interval=1)
def get_memory_usage():
"""
Current memory utilization
:return: Current memory utilization in megabytes
"""
return psutil.phymem_usage().total/1024/1024
def ascii_to_hex_repr(string, width=40):
"""
Returns a string with the hex representation of each character of the given string value
:param string: Input string containing the byte values to be converted
:return: string containing the hex representation
"""
result = '[XX]'
for cols in range(0, min(width,len(string))):
result += " {:02}".format(cols)
result += "\n[00]"
cnt = 0
for x in string:
if (cnt % width) == 0 and cnt > 1:
result += "\n[{:02}]".format(cnt)
result += " {}".format(format(x, '02x'))
cnt += 1
return result
def hex_log(string, width=40):
result = '\n[XX]'
for cols in range(0, min(width,len(string))):
result += " {:02}".format(cols)
result += "\n[00]"
cnt = 0
for x in string:
if (cnt % width) == 0 and cnt > 1:
result += "\n[{:02}]".format(cnt)
result += " {}".format(format(x, '02x'))
cnt += 1
return result
def get_interface_ip(if_name):
"""
Retrieves mac address and ip address from the given interface name
:param if_name:
:return: (mac, ip) Returns a tuple with the interface details
"""
interfaces = netifaces.interfaces()
mac = None
ip = None
if if_name not in interfaces:
raise Exception('Unable to locate interface {}'.format(if_name))
try:
address = netifaces.ifaddresses(if_name)
except Exception as ex:
log_error(ex)
return
try:
if netifaces.AF_INET in address:
ip = address[netifaces.AF_INET][0]['addr']
except Exception as ex:
log_error(ex)
return
return ip
def get_interface_details(if_name):
"""
Retrieves mac address and ip address from the given interface name
:param if_name:
:return: (mac, ip) Returns a tuple with the interface details
"""
interfaces = netifaces.interfaces()
mac = None
ip = None
if if_name not in interfaces:
raise Exception('Unable to locate interface {}'.format(if_name))
try:
address = netifaces.ifaddresses(if_name)
except Exception as ex:
log_error(ex)
return None, None
try:
if netifaces.AF_LINK in address:
mac = address[netifaces.AF_LINK][0]['addr']
except Exception as ex:
log_error(ex)
return None, None
try:
if netifaces.AF_INET in address:
ip = address[netifaces.AF_INET][0]['addr']
except Exception as ex:
log_error(ex)
return mac, ip
def get_gateway():
"""
Retrieves the currently configured gateway address, None in case of failure
:return: Address of the current gateway
"""
try:
gw = netifaces.gateways()
return gw['default'][netifaces.AF_INET][0]
except Exception as ex:
log_error(ex)
return None
def get_host_id():
"""
Returns the current host id calculated from the local network mac address
:return: string containing the current host id
"""
# Available interfaces
available_ifaces = netifaces.interfaces()
available_ifaces = [item for item in available_ifaces if item == 'eth0']
# There are no interfaces, quitting application
if len(available_ifaces) == 0:
return False
# Using the first interface available for mac address
return get_interface_details(available_ifaces[0])[0].replace(":", "-")
def ping(host):
"""
Returns True if host (str) responds to a ping request.
Remember that some hosts may not respond to a ping request even if the host name is valid.
"""
# Ping parameters as function of OS
parameters = "-n 1" if system_name().lower() == "windows" else "-c 1"
# Pinging
return system_call("ping " + parameters + " " + host) == 0
def get_uptime():
r = os.popen("awk '{print $0/60;}' /proc/uptime").read().replace('\n','')
return float(r)
def get_client_datetime():
return datetime.now().isoformat()
def get_kites():
kites = []
pagekite_rows = os.popen("cat /etc/pagekite.d/10_default.rc").read().split('\n')
for row in pagekite_rows:
result = re.search('^service_on=[a-zA-Z]*-([0-9]*):([a-zA-Z0-9\.]*):[a-zA-Z0-9]*:[0-9]*:[a-zA-Z0-9\-]*$', row)
if (result):
kites.append("{}:{}".format(result.group(2),result.group(1)))
return kites