Acknowledging Icinga2 service problems with python3

#!/usr/bin/env python3

import sys
import os
import argparse
import json
import requests

from requests.packages.urllib3.exceptions import InsecureRequestWarning

from pygments import highlight
from pygments.lexers import JsonLexer
from pygments.formatters import TerminalFormatter

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

parser = argparse.ArgumentParser(description='Script to ack host services')

parser.add_argument(
    '--host',
    help='Host to ack'
)

parser.add_argument(
    '--comment',
    default='ack',
    help='Set a comment'
)

parser.add_argument(
    '--author',
    default=os.environ['USER'],
    help='Set an author'
)

args = parser.parse_args()

if args.host is None:
    parser.print_help()
    sys.exit(1)

ICINGA_USER = ''
ICINGA_PASS = ''
ICINGA_URL = 'https://icinga2.example.com:6556/v1/actions/acknowledge-problem'

headers = {
    'Accept': 'application/json'
}

data = {
    'type': 'Service',
    'filter': f'match("{args.host}",host.name) && service.state!=0 && service.acknowledgement == 0',
    'author': args.author,
    'comment': args.comment,
    'notify': False
}

r = requests.post(
    ICINGA_URL,
    verify=False,
    auth=(ICINGA_USER, ICINGA_PASS),
    headers=headers,
    data=json.dumps(data),
)

json_object = json.loads(r.text)
json_str = json.dumps(json_object, indent=4, sort_keys=True)
print(highlight(json_str, JsonLexer(), TerminalFormatter()))
Published
Categorized as General