Max | 34d2ca5 | 2018-01-12 16:41:24 +0100 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | # -*- mode: python-mode; py-indent-tabs-mode: nil -*- |
| 3 | """ |
| 4 | /* |
| 5 | * Copyright (C) 2017 sysmocom s.f.m.c. GmbH |
| 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | */ |
| 23 | """ |
| 24 | |
| 25 | from osmopy.osmo_ipa import Ctrl |
| 26 | import socket, argparse, sys, logging, csv |
| 27 | |
| 28 | __version__ = "0.0.1" # bump this on every non-trivial change |
| 29 | |
| 30 | def connect(host, port): |
| 31 | sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 32 | sck.setblocking(1) |
| 33 | sck.connect((host, port)) |
| 34 | return sck |
| 35 | |
| 36 | def get_var(sck, var): |
| 37 | (_, c) = Ctrl().cmd(var, None) |
| 38 | sck.send(c) |
| 39 | return Ctrl().parse_kv(sck.recv(4096)) |
| 40 | |
| 41 | def get_interval(group_name, group_counters, interval): |
| 42 | log.debug('Getting %s counter values: %s...' % (group_name, interval)) |
| 43 | (_, c) = get_var(sock, 'rate_ctr.%s.%s' % (interval, group_name)) |
| 44 | for ctr in c.split(';'): |
| 45 | if len(ctr): |
| 46 | (k, v) = ctr.split() |
| 47 | group_counters[k] = group_counters.get(k, (group_name,)) + (v,) |
| 48 | return len(group_counters) |
| 49 | |
| 50 | |
| 51 | if __name__ == '__main__': |
| 52 | p = argparse.ArgumentParser(description='Dump rate counters into csv via Osmocom CTRL protocol.') |
| 53 | p.add_argument('-v', '--version', action='version', version=("%(prog)s v" + __version__)) |
| 54 | p.add_argument('-p', '--port', type=int, default=4249, help="Port to use for CTRL interface, defaults to 4249") |
| 55 | p.add_argument('-c', '--ctrl', default='localhost', help="Adress to use for CTRL interface, defaults to localhost") |
| 56 | p.add_argument('-d', '--debug', action='store_true', help="Enable debug log") |
| 57 | p.add_argument('--header', action='store_true', help="Prepend column header to output") |
| 58 | p.add_argument('-o', '--output', nargs='?', type=argparse.FileType('w'), default=sys.stdout, help="Output file, defaults to stdout") |
| 59 | args = p.parse_args() |
| 60 | |
| 61 | log = logging.getLogger('rate_ctr2csv') |
| 62 | log.setLevel(logging.DEBUG if args.debug else logging.INFO) |
| 63 | log.addHandler(logging.StreamHandler(sys.stderr)) |
| 64 | |
| 65 | log.info('Connecting to %s:%d...' % (args.ctrl, args.port)) |
| 66 | sock = connect(args.ctrl, args.port) |
| 67 | |
| 68 | log.info('Getting rate counter groups info...') |
| 69 | (_, g) = get_var(sock, 'rate_ctr.*') |
| 70 | |
| 71 | w = csv.writer(args.output, dialect='unix') |
| 72 | total_groups = 0 |
| 73 | total_rows = 0 |
| 74 | |
| 75 | if args.header: |
| 76 | w.writerow(['group', 'counter', 'absolute', 'second', 'minute', 'hour', 'day']) |
| 77 | |
| 78 | for group in g.split(';'): |
| 79 | if len(group): |
| 80 | g_counters = {} |
| 81 | total_groups += 1 |
| 82 | total_rows += list(map(lambda x: get_interval(group, g_counters, x), ('abs', 'per_sec', 'per_min', 'per_hour', 'per_day')))[0] |
| 83 | for (k, (gr, absolute, per_sec, per_min, per_hour, per_day)) in g_counters.items(): |
| 84 | w.writerow([gr, k, absolute, per_sec, per_min, per_hour, per_day]) |
| 85 | |
| 86 | log.info('Completed: %d counters from %d groups received.' % (total_rows, total_groups)) |