Add hardware type for Ampere Hawk server
[ta/hw-detector.git] / src / hw_detector / hw_ipmi_lib.py
1 # Copyright 2019 Nokia
2
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
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import subprocess
16 import os
17
18 from hw_detector.hw_exception import HWException
19
20 def get_ipmi_info(ipmi_addr, ipmi_user, ipmi_pass, ipmi_priv_level='ADMINISTRATOR'):
21     command = "ipmitool -I lanplus -H %s  -U %s -P %s -L %s fru print 0" % (ipmi_addr, ipmi_user, ipmi_pass, ipmi_priv_level)
22     info = ipmi_info(command)
23
24     command = "ipmitool -I lanplus -H %s  -U %s -P %s -L %s lan print" % (ipmi_addr, ipmi_user, ipmi_pass, ipmi_priv_level)
25     info.update(ipmi_info(command))
26
27     return info
28
29 def _load_ipmi_drivers():
30     with open(os.devnull, 'w') as devnull:
31         subprocess.call('modprobe ipmi_msghandler', stderr=devnull, shell=True)
32         subprocess.call('modprobe ipmi_devintf', stderr=devnull, shell=True)
33         subprocess.call('modprobe ipmi_si', stderr=devnull, shell=True)
34         subprocess.call('modprobe ipmi_ssif', stderr=devnull, shell=True)
35
36 def get_local_ipmi_info():
37     _load_ipmi_drivers()
38     info = ipmi_info("ipmitool fru print 0")
39     info.update(ipmi_info("ipmitool lan print"))
40     return info
41
42 def ipmi_info(command):
43     out = None
44     p = subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
45     out, _ = p.communicate()
46
47     if p.returncode:
48         raise HWException(out)
49
50     info = {}
51     for line in out.split('\n'):
52         spl = line.find(':')
53         if spl == -1:
54             continue
55         else:
56             key = line[0:spl].strip()
57             if key == '':
58                 continue
59             info[line[0:spl].strip()] = line[spl+1:].strip()
60     return info