#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2017             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.


#<<<hivemanager_ng_devices:sep(124)>>>
#osVersion::8.1.2.1|ip::10.8.92.100|hostName::Host-1|lastUpdated::2017-11-08T10:01:43.674Z|activeClients::0|connected::True|serialId::00000000000001
#osVersion::8.1.2.1|ip::10.8.92.130|hostName::Host-2|lastUpdated::2017-11-08T10:01:44.056Z|activeClients::14|connected::True|serialId::00000000000002
#osVersion::8.1.2.1|ip::10.8.92.135|hostName::Host-3|lastUpdated::2017-11-08T10:01:44.153Z|activeClients::12|connected::True|serialId::00000000000003
#osVersion::8.1.2.1|ip::10.8.92.140|hostName::Host-4|lastUpdated::2017-11-08T10:01:44.361Z|activeClients::10|connected::True|serialId::00000000000004
#osVersion::8.1.2.1|ip::10.8.92.145|hostName::Host-5|lastUpdated::2017-11-08T10:01:44.362Z|activeClients::5|connected::True|serialId::00000000000005
#osVersion::8.1.2.1|ip::10.8.92.150|hostName::Host-6|lastUpdated::2017-11-08T10:01:44.638Z|activeClients::13|connected::True|serialId::00000000000006
#osVersion::8.1.2.1|ip::10.8.92.155|hostName::Host-7|lastUpdated::2017-11-08T10:01:44.667Z|activeClients::14|connected::True|serialId::00000000000007
#osVersion::8.1.2.1|ip::10.8.92.160|hostName::Host-8|lastUpdated::2017-11-08T10:01:44.690Z|activeClients::10|connected::True|serialId::00000000000008
#osVersion::6.5.8.1|ip::10.8.95.100|hostName::Host-9|lastUpdated::2017-11-07T22:50:20.425Z|activeClients::9|connected::True|serialId::12345678912345


factory_settings['hivemanger_ng_devices'] = {
    'max_clients': (25, 50),
}


def parse_hivemanager_ng_devices(info):
    parsed = {}
    for device in info:
        data = dict(element.split('::') for element in device)

        data['connected'] = (data['connected'] == 'True')
        data['activeClients'] = int(data['activeClients'])

        host = data.pop('hostName')
        parsed[host] = data

    return parsed


def inventory_hivemanager_ng_devices(parsed):
    for host in parsed:
        yield host, {}


def check_hivemanager_ng_devices(item, params, parsed):
    device = parsed.get(item)
    if not device:
        yield 2, 'No data for device.'

    status, connected = 0, device['connected']
    if connected != True:
        status = 2
    yield status, 'Connected: %s' % connected

    status, clients = 0, device['activeClients']
    infotext = 'active clients: %s' % clients
    warn, crit = params['max_clients']
    if clients >= crit:
        status = 2
        infotext += ' (warn/crit at %s/%s)' % (warn, crit)
    elif clients >= warn:
        status = 1
        infotext += ' (warn/crit at %s/%s)' % (warn, crit)
    perfdata = [('connections', clients, warn, crit)]
    yield status, infotext, perfdata

    informational = [
        ('ip', 'IP address'),
        ('serialId', 'serial ID'),
        ('osVersion', 'OS version'),
        ('lastUpdated', 'last updated'),
    ]
    for (key, text) in informational:
        yield 0, "%s: %s" % (text, device[key])


check_info["hivemanager_ng_devices"] = {
    "parse_function"          : parse_hivemanager_ng_devices,
    "check_function"          : check_hivemanager_ng_devices,
    "inventory_function"      : inventory_hivemanager_ng_devices,
    "service_description"     : "Client %s",
    "default_levels_variable" : "hivemanger_ng_devices",
    "group"                   : "hivemanager_ng_devices",
    "has_perfdata"            : True,
}

