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

tcp_conn_stats_states = [
 ( "ESTABLISHED", "01"),  # connection up and passing data
 ( "SYN_SENT",    "02"),  # session has been requested by us; waiting for reply from remote endpoint
 ( "SYN_RECV",    "03"),  # session has been requested by a remote endpoint for a socket on which we were listening
 ( "LAST_ACK",    "09"),  # our socket is closed; remote endpoint has also shut down; we are waiting for a final acknowledgement
 ( "CLOSE_WAIT",  "08"),  # remote endpoint has shut down; the kernel is waiting for the application to close the socket
 ( "TIME_WAIT",   "06"),  # socket is waiting after closing for any packets left on the network
 ( "CLOSED",      "07"),  # socket is not being used (FIXME. What does mean?)
 ( "CLOSING",     "0B"),  # our socket is shut down; remote endpoint is shut down; not all data has been sent
 ( "FIN_WAIT1",   "04"),  # our socket has closed; we are in the process of tearing down the connection
 ( "FIN_WAIT2",   "05"),  # the connection has been closed; our socket is waiting for the remote endpoint to shut down
 ( "BOUND",       None),  # Socket did a bound() but TCP stack not yet active (Solaris)
]


tcp_conn_stats_default_levels = { }

def inventory_tcp_conn_stats(info):
    if len(info) > 0:
        return [ (None, 'tcp_conn_stats_default_levels') ]

def check_tcp_conn_stats(item, params, info):
    stats = dict(info)
    hit = False
    for stat_state, hex in tcp_conn_stats_states:
        num = int(stats.get(stat_state, stats.get(hex, 0)))
        state = 0
        perf = [stat_state, num]
        if num > 0: # Only check positive counts
            hit = True
            infotext = "%s: %d" % (stat_state, num)
            levels = params.get(stat_state)
            if levels:
                warn, crit = levels
                perf.append(warn)
                perf.append(crit)
                if num >= crit:
                    state = 2
                    infotext += " (critical at %d)" % crit
                elif num >= warn:
                    state = 1
                    infotext += " (warning at %d)" % warn
            yield state, infotext, [ tuple(perf) ]
        else:
            yield 0, None, [ tuple(perf) ]

    if not hit:
        yield 0, "Currently no TCP connections"


check_info["tcp_conn_stats"] = {
    'check_function':          check_tcp_conn_stats,
    'inventory_function':      inventory_tcp_conn_stats,
    'service_description':     'TCP Connections',
    'has_perfdata':            True,
    'group':                   'tcp_conn_stats',
}
