|
| 1 | +#! /usr/bin/env python3 |
| 2 | + |
| 3 | +"""Fetch daily generated data from tirex-tiledir-check. |
| 4 | +""" |
| 5 | + |
| 6 | +import argparse |
| 7 | +from enum import Enum |
| 8 | +import json |
| 9 | +import os |
| 10 | +import sys |
| 11 | + |
| 12 | + |
| 13 | +class ServiceStatus(Enum): |
| 14 | + OK = 0 |
| 15 | + WARNING = 1 |
| 16 | + CRITICAL = 2 |
| 17 | + UNKNOWN = 3 |
| 18 | + |
| 19 | + |
| 20 | +def respond(status, message, measurements): |
| 21 | + if not measurements: |
| 22 | + line = "{}: {}\n".format(status.name, message) |
| 23 | + else: |
| 24 | + line = "{}: {} | {}\n".format(status.name, message, measurements) |
| 25 | + print(line) |
| 26 | + sys.exit(status.value) |
| 27 | + |
| 28 | + |
| 29 | +def get_data(path): |
| 30 | + with open(path, "r") as f: |
| 31 | + json_data = json.load(f) |
| 32 | + return json_data |
| 33 | + |
| 34 | + |
| 35 | +def format_measurement(k, v): |
| 36 | + return "'{}'={}".format(k, v) |
| 37 | + |
| 38 | + |
| 39 | +def join_values(measurements): |
| 40 | + parts = [] |
| 41 | + for m in measurements: |
| 42 | + parts.append(format_measurement(m[0], m[1])) |
| 43 | + return " ".join(parts) |
| 44 | + |
| 45 | + |
| 46 | +parser = argparse.ArgumentParser(description="Monitoring plugin for tirex-tiledir-check results. This plugin reads the generated stats and supplies them to Icinga. It does not validate or check the retrieved numbers.") |
| 47 | +parser.add_argument("-s", "--style", type=str, help="Style", required=True) |
| 48 | +parser.add_argument("-d", "--dir", type=str, help="Path where the JOSN files generated by the nightly job are located.") |
| 49 | +args = parser.parse_args() |
| 50 | + |
| 51 | +json_path = os.path.join(args.dir, "tiles-{}.stats".format(args.style)) |
| 52 | +data = get_data(json_path) |
| 53 | +result = [] |
| 54 | +try: |
| 55 | + for style, style_data in data.items(): |
| 56 | + if style != args.style: |
| 57 | + respond(ServiceStatus.UNKNOWN, "Wrong style {} found in {}".format(style, json_path)) |
| 58 | + for zoom in range(len(style_data)): |
| 59 | + result.append(("{}_{:02d}_count".format(style, zoom), style_data[zoom]["count"])) |
| 60 | + result.append(("{}_{:02d}_sumsize".format(style, zoom), style_data[zoom]["sumsize"])) |
| 61 | + respond(ServiceStatus.OK, "Tirex tile cache stats for style {}".format(args.style), join_values(result)) |
| 62 | +except KeyError as e: |
| 63 | + respond(ServiceStatus.UNKNOWN, "Could not parse JSON data: {}".format(e)) |
0 commit comments