[PATCH] python/osmo-python-tests[master]: Add rate counter dumper

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Max gerrit-no-reply at lists.osmocom.org
Tue Dec 19 16:35:08 UTC 2017


Hello Neels Hofmeyr, Harald Welte, Jenkins Builder,

I'd like you to reexamine a change.  Please visit

    https://gerrit.osmocom.org/5029

to look at the new patch set (#7).

Add rate counter dumper

This simple tool dump all the rate counters available via ctrl interface
to cvs file (or stdout).

Change-Id: I12b369434e4ee9b38f92872f297e1f3d4f0698c2
Fixes: OS#2550
---
M README
M contrib/jenkins.sh
A scripts/osmo_rate_ctr2csv.py
M setup.py
4 files changed, 89 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/python/osmo-python-tests refs/changes/29/5029/7

diff --git a/README b/README
index 25be782..5b02bfa 100644
--- a/README
+++ b/README
@@ -11,6 +11,7 @@
 osmotestconfig.py - test that apps start/write with example configs
 osmotestvty.py - test vty operations (currently, tests very few)
 soap.py - implementation of SOAP <-> Ctrl proxy implemented on top of Twisted
+osmo_rate_ctr2csv.py - rate counter dumper on top of osmo_ipa
 
 Each of these scripts imports a project-specific osmoappdesc.py,
 which provides information about the available apps, configs, vty ports, etc.
diff --git a/contrib/jenkins.sh b/contrib/jenkins.sh
index 36f3336..126f66b 100755
--- a/contrib/jenkins.sh
+++ b/contrib/jenkins.sh
@@ -15,6 +15,7 @@
 
 python3 -m compileall osmopy
 python3 -m compileall scripts/osmo_ctrl.py
+python3 -m compileall scripts/osmo_rate_ctr2csv.py
 python3 -m compileall scripts/osmo_interact_common.py
 python3 -m compileall scripts/osmo_interact_ctrl.py
 python3 -m compileall scripts/osmo_interact_vty.py
diff --git a/scripts/osmo_rate_ctr2csv.py b/scripts/osmo_rate_ctr2csv.py
new file mode 100644
index 0000000..1e3fc9f
--- /dev/null
+++ b/scripts/osmo_rate_ctr2csv.py
@@ -0,0 +1,86 @@
+#!/usr/bin/python3
+# -*- mode: python-mode; py-indent-tabs-mode: nil -*-
+"""
+/*
+ * Copyright (C) 2017 sysmocom s.f.m.c. GmbH
+ *
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+"""
+
+from osmopy.osmo_ipa import Ctrl
+import socket, argparse, sys, logging, csv
+
+__version__ = "0.0.1" # bump this on every non-trivial change
+
+def connect(host, port):
+        sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        sck.setblocking(1)
+        sck.connect((host, port))
+        return sck
+
+def get_var(sck, var):
+        (_, c) = Ctrl().cmd(var, None)
+        sck.send(c)
+        return Ctrl().parse_kv(sck.recv(4096))
+
+def get_interval(group_name, group_counters, interval):
+        log.debug('Getting %s counter values: %s...' % (group_name, interval))
+        (_, c) = get_var(sock, 'rate_ctr.%s.%s' % (interval, group_name))
+        for ctr in c.split(';'):
+                if len(ctr):
+                        (k, v) = ctr.split()
+                        group_counters[k] = group_counters.get(k, (group_name,)) + (v,)
+        return len(group_counters)
+
+
+if __name__ == '__main__':
+        p = argparse.ArgumentParser(description='Dump rate counters into csv via Osmocom CTRL protocol.')
+        p.add_argument('-v', '--version', action='version', version=("%(prog)s v" + __version__))
+        p.add_argument('-p', '--port', type=int, default=4249, help="Port to use for CTRL interface, defaults to 4249")
+        p.add_argument('-c', '--ctrl', default='localhost', help="Adress to use for CTRL interface, defaults to localhost")
+        p.add_argument('-d', '--debug', action='store_true', help="Enable debug log")
+        p.add_argument('--header', action='store_true', help="Prepend column header to output")
+        p.add_argument('-o', '--output', nargs='?', type=argparse.FileType('w'), default=sys.stdout, help="Output file, defaults to stdout")
+        args = p.parse_args()
+
+        log = logging.getLogger('rate_ctr2csv')
+        log.setLevel(logging.DEBUG if args.debug else logging.INFO)
+        log.addHandler(logging.StreamHandler(sys.stderr))
+
+        log.info('Connecting to %s:%d...' % (args.ctrl, args.port))
+        sock = connect(args.ctrl, args.port)
+
+        log.info('Getting rate counter groups info...')
+        (_, g) = get_var(sock, 'rate_ctr.*')
+
+        w = csv.writer(args.output, dialect='unix')
+        total_groups = 0
+        total_rows = 0
+
+        if args.header:
+                w.writerow(['group', 'counter', 'absolute', 'second', 'minute', 'hour', 'day'])
+
+        for group in g.split(';'):
+                if len(group):
+                        g_counters = {}
+                        total_groups += 1
+                        total_rows += list(map(lambda x: get_interval(group, g_counters, x), ('abs', 'per_sec', 'per_min', 'per_hour', 'per_day')))[0]
+                        for (k, (gr, absolute, per_sec, per_min, per_hour, per_day)) in g_counters.items():
+                                w.writerow([gr, k, absolute, per_sec, per_min, per_hour, per_day])
+
+        log.info('Completed: %d counters from %d groups received.' % (total_rows, total_groups))
diff --git a/setup.py b/setup.py
index bab9c38..494f63f 100755
--- a/setup.py
+++ b/setup.py
@@ -26,6 +26,7 @@
 	scripts = ["scripts/osmo_interact_vty.py",
 		   "scripts/osmo_interact_ctrl.py",
                    "scripts/osmo_ctrl.py",
+                   "scripts/osmo_rate_ctr2csv.py",
 		   "scripts/soap.py",
 		   "scripts/twisted_ipa.py",
 		   "scripts/osmo_verify_transcript_vty.py",

-- 
To view, visit https://gerrit.osmocom.org/5029
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newpatchset
Gerrit-Change-Id: I12b369434e4ee9b38f92872f297e1f3d4f0698c2
Gerrit-PatchSet: 7
Gerrit-Project: python/osmo-python-tests
Gerrit-Branch: master
Gerrit-Owner: Max <msuraev at sysmocom.de>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: Max <msuraev at sysmocom.de>
Gerrit-Reviewer: Neels Hofmeyr <nhofmeyr at sysmocom.de>
Gerrit-Reviewer: Pau Espin Pedrol <pespin at sysmocom.de>



More information about the gerrit-log mailing list