Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not work with Python 3.6 #12

Open
bmccorkle opened this issue Dec 20, 2019 · 0 comments
Open

Does not work with Python 3.6 #12

bmccorkle opened this issue Dec 20, 2019 · 0 comments

Comments

@bmccorkle
Copy link

This script doesn't seem to work with python 3.6. Mainly, print statements now need parenthesis and it seems a 'list' function needed added when getting the host, service, and notifications. I seemed to be able to get it running with the following changes. Not a coding expert but thought I'd post what I had here for you to review...

#!/usr/bin/env python

# pip install icinga2api
# https://github.com/tobiasvdk/icinga2api
import os
import argparse

from icinga2api.client import Client

# use a helper to fetch our cut down object names
def getObjects(client, type_name):
    if 'Notification' == type_name:
        return client.objects.list(type_name, attrs=['__name'])
    else:
        return client.objects.list(type_name, attrs=['__name', 'groups'])

# use a helper to convert the full blown object dictionary into a list of __name elements
def getNameList(objects, group = ''):
    if group:
        return map(lambda x : x['attrs']['__name'], filter(lambda y : group in y['attrs']['groups'], objects))
    else:
        return map(lambda x : x['attrs']['__name'], objects)

def diffList(l1,l2):
    l2 = set(l2)
    return [x for x in l1 if x not in l2]

# add connection details
parser = argparse.ArgumentParser()
parser.add_argument("-H", "--hosturi", help="URL to icinga2 API", required=True)
parser.add_argument("-u", "--username", help="Username to connect to API", required=True)
parser.add_argument("-p", "--password", help="Password to connect to API", required=True)
parser.add_argument("-g", "--group", help="Group to filter", required=False)
args = parser.parse_args()

client = Client(args.hosturi, args.username, args.password)

hosts = getObjects(client, 'Host')
services = getObjects(client, 'Service')
notifications = getObjects(client, 'Notification')

#debug
#print (hosts)
#print (services)
#print (notifications)


if not args.group:
    h_names = list(getNameList(hosts, False))
    s_names = list(getNameList(services, False))
else:
    h_names = list(getNameList(hosts, args.group))
    s_names = list(getNameList(services, args.group))

n_names = list(getNameList(notifications))

found_h_names = []
found_s_names = []

for n in n_names:
    split_arr = n.split("!")
    if len(split_arr) == 2:
        for h in h_names:
            # debug
            # print (n + " " + h)
            if h in split_arr[0]:
                found_h_names.append(h)

    if len(split_arr) == 3:
        for s in s_names:
            # debug
            # print (n + " " + s)

            # rebuild the matched full service name with host!service
            split_s_full_name = split_arr[0] + "!" + split_arr[1]

            if s in split_s_full_name:
                found_s_names.append(s)


print("Hosts without notification")
print (", ".join(diffList(h_names, found_h_names)))
print ("")
print("Services without notification")
print (", ".join(diffList(s_names, found_s_names)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant