1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
13 from collections import OrderedDict
15 from vstf.agent.env.basic.commandline import CommandLine
16 from vstf.common import constants as const
18 log = logging.getLogger(__name__)
22 class Collect(object):
23 """collect host information such as _cpu, memory and so on"""
26 super(Collect, self).__init__()
27 self._system = self._system()
28 self._cpu = self._cpu()
31 """the base _system info
32 {'os info':{'_system':'ubuntu', 'kernel': '3.13.3'}}"""
33 return {const.OS_INFO:
35 '_system': open('/etc/issue.net').readline().strip(),
36 'kernel': platform.uname()[2]
41 """ Return the information in /proc/meminfo
43 meminfo = OrderedDict()
44 with open('/proc/meminfo') as f:
46 meminfo[line.split(':')[0]] = line.split(':')[1].strip()
48 return {const.MEMORY_INFO:
50 "Mem Total": meminfo['MemTotal'],
51 "Mem Swap": meminfo['SwapTotal']
57 with os.popen("lscpu") as f:
59 ret[line.split(':')[0].strip()] = line.split(':')[1].strip()
64 with open('/proc/cpuinfo') as f:
65 cpuinfo = OrderedDict()
69 cpuinfo = OrderedDict()
70 elif len(line.split(':')) == 2:
71 cpuinfo[line.split(':')[0].strip()] = line.split(':')[
74 log.error("_cpu info unknow format <%(c)s>", {'c': line})
75 return {const.CPU_INFO:
78 "Model Name": ret[0]['model name'],
79 "Address sizes": ret[0]['address sizes']
85 def _hw_sysinfo(self):
86 cmdline = "dmidecode | grep -A 2 'System Information' | grep -v 'System Information'"
87 ret, output = CMD.execute(cmdline, shell=True)
91 for tmp in output.strip().split('\n'):
92 if tmp is None or tmp is "":
97 # first item as key, and the other as value
98 result[tmp[0].strip("\t")] = ";".join(tmp[1:])
99 return {const.HW_INFO: result}
102 const.HW_INFO: "get hw info failed. check the host by cmd: dmidecode"}
104 def collect_host_info(self):
105 return [self._system, self._cpu, self._memery(), self._hw_sysinfo()]
108 if __name__ == "__main__":
112 print json.dumps(c.collect_host_info(), indent=4)