def channels_status(mqm): """Returns dictionary with channels data.""" channels = run_mq_command(task='get_channels', mqm=mqm) channels_list = get_channels(channels_data=channels) mq_channels_status = {} for channel in channels_list: channel_name = extract_channel_name(channel=channel) if channel_name: channel_data = run_mq_command(task='get_chstatus', mqm=mqm, channel=channel_name) labels_data = [] stop_flag = "not found" if stop_flag in channel_data: channel_labels = run_mq_command(task='get_channel', mqm=mqm, channel=channel_name) labels_data = format_channel_output( data_to_format=channel_labels) else: labels_data = format_channel_output( data_to_format=channel_data) channel_status = get_channel_status(channel_data=channel_data, labels_data=labels_data) mq_channels_status[channel_name] = channel_status return mq_channels_status
def test_run_mq_command(self, execute_command): """Tests for `run_mq_command` function.""" self.assertEqual(run_mq_command(task='get_mq_managers'), 'dspmq') self.assertEqual( run_mq_command(task='get_mq_manager_status', mqm='TEST'), 'dspmq -m TEST -o all') self.assertEqual( run_mq_command(task='get_listener', mqm='TEST', listener='LISTENER'), 'echo "display listener(LISTENER)"| runmqsc TEST')
def get_queues_metrics(mq_manager): """Returns string with all main queues metrics which ready to push to pushgateway.""" queue_labels_data = run_mq_command(task='get_queues', mqm=mq_manager) queues_labels = get_queues_labels(queue_labels_data=queue_labels_data) queues_metrics = make_metrics_data_for_queues(queues_labels=queues_labels, mq_manager=mq_manager) return queues_metrics
def get_queues_metrics_monitor(mq_manager): """Returns string with all real-time monitoring metrics which ready to push to pushgateway.""" queue_labels_data_monitor = run_mq_command(task='get_queues_monitor', mqm=mq_manager) queues_labels_monitor = get_queues_labels_monitor( queue_labels_data=queue_labels_data_monitor) queues_metrics_monitor = make_metrics_data_for_queues_monitor( queues_labels=queues_labels_monitor, mq_manager=mq_manager) return queues_metrics_monitor
def get_mq_metrics(pushgateway_host, pushgateway_port): start_time = time.time() logger.info("Starting metrics collecting for IBM MQ!") try: mq_managers_data = run_mq_command(task='get_mq_managers') mq_managers = get_mq_managers(mq_managers_data=mq_managers_data) for mq_manager in mq_managers: mq_manager_metrics, status = get_mq_manager_metrics( mq_manager=mq_manager) if status == 1: listeners_data = run_mq_command(task='get_listeners', mqm=mq_manager) listeners = get_listeners(listeners_data=listeners_data) mq_listeners_metrics = get_mq_listeners_metrics( listeners=listeners, mq_manager=mq_manager) mq_channels = channels_status(mqm=mq_manager) mq_channels_metrics = get_mq_channels_metrics( mq_channels=mq_channels, mq_manager=mq_manager) mq_queues_metrics = get_queues_metrics(mq_manager=mq_manager) mq_queues_metrics_monitor = get_queues_metrics_monitor( mq_manager=mq_manager) metric_data = '{0}{1}{2}{3}{4}'.format( mq_manager_metrics, mq_listeners_metrics, mq_channels_metrics, mq_queues_metrics, mq_queues_metrics_monitor) put_metric_to_gateway(metric_data=metric_data, job=mq_manager, pushgateway_host=pushgateway_host, pushgateway_port=pushgateway_port) logger.info("All metrics pushed successfully!") else: put_metric_to_gateway(metric_data=mq_manager_metrics, job=mq_manager, pushgateway_host=pushgateway_host, pushgateway_port=pushgateway_port) logger.warning("Pushed only metric for mq_manager!") logger.info("Script finished in - {0} seconds -".format(time.time() - start_time)) except PrometheusBadResponse as error: logger.error(error) except Exception as err: tb = sys.exc_info()[-1] stk = traceback.extract_tb(tb, 1)[0] logger.error("Function: {0}\n{1}".format(stk, err))
def get_mq_listeners_metrics(listeners, mq_manager): """Returns string with status listeners metrics which ready to push to pushgateway.""" metrics_annotation = get_metric_annotation() prometheus_data_list = list() for listener in listeners: listener_data = run_mq_command(task='get_lsstatus', mqm=mq_manager, listener=listener) listener_labels = run_mq_command(task='get_listener', mqm=mq_manager, listener=listener) listener_status = get_listener_status(listener_data=listener_data, listener_labels=listener_labels) metric_data = make_metric_for_mq_listener_status( mq_listener_status_data=listener_status, mqm=mq_manager) prometheus_data_list.append('{0}'.format(metric_data)) add_annotation(prometheus_data_list, metrics_annotation['status']) prometheus_data_str = ''.join(prometheus_data_list) return prometheus_data_str
def get_mq_manager_metrics(mq_manager): """Returns string with status manager metric which ready to push to pushgateway and numeric status value.""" metrics_annotation = get_metric_annotation() mq_manager_data = run_mq_command(task='get_mq_manager_status', mqm=mq_manager) mqm_status_data = get_mq_manager_status(mq_manager_data=mq_manager_data) metric_data, status = make_metric_for_mq_manager_status(mq_manager_status_data=mqm_status_data) metric_data = '{0}{1}'.format( metrics_annotation['status'], metric_data) if status != 1: logger.warning("The status of MQ Manager - {0} is {1} !\n \ Other metrics will not be collected!".format(mq_manager, status)) return metric_data, status