def test_icingaNotification_getOutput1():
    o = test_objects.icingaNotifGen(1).getObj("list")[0]
    o["_source"]["icinga"]["check_result"]["state"] = "44"
    o["_source"]["icinga"]["check_result"]["vars_before"]["state"] = "55"
    o["_source"]["icinga"]["notification_type"] = "PROBLEM"
    assert (icingaNotification.icingaNotification(o, "user").getNormalOutput()
            == "test.ls.intra - CRITICAL: HOST dummy output. test text")
def test_icingaNotification_getOutput2():
    o = test_objects.icingaNotifGen(1).getObj("list")[0]
    o["_source"]["icinga"]["notification_type"] = "RECOVERY"
    o["_source"]["icinga"]["check_result"]["state"] = 0
    assert (icingaNotification.icingaNotification(
        o,
        "user").getNormalOutput() == "test.ls.intra - OK: HOST -  test text")
Exemple #3
0
    def __init__(self,
                 count=2000,
                 serviceCount=0,
                 hostCount=0,
                 notificationType="PROBLEM"):
        self.notifyRawList = list()
        self.notifyClassList = list()

        # generate
        for i in range(0, count):
            obj = {
                "_source": {
                    "icinga": {
                        "a": "1",
                        "b": str(random.random()),
                        "d": ["one", "two"],
                        "check_result": {
                            "output": "dummy output",
                            "state": i % 4,
                            "vars_before": {
                                "state": i % 4,
                                "state_type": "dummy state type",
                            },
                        },
                        "text": "test text",
                        "notification_type": notificationType,
                        "users": ["testuser"],
                    }
                }
            }
            if serviceCount > 0:
                obj["_source"]["icinga"]["service"] = "test.ls.intra" + str(
                    i % serviceCount) + "!service"
            obj["_source"]["icinga"]["host"] = "test.ls.intra" + str(i % hostCount) if hostCount > 0 else "test.ls" \
                                                                                                          ".intra"
            self.notifyRawList.append(obj)
            self.notifyClassList.append(
                icingaNotification.icingaNotification(obj, "dummy"))
def handleNotifications(
    notificationsToHandle,
    icingaUsers,
    user,
    smsModem,
    smtpServerHost,
    lastCall,
    callModemObj,
    slackObj,
    icingaWebUrl,
):
    """ Function which takes notifications, parses them and then send them to all of coresponding users """
    if ((user is None) or (user.lower() not in icingaUsers is None)
            or ("vars" not in icingaUsers[user.lower()])
            or (icingaUsers[user.lower()] is None) or
        ("notification_options" not in icingaUsers[user.lower()]["vars"]) or
        (icingaUsers[user.lower()]["vars"]["notification_options"] is None)):
        try:
            if icingaUsers[user.lower()]["vars"]["ignore_notificator"] is True:
                logging.debug("Not handling notifications for user %s", user)
            else:
                logging.info(
                    "Cannot handle notifications, something is wrong for this user.."
                )
        except KeyError:
            logging.info(
                "Cannot handle notifications, something is wrong for this user.."
            )
        finally:
            return (1, lastCall)

    options = icingaUsers[user.lower()]["vars"]["notification_options"]

    # Init some things
    ret = -1
    # Iterrate over sending types to correctly filter this shit.
    for nType, states in options.items():
        if ret == -1:
            ret = 0

        message = ""
        notificationsList = list()

        # Iterate and filter notifications, fill list
        for notification in notificationsToHandle:
            icingaNotifObj = icn.icingaNotification(notification, user)

            logging.debug("Notification:")
            logging.debug("\t type: %s",
                          icingaNotifObj.notificationType.lower())
            logging.debug("\t state: %s",
                          icingaNotifObj.notificationState.lower())
            logging.debug("\t stateBefore: %s",
                          icingaNotifObj.notificationStateBefore.lower())
            logging.debug("\t %s : %s", nType, states)

            # filter notifications for type & user
            # if type is problem and state match, append
            if (icingaNotifObj.notificationType.lower() == "problem"
                    and icingaNotifObj.notificationState.lower() in states):
                notificationsList.append(icingaNotifObj)

            # if type is recovery, and previous state is in states, append
            elif (icingaNotifObj.notificationType.lower() == "recovery"
                  and icingaNotifObj.notificationStateBefore.lower() in states
                  and icingaNotifObj.notificationState.lower() in states):
                notificationsList.append(icingaNotifObj)

            # if type is something else, and matches, append
            elif icingaNotifObj.notificationType.lower() in states:
                notificationsList.append(icingaNotifObj)

            else:
                logging.debug("States not found in user, skipping current run")
                continue

        # Parse notifications and send them
        # If there are some, parse them and send.
        if len(notificationsList) != 0:
            notificationOutput = parsing.parseNotifications(
                notificationsList, nType, icingaWebUrl)

            # Type resolution
            if nType == "sms":
                message = "\n".join(notificationOutput)
                ret += sending.sendSMS(smsModem, message,
                                       icingaUsers[user.lower()])
            if nType == "email":
                ret += sending.sendMail(smtpServerHost, notificationOutput,
                                        icingaUsers[user.lower()])
            if nType == "slack":
                ret += sending.sendSlackMessage(slackObj, notificationOutput,
                                                icingaUsers[user.lower()])
            if nType == "call":
                (r, lc) = sending.sendCall(lastCall, icingaUsers[user.lower()],
                                           callModemObj)
                ret += r
                lastCall = lc

    return (ret, lastCall)
def test_icingaNotification_getOutput4():
    o = test_objects.icingaNotifGen(1).getObj("list")[0]
    o["_source"]["icinga"]["notification_type"] = "IMPOSSIBLE"

    assert (icingaNotification.icingaNotification(o, "user").getNormalOutput()
            == "test.ls.intra - PROBLEM: HOST dummy output. test text")
def test_icingaNotification_getOutput3():
    o = test_objects.icingaNotifGen(1).getObj("list")[0]
    o["_source"]["icinga"]["notification_type"] = "CUSTOM"

    assert (icingaNotification.icingaNotification(o, "user").getNormalOutput()
            == "test.ls.intra - CUSTOM: HOST -  test text")
def test_icingaNotification_BadArgs1():
    with pytest.raises(TypeError):
        assert icingaNotification.icingaNotification(None, None)
def test_icingaNotification_BasicInit2():
    o = test_objects.icingaNotifGen(1).getObj("list")[0]
    o["_source"]["icinga"]["service"] = "Dummy"

    assert icingaNotification.icingaNotification(o, "user")
def test_icingaNotification_BasicInit1():
    o = test_objects.icingaNotifGen(1).getObj("list")[0]
    assert icingaNotification.icingaNotification(o, "user")
def test_icingaNotification_getNormalOutput_BadArgs1():
    o = (test_objects.icingaNotifGen().getObj("list")[0]["_source"]
         ["icinga"].pop("check_result"))
    with pytest.raises(KeyError):
        assert icingaNotification.icingaNotification(o, None).getNormalOutput()
def test_icingaNotification_BadArgs3():
    o = test_objects.icingaNotifGen().getObj(
        "list")[0]["_source"]["icinga"].pop("host")
    with pytest.raises(TypeError):
        assert icingaNotification.icingaNotification(o, "user")
def test_icingaNotification_BadArgs2():
    o = test_objects.icingaNotifGen().getObj("list")
    with pytest.raises(TypeError):
        assert icingaNotification.icingaNotification(o, None)