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

apc_powerswitch_warning_states = (2,)
apc_powerswitch_critical_states = ()


def inventory_apc_powerswitch(info):
    return [(x[0], saveint(x[2])) for x in info if x[2] == "1"]


def check_apc_powerswitch(item, params, info):
    state_name = {
        1: 'on',
        2: 'off',
    }
    r_state = 0
    for line in info:
        if line[0] == item:
            state = saveint(line[2])
            if state in apc_powerswitch_warning_states:
                r_state = 1
            if state in apc_powerswitch_critical_states:
                r_state = 2
            return (r_state, 'Port %s has status %s' % (line[1], state_name[state]))

    return 3, "Port not found"


check_info['apc_powerswitch'] = {
    'check_function': check_apc_powerswitch,
    'inventory_function': inventory_apc_powerswitch,
    'snmp_scan_function': lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.318.1.3.4"
                                                                          ),
    'service_description': 'Power Outlet Port %s',
    'snmp_info': ('.1.3.6.1.4.1.318.1.1.12.3.5.1.1', [1, 2, 4]),
}
