#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# vutlan is not a typo!
# MIB can also be browsed on
# https://mibs.observium.org/mib/SKYCONTROL-SYSTEM-MIB/#


def parse_vutlan_ems_smoke(info):
    parsed = {}
    for line in info[0]:
        if line[0].startswith('106'):
            # all OIDs 106xxx are smoke-related
            parsed[line[1]] = bool((int(line[2])))

    return parsed


def discover_vutlan_ems_smoke(parsed):
    for sensor_name in parsed:
        yield sensor_name, {}


def check_vutlan_ems_smoke(item, _no_params, parsed):
    if parsed.get(item) is None:
        return

    smoke_detected = parsed[item]
    if smoke_detected:
        yield 2, 'Smoke detected'
        return

    yield 0, 'No smoke detected'


check_info['vutlan_ems_smoke'] = {
    'parse_function': parse_vutlan_ems_smoke,
    'inventory_function': discover_vutlan_ems_smoke,
    'check_function': check_vutlan_ems_smoke,
    'service_description': 'Smoke Detector %s',
    'snmp_info': [
        (
            '.1.3.6.1.4.1.39052.1.3.1',
            [
                OID_END,
                7,  # vutlan.mib::ctlUnitElementName (can be user-defined)
                9,  # vutlan.mib::ctlUnitElementValue
            ],
        ),
    ],
    'snmp_scan_function': lambda oid: 'vutlan ems' in oid('.1.3.6.1.2.1.1.1.0').lower(),
}
