-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_sel_logs.py
169 lines (126 loc) · 6.24 KB
/
check_sel_logs.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/python2.6
# Description :
# -----------
#
# This script will check the occupency of the IPMI SEL Logs and if the threshold is affected it will dump the logs and erase the IPMI SEL logs
# The dump of the IPMI SEL Logs will be stored into /var/log/ directory into the following format ipmi_sel_logs.log
# A mechanism of logrotate will rotate the dump and error log each month
# The script should be generic and robbust to handle all the brands and model of BMC
# It will use the third party tool ipmitool
from datetime import date
import subprocess
import re
import os
import sys
class SelLogs():
# Contructor SelLogs
def __init__(self):
# Declaration of attributes
self.path_ipmitool = None
self.option_sel_ipmitool = None
self.option_clear_ipmitool = None
self.option_elist_ipmitool = None
self.date_for_logs = None
self.path_to_dump_logs = None
self.name_of_sel_logs = None
self.percentage_used = None
try:
# Definition of attributes
self.path_ipmitool = "/usr/bin/ipmitool"
self.option_sel_ipmitool = "sel"
self.option_clear_ipmitool = "clear"
self.option_elist_ipmitool = "elist"
try:
self.date_for_logs = date.today().strftime("%m/%d/%Y")
except Exception:
self.date_for_logs = "00-00-0000"
self.path_to_dump_logs = "/var/log/"
self.name_of_sel_logs = "ipmi_sel_logs.log"
except Exception:
sys.exit("[ERROR] - " + str(self.date_for_logs) + " The assignement of the attributes has failed\n")
# Function that check the SEL logs of the BMC
def check_logs(self):
try:
# Running the command ipmitool to get the SEL informations
ipmitool_output = subprocess.Popen([self.path_ipmitool,self.option_sel_ipmitool], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].strip()
except Exception:
sys.exit("[ERROR] - " + str(self.date_for_logs) + " The command ipmitool sel has failed...\n")
try:
# Definition of the regex for the needed values
alloc_unit_size = re.search('^Alloc\\s+Unit\\s+Size\\s+:\\s+(\\d+)$', ipmitool_output, re.MULTILINE)
alloc_total_size = re.search('^#\\s+of\\s+Alloc\\s+Units\\s+:\\s+(\\d+)$', ipmitool_output, re.MULTILINE)
alloc_free_size = re.search('^#\\s+Free\\s+Units\\s+:\\s+(\\d+)$', ipmitool_output, re.MULTILINE)
entries = re.search('^Entries\\s+:\\s+(\\d+)$', ipmitool_output, re.MULTILINE)
except Exception:
sys.exit("[ERROR] - " + str(self.date_for_logs) + " An error occured on the regex search...\n")
try:
# Compute the percentage used
number_of_bytes_used = float(entries.group(1)) * float(alloc_unit_size.group(1))
number_of_bytes_total = float(alloc_total_size.group(1)) * float(alloc_unit_size.group(1))
percentage_used = ( number_of_bytes_used / number_of_bytes_total ) * 100
except Exception:
sys.exit("[ERROR] - " + str(self.date_for_logs) + " Something is wrong in computing the percentage used...\n")
try:
percentage = int(percentage_used)
self.percentage_used = percentage
except Exception:
sys.exit("[ERROR] - " + str(self.date_for_logs) + " Something is wrong in casting to integer the percentage...\n")
if percentage >= 80:
return True
else:
return False
# Function that clear the SEL Logs of the BMC
def clear_logs(self):
try:
# Running the command ipmitool to clear the SEL logs
ipmitool_clear_output = subprocess.Popen([self.path_ipmitool,self.option_sel_ipmitool,self.option_clear_ipmitool], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].strip()
except Exception:
sys.exit("[ERROR] - " + str(self.date_for_logs) + " The command ipmitool sel clear has failed...\n")
# Function that dump the logs
def dump_logs(self):
try:
# Check first if the log file exist ?
if os.path.isfile(self.path_to_dump_logs + self.name_of_sel_logs) == False:
file = open(self.path_to_dump_logs + self.name_of_sel_logs, "w")
file.close()
# Opening the log file for adding some data
file = open(self.path_to_dump_logs + self.name_of_sel_logs, "a")
file.write("\n################# START : " + str(self.date_for_logs) + " #################\n")
ipmitool_sel_logs = subprocess.Popen([self.path_ipmitool,self.option_sel_ipmitool,self.option_elist_ipmitool], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].strip()
# Writing on the Log file
for line in ipmitool_sel_logs.split('\n'):
file.write(str(line) + "\n")
file.write("\n################# END : " + str(self.date_for_logs) + " #################\n")
# Closing the Log file
file.close()
except Exception:
sys.exit("[ERROR] - " + str(self.date_for_logs) + " An error has occured in openning/writting to the file log in the dump_logs function...\n")
# Main function
def main():
# Instanciate the SelLogs object
AnalyseSelLogs = SelLogs()
try:
# Checking if the restore_kipmi_kernel_helper_thread file is present
if os.path.isfile('/var/lock/subsys/restore_kipmi_kernel_helper_thread'):
sys.exit("[ERROR] - " + str(AnalyseSelLogs.date_for_logs) + " Exiting, the kipmi kernel helper thread is being restored...\n")
else:
# If the Sel logs are almost full we dump the logs and clear the BMC
if AnalyseSelLogs.check_logs() == True:
AnalyseSelLogs.dump_logs()
AnalyseSelLogs.clear_logs()
else:
# Check first if the log file exist ?
if os.path.isfile(AnalyseSelLogs.path_to_dump_logs + AnalyseSelLogs.name_of_sel_logs) == False:
file = open(AnalyseSelLogs.path_to_dump_logs + AnalyseSelLogs.name_of_sel_logs, "w")
file.close()
# Writing to the Log file that nothing need to be dump
file = open(AnalyseSelLogs.path_to_dump_logs + AnalyseSelLogs.name_of_sel_logs, "a")
file.write("\n################# START : " + str(AnalyseSelLogs.date_for_logs) + " #################\n")
file.write("\nThe percentage used of the SEL logs is : " + str(AnalyseSelLogs.percentage_used) + " %\n")
file.write("\n################# END : " + str(AnalyseSelLogs.date_for_logs) + " #################\n")
file.close()
return 0
except Exception:
sys.exit("[ERROR] - " + str(AnalyseSelLogs.date_for_logs) + " An error has occured in openning/writting to the file log in the main funtion ...\n")
if __name__ == '__main__':
main()