#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2014             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk 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 in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# tails. You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.

# Example output from agent:
# <<<chrony>>>
# Reference ID    : 212.18.3.18 (ntp1.m-online.net)
# Stratum         : 3
# Ref time (UTC)  : Tue Aug 19 16:56:21 2014
# System time     : 0.000000353 seconds fast of NTP time
# Frequency       : 10.725 ppm slow
# Residual freq   : 195.475 ppm
# Skew            : 10.639 ppm
# Root delay      : 0.027455 seconds
# Root dispersion : 0.024512 seconds

# <<<chrony>>>
# 506 Cannot talk to daemon

ntp_default_levels = (10, 200.0, 500.0)  # stratum, ms offset

factory_settings["ntp_time_default_levels"] = {
    "ntp_levels": ntp_default_levels,
    "alert_delay": (300, 3600),
}


def parse_chrony(info):
    """
    parse info list into dictionary

    expected info:
    [[u'Reference', u'ID', u':', u'0.0.0.0', u'()'],
     [u'Stratum', u':', u'0'],
     [u'Ref', u'time', u'(UTC)', u':', u'Thu', u'Jan', u'1', u'00:00:00', u'1970'],
     ...
    ]

    parsed dictionary:
    { u'Reference ID': u'0.0.0.0 ()',
      u'Stratum': u'0',
      u'Ref time(utc)': u'Thu Jan 1 00:00:00 1970',
      ...
    }

    :param info: chrony output as lists
    :return: dictionary
    """
    info_dict = {}

    if is_error_message(info):
        info_dict["error"] = " ".join(info[0])
        return info_dict

    for line in info:
        if ":" in line:
            key, value = " ".join(line).split(":", 1)
            info_dict[key.strip()] = value.strip()
    return info_dict


def is_error_message(info):
    return len(info) == 1 and isinstance(info[0], list) and ":" not in info[0][0]


def check_chrony(_item, params, parsed):
    """
    check if agent returned error message
    check if chrony can reach NTP servers
    check if offset is in range
    check if stratum is too high

    :param _item: <not used>
    :param params: stratum and offset limits
    :param parsed: dictionary
    :return: status
    """
    # Check error message
    if "error" in parsed:
        yield 2, "%s" % parsed.get("error")
        return

    # Prepare parameters
    if isinstance(params, tuple):
        params = {
            "ntp_levels": params,
            "alert_delay": (300, 3600),
        }
    crit_stratum, warn, crit = params["ntp_levels"]

    # extract and convert check values
    address = parsed.get("Reference ID").split(' ')[1]
    offset = float(parsed.get("System time").split(' ')[0]) * 1000  # ms
    stratum = int(parsed.get("Stratum"))

    # Check reference id: if brackets are empty, NTP servers are unreachable
    if len(address) <= 2:
        yield 1, "NTP servers unreachable. Reference ID: %s" % parsed["Reference ID"]
        return

    # Check stratum
    yield check_levels_backport(stratum,
                       None, (crit_stratum, crit_stratum),
                       human_readable_func=lambda v: "%d" % v,
                       infoname="Stratum")

    # Check offset
    yield check_levels_backport(abs(offset),
                       "offset", (warn, crit),
                       human_readable_func=lambda x: "%.4f ms" % x,
                       infoname="Offset")

    # Show additional information
    yield 0, "Reference ID: %s" % parsed["Reference ID"]


check_info["chrony"] = {
    "default_levels_variable": "ntp_time_default_levels",
    "parse_function": parse_chrony,
    "check_function": check_chrony,
    "inventory_function": discover_single,
    "service_description": "NTP Time",
    "has_perfdata": True,
    "group": "ntp_time",
    "includes": ["backport.include"]
}
