aboutsummaryrefslogtreecommitdiffstats
path: root/torouterui/sysstatus.py
blob: 38877245301de881cbabb0ae3435adfe3db893dd (plain)
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
"""
Helper code for agregating general system status information, and for reading
in system log files.
"""

import os

from util import *

def get_system_status():
    d = dict()
    d['hostname'] = cli_read('hostname')
    d['current_time'] = cli_read('date -R')
    d['uptime'] = cli_read('uptime')
    # the following do DNS lookups and execute very slowly
    #d['fqdn'] = cli_read('hostname -f')
    #d['domain'] = cli_read('hostname -d')
    return d

def get_resources_status():
    """
    Example ``df -h /home`` output:

        Filesystem              Size  Used Avail Use% Mounted on
        /dev/mapper/xxxxx-root  231G  184G   35G  85% /

    Example ``free -m`` output:

                    total       used       free     shared    buffers     cached
        Mem:          3862       2292       1570          0        104        689
        -/+ buffers/cache:       1498       2364
        Swap:         7983        202       7781

    Example ``uptime`` output:

         21:38:55 up 7 days,  5:43, 11 users,  load average: 0.60, 0.63, 0.63
    """
    d = dict()
    disk_info = cli_read_lines('df -h /home')[1].split()
    d['disk_used'] = disk_info[2]
    d['disk_avail'] = disk_info[1]
    d['disk_percent'] = int(disk_info[4][:-1])

    ram_info = cli_read_lines('free -m')
    d['ram_used'] = "%sMB" % ram_info[2].split()[2]
    d['ram_avail'] = "%sMB" % ram_info[1].split()[1]
    d['ram_percent'] = int(float(d['ram_used'].strip('MB'))/float(d['ram_avail'].strip('MB')) * 100.0)

    d['cpu_cores'] = 1
    for l in open('/proc/cpuinfo', 'r'):
        if l.split(":")[0].strip() == "cpu cores":
            d['cpu_cores'] = int(l.split(":")[-1].strip())
            break
    cpu_info = cli_read('uptime')
    d['cpu_load'] = cpu_info.split()[-3].strip(',')
    d['cpu_percent'] = int(float(d['cpu_load'])/float(d['cpu_cores']) * 100.0)
    return d

def get_dmesg():
    try:
        return cli_read('dmesg')
    except IOError:
        return None

def get_authlog():
    try:
        with open('/var/log/auth.log') as f:
            return ''.join(f.readlines())
    except IOError:
        return None

def get_syslog():
    try:
        with open('/var/log/syslog') as f:
            return ''.join(f.readlines())
    except IOError:
        return None

def get_torlog():
    try:
        with open('/var/log/tor/notices.log') as f:
            return ''.join(f.readlines())
    except IOError:
        return None

def get_process_list():
    """
    Example ``ps aux`` output:

        USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
        root         1  0.0  0.0  10636   676 ?        Ss   Sep08   0:05 init [2]
        root         2  0.0  0.0      0     0 ?        S    Sep08   0:00 [kthreadd]
        root         3  0.0  0.0      0     0 ?        S    Sep08   0:40 [ksoftirqd/0]
        root         6  0.0  0.0      0     0 ?        S    Sep08   0:00 [migration/0]
    """
    plist = list()
    first = True
    for l in cli_read_lines('ps aux --sort -%mem')[1:]:
        l = l.split()
        plist.append(dict(
            user = l[0],
            pid = l[1],
            perc_cpu = l[2],
            perc_mem = l[3],
            status_code = l[7],
            started = l[8],
            time = l[9],
            command = ' '.join(l[10:])))
    return plist