1 # Copyright 2015 Intel Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 """Tools for access to OS details
23 from conf import settings
28 :returns: Return distro name as a string
30 return ' '.join(platform.dist())
33 """Get kernel version.
35 :returns: Return kernel version as a string
37 return platform.release()
40 """Get CPU information.
42 :returns: Return CPU information as a string
44 with open('/proc/cpuinfo') as file_:
48 if not line.rstrip('\n').startswith('model name'):
51 return line.rstrip('\n').split(':')[1]
54 """Get NIC(s) information.
56 :returns: Return NIC(s) information as a string
59 output = subprocess.check_output('lspci', shell=True)
60 output = output.decode(locale.getdefaultlocale()[1])
61 for line in output.split('\n'):
62 for nic_pciid in settings.getValue('WHITELIST_NICS'):
63 if line.startswith(nic_pciid):
64 nics.append(''.join(line.split(':')[2:]).strip())
65 return ', '.join(nics).strip()
68 """Get platform information.
70 Currently this is the motherboard vendor, name and socket
73 :returns: Return platform information as a string
77 with open('/sys/class/dmi/id/board_vendor', 'r') as file_:
78 output.append(file_.readline().rstrip())
80 with open('/sys/class/dmi/id/board_name', 'r') as file_:
81 output.append(file_.readline().rstrip())
83 num_nodes = len([name for name in os.listdir(
84 '/sys/devices/system/node/') if name.startswith('node')])
85 output.append(''.join(['[', str(num_nodes), ' sockets]']))
87 return ' '.join(output).strip()
90 """Get number of CPU cores.
92 :returns: Return number of CPU cores
95 with open('/proc/cpuinfo') as file_:
97 if line.rstrip('\n').startswith('processor'):
101 # this code must be executed by at leat one core...
107 """Get memory information.
109 :returns: amount of system memory as string together with unit
111 with open('/proc/meminfo') as file_:
115 if not line.rstrip('\n').startswith('MemTotal'):
118 return line.rstrip('\n').split(':')[1].strip()
120 def get_memory_bytes():
121 """Get memory information in bytes
123 :returns: amount of system memory
125 mem_list = get_memory().split(' ')
126 mem = float(mem_list[0].strip())
127 if mem_list.__len__() > 1:
128 unit = mem_list[1].strip().lower()
140 def get_pids(proc_names_list):
141 """ Get pid(s) of process(es) with given name(s)
143 :returns: list with pid(s) of given processes or None if processes
144 with given names are not running
148 pids = subprocess.check_output(['pidof'] + proc_names_list)
149 except subprocess.CalledProcessError:
150 # such process isn't running
153 return list(map(str, map(int, pids.split())))
155 def get_pid(proc_name_str):
156 """ Get pid(s) of process with given name
158 :returns: list with pid(s) of given process or None if process
159 with given name is not running
161 return get_pids([proc_name_str])