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


# these defaults were specified by customer
factory_settings['skype_mobile_defaultlevels'] = {
    'requests_processing':             {"upper": (10000, 20000)}
}

factory_settings['skype_xmpp_defaultlevels'] = {
    'failed_outbound_streams':         {"upper": (0.01, 0.02)},
    'failed_inbound_streams':          {"upper": (0.01, 0.02)}
}

factory_settings['skype_edgeauth_defaultlevels'] = {
    'bad_requests':                    {"upper": (20, 40)},
}

factory_settings['skype_mediation_server_defaultlevels'] = {
    'load_call_failure_index':         {"upper": (10, 20)},
    'failed_calls_because_of_proxy':   {"upper": (10, 20)},
    'failed_calls_because_of_gateway': {"upper": (10, 20)},
    'media_connectivity_failure':      {"upper": (1, 2)},
}

factory_settings['skype_sip_defaultlevels'] = {
    'message_processing_time':         {"upper": (1.0, 2.0)}, # for edge servers: < 3
    'incoming_responses_dropped':      {"upper": (1.0, 2.0)},
    'incoming_requests_dropped':       {"upper": (1.0, 2.0)},
    'queue_latency':                   {"upper": (0.1, 0.2)},
    'sproc_latency':                   {"upper": (0.1, 0.2)},
    'throttled_requests':              {"upper": (0.2, 0.4)},
    'local_503_responses':             {"upper": (0.01, 0.02)},
    'timedout_incoming_messages':      {"upper": (2, 4)},
    'holding_time_incoming':           {"upper": (6.0, 12.0)},
    'flow_controlled_connections':     {"upper": (1, 2)},
    'outgoing_queue_delay':            {"upper": (2.0, 4.0)},
    'timedout_sends':                  {"upper": (0.01, 0.02)},
    'authentication_errors':           {"upper": (1, 2)},
}

factory_settings['skype_conferencing_defaultlevels'] = {
    'incomplete_calls':                {"upper": (20, 40)},
    'create_conference_latency':       {"upper": (5000, 10000)},
    'allocation_latency':              {"upper": (5000, 10000)},
}

factory_settings["skype_edge_defaultlevels"] = {
    'authentication_failures':         {"upper": (20, 40)},
    'allocate_requests_exceeding':     {"upper": (20, 40)},
    'packets_dropped':                 {"upper": (200, 400)},
}

factory_settings["skype_proxy_defaultlevels"] = {
    'throttled_connections':           {"upper": (1, 2)},
}

factory_settings['skype_defaultlevels'] = {
    'failed_search_requests':          {"upper": (1.0, 2.0)},
    'failed_locations_requests':       {"upper": (1.0, 2.0)},
    'timedout_ad_requests':            {"upper": (0.01, 0.02)},
    '5xx_responses':                   {"upper": (1.0, 2.0)},

    'asp_requests_rejected':           {"upper": (1, 2)},
    'failed_file_requests':            {"upper": (1.0, 2.0)},
    'join_failures':                   {"upper": (1, 2)},
    'failed_validate_cert':            {"upper": (1, 2)},
}


def check_skype(item, params, parsed):
    # LS:WEB - Address Book Web Query\WEB - Failed search requests/sec
    # LS:WEB - Location Information Service\WEB - Failed Get Locations Requests/Second
    # LS:WEB - Distribution List Expansion\WEB - Timed out Active Directory Requests/sec
    # LS:WEB - UCWA\UCWA - HTTP 5xx Responses/Second
    # ASP.NET Apps v4.0.30319(*)\Requests Rejected
    #
    # LS:WEB - Address Book File Download\WEB – Failed File Requests/Second
    # LS: JoinLauncher – Join Launcher Service Failure\JOINLAUNCHER – Join Failures
    # LS:WEB – Auth Provider related calls\WEB – Failed validate cert calls to the cert auth provider


    yield wmi_yield_raw_persec(parsed["LS:WEB - Address Book Web Query"], item,
                               "WEB - Failed search requests/sec",
                               label=" failed search requests/sec",
                               perfvar="failed_search_requests",
                               levels=params['failed_search_requests'])

    yield wmi_yield_raw_persec(parsed["LS:WEB - Location Information Service"], item,
                               "WEB - Failed Get Locations Requests/Second",
                               label=" failed location requests/sec",
                               perfvar="failed_location_requests",
                               levels=params['failed_locations_requests'])

    yield wmi_yield_raw_persec(parsed["LS:WEB - Distribution List Expansion"], item,
                               "WEB - Timed out Active Directory Requests/sec",
                               label=" timeout AD requests/sec",
                               perfvar="failed_ad_requests",
                               levels=params['timedout_ad_requests'])

    yield wmi_yield_raw_persec(parsed["LS:WEB - UCWA"], item,
                               "UCWA - HTTP 5xx Responses/Second",
                               label="  HTTP 5xx/sec",
                               perfvar="http_5xx",
                               levels=params['5xx_responses']
                               )

    yield wmi_yield_raw_counter(parsed["ASP.NET Apps v4.0.30319"], item,
                                "Requests Rejected",
                                label=" requests rejected",
                                perfvar="asp_requests_rejected",
                                levels=params['asp_requests_rejected'])

    if "LS:WEB - Address Book File Download" in parsed:
        yield wmi_yield_raw_persec(parsed["LS:WEB - Address Book File Download"], item,
                                "WEB - Failed File Requests/Second",
                                label=" failed file requests/sec",
                                perfvar="failed_file_requests",
                                levels=params['failed_file_requests'])

    if "LS:JoinLauncher - Join Launcher Service Failure" in parsed:
        yield wmi_yield_raw_counter(parsed["LS:JoinLauncher - Join Launcher Service Failure"],
                                    item, "JOINLAUNCHER - Join failures",
                                    label=" join failures",
                                    perfvar="join_failures",
                                    levels=params['join_failures'])

    if "LS:WEB - Auth Provider related calls" in parsed:
        yield wmi_yield_raw_counter(parsed["LS:WEB - Auth Provider related calls"], item,
                                    "WEB - Failed validate cert calls to the cert auth provider",
                                    label=" failed cert validations",
                                    perfvar="failed_validate_cert_calls",
                                    levels=params['failed_validate_cert'])


check_info['skype'] = {
    'inventory_function'      : lambda table: inventory_wmi_table(table, required_tables=[
        "LS:WEB - Address Book Web Query",
        "LS:WEB - Location Information Service",
        "LS:WEB - Distribution List Expansion",
        "LS:WEB - UCWA",
        "ASP.NET Apps v4.0.30319"
    ]),
    'check_function'          : check_skype,
    'parse_function'          : lambda info: parse_wmi_table(info, key="instance"),
    'has_perfdata'            : True,
    'service_description'     : "Skype Web Components",
    'includes'                : ['wmi.include'],
    'group'                   : 'skype',
    'default_levels_variable' : 'skype_defaultlevels'
}

def check_skype_mcu(item, params, parsed):
    # LS:DATAMCU - MCU Health And Performance\DATAMCU - MCU Health State
    # LS:AVMCU - MCU Health And Performance\AVMCU - MCU Health State
    # LS:AsMcu - MCU Health And Performance\ASMCU - MCU Health State
    # LS:ImMcu - MCU Health And Performance\IMMCU - MCU Health State

    def health(value, label):
        # The current health of the MCU. 0 = Normal. 1 = Loaded. 2 = Full. 3 = Unavailable.
        state = {
            "0": (0, "Normal"),
            "1": (1, "Loaded"),
            "2": (1, "Full"),
            "3": (2, "Unavailable")
        }.get(value, (2, "unknown (%s)" % value))

        return state[0], "%s: %s" % (label, state[1])

    yield health(parsed["LS:DATAMCU - MCU Health And Performance"]
                .get(0, "DATAMCU - MCU Health State"),
                "DATAMCU")

    yield health(parsed["LS:AVMCU - MCU Health And Performance"]
                .get(0, "AVMCU - MCU Health State"),
                "AVMCU")

    yield health(parsed["LS:AsMcu - MCU Health And Performance"]
                .get(0, "ASMCU - MCU Health State"),
                "ASMCU")

    yield health(parsed["LS:ImMcu - MCU Health And Performance"]
                .get(0, "IMMCU - MCU Health State"),
                "IMMCU")

check_info['skype.mcu'] = {
    'inventory_function'  : lambda parsed: inventory_wmi_table(parsed, required_tables = [
        "LS:DATAMCU - MCU Health And Performance",
        "LS:AVMCU - MCU Health And Performance",
        "LS:AsMcu - MCU Health And Performance",
        "LS:ImMcu - MCU Health And Performance",
    ]),
    'check_function'      : check_skype_mcu,
    'service_description' : "Skype MCU Health",
}


def check_skype_conferencing(item, params, parsed):
    # LS:CAA - Operations\CAA - Incomplete calls per sec
    # LS:USrv - Conference Mcu Allocator\USrv - Create Conference Latency (msec)
    # LS:USrv - Conference Mcu Allocator\USrv – Allocation Latency (msec)

    yield wmi_yield_raw_persec(parsed["LS:CAA - Operations"], item,
                               "CAA - Incomplete calls per sec",
                               label=" incomplete calls/sec",
                               perfvar="caa_incomplete_calls",
                               levels=params['incomplete_calls'])

    yield wmi_yield_raw_average(parsed["LS:USrv - Conference Mcu Allocator"], item,
                                "USrv - Create Conference Latency (msec)",
                                label=" create conference latency",
                                perfvar="usrv_create_conference_latency",
                                levels=params['create_conference_latency'])

    yield wmi_yield_raw_average(parsed["LS:USrv - Conference Mcu Allocator"], item,
                                "USrv - Allocation Latency (msec)",
                                label=" allocation latency",
                                perfvar="usrv_allocation_latency",
                                levels=params['allocation_latency'])


check_info['skype.conferencing'] = {
    'inventory_function'      : lambda table: inventory_wmi_table(table, required_tables=[
        "LS:CAA - Operations",
        "LS:USrv - Conference Mcu Allocator",
    ]),
    'check_function'          : check_skype_conferencing,
    'parse_function'          : lambda info: parse_wmi_table(info, key="instance"),
    'has_perfdata'            : True,
    'service_description'     : "Skype Conferencing",
    'includes'                : ['wmi.include'],
    'group'                   : 'skype_conferencing',
    'default_levels_variable' : 'skype_conferencing_defaultlevels'
}


def check_skype_sip_stack(item, params, parsed):
    # LS:SIP - Protocol\SIP - Average Incoming Message Processing Time
    # LS:SIP - Protocol\SIP - Incoming Responses Dropped /Sec
    # LS:SIP - Protocol\SIP - Incoming Requests Dropped /Sec
    # LS:USrv - DBStore\USrv - Queue Latency (msec)
    # LS:USrv - DBStore\USrv - Sproc Latency (msec)
    # LS:USrv - DBStore\USrv - Throttled requests/sec

    # LS:SIP - Responses\SIP - Local 503 Responses/sec
    # LS:SIP - Load Management\SIP - Incoming Messages Timed out
    # LS:SIP - Load Management\SIP - Average Holding Time For Incoming Messages
    # LS:SIP - Peers\SIP - Flow-controlled Connections
    # LS:SIP - Peers\SIP - Average Outgoing Queue Delay
    # LS:SIP - Peers(*)\SIP-Sends Timed-Out/sec
    yield wmi_yield_raw_average_timer(parsed["LS:SIP - Protocol"], item,
                                      "SIP - Average Incoming Message Processing Time",
                                      label=" avg incoming message processing time",
                                      perfvar="sip_message_processing_time",
                                      levels=params['message_processing_time'])

    yield wmi_yield_raw_persec(parsed["LS:SIP - Protocol"], item,
                               "SIP - Incoming Responses Dropped /Sec",
                               label=" incoming responses dropped/sec",
                               perfvar="sip_incoming_responses_dropped",
                               levels=params['incoming_responses_dropped'])

    yield wmi_yield_raw_persec(parsed["LS:SIP - Protocol"], item,
                               "SIP - Incoming Requests Dropped /Sec",
                               label=" incoming requests dropped/sec",
                               perfvar="sip_incoming_requests_dropped",
                               levels=params['incoming_requests_dropped'])

    yield wmi_yield_raw_average(parsed["LS:USrv - DBStore"], item,
                                "USrv - Queue Latency (msec)",
                                label=" ms queue latency",
                                perfvar="usrv_queue_latency",
                                perfscale=0.001,
                                levels=params['queue_latency'])

    yield wmi_yield_raw_average(parsed["LS:USrv - DBStore"], item,
                                "USrv - Sproc Latency (msec)",
                                label=" ms sproc latency",
                                perfvar="usrv_sproc_latency",
                                perfscale=0.001,
                                levels=params['sproc_latency'])

    yield wmi_yield_raw_persec(parsed["LS:USrv - DBStore"], item,
                               "USrv - Throttled requests/sec",
                               label=" throttled requests/sec",
                               perfvar="usrv_throttled_requests",
                               levels=params['throttled_requests'])

    yield wmi_yield_raw_persec(parsed["LS:SIP - Responses"], item,
                               "SIP - Local 503 Responses /Sec",
                               label=" local 503 responses/sec",
                               perfvar="sip_503_responses",
                               levels=params['local_503_responses'])

    yield wmi_yield_raw_counter(parsed["LS:SIP - Load Management"], item,
                                "SIP - Incoming Messages Timed out",
                                label=" incoming messages timed out",
                                perfvar="sip_incoming_messages_timed_out",
                                levels=params['timedout_incoming_messages'])

    yield wmi_yield_raw_average_timer(parsed["LS:SIP - Load Management"], item,
                                      "SIP - Average Holding Time For Incoming Messages",
                                      label=" avg holding time for incoming messages",
                                      perfvar="sip_avg_holding_time_incoming_messages",
                                      levels=params['holding_time_incoming'])

    yield wmi_yield_raw_counter(parsed["LS:SIP - Peers"], item,
                                "SIP - Flow-controlled Connections",
                                label=" flow-controlled connections",
                                perfvar="sip_flow_controlled_connections",
                                levels=params['flow_controlled_connections'])

    yield wmi_yield_raw_average_timer(parsed["LS:SIP - Peers"], item,
                                      "SIP - Average Outgoing Queue Delay",
                                      label=" avg outgoing queue delay",
                                      perfvar="sip_avg_outgoing_queue_delay",
                                      levels=params['outgoing_queue_delay'])

    yield wmi_yield_raw_persec(parsed["LS:SIP - Peers"], item,
                                "SIP - Sends Timed-Out /Sec",
                                label=" sends timed out/sec",
                                perfvar="sip_sends_timed_out",
                                levels=params['timedout_sends'])

    if "LS:SIP - Authentication" in parsed:
        yield wmi_yield_raw_persec(parsed["LS:SIP - Authentication"], item,
                                    "SIP - Authentication System Errors /Sec",
                                    label=" authentication errors/sec",
                                    perfvar="sip_authentication_errors",
                                    levels=params['authentication_errors'])


check_info['skype.sip_stack'] = {
    'inventory_function'      : lambda parsed: inventory_wmi_table(parsed, required_tables=[
        "LS:SIP - Protocol",
        "LS:USrv - DBStore",
        "LS:SIP - Responses",
        "LS:SIP - Load Management",
        "LS:SIP - Peers",
    ]),
    'check_function'          : check_skype_sip_stack,
    'has_perfdata'            : True,
    'service_description'     : "Skype SIP Stack",
    'group'                   : 'skype_sip',
    'default_levels_variable' : 'skype_sip_defaultlevels'
}


def check_skype_mediation_server(item, params, parsed):
    # LS:MediationServer - Health Indices\- Load Call Failure Index
    # LS:MediationServer - Global Counters\- Total failed calls caused by unexpected interaction from the Proxy
    # LS:MediationServer - Global Per Gateway Counters(*)\- Total failed calls caused by unexpected interaction from a gateway
    # LS:MediationServer - Media Relay\- Media Connectivity Check Failure

    yield wmi_yield_raw_counter(parsed["LS:MediationServer - Health Indices"], item,
                                "- Load Call Failure Index",
                                label=" load call failure index",
                                perfvar="mediation_load_call_failure_index",
                                levels=params["load_call_failure_index"])

    yield wmi_yield_raw_counter(parsed["LS:MediationServer - Global Counters"], item,
                                "- Total failed calls caused by unexpected interaction from the Proxy",
                                label=" failed calls because of proxy",
                                perfvar="mediation_failed_calls_because_of_proxy",
                                levels=params['failed_calls_because_of_proxy'])

    yield wmi_yield_raw_counter(parsed["LS:MediationServer - Global Per Gateway Counters"], item,
                                "- Total failed calls caused by unexpected interaction from a gateway",
                                label=" failed calls because of gateway",
                                perfvar="mediation_failed_calls_because_of_gateway",
                                levels=params['failed_calls_because_of_gateway'])

    yield wmi_yield_raw_counter(parsed["LS:MediationServer - Media Relay"], item,
                                "- Media Connectivity Check Failure",
                                label=" media connectivity check failure",
                                perfvar="mediation_media_connectivity_failure",
                                levels=params['media_connectivity_failure'])


check_info['skype.mediation_server'] = {
    'inventory_function'      : lambda parsed: inventory_wmi_table(parsed, required_tables=[
        "LS:MediationServer - Health Indices",
        "LS:MediationServer - Global Counters",
        "LS:MediationServer - Global Per Gateway Counters",
        "LS:MediationServer - Media Relay",
    ]),
    'check_function'          : check_skype_mediation_server,
    'has_perfdata'            : True,
    'service_description'     : "Skype Mediation Server",
    'group'                   : 'skype_mediation_server',
    'default_levels_variable' : 'skype_mediation_server_defaultlevels'
}


def check_skype_edge_auth(item, params, parsed):
    # LS:A/V Auth - Requests\- Bad Requests Received/sec
    yield wmi_yield_raw_persec(parsed["LS:A/V Auth - Requests"], item,
                               "- Bad Requests Received/sec",
                               label=" bad requests/sec",
                               perfvar="avauth_failed_requests",
                               levels=params['bad_requests'])


check_info['skype.edge_auth'] = {
    'inventory_function'      : lambda parsed: inventory_wmi_table(parsed, required_tables=[
        "LS:A/V Auth - Requests"]),
    'check_function'          : check_skype_edge_auth,
    'has_perfdata'            : True,
    'service_description'     : "Skype Edge Authentification",
    'group'                   : 'skype_edgeauth',
    'default_levels_variable' : 'skype_edgeauth_defaultlevels'
}

def check_skype_av_edge(item, params, parsed):
    # LS:A/V Edge - UDP Counters(*)\A/V Edge - Authentication Failures/sec
    # LS:A/V Edge - TCP Counters(*)\A/V Edge - Authentication Failures/sec
    # LS:A/V Edge - UDP Counters(*)\A/V Edge - Allocate Requests Exceeding Port Limit
    # LS:A/V Edge - TCP Counters(*)\A/V Edge - Allocate Requests Exceeding Port Limit
    # LS:A/V Edge - UDP Counters(*)\A/V Edge - Packets Dropped/sec
    # LS:A/V Edge - TCP Counters(*)\A/V Edge - Packets Dropped/sec
    yield wmi_yield_raw_persec(parsed["LS:A/V Edge - UDP Counters"], item,
                               "A/V Edge - Authentication Failures/sec",
                               label=" UDP auth failures/sec",
                               perfvar="edge_udp_failed_auth",
                               levels=params['authentication_failures'])

    yield wmi_yield_raw_persec(parsed["LS:A/V Edge - TCP Counters"], item,
                               "A/V Edge - Authentication Failures/sec",
                               label=" TCP auth failures/sec",
                               perfvar="edge_tcp_failed_auth",
                               levels=params['authentication_failures'])

    yield wmi_yield_raw_persec(parsed["LS:A/V Edge - UDP Counters"], item,
                               "A/V Edge - Allocate Requests Exceeding Port Limit/sec",
                               label=" UDP allocate requests > port limit/sec",
                               perfvar="edge_udp_allocate_requests_exceeding_port_limit",
                               levels=params['allocate_requests_exceeding'])

    yield wmi_yield_raw_persec(parsed["LS:A/V Edge - TCP Counters"], item,
                               "A/V Edge - Allocate Requests Exceeding Port Limit/sec",
                               label=" TCP allocate requests > port limit/sec",
                               perfvar="edge_tcp_allocate_requests_exceeding_port_limit",
                               levels=params['allocate_requests_exceeding'])

    yield wmi_yield_raw_persec(parsed["LS:A/V Edge - UDP Counters"], item,
                               "A/V Edge - Packets Dropped/sec",
                               label=" UDP packets dropped/sec",
                               perfvar="edge_udp_packets_dropped",
                               levels=params['packets_dropped'])

    yield wmi_yield_raw_persec(parsed["LS:A/V Edge - TCP Counters"], item,
                               "A/V Edge - Packets Dropped/sec",
                               label=" TCP packets dropped/sec",
                               perfvar="edge_tcp_packets_dropped",
                               levels=params['packets_dropped'])


check_info['skype.edge'] = {
    'inventory_function'      : lambda parsed: inventory_wmi_table(parsed, required_tables=[
        "LS:A/V Edge - TCP Counters",
        "LS:A/V Edge - UDP Counters"]),
    'check_function'          : check_skype_av_edge,
    'has_perfdata'            : True,
    'service_description'     : "Skype AV Edge %s",
    'group'                   : 'skype_edge',
    'default_levels_variable' : 'skype_edge_defaultlevels'
}


def check_skype_data_proxy(item, params, parsed):
    # LS:DATAPROXY - Server Connections(*)\DATAPROXY - Current count of server connections that are throttled
    # LS:DATAPROXY - Server Connections(*)\DATAPROXY - System is throttling
    yield wmi_yield_raw_counter(parsed["LS:DATAPROXY - Server Connections"], item,
                                "DATAPROXY - Current count of server connections that are throttled",
                                label=" server connections throttled",
                                perfvar="dataproxy_connections_throttled",
                                levels=params['throttled_connections'])

    throttling = int(parsed["LS:DATAPROXY - Server Connections"].get(0, "DATAPROXY - System is throttling"))

    if throttling != 0:
        yield 2, "System is throttling"


check_info['skype.data_proxy'] = {
    'inventory_function'      : lambda parsed: inventory_wmi_table(parsed, required_tables=[
        "LS:DATAPROXY - Server Connections"]),
    'check_function'          : check_skype_data_proxy,
    'has_perfdata'            : True,
    'service_description'     : "Skype Data Proxy %s",
    'group'                   : 'skype_proxy',
    'default_levels_variable' : 'skype_proxy_defaultlevels'
}


def check_skype_xmpp_proxy(item, params, parsed):
    # LS:XmppFederationProxy - Streams\XmppFederationProxy - Failed outbound stream establishes/sec
    # LS:XmppFederationProxy - Streams\XmppFederationProxy - Failed inbound stream establishes/sec
    yield wmi_yield_raw_persec(parsed["LS:XmppFederationProxy - Streams"], item,
                               "XmppFederationProxy - Failed outbound stream establishes/sec",
                               label=" failed outbound streams",
                               perfvar="xmpp_failed_outbound_streams",
                               levels=params['failed_outbound_streams'])

    yield wmi_yield_raw_persec(parsed["LS:XmppFederationProxy - Streams"], item,
                               "XmppFederationProxy - Failed inbound stream establishes/sec",
                               label=" failed inbound streams",
                               perfvar="xmpp_failed_inbound_streams",
                               levels=params['failed_inbound_streams'])


check_info['skype.xmpp_proxy'] = {
    'inventory_function'      : lambda parsed: inventory_wmi_table(parsed, required_tables=[
        "LS:XmppFederationProxy - Streams"]),
    'check_function'          : check_skype_xmpp_proxy,
    'has_perfdata'            : True,
    'service_description'     : "Skype XMPP Proxy",
    'group'                   : 'skype_xmpp',
    'default_levels_variable' : 'skype_xmpp_defaultlevels'
}


def check_skype_mobile(item, params, parsed):
    # LS:WEB - UCWA
    # LS:WEB - Throttling and Authentication\WEB - Total Requests in Processing

    for instance, name in [("AndroidLync", "Android"),
                           ("iPadLync", "iPad"),
                           ("iPhoneLync", "iPhone"),
                           ("LyncForMac", "Mac")]:
        status, infotext, perfdata =\
            wmi_yield_raw_counter(parsed["LS:WEB - UCWA"], instance,
                                  "UCWA - Active Session Count",
                                  label=" active",
                                  perfvar="ucwa_active_sessions")
        if status != 3:
            if perfdata:
                perfdata = list(perfdata[0])
                perfdata[0] += "_" + name.lower()
                perfdata = [perfdata]

            yield status, "%s: %s" % (name, infotext), perfdata
        else:
            # add a perfcounter with 0 sessions for all missing devices so we can
            # still properly build a combined graph for active sessions
            yield 0, None, [("active_sessions_%s" % name.lower(), 0)]

    yield wmi_yield_raw_counter(parsed["LS:WEB - Throttling and Authentication"], item,
                                "WEB - Total Requests In Processing",
                                label=" requested",
                                perfvar="web_requests_processing",
                                levels=params['requests_processing'])

check_info['skype.mobile'] = {
    'inventory_function'      : lambda parsed: inventory_wmi_table(parsed, required_tables=[
        "LS:WEB - UCWA",
        "LS:WEB - Throttling and Authentication",
    ]),
    'check_function'          : check_skype_mobile,
    'has_perfdata'            : True,
    'service_description'     : "Skype Mobile Sessions",
    'group'                   : 'skype_mobile',
    'default_levels_variable' : 'skype_mobile_defaultlevels'
}
