From 9da3372a718e08a303f7713518a0f834c36afa13 Mon Sep 17 00:00:00 2001 From: Daniel Vrcic Date: Sat, 6 Apr 2019 18:15:30 +0200 Subject: [PATCH 1/3] include site name in metric result --- pymod/metrictoqueue.py | 6 +++++- pymod/publish.py | 32 ++++++++++++++++++++++++++++++++ pymod/run.py | 4 ++-- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/pymod/metrictoqueue.py b/pymod/metrictoqueue.py index 361a4e4..0f4c909 100644 --- a/pymod/metrictoqueue.py +++ b/pymod/metrictoqueue.py @@ -17,10 +17,12 @@ conf = '/etc/argo-nagios-ams-publisher/ams-publisher.conf' logfile = '/var/log/argo-nagios-ams-publisher/ams-publisher.log' + def seteuser(user): os.setegid(user.pw_gid) os.seteuid(user.pw_uid) + def build_msg(args, *headers): msg = Message() msg.header = dict() @@ -35,13 +37,14 @@ def build_msg(args, *headers): msg.header.update({'status': status.encode('utf-8')}) msg.header.update({'monitoring_host': nagioshost.encode('utf-8')}) - for bs in ['summary', 'message', 'vofqan', 'voname', 'roc', 'actual_data']: + for bs in ['summary', 'message', 'vofqan', 'voname', 'roc', 'actual_data', 'site']: code = "msg.body += '%s: ' + args.%s.encode(\'utf-8\') + '\\n' if args.%s else ''" % (bs, bs, bs) exec code msg.text = True return msg + def main(): parser = argparse.ArgumentParser() lobj = log.Logger(sys.argv[0], logfile) @@ -67,6 +70,7 @@ def main(): parser.add_argument('--summary', required=False, type=str) parser.add_argument('--vofqan', required=False, type=str) parser.add_argument('--voname', required=False, type=str) + parser.add_argument('--site', required=False, type=str) args = parser.parse_args() diff --git a/pymod/publish.py b/pymod/publish.py index f71b0c3..31e41de 100644 --- a/pymod/publish.py +++ b/pymod/publish.py @@ -11,6 +11,7 @@ from argo_nagios_ams_publisher.stats import StatSig from argo_ams_library.amsexceptions import AmsConnectionException, AmsServiceException + class Publish(StatSig): """ Base publisher class that initialize statistic data @@ -27,6 +28,7 @@ def _increm_intervalcounters(self, num): counter[now] = num + counter.get(now, 0) self.shared.statint[self.name]['published_periodic'] += num + class FilePublisher(Publish): """ Publisher that write the messages into a file. Used only for debugging @@ -57,6 +59,7 @@ def write(self, num=0): self.shared.log.error(e) return False, published + class MessagingPublisher(Publish): """ MessagingPublisher class that dispatch messages to ARGO Messaging @@ -97,6 +100,7 @@ def _avro_serialize(msg): plainmsg = dict() plainmsg.update(msg.header) plainmsg.update(self.body2dict(msg.body)) + plainmsg.update(tags=self.tag2dict(msg.body)) timestamp = plainmsg.get('timestamp', None) m = None @@ -108,13 +112,41 @@ def _avro_serialize(msg): return _part_date(timestamp), m def body2dict(self, body): + part_of_body = ['summary', 'message', 'actual_data'] d = dict() + bodylines = body.split('\n') for line in bodylines: split = line.split(': ', 1) if len(split) > 1: key = split[0] value = split[1] + + if key not in set(part_of_body): + continue + + d[key] = value.decode('utf-8', 'replace') + + return d + + def tag2dict(self, body): + part_of_tag = ['vofqan', 'voname', 'roc', 'site'] + header_map = dict(site='endpoint_group') + d = dict() + + bodylines = body.split('\n') + for line in bodylines: + split = line.split(': ', 1) + if len(split) > 1: + key = split[0] + value = split[1] + + if key not in set(part_of_tag): + continue + + if key in header_map: + key = header_map[key] + d[key] = value.decode('utf-8', 'replace') return d diff --git a/pymod/run.py b/pymod/run.py index af13337..6bab77b 100644 --- a/pymod/run.py +++ b/pymod/run.py @@ -47,8 +47,8 @@ def init_dirq_consume(workers, daemonized, sockstat): if not getattr(shared, 'runtime', False): shared.runtime = dict() - shared.runtime['started'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') - shared.runtime['started_epoch'] = str(int(time.time())) + shared.runtime['started'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + shared.runtime['started_epoch'] = str(int(time.time())) if shared.general['publishmsgfile']: shared.runtime.update(publisher=FilePublisher) From 0ea6a6f5f81458fb3af3c7a05b511b3474d023ad Mon Sep 17 00:00:00 2001 From: Daniel Vrcic Date: Sat, 6 Apr 2019 18:34:37 +0200 Subject: [PATCH 2/3] refactor body fields extraction from local result --- pymod/publish.py | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/pymod/publish.py b/pymod/publish.py index 31e41de..e6b5f57 100644 --- a/pymod/publish.py +++ b/pymod/publish.py @@ -111,9 +111,8 @@ def _avro_serialize(msg): return _part_date(timestamp), m - def body2dict(self, body): - part_of_body = ['summary', 'message', 'actual_data'] - d = dict() + def _extract_body(self, body, fields, maps=None): + msg = dict() bodylines = body.split('\n') for line in bodylines: @@ -122,34 +121,26 @@ def body2dict(self, body): key = split[0] value = split[1] - if key not in set(part_of_body): + if key not in set(fields): continue - d[key] = value.decode('utf-8', 'replace') - - return d + if maps and key in maps: + key = maps[key] - def tag2dict(self, body): - part_of_tag = ['vofqan', 'voname', 'roc', 'site'] - header_map = dict(site='endpoint_group') - d = dict() + msg[key] = value.decode('utf-8', 'replace') - bodylines = body.split('\n') - for line in bodylines: - split = line.split(': ', 1) - if len(split) > 1: - key = split[0] - value = split[1] + return msg - if key not in set(part_of_tag): - continue + def body2dict(self, body): + body_fields = ['summary', 'message', 'actual_data'] - if key in header_map: - key = header_map[key] + return self._extract_body(body, body_fields) - d[key] = value.decode('utf-8', 'replace') + def tag2dict(self, body): + tag_fields = ['vofqan', 'voname', 'roc', 'site'] + body_to_tagname = dict(site='endpoint_group') - return d + return self._extract_body(body, tag_fields, body_to_tagname) def _write(self, msgs): t = 1 From 953a0d9b2c8777aee03e9f7ef714146f4a592cb7 Mon Sep 17 00:00:00 2001 From: Daniel Vrcic Date: Wed, 17 Apr 2019 15:06:27 +0200 Subject: [PATCH 3/3] version bump for release --- argo-nagios-ams-publisher.spec | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/argo-nagios-ams-publisher.spec b/argo-nagios-ams-publisher.spec index b7c12b1..6df9e38 100644 --- a/argo-nagios-ams-publisher.spec +++ b/argo-nagios-ams-publisher.spec @@ -10,7 +10,7 @@ %endif Name: argo-nagios-ams-publisher -Version: 0.3.4 +Version: 0.3.5 Release: 1%{mydist} Summary: Bridge from Nagios to the ARGO Messaging system @@ -109,6 +109,8 @@ if ! /usr/bin/getent group nagiocmd &>/dev/null; then fi %changelog +* Wed Apr 17 2019 Daniel Vrcic - 0.3.5-1%{?dist} +- ARGO-1726 Pass site name in metric results * Wed Mar 6 2019 Daniel Vrcic - 0.3.4-1%{?dist} - verbose log messages for errors not handled in argo-ams-library * Tue Feb 5 2019 Daniel Vrcic - 0.3.3-1%{?dist}