Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBM MQ metadata #6979

Merged
merged 26 commits into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e1e7c00
added logic for metadata collection from cmdline
steveny91 Jun 25, 2020
04784f5
logic fix and added simple test to check version parsing logic
steveny91 Jun 25, 2020
05307cf
styling to adhere to linter
steveny91 Jun 25, 2020
a36c9b8
attempt to add metadata test to e2e test
steveny91 Jun 25, 2020
84c560a
styling fix
steveny91 Jun 25, 2020
04462f8
changes to logging levels and copy right year to 2020
steveny91 Jun 25, 2020
64e7a49
version metadata collection parameter
steveny91 Jun 25, 2020
e02bc4c
more intuitive test naming
steveny91 Jun 25, 2020
5c448f2
moved cmd to common.py
steveny91 Jun 25, 2020
c97b643
changed and documented the param version_output_cmd
steveny91 Jun 26, 2020
d41366e
error handling for version parsing
steveny91 Jun 26, 2020
7c3382d
styling
steveny91 Jun 27, 2020
3e556fd
dfferent approach to collecting metadata and clean up unused code and…
steveny91 Jun 29, 2020
2b5f536
error handling and only if version output is right format VVRRMMFF
steveny91 Jun 30, 2020
0873226
moved metadata collection to separate file
steveny91 Jul 8, 2020
5e4e457
revert too pymqi 1.10.1 and styling fixes
steveny91 Jul 8, 2020
4c8aba5
styling and better debug messages
steveny91 Jul 8, 2020
7d6d837
log raw version before parsing
steveny91 Jul 8, 2020
f2b0ffb
reomve pdb
steveny91 Jul 9, 2020
97f9054
Merge remote-tracking branch 'origin/master' into steven-y-ibm-mq-met…
hithwen Jul 13, 2020
58737ce
Minor nits
hithwen Jul 13, 2020
c1a055b
bump dependency
hithwen Jul 13, 2020
7e2f890
Collect only version
hithwen Jul 13, 2020
ff7780a
Downgrade pymqi
hithwen Jul 13, 2020
04df8a4
Merge remote-tracking branch 'origin/master' into steven-y-ibm-mq-met…
hithwen Jul 15, 2020
e3e99cc
Small nit on string formatting
hithwen Jul 15, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ibm_mq/datadog_checks/ibm_mq/data/conf.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ instances:
# SYSTEM.*: queue_type:system
# DEV.*: role:dev,queue_type:default

## @param version_output_cmd - string - optional - default: dspmqver
## Custom command to run dspmqver to collect IBM MQ version if applicable. The agent will try to run
## the `dspmqver` command by default to collect the version, but if the command doesn't work or another
## command is needed (e.g. docker exec ibm_mq dspmqver) the version will not be collected. This will not
## affect the collection of any metrics.
#
# version_output_cmd: dspmqver
AlexandreYang marked this conversation as resolved.
Show resolved Hide resolved

## @param tags - list of strings - optional
## A list of tags to attach to every metric and service check emitted by this instance.
##
Expand Down
37 changes: 36 additions & 1 deletion ibm_mq/datadog_checks/ibm_mq/ibm_mq.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# (C) Datadog, Inc. 2018-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)

from typing import Any

from datadog_checks.base import AgentCheck
from datadog_checks.base.utils.subprocess_output import get_subprocess_output
from datadog_checks.ibm_mq.metrics import COUNT, GAUGE

from . import connection, errors
Expand Down Expand Up @@ -44,6 +44,9 @@ def check(self, _):
self.service_check(self.SERVICE_CHECK, AgentCheck.CRITICAL, self.config.tags)
return

if self.is_metadata_collection_enabled():
self._collect_metadata()

try:
self.channel_metric_collector.get_pcf_channel_metrics(queue_manager)
self.queue_metric_collector.collect_queue_metrics(queue_manager)
Expand All @@ -55,3 +58,35 @@ def send_metric(self, metric_type, metric_name, metric_value, tags):
getattr(self, metric_type)(metric_name, metric_value, tags=tags)
else:
self.log.warning("Unknown metric type `%s` for metric `%s`", metric_type, metric_name)

def _collect_metadata(self):
hithwen marked this conversation as resolved.
Show resolved Hide resolved
raw_version = self._get_version()
if not raw_version:
self.log.debug("Version not found in stdout")
return
self.log.debug('IBM MQ version: %s', raw_version)
self.set_metadata('version', raw_version)

def _get_version(self):

cmd = self.instance.get("version_output_cmd", "dspmqver")
try:
pc_out, pc_err, _ = get_subprocess_output(cmd, self.log, False)
except OSError as e:
self.log.debug("Error collecting IBM MQ version: %s", e)
return None

if pc_out is None:
return None
return self._parse_version(pc_out)

@staticmethod
def _parse_version(output):
for line in output.splitlines():
line = line.strip()
try:
if line.startswith("Version:"):
return line.split()[1]
except BaseException:
return None

22 changes: 21 additions & 1 deletion ibm_mq/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
BAD_CHANNEL = 'DEV.NOTHERE.SVRCONN'

MQ_VERSION = os.environ.get('IBM_MQ_VERSION', '9')
MQ_VERSION_RAW = os.environ.get('IBM_MQ_VERSION_RAW', '9.1.1.0')

COMPOSE_FILE_NAME = 'docker-compose-v{}.yml'.format(MQ_VERSION)

COMPOSE_FILE_PATH = os.path.join(COMPOSE_DIR, COMPOSE_FILE_NAME)

GET_VERSION_CMD = ["docker", "exec", "ibm_mq", "dspmqver"]

INSTANCE = {
'channel': CHANNEL,
'queue_manager': QUEUE_MANAGER,
Expand All @@ -41,6 +44,19 @@
'tags': ['foo:bar'],
}

INSTANCE_METADATA = {
'channel': CHANNEL,
'queue_manager': QUEUE_MANAGER,
'host': HOST,
'port': PORT,
'username': USERNAME,
'password': PASSWORD,
'queues': [QUEUE],
'channels': [CHANNEL, BAD_CHANNEL],
'tags': ['foo:bar'],
'version_output_cmd': 'docker exec ibm_mq dspmqver',
}

INSTANCE_WITH_CONNECTION_NAME = {
'channel': CHANNEL,
'queue_manager': QUEUE_MANAGER,
Expand Down Expand Up @@ -82,6 +98,7 @@
'password': PASSWORD,
'auto_discover_queues': True,
'channels': [CHANNEL, BAD_CHANNEL],
'version_output_cmd': 'docker exec ibm_mq dspmqver',
}

INSTANCE_QUEUE_REGEX_TAG = {
Expand All @@ -96,7 +113,10 @@
}

E2E_METADATA = {
'docker_volumes': ['{}/scripts/start_commands.sh:/tmp/start_commands.sh'.format(HERE)],
'docker_volumes': [
'{}/scripts/start_commands.sh:/tmp/start_commands.sh'.format(HERE),
'/var/run/docker.sock:/var/run/docker.sock',
],
'start_commands': ['bash /tmp/start_commands.sh'],
'env_vars': {'LD_LIBRARY_PATH': '/opt/mqm/lib64:/opt/mqm/lib', 'C_INCLUDE_PATH': '/opt/mqm/inc'},
}
Expand Down
6 changes: 6 additions & 0 deletions ibm_mq/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ def instance():
return inst


@pytest.fixture
def instance_metadata():
inst = copy.deepcopy(common.INSTANCE_METADATA)
return inst
hithwen marked this conversation as resolved.
Show resolved Hide resolved


@pytest.fixture
def instance_with_connection_name():
inst = copy.deepcopy(common.INSTANCE_WITH_CONNECTION_NAME)
Expand Down
18 changes: 17 additions & 1 deletion ibm_mq/tests/test_ibm_mq_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

from datadog_checks.base import AgentCheck
from datadog_checks.ibm_mq import IbmMqCheck
from datadog_checks.dev import run_command

from . import common
from .common import QUEUE_METRICS, assert_all_metrics
from .common import QUEUE_METRICS, assert_all_metrics, MQ_VERSION_RAW, GET_VERSION_CMD

pytestmark = [pytest.mark.usefixtures("dd_environment"), pytest.mark.integration]

Expand Down Expand Up @@ -173,3 +174,18 @@ def test_check_regex_tag(aggregator, instance_queue_regex_tag, seed_data):

for metric, _ in QUEUE_METRICS:
aggregator.assert_metric(metric, tags=tags)

hithwen marked this conversation as resolved.
Show resolved Hide resolved

def test_metadata_parsing():
result = run_command(GET_VERSION_CMD, capture="out", check=True)

raw_version = IbmMqCheck._parse_version(result.stdout)
assert raw_version == MQ_VERSION_RAW


def test_metadata_parsing_fail():
cmd = ["docker", "exec", "ibm_mq", "ls"]
result = run_command(cmd, capture="out", check=True)

raw_version = IbmMqCheck._parse_version(result.stdout)
assert raw_version is None
31 changes: 31 additions & 0 deletions ibm_mq/tests/test_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# (C) Datadog, Inc. 2020-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
import pytest

from datadog_checks.dev import run_command
from datadog_checks.ibm_mq import IbmMqCheck

from .common import GET_VERSION_CMD

pytestmark = pytest.mark.e2e


def test_metadata(instance_metadata, datadog_agent):
check = IbmMqCheck('ibm_mq', {}, [instance_metadata])
check.check_id = 'test:123'
check.check(instance_metadata)

result = run_command(GET_VERSION_CMD, capture="out", check=True)

raw_version = IbmMqCheck._parse_version(result.stdout)
major, minor, patch, _ = raw_version.split('.')
version_metadata = {
'version.scheme': 'semver',
'version.major': major,
'version.minor': minor,
'version.patch': patch,
'version.raw': raw_version,
}

datadog_agent.assert_metadata('test:123', version_metadata)
2 changes: 2 additions & 0 deletions ibm_mq/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ commands =
setenv =
LD_LIBRARY_PATH=/opt/mqm/lib64:/opt/mqm/lib:{env:LD_LIBRARY_PATH:none}
8: IBM_MQ_VERSION = 8
8: IBM_MQ_VERSION_RAW = 8.0.0.4
9: IBM_MQ_VERSION = 9
9: IBM_MQ_VERSION_RAW = 9.1.1.0