#!/usr/bin/env python3
# -*- coding: utf-8; py-indent-offset: 4 -*-
#
# Author:  Linuxfabrik GmbH, Zurich, Switzerland
# Contact: info (at) linuxfabrik (dot) ch
#          https://www.linuxfabrik.ch/
# License: The Unlicense, see LICENSE file.

# https://github.com/Linuxfabrik/monitoring-plugins/blob/main/CONTRIBUTING.md

"""See the plugins's README for more details.
"""

import argparse                        # pylint: disable=C0413
import socket                          # pylint: disable=C0413
import sys                             # pylint: disable=C0413
import textwrap                        # pylint: disable=C0413
import urllib.parse                    # pylint: disable=C0413
import urllib.request                  # pylint: disable=C0413

import lib.args                       # pylint: disable=C0413
import lib.base                       # pylint: disable=C0413
import lib.url                        # pylint: disable=C0413
from lib.globals import STATE_UNKNOWN # pylint: disable=C0413

__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2026040801'

DESCRIPTION = 'Sends host notifications using the RocketChat API.'

TIMEOUT = 4 # seconds

def parse_args():
    """Parse command line arguments using argparse.
    """
    parser = argparse.ArgumentParser(description=DESCRIPTION)

    parser.add_argument(
        '--datetime',
        help='Set the message timestamp.',
        default=None,
        type=str,
        dest='DATETIME',
        required=True,
    )

    parser.add_argument(
        '--host-displayname',
        help='Set the display name of the host.',
        default=None,
        type=str,
        dest='HOST_DISPLAYNAME',
        required=True,
    )

    parser.add_argument(
        '--host-output',
        help='Set the host output.',
        default=None,
        type=str,
        dest='HOST_OUTPUT',
    )

    parser.add_argument(
        '--host-state',
        help='Set the host state.',
        default=None,
        type=str,
        dest='HOST_STATE',
        required=True,
    )

    parser.add_argument(
        '--hostname',
        help='Set the hostname.',
        default=None,
        type=str,
        dest='HOSTNAME',
    )

    parser.add_argument(
        '--icingaweb2-url',
        help='Set the Icinga Web 2 URL, for example "https://example.com/icingaweb2".',
        default=None,
        type=str,
        dest='ICINGAWEB2_URL',
    )

    parser.add_argument(
        '--notification-author',
        help='Set the author of the comment.',
        default=None,
        type=str,
        dest='NOTIFICATION_AUTHOR',
    )

    parser.add_argument(
        '--notification-comment',
        help='Set the comment.',
        default=None,
        type=str,
        dest='NOTIFICATION_COMMENT',
    )

    parser.add_argument(
        '--rocketchat-mentions',
        help='Set the Rocket.Chat Mentions (repeating).',
        type=str,
        action='append',
        dest='ROCKETCHAT_MENTIONS',
    )

    parser.add_argument(
        '--rocketchat-url',
        help='Set the Rocket.Chat Webhook API URL.',
        type=str,
        dest='ROCKETCHAT_URL',
        required=True,
    )

    parser.add_argument(
        '-V', '--version',
        action='version',
        version=f'%(prog)s: v{__version__} by {__author__}'
    )

    args, _ = parser.parse_known_args()
    return args


def main():
    """The main function. Hier spielt die Musik.
    """

    # parse the command line
    try:
        args = parse_args()
    except SystemExit:
        sys.exit(STATE_UNKNOWN)

    message = (
        f'{args.HOST_STATE:.4}:'
        f' {args.HOST_DISPLAYNAME}'
        f' ({args.DATETIME})\n'
        f'```\n'
        f'{args.HOST_OUTPUT}\n'
        f'```'
    )

    if args.NOTIFICATION_COMMENT:
        comment = f'\nCOMMENT: {args.NOTIFICATION_COMMENT} ({args.NOTIFICATION_AUTHOR})'
        message += comment

    notifying_hostname = socket.gethostname().split('.', 1)[0] # short hostname of the icinga2 server sending the notif
    icingaweb2_url = None
    if args.ICINGAWEB2_URL and args.HOSTNAME:
        params = urllib.parse.urlencode(
            {
                'name': args.HOSTNAME,
            },
            quote_via=urllib.parse.quote
        )

        icingaweb2_url = urllib.parse.urljoin(
            f'{args.ICINGAWEB2_URL}/',
            f'icingadb/host?{params}'
        )

    if args.ROCKETCHAT_MENTIONS:
        message += '\n'
        for mention in args.ROCKETCHAT_MENTIONS:
            message += f'@{mention} '

    if args.HOST_STATE == 'UP':
        icon = ':host_up:'
    elif args.HOST_STATE == 'DOWN':
        icon = ':host_down:'
    else:
        icon = ':Pingu:'

    if icingaweb2_url:
        data = {
            "icon_emoji": icon,
            "text": message,
            "attachments": [
                {
                    "title": notifying_hostname,
                    "title_link": icingaweb2_url,
                }
            ]
        }
    else:
        data = {
            "emoji": icon,
            "text": message
        }

    lib.base.coe(lib.url.fetch_json(
        args.ROCKETCHAT_URL,
        header={
            'Content-Type': 'application/json',
        },
        data=data,
        encoding='serialized-json',
        extended=True,
        timeout=TIMEOUT,
    ))


if __name__ == '__main__':
    try:
        main()
    except Exception:   # pylint: disable=W0703
        lib.base.cu()
