-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipmi_configurator.py
executable file
·102 lines (79 loc) · 4.07 KB
/
ipmi_configurator.py
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
#!/usr/bin/env python3
# IPMI Configurator
#
# Read from a INI config file a list of machines HW type, followed by records in this form:
# sensor_NUM="EventFilter, Sensor Number, SensorID, Upper Non-Critical Threshold, Upper Critical Threshold"
#
# 1. Iterate through the [sections] of the INI file.
# 2. Is there a product name correspondance in the config file?
# 3. If yes then proceed further to parser the data for the sensor
# and create a PEF event.
import getopt
import os
import shlex
import subprocess
import sys
import configparser
from subprocess import PIPE
# EventFilter, Sensor Number, SensorID, Upper Non-Critical Threshold, Upper Critical Threshold
def pef_config(sensor_data):
eventFilter, sensorNumber, sensorID, upNcTh, upCrTh = sensor_data.split(",")
# Light sanity check:
if eventFilter.isdigit() and upNcTh.isdigit() and upCrTh.isdigit() and sensorNumber.isdigit():
# Get the PEF list and verify if we have already defined an alert for the sensor in question:
args_ipmitool = ['/usr/bin/ipmitool','pef','filter','list']
args_grep = ['grep','enabled']
ipmitool_cmd = subprocess.Popen(args_ipmitool, stdout=subprocess.PIPE, shell=False)
grep_cmd = subprocess.Popen(args_grep, stdin=ipmitool_cmd.stdout, stdout=subprocess.PIPE, shell=False)
ipmitool_cmd.stdout.close() # Allow the ipmitool process to receive a SIGPIPE if the grep process exits.
pef_list = grep_cmd.communicate()[0]
if sensorNumber not in pef_list.decode():
# Set IPMI non-critical/critical thresholds:
subprocess.call(shlex.split("/usr/sbin/ipmi-config --category=sensors --commit -e "+sensorID+":Upper_Non_Critical_Threshold="+upNcTh))
subprocess.call(shlex.split("/usr/sbin/ipmi-config --category=sensors --commit -e "+sensorID+":Upper_Critical_Threshold="+upCrTh))
# Build the event filter string:
eventFilter_string = "Event_Filter_"+eventFilter
# Setup IPMI PEF:
if subprocess.call(shlex.split("/usr/sbin/ipmi-config --category=pef --commit -e "+eventFilter_string+":Sensor_Type=Temperature \
-e "+eventFilter_string+":Event_Severity=Critical \
-e "+eventFilter_string+":Event_Filter_Action_Power_Off=yes \
-e "+eventFilter_string+":Enable_Filter=yes \
-e "+eventFilter_string+":Sensor_Number="+sensorNumber)): return 0
else:
return 1
else:
return 0 # PEF already defined.
else:
print("WARNING: Check your temperature thresholds or the sensor/event filter number --> they cannot be in alphanumerical format!!")
sys.exit(1)
def main(argv):
if not os.geteuid() == 0:
sys.exit("\nOnly root can run this script\n")
# Preset the return status to a safe value:
pef_status = 0
# Initizialize the parser:
parser = configparser.ConfigParser()
# Config filename (hardcoded in case no config file is provided on the cmd line):
config_file = 'ipmi_sensors.ini'
# The script will accept only two options:
# -h print (generic help msg)
# -f config_file (a config file in INI format)
opts, args = getopt.getopt(argv,"hf::")
for opt, arg in opts:
if opt == '-h':
print(sys.argv[0] + ' -f <cfgfile>')
sys.exit()
elif opt in ("-f"):
config_file = arg
parser.read(config_file)
for section_name in parser.sections():
system_name = system_name=subprocess.run(['/usr/sbin/dmidecode','-s', 'system-product-name'], stdout=PIPE, universal_newlines=True)
if (system_name.stdout.replace(" ", "")).rstrip('\n') in section_name:
for name, value in parser.items(section_name):
pef_status = pef_config(value)
if pef_status == 0:
sys.exit(0)
else:
sys.exit(1)
if __name__ == "__main__":
main(sys.argv[1:])