#!/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.

mbg_lantime_refclock_refmode_map = {
    '0': 'notavailable',
    '1': 'normalOperation',
    '2': 'trackingSearching',
    '3': 'antennaFaulty',
    '4': 'warmBoot',
    '5': 'coldBoot',
    '6': 'antennaShortcircuit',
}

mbg_lantime_refclock_gpsstate_map = {
    '0': 'not available',
    '1': 'synchronized',
    '2': 'not synchronized',
}

# number of good satellites
mbg_lantime_refclock_default_levels = (3, 3)


def inventory_mbg_lantime_refclock(info):
    if len(info) > 0 and len(info[0]) == 6:
        return [(None, None, "mbg_lantime_refclock_default_levels")]


def check_mbg_lantime_refclock(item, params, info):
    if len(info) > 0 and len(info[0]) == 6:
        ref_mode, gps_state, gps_pos, gps_sat_good, gps_sat_total, _gps_mode = info[0]

        state = 0
        state_txt = []

        # Handle the reported refclock mode
        thr_txt = ''
        if ref_mode in ['0', '3', '6']:
            state = max(state, 2)
            thr_txt = ' (!!)'
        elif ref_mode in ['2', '4', '5']:
            state = max(state, 1)
            thr_txt = ' (!)'
        state_txt.append('Refclock State: %s%s' %
                         (mbg_lantime_refclock_refmode_map.get(ref_mode, 'UNKNOWN'), thr_txt))

        # Handle gps state
        thr_txt = ''
        if gps_state in ['0', '2']:
            state = max(state, 2)
            thr_txt = ' (!!)'
        state_txt.append('GPS State: %s%s' %
                         (mbg_lantime_refclock_gpsstate_map.get(gps_state, 'UNKNOWN'), thr_txt))

        # Add gps position
        state_txt.append(gps_pos)

        # Handle number of satellites
        thr_txt = ''
        if params[0] is not None and int(gps_sat_good) < params[1]:
            state = max(state, 2)
            thr_txt = ' (!!)'
        elif params[1] is not None and int(gps_sat_good) < params[0]:
            state = max(state, 1)
            thr_txt = ' (!)'
        state_txt.append('Satellites: %s/%s%s' % (gps_sat_good, gps_sat_total, thr_txt))

        perfdata = [('sat_good', gps_sat_good, params[0], params[1]), ('sat_total', gps_sat_total)]

        return (state, ', '.join(state_txt), perfdata)

    return (3, 'Got no state information')


check_info["mbg_lantime_refclock"] = {
    'check_function': check_mbg_lantime_refclock,
    'inventory_function': inventory_mbg_lantime_refclock,
    'service_description': 'LANTIME Refclock',
    'has_perfdata': True,
    'snmp_info': (
        '.1.3.6.1.4.1.5597.3.2',
        [
            4,  # MBG-SNMP-MIB::mbgLtRefClockModeVal
            6,  # MBG-SNMP-MIB::mbgLtRefGpsStateVal
            7,  # MBG-SNMP-MIB::mbgLtRefGpsPosition
            9,  # MBG-SNMP-MIB::mbgLtRefGpsSatellitesGood
            10,  # MBG-SNMP-MIB::mbgLtRefGpsSatellitesInView
            16,  # MBG-SNMP-MIB::mbgLtRefGpsModeVal
        ]),
    'snmp_scan_function': lambda oid: oid(".1.3.6.1.2.1.1.2.0") == ".1.3.6.1.4.1.5597.3",
}
