|
1 | 1 | # -*- coding: utf-8 -*- |
2 | | -import requests |
3 | | -from log.logger_client import set_logger |
4 | | -from requests import ConnectionError |
5 | | -from urllib3.exceptions import ResponseError |
| 2 | +"""Python client for collecting IBM Integration Bus metrics and exporting to Prometheus pushgateway.""" |
6 | 3 | import sys |
| 4 | +import time |
7 | 5 | import traceback |
8 | 6 | import platform |
9 | | -import time |
| 7 | +import requests |
| 8 | +from requests import ConnectionError |
| 9 | +from urllib3.exceptions import ResponseError |
| 10 | +from modules.iib_brokers import ( |
| 11 | + get_brokers_status, |
| 12 | + format_broker, |
| 13 | + get_broker_items) |
| 14 | +from modules.iib_exec_groups import format_exec_groups |
| 15 | +from modules.iib_applications import format_applications |
| 16 | +from modules.iib_message_flows import format_message_flows |
| 17 | +from log.logger_client import set_logger |
10 | 18 | from modules.iib_api import run_iib_command |
11 | 19 |
|
12 | 20 |
|
13 | 21 | logger = set_logger() |
14 | 22 |
|
15 | | -STATUS_MAP = { |
16 | | - 'running.': 1, |
17 | | - 'stopped.': 0 |
18 | | - } |
19 | | - |
20 | 23 |
|
21 | 24 | class PrometheusBadResponse(Exception): |
22 | 25 | pass |
23 | 26 |
|
24 | 27 |
|
| 28 | +def static_content(): |
| 29 | + """Client name and version.""" |
| 30 | + name = "ib-metrics-pyclient" |
| 31 | + version = "0.2" |
| 32 | + return '{0} v.{1}'.format(name, version) |
| 33 | + |
| 34 | + |
25 | 35 | def put_metric_to_gateway(metric_data, job): |
| 36 | + """Sends data to Prometheus pushgateway.""" |
26 | 37 | hostname = platform.node() |
27 | 38 | port = 9091 |
28 | 39 | src_url = "http://{0}:{1}".format(hostname, port) |
29 | 40 | headers = {"Content-Type": "text/plain; version=0.0.4"} |
30 | 41 | dest_url = "{0}/metrics/job/{1}".format(src_url, job) |
31 | 42 | logger.info("Destination url: {0}".format(dest_url)) |
| 43 | + # Debug info |
32 | 44 | # logger.info("Metric data to push: {0}".format(metric_data)) |
33 | 45 | try: |
34 | 46 | response = requests.put(dest_url, data=metric_data, headers=headers) |
35 | 47 | if not response.ok: |
36 | | - raise PrometheusBadResponse("Bad response - {0} \ |
37 | | - from {1}\nResponseText: {2}".format( |
38 | | - response, |
39 | | - dest_url, |
40 | | - response.text |
41 | | - )) |
| 48 | + raise PrometheusBadResponse("Bad response - {0} from {1}\nResponseText: {2}".format(response, dest_url, response.text)) |
42 | 49 | logger.info("Success! Server response: {0}".format(response)) |
43 | 50 | except (ConnectionError, ResponseError): |
44 | 51 | raise PrometheusBadResponse("{0} is not available!".format(dest_url)) |
45 | 52 |
|
46 | 53 |
|
47 | | -def get_brokers_status(brokers_data): |
48 | | - output_list = brokers_data.split('\n') |
49 | | - brokers = [] |
50 | | - for record in filter(None, output_list): |
51 | | - record_list = record.split() |
52 | | - broker_name = record_list[2].replace("'", "") |
53 | | - qm_name = record_list[6].replace("'", "") |
54 | | - status = record_list[8].replace("'", "") |
55 | | - brokers.append([broker_name, status, qm_name]) |
56 | | - return brokers |
57 | | - |
58 | | - |
59 | | -def get_broker_items(broker_row_data): |
60 | | - output_list = broker_row_data.split('\n') |
61 | | - exec_groups = [] |
62 | | - applications = [] |
63 | | - message_flows = [] |
64 | | - # See IBM diagnostic messages: |
65 | | - # https://www.ibm.com/support/knowledgecenter/en/SSMKHH_9.0.0/com.ibm.etools.mft.bipmsgs.doc/ay_bip1.htm |
66 | | - # Also you can use command: mqsiexplain <bip_code> |
67 | | - bip_codes = { |
68 | | - 'BIP1286I': exec_groups, |
69 | | - 'BIP1287I': exec_groups, |
70 | | - 'BIP1275I': applications, |
71 | | - 'BIP1276I': applications, |
72 | | - 'BIP1277I': message_flows, |
73 | | - 'BIP1278I': message_flows |
74 | | - } |
75 | | - for record in output_list: |
76 | | - if record: |
77 | | - bip_code = record.split()[0].replace(':', '') |
78 | | - if bip_code in bip_codes.keys(): |
79 | | - bip_codes[bip_code].append(record) |
80 | | - return exec_groups, applications, message_flows |
81 | | - |
82 | | - |
83 | | -def format_broker(broker_name, status, qm_name): |
84 | | - broker_metric = 'ib_broker_status{brokername="%s", qmname="%s"} %d\n' % ( |
85 | | - broker_name, |
86 | | - qm_name, |
87 | | - STATUS_MAP[status] |
88 | | - ) |
89 | | - return broker_metric |
90 | | - |
91 | | - |
92 | | -def format_applications(applications, broker_name): |
93 | | - app_metric_data = '' |
94 | | - for app in applications: |
95 | | - app_list = app.split() |
96 | | - egname, app_name, status = app_list[6], app_list[2], app_list[8] |
97 | | - app_metric = 'ib_application_status{egname="%s", brokername="%s", appname="%s"} %d\n' % ( |
98 | | - egname.replace("'", ""), |
99 | | - broker_name, |
100 | | - app_name.replace("'", ""), |
101 | | - STATUS_MAP[status] |
102 | | - ) |
103 | | - app_metric_data += app_metric |
104 | | - return app_metric_data |
105 | | - |
106 | | - |
107 | | -def format_exec_groups(exec_groups): |
108 | | - eg_metric_data = '' |
109 | | - for eg in exec_groups: |
110 | | - eg_list = eg.split() |
111 | | - broker_name, egname, status = eg_list[6], eg_list[3], eg_list[8] |
112 | | - eg_metric = 'ib_exec_group_status{brokername="%s", egname="%s"} %d\n' % ( |
113 | | - broker_name.replace("'", ""), |
114 | | - egname.replace("'", ""), |
115 | | - STATUS_MAP[status] |
116 | | - ) |
117 | | - eg_metric_data += eg_metric |
118 | | - return eg_metric_data |
119 | | - |
120 | | - |
121 | | -def format_message_flows(message_flows, broker_name): |
122 | | - msg_flow_metric_data = '' |
123 | | - for msg_flow in message_flows: |
124 | | - msg_flow_list = msg_flow.split() |
125 | | - egname, app_name, message_flow_name, status = msg_flow_list[7], msg_flow_list[11], msg_flow_list[3], msg_flow_list[9] |
126 | | - msg_flow_metric = 'ib_message_flow_status{egname="%s", brokername="%s", appname="%s", messageflowname="%s"} %d\n' % ( |
127 | | - egname.replace("'", ""), |
128 | | - broker_name, |
129 | | - app_name.replace("'", "").replace(",", ""), |
130 | | - message_flow_name.replace("'", ""), |
131 | | - STATUS_MAP[status] |
132 | | - ) |
133 | | - msg_flow_metric_data += msg_flow_metric |
134 | | - return msg_flow_metric_data |
135 | | - |
136 | | - |
137 | 54 | def main(): |
138 | 55 | start_time = time.time() |
139 | 56 | logger.info("Starting metrics collecting for Integration Bus!") |
140 | 57 | try: |
141 | 58 | brokers_data = run_iib_command(task='get_brokers_status') |
142 | | - brokers = get_brokers_status(brokers_data) |
| 59 | + brokers = get_brokers_status(brokers_data=brokers_data) |
143 | 60 | for broker in brokers: |
144 | 61 | broker_name, status, qm_name = broker |
145 | | - broker_data = format_broker(broker_name, status, qm_name) |
| 62 | + broker_data = format_broker( |
| 63 | + broker_name=broker_name, |
| 64 | + status=status, |
| 65 | + qm_name=qm_name) |
146 | 66 | if status == 'running.': |
147 | 67 | broker_row_data = run_iib_command( |
148 | 68 | task='get_broker_objects', |
149 | | - broker_name=broker_name |
150 | | - ) |
151 | | - exec_groups, applications, message_flows = get_broker_items(broker_row_data) |
152 | | - exec_groups_data = format_exec_groups(exec_groups) |
153 | | - applications_data = format_applications(applications, broker_name) |
154 | | - message_flows_data = format_message_flows(message_flows, broker_name) |
| 69 | + broker_name=broker_name) |
| 70 | + exec_groups, applications, message_flows = get_broker_items(broker_row_data=broker_row_data) |
| 71 | + exec_groups_data = format_exec_groups(exec_groups=exec_groups) |
| 72 | + applications_data = format_applications(applications=applications, broker_name=broker_name) |
| 73 | + message_flows_data = format_message_flows(message_flows=message_flows, broker_name=broker_name) |
155 | 74 | metric_data = "{0}{1}{2}{3}".format( |
156 | 75 | broker_data, |
157 | 76 | exec_groups_data, |
158 | 77 | applications_data, |
159 | | - message_flows_data |
160 | | - ) |
161 | | - put_metric_to_gateway(metric_data, broker_name) |
| 78 | + message_flows_data) |
| 79 | + put_metric_to_gateway(metric_data=metric_data, job=broker_name) |
162 | 80 | logger.info("All metrics pushed successfully!") |
163 | 81 | else: |
| 82 | + put_metric_to_gateway(metric_data=broker_data, job=broker_name) |
164 | 83 | logger.warning("The status of broker is {0}\nOther metrics will not be collected!".format(status)) |
165 | | - put_metric_to_gateway(broker_data, broker_name) |
| 84 | + logger.info("Script finished in - {0} seconds -".format(time.time() - start_time)) |
166 | 85 | except PrometheusBadResponse as error: |
167 | 86 | logger.error(error) |
168 | 87 | except Exception as e: |
169 | 88 | tb = sys.exc_info()[-1] |
170 | 89 | stk = traceback.extract_tb(tb, 1)[0] |
171 | 90 | logger.error("Function: {0}\n{1}".format(stk, e)) |
172 | | - logger.info("Script finished in - %s seconds -" % (time.time() - start_time)) |
173 | 91 |
|
174 | 92 |
|
175 | 93 | if __name__ == "__main__": |
| 94 | + logger.info("Run {0}".format(static_content())) |
176 | 95 | while True: |
177 | 96 | main() |
178 | 97 | time.sleep(60) |
0 commit comments