4 from collections import OrderedDict
6 from vstf.agent.env.basic.commandline import CommandLine
7 from vstf.common import constants as const
9 log = logging.getLogger(__name__)
13 class Collect(object):
14 """collect host information such as _cpu, memory and so on"""
17 super(Collect, self).__init__()
18 self._system = self._system()
19 self._cpu = self._cpu()
22 """the base _system info
23 {'os info':{'_system':'ubuntu', 'kernel': '3.13.3'}}"""
24 return {const.OS_INFO:
26 '_system': open('/etc/issue.net').readline().strip(),
27 'kernel': platform.uname()[2]
32 """ Return the information in /proc/meminfo
34 meminfo = OrderedDict()
35 with open('/proc/meminfo') as f:
37 meminfo[line.split(':')[0]] = line.split(':')[1].strip()
39 return {const.MEMORY_INFO:
41 "Mem Total": meminfo['MemTotal'],
42 "Mem Swap": meminfo['SwapTotal']
48 with os.popen("lscpu") as f:
50 ret[line.split(':')[0].strip()] = line.split(':')[1].strip()
55 with open('/proc/cpuinfo') as f:
56 cpuinfo = OrderedDict()
60 cpuinfo = OrderedDict()
61 elif len(line.split(':')) == 2:
62 cpuinfo[line.split(':')[0].strip()] = line.split(':')[1].strip()
64 log.error("_cpu info unknow format <%(c)s>", {'c': line})
65 return {const.CPU_INFO:
68 "Model Name": ret[0]['model name'],
69 "Address sizes": ret[0]['address sizes']
75 def _hw_sysinfo(self):
76 cmdline = "dmidecode | grep -A 2 'System Information' | grep -v 'System Information'"
77 ret, output = CMD.execute(cmdline, shell=True)
81 for tmp in output.strip().split('\n'):
82 if tmp is None or tmp is "":
87 # first item as key, and the other as value
88 result[tmp[0].strip("\t")] = ";".join(tmp[1:])
89 return {const.HW_INFO: result}
91 return {const.HW_INFO: "get hw info failed. check the host by cmd: dmidecode"}
93 def collect_host_info(self):
94 return [self._system, self._cpu, self._memery(), self._hw_sysinfo()]
97 if __name__ == "__main__":
101 print json.dumps(c.collect_host_info(), indent=4)