#!/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.
#
# 2017 comNET GmbH, Bjoern Mueller

def inventory_kentix_motion(info):
    if info:
        index = info[0][0].split('.', 3)[-1]
        yield index, {}


def check_kentix_motion(item, params, info):
    def test_in_period(time, periods):
        time_mins = time[0] * 60 + time[1]
        for per in periods:
            per_mins_low  = per[0][0] * 60 + per[0][1]
            per_mins_high = per[1][0] * 60 + per[1][1]
            if time_mins >= per_mins_low and time_mins < per_mins_high:
                return True
        return False

    for line in info:
        if line[0].split('.', 3)[-1] == item:
            value, valmax = map(int, line[1:3])
            weekdays = ['monday', 'tuesday', 'wednesday', 'thursday',
                            'friday', 'saturday', 'sunday']
            today = time.localtime()
            if params != None and 'time_periods' in params:
                periods = params['time_periods'][weekdays[today.tm_wday]]
            else:
                periods = [((0, 0), (24, 0))]
            if value >= valmax:
                status = test_in_period((today.tm_hour, today.tm_min), periods) and 1 or 0
                return status, 'Motion detected', [('motion', value, valmax, None, 0, 100)]
            else:
                return 0, 'No motion detected', [('motion', value, valmax, None, 0, 100)]


check_info['kentix_motion'] = {
    'inventory_function'        : inventory_kentix_motion,
    'check_function'            : check_kentix_motion,
    'service_description'       : 'Motion Detector %s',
    'has_perfdata'              : True,
    'group'                     : 'motion',
    'snmp_info'                 : ('.1.3.6.1.4.1.37954', [ '2.1.5', '3.1.5' ],
                                    [
                                        0,   # Numeric index
                                        '1', # MotionValue
                                        '2', # MotionMax
                                    ]
                                    ),
    'snmp_scan_function'        : lambda oid: oid('.1.3.6.1.2.1.1.2.0').startswith('.1.3.6.1.4.1.332.11.6'),
}
