@@ -279,7 +279,7 @@ def collect(self):
279
279
try :
280
280
status , entity_data = self .collect_data (entity )
281
281
except Exception as e :
282
- logger .error ('Could not collect metric :{}' .format (entity ))
282
+ logger .error ('Could not collect metric :{}' .format (e ))
283
283
284
284
if status == self .FAILURE :
285
285
self .ns_session_clear ()
@@ -288,6 +288,18 @@ def collect(self):
288
288
if entity_data :
289
289
data [entity ] = entity_data
290
290
291
+ if 'k8s_ingress_lbvs' in self .metrics and \
292
+ os .environ .get ('KUBERNETES_SERVICE_HOST' ) is not None :
293
+ lbvs_dict = None
294
+ try :
295
+ status , lbvs_dict = self .collect_lbvs_config ()
296
+ except Exception as e :
297
+ logger .error ('Could not collect config entries for lbvs: {}' .format (e ))
298
+
299
+ if status == self .FAILURE :
300
+ self .ns_session_clear ()
301
+ return
302
+
291
303
# Add labels to metrics and provide to Prometheus
292
304
log_prefix_match = True
293
305
for entity_name , entity in self .metrics .items ():
@@ -322,7 +334,7 @@ def collect(self):
322
334
if entity_name == "k8s_ingress_lbvs" :
323
335
if os .environ .get ('KUBERNETES_SERVICE_HOST' ) is not None :
324
336
prefix_match = self .update_lbvs_label (
325
- label_values , ns_metric_name , log_prefix_match )
337
+ label_values , lbvs_dict , log_prefix_match )
326
338
if not prefix_match :
327
339
log_prefix_match = False
328
340
continue
@@ -335,7 +347,7 @@ def collect(self):
335
347
c .add_metric (label_values , float (
336
348
data_item [ns_metric_name ]))
337
349
except Exception as e :
338
- logger .error ('Caught exception while adding counter %s to %s: %s' % (ns_metric_name , entity_name , str (e )))
350
+ logger .error ('Caught exception while adding counter {} to {}: {}' . format (ns_metric_name , entity_name , str (e )))
339
351
340
352
yield c
341
353
@@ -360,7 +372,7 @@ def collect(self):
360
372
if entity_name == "k8s_ingress_lbvs" :
361
373
if os .environ .get ('KUBERNETES_SERVICE_HOST' ) is not None :
362
374
prefix_match = self .update_lbvs_label (
363
- label_values , ns_metric_name , log_prefix_match )
375
+ label_values , lbvs_dict , log_prefix_match )
364
376
if not prefix_match :
365
377
log_prefix_match = False
366
378
continue
@@ -388,8 +400,11 @@ def collect_data(self, entity):
388
400
return self .get_lbvs_bindings_status ()
389
401
390
402
# this is to fetch lb status for ingress/services in k8s enviroment
391
- if (entity == 'k8s_ingress_lbvs' ):
392
- entity = 'lbvserver'
403
+ if (entity == 'k8s_ingress_lbvs' ):
404
+ if os .environ .get ('KUBERNETES_SERVICE_HOST' ) is not None :
405
+ entity = 'lbvserver'
406
+ else :
407
+ return self .SUCCESS , None
393
408
394
409
# nitro call for all entities except 'services' (ie. servicegroups)
395
410
if (entity == 'services' ):
@@ -429,6 +444,9 @@ def get_svc_grp_services_stats(self):
429
444
if servicegroup_list_ds :
430
445
if 'servicegroup' not in servicegroup_list_ds :
431
446
logger .info ('No metric data available for servicegroup' )
447
+ if status == self .INVALID :
448
+ logger .debug ('Invalid metric fetch for servicegroup' \
449
+ 'with errorcode:{} ' .format (servicegroup_list_ds ['errorcode' ]))
432
450
return status , None
433
451
else :
434
452
logger .warning ('Unable to fetch data for servicegroup' )
@@ -524,7 +542,7 @@ def get_entity_stat(self, url):
524
542
logger .error ('Stat Access Failed {}' .format (e ))
525
543
return self .FAILURE , None
526
544
527
- def update_lbvs_label (self , label_values , ns_metric_name , log_prefix_match ):
545
+ def update_lbvs_label (self , label_values , lbvs_dict , log_prefix_match ):
528
546
'''Updates lbvserver lables for ingress and services for k8s_cic_ingress_service_stat dashboard.'''
529
547
try :
530
548
# If lbvs name ends with expected _svc, then label values are updated with ingress/service info.
@@ -536,8 +554,8 @@ def update_lbvs_label(self, label_values, ns_metric_name, log_prefix_match):
536
554
# return if ingress name as a service
537
555
if label_values [0 ].split ("_" )[3 ] == 'svc' :
538
556
if log_prefix_match :
539
- logger .debug (
540
- 'k8s_ingress_service_stat Ingress dashboard cannot be used without ingress with CIC' )
557
+ logger .debug ('k8s_ingress_service_stat dashboard' \
558
+ ' cannot be used without ingress with CIC' )
541
559
return False
542
560
# update label "citrixadc_k8s_ing_lb_ingress_name" with ingress name
543
561
label_values [0 ] = label_values [0 ].split (
@@ -552,7 +570,32 @@ def update_lbvs_label(self, label_values, ns_metric_name, log_prefix_match):
552
570
return True
553
571
else :
554
572
if log_prefix_match :
555
- logger .debug ('k8s_cic_ingress_service_stat Ingress dashboard cannot be used for CIC prefix {}' .format (cur_prefix ))
573
+ logger .debug ('k8s_cic_ingress_service_stat dashboard' \
574
+ ' cannot be used for CIC prefix {}' .format (cur_prefix ))
575
+ return False
576
+ elif lbvs_dict :
577
+ cur_prefix = str (label_values [0 ].split ("_" )[
578
+ 0 ].split ("-" , 1 )[0 ])
579
+ # update lables only if prefix provided is same as CIC prefix used
580
+ if cur_prefix == self .k8s_cic_prefix :
581
+ comments = lbvs_dict [label_values [0 ]]
582
+ comments = comments .split (',' )
583
+ if comments [0 ].split (':' )[0 ] == 'lbsvc' :
584
+ if log_prefix_match :
585
+ logger .debug ('k8s_ingress_service_stat dashboard' \
586
+ ' cannot be used without ingress with CIC' )
587
+ return False
588
+
589
+ if comments [0 ].split (':' )[0 ] == 'ing' :
590
+ label_values [0 ] = comments [0 ].split (':' )[1 ]
591
+ label_values [1 ] = comments [1 ].split (':' )[1 ]
592
+ label_values [2 ] = comments [3 ].split (':' )[1 ]
593
+ label_values [3 ] = comments [4 ].split (':' )[1 ]
594
+ return True
595
+ else :
596
+ if log_prefix_match :
597
+ logger .debug ('k8s_cic_ingress_service_stat dashboard' \
598
+ ' cannot be used for CIC prefix {}' .format (cur_prefix ))
556
599
return False
557
600
else :
558
601
return False
@@ -608,6 +651,32 @@ def ns_session_login(self):
608
651
logger .error ('Login Session Failed : {}' .format (e ))
609
652
return self .FAILURE
610
653
654
+ def collect_lbvs_config (self ):
655
+ ''' This method get lbvs config entries for k8s prefix'''
656
+ url = '%s://%s/nitro/v1/config/lbvserver?filter=name:%%2f^%s%%2f&attrs=name,comment' % (self .protocol , self .nsip , self .k8s_cic_prefix )
657
+ try :
658
+ status , data = self .get_entity_stat (url )
659
+ if data :
660
+ if 'lbvserver' in data :
661
+ lbvs_dict = {}
662
+ lbvs_list = list (data ['lbvserver' ])
663
+ for item in lbvs_list :
664
+ if 'comment' in item :
665
+ lbvs_dict .update ({item ['name' ]:item ['comment' ]})
666
+ return status , lbvs_dict
667
+ else :
668
+ logger .debug ('No lbvs config for ingress dashboard with k8s prefix "{}"' .format (self .k8s_cic_prefix ))
669
+ if status == self .INVALID :
670
+ logger .debug ('Invalid metric fetch for lbvs' \
671
+ 'with errorcode:{} ' .format (data ['errorcode' ]))
672
+ return status , None
673
+ else :
674
+ logger .warning ('Unable to fetch data for entity lbvserver' )
675
+ return status , None
676
+ except Exception as e :
677
+ logger .error ('Error in fetching lbvs config entries {}' .format (e ))
678
+ return self .FAILURE , None
679
+
611
680
612
681
def main ():
613
682
parser = argparse .ArgumentParser ()
0 commit comments