#!/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_ftp_arguments(params):
    if type(params) == tuple:
        host, settings = params
    else:
        host = "$HOSTADDRESS$"
        settings = params

    args = ' -H %s' % quote_shell_string(host)

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

    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 "refuse_state" in settings:
        args += ' -r %s' % settings["refuse_state"]

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

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

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

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

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

    return args


def check_ftp_get_item(params):
    if type(params) == tuple:
        return "FTP " + params[0]
    else:
        if "port" in params and params["port"] != 21:
            return "FTP Port " + str(params["port"])
        return "FTP"

active_check_info['ftp'] = {
    "command_line"        : '$USER1$/check_ftp $ARG1$',
    "argument_function"   : check_ftp_arguments,
    "service_description" : check_ftp_get_item,
    "has_perfdata"        : True,
}

