-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiloPromConnector.v1.0.py
183 lines (147 loc) · 6.37 KB
/
iloPromConnector.v1.0.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# -*- coding: utf-8 -*-
"""
Created on July 22, 2020
Version 4.1
Used for a Kubernetes deployment with configmaps instead of runtime arguments.
Copyright (c) 2020 Thomas Beha
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
https://www.gnu.org/licenses/gpl-3.0.en.html
This release of the Prometheus Connector requires: SimpliVityClass v4.0
"""
from cryptography.fernet import *
from lxml import etree
import time
from SimpliVityClass import *
from datetime import datetime
from prometheus_client import Counter, Gauge, start_http_server, Info, Enum
import sys
from redfish import RedfishClient
from redfish.rest.v1 import ServerDownOrUnreachableError
BtoGB=pow(1024,3)
BtoMB=pow(1024,2)
power_state={
'Unknown': 0,
'On': 1,
'Off': 2,
'Suspended': 3
}
def logwriter(f, text):
output=str(datetime.today()) +": "+text+" \n"
print(output)
f.write(output)
def logopen(filename):
f = open(filename,'a')
f.write(str(datetime.today())+": Logfile opened \n")
return f
def logclose(f):
f.write(str(datetime.today())+": Logfile closed \n")
f.close()
def get_resource_directory(redfishobj):
try:
resource_uri = redfishobj.root.obj.Oem.Hpe.Links.ResourceDirectory['@odata.id']
except KeyError:
sys.stderr.write("Resource directory is only available on HPE servers.\n")
return None
response = redfishobj.get(resource_uri)
resources = []
if response.status == 200:
#sys.stdout.write("\tFound resource directory at /redfish/v1/resourcedirectory" + "\n\n")
resources = response.dict["Instances"]
else:
sys.stderr.write("\tResource directory missing at /redfish/v1/resourcedirectory" + "\n")
return resources
def get_server_data( uri, login_account, login_password):
server_data={}
try:
# Create a Redfish client object
REDFISHOBJ = RedfishClient(base_url=uri, username=login_account, password=login_password)
# Login with the Redfish client
REDFISHOBJ.login()
except ServerDownOrUnreachableError as excp:
sys.stderr.write("ERROR: server not reachable or does not support RedFish.\n")
sys.exit()
resource_instances = get_resource_directory(REDFISHOBJ)
for instance in resource_instances:
if '#Power.' in instance ['@odata.type']:
uri = REDFISHOBJ.get(instance['@odata.id']).obj.Oem.Hpe['Links']['PowerMeter']['@odata.id']
server_data["PowerMeter"] = REDFISHOBJ.get(uri).obj
if '#Thermal.' in instance ['@odata.type']:
server_data['Temperatures'] = REDFISHOBJ.get(instance['@odata.id']).obj["Temperatures"]
if '#ComputerSystem.' in instance ['@odata.type']:
server_data['System'] = REDFISHOBJ.get(instance['@odata.id']).obj
REDFISHOBJ.logout()
return server_data;
### Main ###########################################################################
if __name__ == "__main__":
""" read the key and input file"""
""" Debug: """
"""
path= os.getcwd()
keyfile= path+'\\svttest.key'
xmlfile= path+'\\svttest.xml'
print("Path: ", path)
print("Keyfile: ", keyfile)
print("XMLfile: ", xmlfile)
"""
path = '.'
keyfile = path + '/iloprometheus.key'
xmlfile = path + '/iloprometheus.xml'
""" Read keyfile """
f = open(keyfile, 'r')
k2=f.readline()
f.close()
key2=k2.encode('ASCII')
""" Read the parameter file """
tree = etree.parse(xmlfile)
u2=(tree.find("user")).text
p2=(tree.find("password")).text
f = Fernet(key2)
user = f.decrypt(u2.encode('ASCII')).decode('ASCII')
password = f.decrypt(p2.encode('ASCII')).decode('ASCII')
lfile=(tree.find("logfile")).text
port=int((tree.find("port")).text)
mintervall = int((tree.find("monitoringintervall")).text)
ilos=tree.findall("ILO_ip") # List of all ILOs
log=logopen(path+lfile)
logwriter(log,"Started ILO Prometheus Connector Test")
start_http_server(port)
c = Counter('ilorest_sample','ILO REST sample number')
node = Gauge('ilorest_node','ILO Node Data',['nodename','nodemetric'])
delta = Gauge('ConnectorRuntime','Time required for last data collection in seconds')
#icon = Info('Connector','Connector Paramter Info')
inode = Info('node','Additional Node Info',['node'])
mintervall = 60
while True:
try:
t0 = time.time()
c.inc()
for ilo in ilos:
ilo_url="https://"+ilo.text
server_metrics = get_server_data(ilo_url, user, password)
cn = (server_metrics['System']['HostName']).split('.')[0].replace('-','_')
inode.labels(cn).info({"Model":server_metrics['System']["Model"],"Manufacturer":server_metrics['System']["Manufacturer"],"SerialNumber":server_metrics['System']["SerialNumber"]})
node.labels(cn,'PowerState').set(power_state[server_metrics['System']["PowerState"]])
node.labels(cn,"PowerAverage").set(server_metrics["PowerMeter"]['Average'])
node.labels(cn,"PowerMaximum").set(server_metrics["PowerMeter"]['Maximum'])
node.labels(cn,"PowerMinimum").set(server_metrics["PowerMeter"]['Minimum'])
for temperature in server_metrics['Temperatures']:
node.labels(cn,temperature["Name"]).set(temperature['ReadingCelsius'])
t1 = time.time()
delta.set((t1-t0))
while ((t1-t0) < mintervall):
time.sleep(1.0)
t1 = time.time()
except Exception as ex:
print(ex)
log=logopen(path+lfile)
logwriter(log,'Exception')
logwriter(log,str(ex))
logclose(log)
pass