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

def check_http_arguments(params):
    description, settings = params
    args = ''

    if "vhost" in settings:
        args += ' -H %s' % quote_shell_string(settings["vhost"])

    if "virthost" in settings:
        vhost, omit_ip = settings["virthost"]
        args += ' -H %s' % quote_shell_string(vhost)
    else:
        omit_ip = False

    if "uri" in settings:
        args += ' -u %s' % quote_shell_string(settings["uri"])

    if "port" in settings:
        args += ' -p %d' % settings["port"]

    # Use the address family of the monitored host by default
    address_family = settings.get("address_family")
    if address_family == None:
        address_family = is_ipv6_primary(g_hostname) and "ipv6" or "ipv4"

    if address_family == "ipv6":
        args += " -6"
        address = "$_HOSTADDRESS_6$"
    else:
        address = "$_HOSTADDRESS_4$"

    ssl = settings.get("ssl")
    if ssl in [ True, "auto" ]:
        args += ' --ssl'
    elif ssl:
        args += ' --ssl=%s' % quote_shell_string(ssl)

    if "cert_host" in settings:
        omit_ip = True
        if settings.get("sni"):
            args += ' -H %s' % quote_shell_string(settings['cert_host'])
        else:
            args += ' -H ' + address
            args += ' -I %s' % quote_shell_string(settings['cert_host'])

    if "cert_days" in settings:
        # legacy behavior
        if type(settings["cert_days"]) == int:
            args += ' -C %d' % settings["cert_days"]
        else:
            warn, crit = settings["cert_days"]
            args += ' -C %d,%d' % (warn, crit)

    if settings.get("sni"):
        args += ' --sni'

    if "response_time" in settings:
        args += ' -w %f -c %f' % (
            settings["response_time"][0]/1000.0,
            settings["response_time"][1]/1000.0)

    if "timeout" in settings:
        args += ' -t %d' % settings["timeout"]

    if "user_agent" in settings:
        args += ' -A %s' % quote_shell_string(settings["user_agent"])

    for header in settings.get("add_headers", []):
        args += ' -k %s' % quote_shell_string(header)

    if "auth" in settings:
        auth = settings["auth"]
        args += ' -a %s:%s' % (quote_shell_string(auth[0]), quote_shell_string(auth[1]))

    if "proxy_auth" in settings:
        auth = settings["proxy_auth"]
        args += ' -b %s:%s' % (quote_shell_string(auth[0]), quote_shell_string(auth[1]))

    if "onredirect" in settings:
        args += ' --onredirect=%s' % settings["onredirect"]

    if "expect_response" in settings:
        args += ' -e %s' % quote_shell_string(",".join(settings["expect_response"]))

    if "expect_string" in settings:
        args += ' -s %s' % quote_shell_string(settings["expect_string"])

    if "expect_response_header" in settings:
        args += ' -d %s' % quote_shell_string(settings["expect_response_header"])

    if "expect_regex" in settings:
        if len(settings['expect_regex']) >= 4 and settings['expect_regex'][3]:
            args += ' -l '
        if settings['expect_regex'][1]:
            args += ' -R'
        else:
            args += ' -r'
        args += ' %s' % quote_shell_string(settings['expect_regex'][0])
        if settings['expect_regex'][2]:
            args += ' --invert-regex'

    if settings.get("extended_perfdata"):
        args += ' --extended-perfdata'

    if "post_data" in settings:
        data, content_type = settings["post_data"]
        args += ' -P %s -T %s' % \
                (quote_shell_string(data),
                 quote_shell_string(content_type))

    if "method" in settings:
        args += ' -j %s' % settings["method"]

    if settings.get("no_body"):
        args += ' --no-body'

    if "page_size" in settings:
        args += ' -m %d:%d' % settings["page_size"]

    if "max_age" in settings:
        args += ' -M %d' % settings["max_age"]

    if settings.get("urlize"):
        args += ' -L'

    if not omit_ip:
        args = " -I " + address + args
    return args

def check_http_desc(params):
    if params[0].startswith("^"):
        return params[0][1:]
    #if params[1].get("ssl"):
    #    return "HTTPS %s" % params[0]
    return "HTTP %s" % params[0]

active_check_info['http'] = {
    "command_line"        : '$USER1$/check_http $ARG1$',
    "argument_function"   : check_http_arguments,
    "service_description" : check_http_desc,
    "has_perfdata"        : True,
}

