Skip to content

Commit a112746

Browse files
committed
Fix pep8 warnings; add requirements; add Grafana dashboard; add run tests
1 parent 60db3dd commit a112746

File tree

11 files changed

+1406
-0
lines changed

11 files changed

+1406
-0
lines changed

.travis.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
branches:
3+
only:
4+
- master
5+
language: python
6+
python:
7+
- "2.7"
8+
- "3.6"
9+
install:
10+
- pip install -r requirements.txt
11+
script:
12+
- pytest --cov=.
13+
after_success:
14+
- coveralls

__init__.py

Whitespace-only changes.

iib_metrics_client.py

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

0 commit comments

Comments
 (0)