#!/usr/bin/env python3
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# Auto Acknowledge Problems

# (c) 2023 Heinlein Support GmbH
#          Robert Sander <r.sander@heinlein-support.de>

# 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 os
import checkmkapi

comment = os.environ['PARAMETER_COMMENT']
username = os.environ.get('PARAMETER_USERNAME', 'automation')
only_host = os.environ.get('PARAMETER_HOST_PROBLEMS', False)
only_service = os.environ.get('PARAMETER_SERVICE_PROBLEMS', False)

host_problem = (os.environ['ALERT_WHAT'] == 'HOST')
service_problem = (os.environ['ALERT_WHAT'] == 'SERVICE')
hostname = os.environ['ALERT_HOSTNAME']

cmkapi = checkmkapi.CMKRESTAPI(api_user=username)

if host_problem:
    if only_service:
        print("Not acking host problem.")
    else:
        print(f"ACK {hostname}: {comment}")
        cmkapi.acknowledge_host_problem(hostname, comment)
if service_problem:
    if only_host:
        print("Not acking service problem.")
    else:
        service = os.environ['ALERT_SERVICEDESC']
        print(f"ACK {hostname} {service}: {comment}")
        cmkapi.acknowledge_service_problem(hostname, service, comment)
