Skip to content

Commit 2f3bae7

Browse files
authored
Merge pull request #9 from AATools/custom_collect_interval
Сustom time interval between collecting metrics
2 parents af594da + 543af5d commit 2f3bae7

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

.coveragerc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[report]
2+
exclude_lines =
3+
if __name__ == .__main__.:

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
This is python client for collecting IBM Integration Bus metrics and exporting to [Prometheus pushgateway](https://github.com/prometheus/pushgateway).
66
The collected metrics can be explored in Prometheus or Grafana.
77

8-
The metrics are collected using [mqsilist](https://www.ibm.com/support/knowledgecenter/en/SSMKHH_9.0.0/com.ibm.etools.mft.doc/an07250_.htm) command. So, you need to install `IBM Integration Bus`.
8+
The metrics are collected by using [mqsilist](https://www.ibm.com/support/knowledgecenter/en/SSMKHH_9.0.0/com.ibm.etools.mft.doc/an07250_.htm) command. The metrics are collected for **all local** Brokers. You need to run `IB metrics pyclient` in the same host where `IBM Integration Bus` was installed.
99

1010
Tested for IBM IB v9 and v10 and Python 3.6, 3.7 on Linux.
1111

@@ -20,7 +20,6 @@ The metrics provided by the client:
2020
* `ib_application_status...` - current status of IB application;
2121
* `ib_message_flow_status...` - current status of IB message flow.
2222

23-
2423
See [detailed description of the metrics](#metrics-detailed-description) for an in-depth understanding.
2524

2625
You can run `IB metrics pyclient` and [MQ metrics pyclient](https://github.com/AATools/mq-metrics-pyclient) together. Metrics from both clients will be sent to the same pushgateway. Conflicts will not arise.
@@ -48,12 +47,12 @@ nohup python3 iib_metrics_client.py &
4847

4948
After that, you should set up your Prometheus server to collect metrics from Pushgateway (`http://<hostname>:9091/metrics`).
5049

51-
You can specify `host` and `port` for pushgateway and Integration Bus version via command-line arguments.
50+
You can specify `host` and `port` for pushgateway, Integration Bus version and time interval in seconds between collecting metrics via command-line arguments.
5251

5352
```bash
5453
python3 iib_metrics_client.py -h
5554

56-
usage: iib_metrics_client.py [-h] [--pghost [pushgatewayHost]] [--pgport [pushgatewayPort]] [--iibver [iibVersion]]
55+
usage: iib_metrics_client.py [-h] [--pghost [pushgatewayHost]] [--pgport [pushgatewayPort]] [--iibver [iibVersion]] [--collectint [collectInterval]]
5756

5857
optional arguments:
5958
-h, --help show this help message and exit
@@ -63,6 +62,8 @@ optional arguments:
6362
pushgateway port
6463
--iibver [iibVersion]
6564
IIB version: 9 or 10
65+
--collectint [collectInterval]
66+
time interval between collecting metrics
6667
```
6768
6869
If argument is not set the default value is used.
@@ -72,6 +73,7 @@ If argument is not set the default value is used.
7273
| `pghost` | Pushgateway host | Hostname on which client is started.<br> Value define via `platform.node()`. |
7374
| `pgport` | Pushgateway port | `9091` |
7475
| `iibver` | IIB version | `9`<br> Valid value: **9** or **10**.<br> If argument is omitted or invalid value is passed, the client will try to determine version via environment variable `MQSI_VERSION_V`. If it can't determine the version using the environment variable, the default value will be used. |
76+
| `collectint` | Time interval between collecting metrics | `60` <br> Time in seconds. |
7577
7678
## Grafana dashboard
7779

iib_metrics_client.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class PrometheusBadResponse(Exception):
3232
def static_content():
3333
"""Client name and version."""
3434
name = "ib-metrics-pyclient"
35-
version = "0.3"
35+
version = "0.4"
3636
return '{0} v.{1}'.format(name, version)
3737

3838

@@ -53,13 +53,14 @@ def parse_commandline_args():
5353
parser.add_argument('--pghost', metavar='pushgatewayHost', nargs='?', default=platform.node(), dest='pushgateway_host', help='pushgateway host')
5454
parser.add_argument('--pgport', metavar='pushgatewayPort', nargs='?', default='9091', dest='pushgateway_port', help='pushgateway port')
5555
parser.add_argument('--iibver', metavar='iibVersion', nargs='?', default=None, dest='iib_cmd_ver', help='IIB version: 9 or 10')
56+
parser.add_argument('--collectint', metavar='collectInterval', nargs='?', default=60, type=int, dest='sleep_interval', help='time interval between collecting metrics')
5657
args = parser.parse_args()
5758
if (args.iib_cmd_ver is None) or ((args.iib_cmd_ver != '9') and (args.iib_cmd_ver != '10')):
5859
logger.info("Trying to determine Integration Bus version from environment variable MQSI_VERSION_V.")
5960
iib_cmd_ver = get_version_from_env()
6061
else:
6162
iib_cmd_ver = args.iib_cmd_ver
62-
return args.pushgateway_host, args.pushgateway_port, iib_cmd_ver
63+
return args.pushgateway_host, args.pushgateway_port, iib_cmd_ver, abs(args.sleep_interval)
6364

6465

6566
def put_metric_to_gateway(metric_data, job, pushgateway_host, pushgateway_port):
@@ -138,8 +139,9 @@ def get_iib_metrics(pushgateway_host, pushgateway_port, mqsilist_command, bip_co
138139

139140
if __name__ == "__main__":
140141
logger.info("Run {0}".format(static_content()))
141-
pushgateway_host, pushgateway_port, iib_ver = parse_commandline_args()
142+
pushgateway_host, pushgateway_port, iib_ver, sleep_interval = parse_commandline_args()
142143
logger.info("Integration Bus version: {0}".format(iib_ver))
144+
logger.info("Metrics will be collected every {0} seconds".format(sleep_interval))
143145
mqsilist_command, bip_codes_brokers, bip_codes_components = get_platform_params_for_commands(iib_ver=iib_ver)
144146
while True:
145147
get_iib_metrics(
@@ -148,4 +150,4 @@ def get_iib_metrics(pushgateway_host, pushgateway_port, mqsilist_command, bip_co
148150
mqsilist_command=mqsilist_command,
149151
bip_codes_brokers=bip_codes_brokers,
150152
bip_codes_components=bip_codes_components)
151-
time.sleep(60)
153+
time.sleep(sleep_interval)

tests/test_iib_metrics_client.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ class TestParseCommandlineArgs(unittest.TestCase):
159159
pushgateway_host = 'testhost'
160160
pushgateway_port = '9091'
161161
iib_ver = '9'
162+
sleep_interval=60
162163

163164
Mocked = MockFunction()
164165
@patch('iib_metrics_client.logger.info', side_effect=Mocked.mock_logging_info)
@@ -167,38 +168,55 @@ class TestParseCommandlineArgs(unittest.TestCase):
167168
return_value=argparse.Namespace(
168169
pushgateway_host= pushgateway_host,
169170
pushgateway_port= pushgateway_port,
170-
iib_cmd_ver= iib_ver))
171+
iib_cmd_ver= iib_ver,
172+
sleep_interval= sleep_interval))
171173
def test_parse_commandline_args(self, mock_logging_info, mock_args):
172174
"""Test for `parse_commandline_args` function."""
173175
self.assertEqual(
174176
parse_commandline_args(),
175-
(self.pushgateway_host, self.pushgateway_port, self.iib_ver))
177+
(self.pushgateway_host, self.pushgateway_port, self.iib_ver, self.sleep_interval))
176178

177179
@patch('iib_metrics_client.logger.info', side_effect=Mocked.mock_logging_info)
178180
@patch(
179181
'argparse.ArgumentParser.parse_args',
180182
return_value=argparse.Namespace(
181183
pushgateway_host= pushgateway_host,
182184
pushgateway_port= pushgateway_port,
183-
iib_cmd_ver=None))
185+
iib_cmd_ver= None,
186+
sleep_interval= sleep_interval))
184187
def test_parse_commandline_args_is_none(self, mock_logging_info, mock_args):
185188
"""Test for `parse_commandline_args` function for `iib_cmd_ver` is None."""
186189
self.assertEqual(
187190
parse_commandline_args(),
188-
(self.pushgateway_host, self.pushgateway_port, self.iib_ver))
191+
(self.pushgateway_host, self.pushgateway_port, self.iib_ver, self.sleep_interval))
189192

190193
@patch('iib_metrics_client.logger.info', side_effect=Mocked.mock_logging_info)
191194
@patch(
192195
'argparse.ArgumentParser.parse_args',
193196
return_value=argparse.Namespace(
194197
pushgateway_host= pushgateway_host,
195198
pushgateway_port= pushgateway_port,
196-
iib_cmd_ver='42'))
199+
iib_cmd_ver= '42',
200+
sleep_interval= sleep_interval))
197201
def test_parse_commandline_args_is_42(self, mock_logging_info, mock_args):
198202
"""Test for `parse_commandline_args` function for `iib_cmd_ver` is not valid."""
199203
self.assertEqual(
200204
parse_commandline_args(),
201-
(self.pushgateway_host, self.pushgateway_port, self.iib_ver))
205+
(self.pushgateway_host, self.pushgateway_port, self.iib_ver, self.sleep_interval))
206+
207+
@patch('iib_metrics_client.logger.info', side_effect=Mocked.mock_logging_info)
208+
@patch(
209+
'argparse.ArgumentParser.parse_args',
210+
return_value=argparse.Namespace(
211+
pushgateway_host= pushgateway_host,
212+
pushgateway_port= pushgateway_port,
213+
iib_cmd_ver= iib_ver,
214+
sleep_interval= -60))
215+
def test_parse_commandline_args_is_negative_60(self, mock_logging_info, mock_args):
216+
"""Test for `parse_commandline_args` function for `sleep_interval` is `-60`."""
217+
self.assertEqual(
218+
parse_commandline_args(),
219+
(self.pushgateway_host, self.pushgateway_port, self.iib_ver, self.sleep_interval))
202220

203221

204222

0 commit comments

Comments
 (0)