#!/usr/bin/env python3
# Enterprise Alert
# -*- encoding: utf-8; py-indent-offset: 4 -*-

# (c) 2024 Derdack GmbH
#          Derdack Support <support@derdack.com>

# This 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.

import requests
import json
import os
import sys
from cmk.notification_plugins.utils import get_password_from_env_or_context

# api_url = "https://your-enterprise-alert-server/EAWebService/rest/events"
# api_key = ""

def main():
    context = dict([ (var[7:], value)
                      for (var, value) in os.environ.items()
                      if var.startswith("NOTIFY_")])

    message    = get_text(context)

    url = context["PARAMETER_URL"]
    password = get_password_from_env_or_context(
        key="NOTIFY_PARAMETER_PASSWORD"
    )

    if message['NotificationType'] == "RECOVERY":
            return send_update(url, password, message)

    return send_alert(url, password, message)

def get_text(context):

    host_name = context['HOSTNAME']
    notification_type = context['NOTIFICATIONTYPE']
    service_state = ''
    service_desc = ''
    service_output = ''
    host_state = ''
    notification_comment = ''
    contact_name = context['CONTACTNAME']
    contact_alias = context['CONTACTALIAS']
    contact_email = context['CONTACTEMAIL']
    contact_pager = context['CONTACTPAGER'].replace(' ', '')
    description = notification_type + ' on ' + host_name
    service_problem_id = ''
    
    host_problem_id = context.get('HOSTPROBLEMID') or ''
    date_time = context['SHORTDATETIME']

    # Prepare Default information and Type PROBLEM, RECOVERY
    if context['WHAT'] == 'SERVICE':
        if notification_type in [ "PROBLEM", "RECOVERY" ]:
            service_state = context['SERVICESTATE']
            service_desc = context['SERVICEDESC']
            service_output = context['SERVICEOUTPUT']
            description += ' (' + service_desc + ')'
            service_problem_id = context['SERVICEPROBLEMID']
        else:
            service_desc = context['SERVICEDESC']
            description += ' (' + service_desc + ')'

    else:
        if notification_type in [ "PROBLEM", "RECOVERY" ]:
            host_state = context['HOSTSTATE']
            description += ' (' + host_state + ')'
        else:
            description += ' (' + host_state + ')'

    message = {
        'Title': description,
        'HostName': host_name,
        'NotificationType': notification_type,
        'ServiceState': service_state,
        'ServiceDescription': service_desc,
        'ServiceOutput': service_output,
        'HostState': host_state,
        'NotificationComment': notification_comment,
        'ContactName': contact_name,
        'ContactAlias': contact_alias,
        'ContactEmail': contact_email,
        'ContactPager': contact_pager,
        'HostProblemId': host_problem_id,
        'ServiceProblemId': service_problem_id,
        'DateTime': date_time,
        'ExternalID': host_name + '-' + host_problem_id + '-' + service_problem_id
    }

    return message

def send_alert(url, password, message):

    resp = requests.post(url + '?apiKey=' + password, params=None, data=json.dumps(message))

    if resp.status_code == 201:
        return 0

    sys.stderr.write(resp.text)
    return 2

def send_update(url, password, message):

    resp = requests.post(url + '/' + message['ExternalID'] + '/reset?apiKey=' + password, params=None, data=json.dumps(message))

    if resp.status_code == 201:
        return 0

    sys.stderr.write(resp.text)
    return 2

sys.exit(main())

