check_supervisord.py
def create_output(data, options):
    """
    Create Nagios and human readable supervisord statuses.

    :param data: supervisord XML-RPC call result.
    :type data: dict.
    :param options: parsed commandline arguments.
    :type options: optparse.Values.
    :return: Nagios and human readable supervisord statuses and exit code.
    :rtype: (str, int).
    """

    output = {}
    programs = map(strip, options.programs.strip().split(",")) if options.programs else map(lambda x: x["name"], data)

    for program in programs:
        try:
            program_data = filter(lambda x: x["name"] == program, data)[0]
            output.update({
                program: {
                    "name": program,
                    "template": STATE2TEMPLATE[program_data["statename"]],
                    "status": program_data["spawnerr"] if program_data["spawnerr"] else program_data["statename"],
                }
            })
        except IndexError:
            output.update({
                program: {
                    "name": program,
                    "template": "unknown",
                    "status": "",
                }
            })

    # getting main status for check (for multiple check need to get main status by priority)
    statuses = [status[0] for status in sorted([(status, OUTPUT_TEMPLATES[status]["priority"]) for status in list(set([output[d]["template"] for d in output.keys()]))], key=lambda x: x[1])]
    # if no programs found or configured by supervisord set status ok and custom message
    status = statuses[0] if statuses else EXIT_CODE_OK
    text = ", ".join([OUTPUT_TEMPLATES[output[program]["template"]]["text"].format(**output[program]) for program in sorted(output.keys(), key=lambda x: OUTPUT_TEMPLATES[output[x]["template"]]["priority"])]) if statuses else "No program configured/found"

    # create exit code (unknown if something happened wrong)
    code = EXIT_CODES.get(status, EXIT_CODE_UNKNOWN)

    # return full status string with main status for multiple programs and all programs states
    return "{status}: {output}\n".format(**{
        "status": status.upper(),
        "output": text,
    }), code
def create_output(data, options):
    """
    Create Nagios and human readable supervisord statuses.

    :param data: supervisord XML-RPC call result.
    :type data: dict.
    :param options: parsed commandline arguments.
    :type options: optparse.Values.
    :return: Nagios and human readable supervisord statuses and exit code.
    :rtype: (str, int).
    """

    output = {}
    programs = map(strip, options.programs.strip().split(",")) if options.programs else map(lambda x: x["name"], data)

    for program in programs:
        try:
            program_data = filter(lambda x: x["name"] == program, data)[0]
            output.update({
                program: {
                    "name": program,
                    "template": STATE2TEMPLATE[program_data["statename"]],
                    "status": program_data["spawnerr"] if program_data["spawnerr"] else program_data["statename"],
                }
            })
        except IndexError:
            output.update({
                program: {
                    "name": program,
                    "template": "unknown",
                    "status": "",
                }
            })

    # getting main status for check (for multiple check need to get main status by priority)
    statuses = [status[0] for status in sorted([(status, OUTPUT_TEMPLATES[status]["priority"]) for status in list(set([output[d]["template"] for d in output.keys()]))], key=lambda x: x[1])]
    # if no programs found or configured by supervisord set status ok and custom message
    status = statuses[0] if statuses else EXIT_CODE_OK
    text = ", ".join([OUTPUT_TEMPLATES[output[program]["template"]]["text"].format(**output[program]) for program in sorted(output.keys(), key=lambda x: OUTPUT_TEMPLATES[output[x]["template"]]["priority"])]) if statuses else "No program configured/found"

    # create exit code (unknown if something happened wrong)
    code = EXIT_CODES.get(status, EXIT_CODE_UNKNOWN)

    # return full status string with main status for multiple programs and all programs states
    return "{status}: {output}\n".format(**{
        "status": status.upper(),
        "output": text,
    }), code
Create Nagios and human readable supervisord statuses.

    :param data: supervisord XML-RPC call result.
    :type data: dict.
    :param options: parsed commandline arguments.
    :type options: optparse.Values.
    :return: Nagios and human readable supervisord statuses and exit code.
    :rtype: (str, int).