Revision c04ea593

View differences:

logsys.py
1
import psutil as psu
2
from time import sleep, time
3

  
4

  
5
def fprint(filename, data):
6
    if filename and len(filename) > 0 and data:
7
        f = open(filename, 'a')
8
        f.write(str(time()) + "," + str(data) + "\n")
9
        f.close()
10

  
11

  
12
def log_sys_resources(prefix, resources, interval=1):
13
    '''
14
    interval must be in second
15
    resources must be a dictionary with value equal to the filename
16
    '''
17
    if not prefix:
18
        prefix = "."
19
    if resources and 'net' in resources:
20
        iostat = psu.net_io_counters()
21
        pkts_recv = iostat.packets_recv
22
        pkts_sent = iostat.packets_sent
23
        bytes_recv = iostat.bytes_recv
24
        bytes_sent = iostat.bytes_sent
25

  
26
    while(True):
27
        if resources and 'cpu' in resources:
28
            fprint(prefix + "/" + resources['cpu'], psu.cpu_percent())
29
        if resources and 'mem' in resources:
30
            fprint(prefix + "/" + resources['mem'],
31
                   str(psu.virtual_memory().available) + "," +
32
                   str(psu.swap_memory().used))
33
        if resources and 'net' in resources:
34
            iostat = psu.net_io_counters()
35
            if iostat.packets_sent - pkts_sent > 0:
36
                fprint(prefix + "/" + resources['net'],
37
                       str((iostat.packets_recv - pkts_recv) /
38
                           float(iostat.packets_sent - pkts_sent)) + "," +
39
                       str((iostat.bytes_recv - bytes_recv) /
40
                           float(iostat.bytes_sent - bytes_sent)))
41
            else:
42
                fprint(prefix + "/" + resources['net'], "0,0")
43
        if interval:
44
            sleep(float(interval))

Also available in: Unified diff