-
Notifications
You must be signed in to change notification settings - Fork 1
/
ipmi_check.py
88 lines (76 loc) · 2.61 KB
/
ipmi_check.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
#!/usr/bin/python
# encoding:utf-8
import os
import sys
import time
server = sys.argv[1]
sensor = sys.argv[2]
request_type = sys.argv[3]
user = 'ipmi_user'
pwd = 'ipmi_pwd'
cache_file = '/var/tmp/ipmi_sensor_' + server
def fru_chassis():
ipmi_cmd = '/usr/sbin/ipmi-{} -D LAN2_0 -h {} -u {} -p {} -l USER -W discretereading'
if sensor == 'fru':
ipmi_cmd += ' -e 0 2>/dev/null'
else:
ipmi_cmd += ' --get-status 2>/dev/null'
ipmi_cmd = ipmi_cmd.format(sensor, server, user, pwd)
ipmi_list = os.popen(ipmi_cmd).readlines()
count = 0
while len(ipmi_list) == 0 and count < 5:
ipmi_list = os.popen(ipmi_cmd).readlines()
count += 1
for ipmi in ipmi_list:
try:
key, value = ipmi.split(':')
except:
continue
if request_type.lower() == key.strip().lower():
print(value.strip())
break
def create_ipmi_file():
ipmi_cmd = 'ipmi-sensors -D LAN2_0 -h {} -u {} -p {} -l USER -W discretereading --no-header-output --quiet-cache --sdr-cache-recreate --comma-separated-output --entity-sensor-names 2>/dev/null'
ipmi_cmd = ipmi_cmd.format(server, user, pwd)
ipmi_list = os.popen(ipmi_cmd).readlines()
count = 0
while len(ipmi_list) == 0 and count < 5:
ipmi_list = os.popen(ipmi_cmd).readlines()
count += 1
if os.path.exists(cache_file + '.lock'):
return
else:
open(cache_file + '.lock', 'a').close()
with open(cache_file, 'w') as f:
f.write(str(time.time()) + '\n')
for sensor in ipmi_list:
f.write(sensor)
os.remove(cache_file + '.lock')
def rewrite_ipmi_file():
with open(cache_file, 'r') as f:
lines = f.readlines()
ipmi_create_time = float(lines[0])
if time.time() - ipmi_create_time > 120 or not (os.path.exists(cache_file)) or os.path.getsize(
cache_file) < 100:
create_ipmi_file()
def ipmi_check():
with open(cache_file, 'r') as txt:
for data_txt in txt:
try:
num, key, section, value, measure, status = data_txt.split(',')
except:
continue
if sensor == key:
if 'number' in request_type:
result = float(value)
print(result)
return
elif 'status' in request_type:
result = status.strip().replace('\'', '')
print(result)
return
if sensor == 'fru' or sensor == 'chassis':
fru_chassis()
else:
rewrite_ipmi_file()
ipmi_check()