forked from yongseokoh/nvme_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvme_exporter.py
executable file
·184 lines (152 loc) · 10.5 KB
/
nvme_exporter.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
184
from prometheus_client import start_http_server, Summary, Counter, Gauge, Histogram
from prometheus_client import Info
import nvme_smart as ns
import nvme_list as nl
import nvme_ctrl as nc
import nvme_simulation as nv_simul
import random
import time
import json
import os
import argparse
def init_nvme_ctrl_gauge():
nvme_ctrl_gauge = dict()
nvme_ctrl_gauge['cc'] = Gauge('nvme_ctrl_cc' ,'nvme_ctrl_configuration' , ['device', 'cc'])
nvme_ctrl_gauge['csts'] = Gauge('nvme_ctrl_csts' ,'nvme_ctrl_status' , ['device', 'csts'])
return nvme_ctrl_gauge
def init_nvme_smart_gauge():
nvme_smart_gauge = dict()
nvme_smart_gauge['critical_warning'] = Gauge('nvme_smart_log_critical_warning' ,'nvme_smart_log_critical_warning' , ['device', 'devno', 'critical_warning'])
nvme_smart_gauge['temperature'] = Gauge('nvme_smart_log_temperature' ,'nvme_smart_log_temperature' , ['device','devno'])
nvme_smart_gauge['avail_spare'] = Gauge('nvme_smart_log_avail_spare' ,'nvme_smart_log_avail_spare' , ['device', 'devno'])
nvme_smart_gauge['spare_thresh'] = Gauge('nvme_smart_log_spare_thresh' ,'nvme_smart_log_spare_thresh' , ['device', 'devno'])
nvme_smart_gauge['percent_used'] = Gauge('nvme_smart_log_percent_used' ,'nvme_smart_log_percent_used' , ['device', 'devno'])
nvme_smart_gauge['data_units_read'] = Gauge('nvme_smart_log_data_units_read' ,'nvme_smart_log_data_units_read' , ['device', 'devno'])
nvme_smart_gauge['data_units_written'] = Gauge('nvme_smart_log_data_units_written' ,'nvme_smart_log_data_units_written' , ['device', 'devno'])
nvme_smart_gauge['host_read_commands'] = Gauge('nvme_smart_log_host_read_commands' ,'nvme_smart_log_host_read_commands' , ['device', 'devno'])
nvme_smart_gauge['host_write_commands'] = Gauge('nvme_smart_log_host_write_commands' ,'nvme_smart_log_host_write_commands' , ['device', 'devno'])
nvme_smart_gauge['controller_busy_time'] = Gauge('nvme_smart_log_controller_busy_time' ,'nvme_smart_log_controller_busy_time' , ['device', 'devno'])
nvme_smart_gauge['power_cycles'] = Gauge('nvme_smart_log_power_cycles' ,'nvme_smart_log_power_cycles' , ['device', 'devno'])
nvme_smart_gauge['power_on_hours'] = Gauge('nvme_smart_log_power_on_hours' ,'nvme_smart_log_power_on_hours' , ['device', 'devno'])
nvme_smart_gauge['unsafe_shutdowns'] = Gauge('nvme_smart_log_unsafe_shutdowns' ,'nvme_smart_log_unsafe_shutdowns' , ['device', 'devno'])
nvme_smart_gauge['media_errors'] = Gauge('nvme_smart_log_media_errors' ,'nvme_smart_log_media_errors' , ['device', 'devno'])
nvme_smart_gauge['num_err_log_entries'] = Gauge('nvme_smart_log_num_error_log_entries' ,'nvme_smart_log_num_error_log_entries' , ['device', 'devno'])
nvme_smart_gauge['warning_temp_time'] = Gauge('nvme_smart_warning_temp_time' ,'nvme_smart_warning_temp_time' , ['device', 'devno'])
nvme_smart_gauge['critical_comp_time'] = Gauge('nvme_smart_critical_comp_time' ,'nvme_smart_critical_comp_time' , ['device', 'devno'])
nvme_smart_gauge['thm_temp1_trans_count'] = Gauge('nvme_smart_thm_temp1_trans_count' ,'nvme_smart_thm_temp1_trans_count' , ['device', 'devno'])
nvme_smart_gauge['thm_temp2_trans_count'] = Gauge('nvme_smart_thm_temp2_trans_count' ,'nvme_smart_thm_temp2_trans_count' , ['device', 'devno'])
return nvme_smart_gauge
def gather_nvme_smart_log(nvme_smart_gauge, nvme_list_json):
for nvme in nvme_list_json['Devices']:
device = nvme['DevicePath']
smart_json = ns.get_smart_log(device)
devno = nvme['DevicePath'][9:11]
if smart_json['critical_warning'] & 1:
nvme_smart_gauge['critical_warning'].labels(device=nvme['DevicePath'], devno=devno, critical_warning='avail_spare').set(1)
else:
nvme_smart_gauge['critical_warning'].labels(device=nvme['DevicePath'], devno=devno, critical_warning='avail_spare').set(0)
if smart_json['critical_warning'] & (1 << 1) :
nvme_smart_gauge['critical_warning'].labels(device=nvme['DevicePath'], devno=devno, critical_warning='temp_threshold').set(1)
else:
nvme_smart_gauge['critical_warning'].labels(device=nvme['DevicePath'], devno=devno, critical_warning='temp_threshold').set(0)
if smart_json['critical_warning'] & (1 << 2) :
nvme_smart_gauge['critical_warning'].labels(device=nvme['DevicePath'], devno=devno, critical_warning='nvm_subsystem_reliability').set(1)
else:
nvme_smart_gauge['critical_warning'].labels(device=nvme['DevicePath'], devno=devno, critical_warning='nvm_subsystem_reliability').set(0)
if smart_json['critical_warning'] & (1 << 3) :
nvme_smart_gauge['critical_warning'].labels(device=nvme['DevicePath'], devno=devno, critical_warning='read_only').set(1)
else:
nvme_smart_gauge['critical_warning'].labels(device=nvme['DevicePath'], devno=devno, critical_warning='read_only').set(0)
if smart_json['critical_warning'] & (1 << 4) :
nvme_smart_gauge['critical_warning'].labels(device=nvme['DevicePath'], devno=devno, critical_warning='volatile_backup_failed').set(1)
else:
nvme_smart_gauge['critical_warning'].labels(device=nvme['DevicePath'], devno=devno, critical_warning='volatile_backup_failed').set(0)
if smart_json['critical_warning'] & (1 << 5) :
nvme_smart_gauge['critical_warning'].labels(device=nvme['DevicePath'], devno=devno, critical_warning='persistent_memory_read_only').set(1)
else:
nvme_smart_gauge['critical_warning'].labels(device=nvme['DevicePath'], devno=devno, critical_warning='persistent_memory_read_only').set(0)
nvme_smart_gauge['temperature'].labels(device=nvme['DevicePath'], devno=devno).set(smart_json['temperature'])
nvme_smart_gauge['avail_spare'].labels(device=nvme['DevicePath'], devno=devno).set(smart_json['avail_spare'])
nvme_smart_gauge['spare_thresh'].labels(device=nvme['DevicePath'], devno=devno).set(smart_json['spare_thresh'])
nvme_smart_gauge['percent_used'].labels(device=nvme['DevicePath'], devno=devno).set(smart_json['percent_used'])
nvme_smart_gauge['data_units_read'].labels(device=nvme['DevicePath'], devno=devno).set(smart_json['data_units_read'])
nvme_smart_gauge['data_units_written'].labels(device=nvme['DevicePath'], devno=devno).set(smart_json['data_units_written'])
nvme_smart_gauge['host_read_commands'].labels(device=nvme['DevicePath'], devno=devno).set(smart_json['host_read_commands'])
nvme_smart_gauge['host_write_commands'].labels(device=nvme['DevicePath'], devno=devno).set(smart_json['host_write_commands'])
nvme_smart_gauge['controller_busy_time'].labels(device=nvme['DevicePath'],devno=devno).set(smart_json['controller_busy_time'])
nvme_smart_gauge['power_cycles'].labels(device=nvme['DevicePath'], devno=devno).set(smart_json['power_cycles'])
nvme_smart_gauge['power_on_hours'].labels(device=nvme['DevicePath'], devno=devno).set(smart_json['power_on_hours'])
nvme_smart_gauge['unsafe_shutdowns'].labels(device=nvme['DevicePath'], devno=devno).set(smart_json['unsafe_shutdowns'])
nvme_smart_gauge['media_errors'].labels(device=nvme['DevicePath'], devno=devno).set(smart_json['media_errors'])
nvme_smart_gauge['num_err_log_entries'].labels(device=nvme['DevicePath'], devno=devno).set(smart_json['num_err_log_entries'])
nvme_smart_gauge['warning_temp_time'].labels(device=nvme['DevicePath'], devno=devno).set(smart_json['warning_temp_time'])
nvme_smart_gauge['critical_comp_time'].labels(device=nvme['DevicePath'], devno=devno).set(smart_json['critical_comp_time'])
nvme_smart_gauge['thm_temp1_trans_count'].labels(device=nvme['DevicePath'], devno=devno).set(smart_json['thm_temp1_trans_count'])
nvme_smart_gauge['thm_temp2_trans_count'].labels(device=nvme['DevicePath'], devno=devno).set(smart_json['thm_temp2_trans_count'])
def gather_nvme_ctrl_info(nvme_ctrl_gauge, nvme_list_json):
for nvme in nvme_list_json['Devices']:
device = nvme['DevicePath']
ctrl_json = nc.get_ctrl_regs(device)
if ctrl_json['cc'] & 1:
nvme_ctrl_gauge['cc'].labels(device=nvme['DevicePath'], cc='en').set(1)
else:
nvme_ctrl_gauge['cc'].labels(device=nvme['DevicePath'], cc='en').set(0)
if ctrl_json['csts'] & 1:
nvme_ctrl_gauge['csts'].labels(device=nvme['DevicePath'], csts='rdy').set(1)
else:
nvme_ctrl_gauge['csts'].labels(device=nvme['DevicePath'], csts='rdy').set(0)
def put_nvme_info(nvme_list_json):
nvme_info = Info('nvme_info', 'Description of NVMe Info', ['device'])
for nvme in nvme_list_json['Devices']:
nvme_info.labels(nvme['DevicePath']).info({'ProduceName': nvme['ProductName']})
nvme_info.labels(nvme['DevicePath']).info({'ModelNumber': nvme['ModelNumber']})
def parse_args():
parser = argparse.ArgumentParser(
description='NVME Export port number and update period time'
)
parser.add_argument(
'-p', '--port',
required=False,
type=int,
help='Port to listen',
default=int(os.environ.get('PORT', '9900'))
)
parser.add_argument(
'-u', '--update',
required=False,
help='export mertic update period in seconds',
default=os.environ.get('UPDATE_PERIOD', '10')
)
parser.add_argument(
'-s', '--simulation',
required=False,
help='making use of NVMe simulation',
default=os.environ.get('SIMULATION', '0')
)
return parser.parse_args()
def main():
try:
args = parse_args()
#print('port = %d' % int(args.port))
#print('update = %d' % int(args.update))
#print('simulation = %d' % int(args.simulation))
nv_simul.NVME_SIMULATION = int(args.simulation)
# Start up the server to expose the metrics.
start_http_server(int(args.port))
# Generate some requests.
if nv_simul.NVME_SIMULATION == 1:
nv_simul.init_nvme_devices()
#nv_simul.init_simulation_smart_log()
nvme_list_json = nl.get_nvme_list()
put_nvme_info(nvme_list_json)
nvme_smart_gauge = init_nvme_smart_gauge()
nvme_ctrl_gauge = init_nvme_ctrl_gauge()
while True:
gather_nvme_smart_log(nvme_smart_gauge, nvme_list_json)
gather_nvme_ctrl_info(nvme_ctrl_gauge, nvme_list_json)
time.sleep(int(args.update))
except KeyboardInterrupt:
print("\nInterrupted")
exit(0)
if __name__ == '__main__':
main()