summaryrefslogtreecommitdiffstats
path: root/vstf/vstf/agent/env/basic/collect.py
blob: bc4f1ee454352e19412fe0c84c299e4f83bea4a1 (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
import os
import platform
import logging
from collections import OrderedDict

from vstf.agent.env.basic.commandline import CommandLine
from vstf.common import constants as const

log = logging.getLogger(__name__)
CMD = CommandLine()


class Collect(object):
    """collect host information such as _cpu, memory and so on"""

    def __init__(self):
        super(Collect, self).__init__()
        self._system = self._system()
        self._cpu = self._cpu()

    def _system(self):
        """the base _system info
        {'os info':{'_system':'ubuntu', 'kernel': '3.13.3'}}"""
        return {const.OS_INFO:
            {
                '_system': open('/etc/issue.net').readline().strip(),
                'kernel': platform.uname()[2]
            }
        }

    def _memery(self):
        """ Return the information in /proc/meminfo
        as a dictionary """
        meminfo = OrderedDict()
        with open('/proc/meminfo') as f:
            for line in f:
                meminfo[line.split(':')[0]] = line.split(':')[1].strip()

        return {const.MEMORY_INFO:
            {
                "Mem Total": meminfo['MemTotal'],
                "Mem Swap": meminfo['SwapTotal']
            }
        }

    def _lscpu(self):
        ret = {}
        with os.popen("lscpu") as f:
            for line in f:
                ret[line.split(':')[0].strip()] = line.split(':')[1].strip()
        return ret

    def _cpu(self):
        ret = []
        with open('/proc/cpuinfo') as f:
            cpuinfo = OrderedDict()
            for line in f:
                if not line.strip():
                    ret.append(cpuinfo)
                    cpuinfo = OrderedDict()
                elif len(line.split(':')) == 2:
                    cpuinfo[line.split(':')[0].strip()] = line.split(':')[1].strip()
                else:
                    log.error("_cpu info unknow format <%(c)s>", {'c': line})
        return {const.CPU_INFO:
            dict(
                {
                    "Model Name": ret[0]['model name'],
                    "Address sizes": ret[0]['address sizes']
                },
                **(self._lscpu())
            )
        }

    def _hw_sysinfo(self):
        cmdline = "dmidecode | grep  -A 2 'System Information' | grep -v 'System Information'"
        ret, output = CMD.execute(cmdline, shell=True)
        if ret:
            result = {}
            # del the stderr
            for tmp in output.strip().split('\n'):
                if tmp is None or tmp is "":
                    continue
                # split the items 
                tmp = tmp.split(":")
                if len(tmp) >= 2:
                    # first item as key, and the other as value
                    result[tmp[0].strip("\t")] = ";".join(tmp[1:])
            return {const.HW_INFO: result}
        else:
            return {const.HW_INFO: "get hw info failed. check the host by cmd: dmidecode"}

    def collect_host_info(self):
        return [self._system, self._cpu, self._memery(), self._hw_sysinfo()]


if __name__ == "__main__":
    c = Collect()
    import json

    print json.dumps(c.collect_host_info(), indent=4)