-
Notifications
You must be signed in to change notification settings - Fork 0
/
info-exporter
executable file
·1019 lines (882 loc) · 38.5 KB
/
info-exporter
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
import csv
import logging
import argparse
import json
import re
import shlex
import tempfile
import os
import subprocess
import time
import sys
from prometheus_client.core import GaugeMetricFamily
from prometheus_client import make_wsgi_app
from wsgiref.simple_server import make_server, WSGIRequestHandler
class ParsingError(Exception):
pass
class InfinibandCollector(object):
def csv_global_parser(self, csv_file_input):
with tempfile.TemporaryDirectory(prefix='ibdiag_pars') as temp_dir:
if csv_file_input == "/var/tmp/ibdiagnet2/ibdiagnet2.db_csv":
logging.debug(f'Start file generation process')
try:
if self.phy:
cmd = f'ibdiagnet --pm_pause_time 0 -o {temp_dir} --get_phy_info --disable_output default --enable_output db_csv'
else :
cmd = f'ibdiagnet --pm_pause_time 0 -o {temp_dir} --disable_output default --enable_output db_csv'
run = subprocess.Popen(shlex.split(cmd),
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
for line in run.stdout:
if line[:3] == b'-E-':
logging.error(line.decode('utf-8'))
return_code = run.wait()
if return_code != 0:
logging.error('ibdiagnet ran doesn\'t return 0')
except FileNotFoundError as e:
self.scrape_with_errors = True
logging.error(f'An error occured with the generation of the csv : {e}')
logging.debug(f'End of file generation process')
cable_info = []
pm_info = []
temp_sensing = []
link_info = []
fan_info = []
power_info = []
temp_info = []
try:
with open(os.path.join(temp_dir, 'ibdiagnet2.db_csv'), mode='r') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
isCableInfoData = False
isTempSensingData = False
isPmInfoData = False
isLinkInfo = False
isFanInfo = False
isPowerInfo = False
isTempInfo = False
for row in reader:
if 'END_CABLE_INFO' in row:
logging.debug('Now out of cable info table')
isCableInfoData = False
if isCableInfoData:
cable_info.append(','.join(row))
if 'START_CABLE_INFO' in row:
logging.debug('Now in cable info table')
isCableInfoData = True
if 'END_TEMP_SENSING' in row:
logging.debug('Now out of temp sensing table')
isTempSensingData = False
if isTempSensingData:
temp_sensing.append(','.join(row))
if 'START_TEMP_SENSING' in row:
logging.debug('Now in temp sensing table')
isTempSensingData = True
if 'END_PM_INFO' in row:
logging.debug('Now out of pm info table')
isPmInfoData = False
if isPmInfoData:
pm_info.append(','.join(row))
if 'START_PM_INFO' in row:
logging.debug('Now in pm info table')
isPmInfoData = True
if 'END_LINKS' in row:
logging.debug('Now out of link info table')
isLinkInfo = False
if isLinkInfo:
link_info.append(','.join(row))
if 'START_LINKS' in row:
logging.debug('Now in link info table')
isLinkInfo = True
if 'END_FANS_SPEED' in row:
logging.debug('Now out of fan info table')
isFanInfo = False
if isFanInfo:
fan_info.append(','.join(row))
if 'START_FANS_SPEED' in row:
logging.debug('Now in fan info table')
isFanInfo = True
if 'END_POWER_SUPPLIES' in row:
logging.debug('Now out of power info table')
isPowerInfo = False
if isPowerInfo:
power_info.append(','.join(row))
if 'START_POWER_SUPPLIES' in row:
logging.debug('Now in fan info table')
isPowerInfo = True
if 'END_TEMPERATURE_SENSORS' in row:
logging.debug('Now out of temperature info table')
isTempInfo = False
if isTempInfo:
temp_info.append(','.join(row))
if 'START_TEMPERATURE_SENSORS' in row:
logging.debug('Now in temperature info table')
isTempInfo = True
except Exception as e:
logging.critical(f"Error while reading the CSV file: {e}")
self.scrape_with_errors = True
return cable_info, pm_info, temp_sensing, link_info, fan_info, power_info, temp_info
def data_filter(self, filter, info):
filtered_row = []
label = []
value = []
try:
with open('/etc/info-exporter/request.json') as f:
filters = json.load(f)[filter]
if filter == "cable_info_filters" and 'NodeGuid' not in filters:
filters['NodeGuid'] = 'label'
if filter == "cable_info_filters" and 'Vendor' not in filters:
filters['Vendor'] = 'value'
if filter == "cable_info_filters" and 'PortNum' not in filters:
filters['PortNum'] = 'label'
if filter == "pm_info_filters" and 'NodeGUID' not in filters:
filters['NodeGUID'] = 'label'
if filter == "pm_info_filters" and 'PortNumber' not in filters:
filters['PortNumber'] = 'label'
if filter == "temp_sensing_filters" and 'NodeGUID' not in filters:
filters['NodeGUID'] = 'label'
if filter == "fan_info_filters" and 'NodeGUID' not in filters:
filters['NodeGUID'] = 'label'
if filter == "fan_info_filters" and 'FanIndex' not in filters:
filters['FanIndex'] = 'label'
if filter == "power_info_filter" and 'NodeGuid' not in filters:
filters['NodeGuid'] = 'label'
if filter == "power_info_filter" and 'PSUIndex' not in filters:
filters['PSUIndex'] = 'label'
if filter == "temperature_sensors_filter" and 'NodeGuid' not in filters:
filters['NodeGuid'] = 'label'
if filter == "temperature_sensors_filter" and 'SensorIndex' not in filters:
filters['SensorIndex'] = 'label'
reader = csv.DictReader(info, delimiter=',')
for row in reader:
filter_row = {}
for key, type in filters.items():
try:
filter_row[key.lower()] = row[key].lower()
if type == 'label':
if key not in label :
label.append(key)
elif type == 'value':
if key not in value:
value.append(key)
else :
logging.warning(f"The type '{type}' of '{key} 'is not recognise, setting as a value by default")
self.scrape_with_errors = True
value.append(key)
except KeyError :
logging.error(f'The key {key} was not recognise')
self.scrape_with_errors = True
continue
filter_row['nodename'] = ''
if self.node_name_map :
for data in self.datas:
if filter_row['nodeguid'] in data:
filter_row['nodename'] = data.split(" ")[1].rstrip("\n")
filtered_row.append(filter_row)
except Exception as e:
logging.error(f"Error while filtering the cable info: {e}")
self.scrape_with_errors = True
return filtered_row, value, label
def double_rm(self, myList):
result = []
marker = set()
for l in myList:
ll = l.lower()
if ll not in marker: # test presence
marker.add(ll)
result.append(l) # preserve order
return result
def join_csv(self, ldict1, ldict2):
for dic1 in ldict1:
for dic2 in ldict2:
if dic1['nodeguid'] == dic2['nodeguid'] and dic1['portnum'] == dic2['portnumber']:
dic1.update(dic2)
return(ldict1)
def get_csv_value(self):
self.cable_info_raw, self.pm_info_raw, self.temp_sensing_raw, self.link_info_raw, self.fan_info_raw, self.power_info_raw, self.temp_info_raw = self.csv_global_parser(self.csv_file_input)
self.cable_info_filtered, self.cable_info_values, self.cable_info_labels = self.data_filter("cable_info_filters", self.cable_info_raw)
self.temp_sensing_filtered, self.temp_sensing_values, self.temp_sensing_labels = self.data_filter("temp_sensing_filters", self.temp_sensing_raw)
self.pm_info_filtered, self.pm_info_values, self.pm_info_labels = self.data_filter("pm_info_filters", self.pm_info_raw)
self.fan_info_filtered, self.fan_info_values, self.fan_info_labels = self.data_filter("fan_info_filter", self.fan_info_raw)
self.power_info_filtered, self.power_info_values, self.power_info_labels = self.data_filter("power_info_filter", self.power_info_raw)
self.temp_info_filtered, self.temp_info_values, self.temp_info_labels = self.data_filter("temperature_sensors_filter", self.temp_info_raw)
if self.phy:
self.info_merged = self.join_csv(self.cable_info_filtered, self.pm_info_filtered)
else :
self.info_merged = self.pm_info_filtered
self.merged_info_labels = self.double_rm(self.cable_info_labels + self.pm_info_labels)
self.merged_info_values = self.double_rm(self.cable_info_values + self.pm_info_values)
if 'NodeGuid' not in self.merged_info_labels:
self.merged_info_labels.append('NodeGuid')
if 'PortNumber' not in self.merged_info_labels:
self.merged_info_labels.append('PortNumber')
if 'PortNum' in self.merged_info_labels:
self.merged_info_labels.remove('PortNum')
if 'NodeGUID' in self.merged_info_labels:
self.merged_info_labels.remove('NodeGUID')
self.temp_sensing_labels.append('NodeName')
self.fan_info_labels.append('NodeName')
self.power_info_labels.append('NodeName')
self.temp_info_labels.append('NodeName')
self.merged_info_labels.extend(['NodeName', 'RemoteGuid', 'RemoteName', 'RemotePort'])
def link_connexion(self, lguid, lport):
rguid = ''
rport = ''
rname = ''
for line in csv.DictReader(self.link_info_raw, delimiter=','):
if line['NodeGuid1'] == lguid and line['PortNum1'] == lport :
rguid = line['NodeGuid2']
rport = line['PortNum2']
if rguid == '':
for line in csv.DictReader(self.link_info_raw, delimiter=','):
if line['NodeGuid2'] == lguid and line['PortNum2'] == lport :
rguid = line['NodeGuid1']
rport = line['PortNum1']
if self.node_name_map :
for data in self.datas:
if rguid in data:
rname = data.split(" ")[1].rstrip("\n")
return rguid, rport, rname
def __init__(self, node_name_map, csv_file_input, can_reset_counter, phy, link_state, asic_temperature):
self.can_reset_counter = can_reset_counter
self.node_name_map = node_name_map
self.csv_file_input = csv_file_input
self.phy = phy
self.link_state = link_state
self.asic_temperature = asic_temperature
if self.node_name_map :
with open(self.node_name_map, 'r') as file:
self.datas = file.readlines()
self.get_csv_value()
self.scrape_with_errors = False
self.metrics = {}
self.asic_temp = {}
self.gauge = {}
if self.asic_temperature:
self.asic_temp['asic_temperature'] = {
'help': 'Asic current temperature'
}
if self.link_state:
self.link_info = {
'Link_State': {
'help': 'Link current state.',
}
}
self.link_speed = {
'Link_Speed' : {
'help': 'Link available Speed.'
}
}
self.link_width = {
'Link_Width' : {
'help': 'Link available Width.'
}
}
if self.phy:
self.fan_info = {
'fan_speed':{
'help':'Fan current speed'
}
}
self.power_info = {
'power_state': {
'help': 'Power current state.',
}
}
self.temp_info = {
'temp_info':{
'help':'Switch current temperatures'
}
}
self.transceiver_state = {
'transceiver_state':{
'help':'Transceiver current state (1 if all good, 0 otherwise)'
}
}
self.link_info_regex = r'^(?P<LGuid>0x\w+)\s+\"\s*(?P<LName>[\w\-_ ]+)\"\s+\d+\s+(?P<LPort>\d+)\[\s+\]\s+\=+\(\s+.+(?P<State>Active|Down)\/\s*(?P<P_State>\w+)(?:.+(?P<RGuid>0x\w+)\s+\d+\s+(?P<RPort>\d+)\[\s+\]\s*\"\s*(?P<RName>[\w\-_ ]+).*|(?:()().*))$'
self.link_speed_width_regex = r'(?P<LGuid>0x\w+)\s+\"\s*(?P<LName>[\w\-_ ]+)\"\s+\d+\s+(?P<LPort>\d+)\[\s*\] ==\(\s*(?P<Width>\d+).\s*(?P<Speed>\d+.\d*).*(?P<RGuid>0x\w+)\s+\d+\s+(?P<RPort>\d+)\[\s*\]\s*\"(?P<RName>[\w\-_ ]+)'
for value in self.merged_info_values :
self.gauge[f'{value}'] = {
'help': f'Device/Cable current {value}.'
}
self.counter_info = {
'LinkDownedCounterExt': {
'bits': 8,
},
'SymbolErrorCounterExt': {
'bits': 16,
},
'PortXmitConstraintErrorsExt': {
'bits': 16,
},
'PortSwLifetimeLimitDiscards': {
'bits': 16,
},
'PortXmitDiscardsExt': {
'bits': 16,
},
'PortSwHOQLifetimeLimitDiscards': {
'bits': 16,
},
'PortXmitWaitExt': {
'bits': 64,
},
'PortXmitDataExtended': {
'bits': 64,
},
'PortRcvDataExtended': {
'bits': 64,
},
'PortXmitPktsExtended': {
'bits': 64,
},
'PortRcvPktsExtended': {
'bits': 64,
},
'PortRcvErrorsExt': {
'bits': 16,
},
'PortUnicastXmitPkts': {
'bits': 64,
},
'PortUnicastRcvPkts': {
'bits': 64,
},
'PortMulticastXmitPkts': {
'bits': 64,
},
'PortMulticastRcvPkts': {
'bits': 64,
},
'PortBufferOverrunErrors': {
'bits': 16,
},
'PortLocalPhysicalErrors': {
'bits': 16,
},
'PortRcvRemotePhysicalErrorsExt': {
'bits': 16,
},
'LinkErrorRecoveryCounterExt': {
'bits': 8,
},
'LocalLinkIntegrityErrorsExt': {
'bits': 4,
},
'VL15DroppedExt': {
'bits': 16,
},
'PortNeighborMTUDiscards': {
'bits': 16,
},
'PortRcvConstraintErrorsExt': {
'bits': 16,
},
'ExcessiveBufferOverrunErrorsExt': {
'bits': 16,
}
}
def init_metrics(self):
for value in self.gauge:
self.metrics[value] = GaugeMetricFamily(
'infiniband_' + value.lower(),
self.gauge[value]['help'],
labels = self.merged_info_labels
)
if self.phy:
for value in self.power_info:
self.metrics[value] =GaugeMetricFamily(
'infiniband_' + value.lower(),
self.power_info[value]['help'],
labels = [
'NodeGuid',
'PSUIndex',
'NodeName'
]
)
for value in self.fan_info:
self.metrics[value] = GaugeMetricFamily(
'infiniband_' + value.lower(),
self.fan_info[value]['help'],
labels = [
'NodeGuid',
'FanIndex',
'NodeName'
]
)
for value in self.temp_info:
self.metrics[value] = GaugeMetricFamily(
'infiniband_' + value.lower(),
self.temp_info[value]['help'],
labels = self.temp_info_labels
)
for value in self.transceiver_state:
self.metrics[value] = GaugeMetricFamily(
'infiniband_' + value.lower(),
self.transceiver_state[value]['help'],
labels = self.merged_info_labels
)
if self.asic_temperature:
for value in self.asic_temp:
self.metrics[value] = GaugeMetricFamily(
'infiniband_' + value.lower(),
self.asic_temp[value]['help'],
labels = [
'NodeGuid',
'NodeName'
]
)
if self.link_state:
for link_name in self.link_info:
self.metrics[link_name] = GaugeMetricFamily(
'infiniband_' + link_name.lower(),
self.link_info[link_name]['help'],
labels=[
'NodeName',
'NodeGuid',
'PortNumber',
'state',
'physical_state',
'RemoteGuid',
'RemotePort',
'RemoteName'
]
)
for link_name in self.link_speed:
self.metrics[link_name] = GaugeMetricFamily(
'infiniband_' + link_name.lower(),
self.link_speed[link_name]['help'],
labels=[
'NodeName',
'NodeGuid',
'PortNumber',
'RemoteGuid',
'RemotePort',
'RemoteName'
]
)
for link_name in self.link_width:
self.metrics[link_name] = GaugeMetricFamily(
'infiniband_' + link_name.lower(),
self.link_width[link_name]['help'],
labels=[
'NodeName',
'NodeGuid',
'PortNumber',
'RemoteGuid',
'RemotePort',
'RemoteName'
]
)
def chunks(self, x, n):
for i in range(0, len(x), n):
yield x[i:i + n]
def reset_counter(self, guid, port, reason):
name = guid
if self.node_name_map :
for data in self.datas:
if guid in data:
name = data.split(" ")[1].rstrip("\n")
if self.can_reset_counter:
logging.info('Reseting counters on %s port %s, due to %s', # noqa: E501
name,
port,
reason)
process = subprocess.Popen(['perfquery', '-R', '-G', guid, port],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
for line in process.stdout:
logging.info(line)
return_code = process.wait()
if return_code != 0:
logging.error('perfquery ran doesn\'t return 0')
else:
logging.warning('Counters on %s port %s due to %s is overload', # noqa: E501
name,
port,
reason)
def parse_state(self, item):
try :
port = f"{int(item[2]):02}"
except ValueError:
logging.error(f"The port given is not an int : {item[2]}")
port = "Unknown"
try :
rport = f"{int(item[6]):02}"
except TypeError :
rport = "Unknown"
except ValueError:
logging.error(f"The port given is neither null or an int : {item[6]}")
rport = "Unknown"
for link in self.link_info:
label_values = [
item[1], #local_guid
item[0], #local_name
port, #local_port
item[3], #state
item[4], #physical_state
item[5] or "Unknown", #remote_gu id
rport, #remote_port
item[7] or "Unknown"] #remote_name
self.metrics[link].add_metric(label_values, 1 if (item[3] == 'Active' and item[4] == 'LinkUp') else 0)
def process_state(self, item):
if not isinstance(item, list):
raise RuntimeError('Wrong data type passed for item: {}'.format(type(item)))
if len(item) != 11:
raise ParsingError('Item data incomplete:\n{}'.format(item[0]))
self.parse_state(item)
def parse_speed_width(self, item):
try :
port = f"{int(item[2]):02}"
except ValueError:
logging.error(f"The port given is not an int : {item[2]}")
port = "Unknown"
try :
rport = f"{int(item[6]):02}"
except TypeError :
rport = "Unknown"
except ValueError:
logging.error(f"The port given is neither null or an int : {item[6]}")
rport = "Unknown"
for link in self.link_speed:
label_values = [
item[1], #local_name
item[0], #local_guid
port,
item[5], #remote_guid
rport, #remote_port
item[7]] #remote_name
self.metrics[link].add_metric(label_values, item[4])
for link in self.link_width:
label_values = [
item[1], #local_name
item[0], #local_guid
port,
item[5], #remote_guid
rport, #remote_port
item[7]] #remote_name
self.metrics[link].add_metric(label_values, item[3])
def process_speed_width(self, item):
if not isinstance(item, list):
raise RuntimeError('Wrong data type passed for item: {}'.format(type(item)))
if len(item) != 9:
raise ParsingError('Item data incomplete:\n{}'.format(item[0]))
self.parse_speed_width(item)
def data_link(self):
for cable_info in self.info_merged :
remoteguid, remoteport, remotename = self.link_connexion(cable_info['nodeguid'], cable_info['portnumber'])
cable_info['remoteport'] = f"{int(remoteport):02}"
cable_info['portnumber'] = f"{int(cable_info['portnumber']):02}"
cable_info['remoteguid'] = remoteguid
cable_info['remotename'] = remotename
self.label_values = []
self.value_values = 0
for label in self.merged_info_labels:
self.label_values.append(cable_info[label.lower()])
for value in self.gauge:
if value == "Vendor":
self.value_values = 0 if cable_info[value.lower()] == "" else 1
self.metrics["transceiver_state"].add_metric(self.label_values, self.value_values)
else :
try :
self.value_values = int(cable_info[value.lower()].rstrip('c'))
if value in self.counter_info:
if self.value_values >= 2 ** (self.counter_info[value]['bits']-1):
self.reset_counter(cable_info['nodeguid'], cable_info['portnumber'], value)
except ValueError:
logging.debug(f'The value {value} is not an int.')
self.metrics[value].add_metric(self.label_values, self.value_values)
def temp_link(self):
for temp_info in self.temp_sensing_filtered :
self.label_values = []
self.value_values = 0
for label in self.temp_sensing_labels:
self.label_values.append(temp_info[label.lower()])
for value in self.asic_temp:
label_values = self.label_values
try :
self.value_values = int(temp_info['currenttemperature'])
except ValueError:
logging.error(f'The value {value} is not an int.')
self.metrics[value].add_metric(label_values, self.value_values)
def fan_link(self):
for fan_info in self.fan_info_filtered :
fan_info['sensorindex'] = f"{int(fan_info['sensorindex']):02}"
self.label_values = []
self.value_values = 0
for label in self.fan_info_labels:
self.label_values.append(fan_info[label.lower()])
for value in self.fan_info:
label_values = self.label_values
try :
self.value_values = int(fan_info['fanspeed'])
except ValueError:
logging.error(f'The value {value} is not an int.')
self.metrics[value].add_metric(label_values, self.value_values)
def power_link(self):
for power_info in self.power_info_filtered :
power_info['psuindex'] = f"{int(power_info['psuindex']):02}"
self.label_values = []
self.value_values = 0
for label in self.power_info_labels:
self.label_values.append(power_info[label.lower()])
for value in self.power_info:
label_values = self.label_values
try :
self.value_values = 1 if power_info['dcstate'] == 'ok' else 0
except ValueError:
logging.error(f'The value {value} is not an int.')
self.metrics[value].add_metric(label_values, self.value_values)
def temp_sens_link(self):
for temp_info in self.temp_info_filtered :
self.label_values = []
self.value_values = 0
for label in self.temp_info_labels:
self.label_values.append(temp_info[label.lower()])
for value in self.temp_info_values:
label_values = self.label_values
try :
self.value_values = int(float(temp_info[value.lower()]))
except ValueError:
logging.info(f'The value {value} is not an int.')
self.metrics['temp_info'].add_metric(label_values, self.value_values)
def collect(self):
logging.debug('Start of collection cycle')
self.scrape_with_errors = False
scrape_duration = GaugeMetricFamily(
'infiniband_scrape_duration_seconds',
'Number of seconds taken to collect and parse the stats.')
scrape_start = time.time()
scrape_ok = GaugeMetricFamily(
'infiniband_scrape_ok',
'Indicates with a 1 if the scrape was successful and complete, '
'otherwise 0 on any non critical errors detected '
'e.g. ignored lines from ibqueryerrors STDERR or parsing errors.')
self.get_csv_value()
self.init_metrics()
self.data_link()
if self.asic_temperature:
self.temp_link()
if self.phy:
self.fan_link()
self.power_link()
self.temp_sens_link()
for value in self.gauge:
if value == "Vendor":
yield self.metrics["transceiver_state"]
else :
yield self.metrics[value]
if self.asic_temperature:
for value in self.asic_temp:
yield self.metrics[value]
if self.phy:
for value in self.fan_info:
yield self.metrics[value]
for value in self.power_info:
yield self.metrics[value]
for value in self.temp_info:
yield self.metrics[value]
if self.link_state:
iblinkinfo_stdout = ""
iblinkinfo_args = [
'iblinkinfo',
'--verbose',
'--line']
if self.node_name_map:
iblinkinfo_args.append('--node-name-map')
iblinkinfo_args.append(self.node_name_map)
process = subprocess.Popen(iblinkinfo_args,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
process_stdout, process_stderr = process.communicate()
iblinkinfo_stdout = process_stdout.decode("utf-8")
if process_stderr:
iblinkinfo_stderr = process_stderr.decode("utf-8")
logging.warning("STDERR output retrieved from iblinkinfo:\n%s",
iblinkinfo_stderr)
self.scrape_with_errors = True
content = re.split(self.link_info_regex,
iblinkinfo_stdout,
flags=re.MULTILINE)
try:
if not content:
raise ParsingError('Input content is empty.')
if not isinstance(content, list):
raise RuntimeError('Input content should be a list.')
# Drop first line that is empty on successful regex split():
if content[0] == '':
del content[0]
else:
raise ParsingError('Inconsistent input content detected:\n{}'.format(content[0]))
input_data_chunks = self.chunks(content, 11)
for data_chunk in input_data_chunks:
self.process_state(data_chunk)
for link_name in self.link_info:
yield self.metrics[link_name]
except ParsingError as e:
logging.error(e)
self.scrape_with_errors = True
contentv2 = re.split(self.link_speed_width_regex,
iblinkinfo_stdout,
flags=re.MULTILINE)
try:
if not contentv2:
raise ParsingError('Input content is empty.')
if not isinstance(contentv2, list):
raise RuntimeError('Input content should be a list.')
# Drop first line that is empty on successful regex split():
if contentv2[0] == '':
del contentv2[0]
else:
raise ParsingError('Inconsistent input content detected:\n{}'.format(content[0]))
input_data_chunks = self.chunks(contentv2, 9)
for data_chunk in input_data_chunks:
self.process_speed_width(data_chunk)
for link_name in self.link_speed:
yield self.metrics[link_name]
for link_name in self.link_width:
yield self.metrics[link_name]
except ParsingError as e:
logging.error(e)
self.scrape_with_errors = True
scrape_duration.add_metric([], time.time() - scrape_start)
yield scrape_duration
if self.scrape_with_errors:
scrape_ok.add_metric([], 0)
else:
scrape_ok.add_metric([], 1)
yield scrape_ok
logging.debug('End of collection cycle')
class NoLoggingWSGIRequestHandler(WSGIRequestHandler):
def log_message(self, format, *args):
pass
def main():
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description='Prometheus collector for a infiniband fabric')
parser.add_argument(
"--verbose",
"-v",
required=False,
help="increase output verbosity",
action="store_true")
parser.add_argument(
'--csv-file-input',
'-i',
required=False,
action='store',
dest='csv_file_input',
default='/var/tmp/ibdiagnet2/ibdiagnet2.db_csv',
help='csv file used to gather data of different cable.')
parser.add_argument(
'--port',
'-p',
required=False,
type=int,
default=9685,
help='Collector http port, default is 9685')
parser.add_argument(
'--node-name-map',
'-m',
required=False,
action='store',
dest='node_name_map',
help='Node name map used to link the guid to the local name.')
parser.add_argument(
'--can-reset-counter',
'-r',
required=False,
dest='can_reset_counter',
help='Will reset counter as required when maxed out.',
action='store_true')
parser.add_argument(
'--phy',
required=False,
dest='phy',
help='Will provided physic info such as fan, temperature sensors and power info.',
action='store_true')
parser.add_argument(
'--link_state',
'-l',
required=False,
dest='link_state',
help='Will provided link state info.',
action='store_true')
parser.add_argument(
'--asic_temp',
'-a',
required=False,
dest='asic_temp',
help='Will provided asic temperature info.',
action='store_true')
parser.add_argument(
'--config',
'-c',
required=False,
type=str,
help='Path to the config file.')
args = parser.parse_args()
if args.config:
with open(args.config) as config_file:
config_data = json.load(config_file)
if "verbose" in config_data and not args.verbose:
args.verbose = config_data["verbose"]
if "port" in config_data and args.port == 9685:
args.port = config_data["port"]
if "node_name_map" in config_data and not args.node_name_map:
args.node_name_map = config_data["node_name_map"]
if "can_reset_counter" in config_data and not args.can_reset_counter:
args.can_reset_counter = config_data["can_reset_counter"]
if "phy" in config_data and not args.phy:
args.phy = config_data["phy"]
if "link_state" in config_data and not args.link_state:
args.link_state = config_data["link_state"]
if "asic_temp" in config_data and not args.asic_temp:
args.asic_temp = config_data["asic_temp"]
if args.verbose:
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s')
else:
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')
if args.csv_file_input:
logging.debug(f'Using csv file input provided in args : {args.csv_file_input}')
csv_file_input = args.csv_file_input
else:
logging.debug(f'Using default csv file input')
csv_file_input = args.csv_file_input
if args.node_name_map:
logging.debug('Using node-name-map provided in args: %s', args.node_name_map)
node_name_map = args.node_name_map
else:
logging.debug('No node-name-map was provided')
node_name_map = None
if args.can_reset_counter:
logging.debug('can_reset_counter provided in args')
can_reset_counter = True
else:
logging.debug('Counters will not reset automatically')
can_reset_counter = False
if args.phy:
logging.debug('phy provided in args')
phy = True
else:
logging.debug('Phy info will not be provided')
phy = False
if args.link_state:
logging.debug('Link State provided in args')
link_state = True