Esempio n. 1
0
def test_parseNotifications_OKArgs9(caplog):
    caplog.set_level(logging.DEBUG)
    nl = test_objects.icingaNotifGen(count=1, serviceCount=1).getObj("class")
    o = "*Icinga alert:*\n:green_heart: *OK:*\n\ttest.ls.intra0 - " \
        "<test.com/dashboard#!/monitoring/service/show?host=test.ls.intra0&service=service|service>: dummy output.  " \
        "test text\n"
    assert parsing.parseNotifications(nl, "slack", "test.com") == o
Esempio n. 2
0
def test_parseNotifications_OKArgs2(caplog):
    caplog.set_level(logging.DEBUG)
    nl = test_objects.icingaNotifGen(count=3).getObj("class")
    tmp = map(lambda x: x.getNormalOutput(), nl)
    o = list(tmp)
    print(o)
    assert parsing.parseNotifications(nl, "sms") == o
Esempio n. 3
0
def test_parseNotifications_OKArgs7(caplog):
    caplog.set_level(logging.DEBUG)
    nl = test_objects.icingaNotifGen(count=5, hostCount=1,
                                     serviceCount=1).getObj("class")
    o = [
        'test.ls.intra0!service: test.ls.intra0 test.ls.intra0 test.ls.intra0 test.ls.intra0 test.ls.intra0 '
    ]
    assert parsing.parseNotifications(nl, "sms") == o
Esempio n. 4
0
def test_parseNotifications_OKArgs5(caplog):
    caplog.set_level(logging.DEBUG)
    nl = test_objects.icingaNotifGen(count=5, serviceCount=5).getObj("class")
    o = [
        "test.ls.intra: test.ls.intra0!service test.ls.intra1!service test.ls.intra2!service test.ls.intra3!service "
        "test.ls.intra4!service "
    ]
    assert parsing.parseNotifications(nl, "sms") == o
Esempio n. 5
0
def test_parseNotifications_OKArgs3(caplog):
    c = 3
    caplog.set_level(logging.DEBUG)
    nl = test_objects.icingaNotifGen(count=c).getObj("class")
    out = parsing.parseNotifications(nl,
                                     "email").get_payload()[0].get_payload()
    assert len(out.split("\n")) == 8 and out.split("\n")[0:3] == [
        "OK: 1x", "WARNING: 1x", "CRITICAL: 1x"
    ]
Esempio n. 6
0
def test_parseNotifications_OKArgs8(caplog):
    caplog.set_level(logging.DEBUG)
    for notif_type in [
            "ACKNOWLEDGEMENT", "CUSTOM", "DOWNTIMESTART", "DOWNTIMEEND",
            "DOWNTIMEREMOVED", "FLAPPINGSTART", "FLAPPINGEND"
    ]:
        nl = test_objects.icingaNotifGen(
            count=1, hostCount=1, serviceCount=1,
            notificationType=notif_type).getObj("class")
        o = [
            'test.ls.intra0 - ' + notif_type +
            ': test.ls.intra0!service -  test text'
        ]
        assert parsing.parseNotifications(nl, "sms") == o
Esempio n. 7
0
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)
Esempio n. 8
0
def test_parseNotifications_BadArgs1():
    assert parsing.parseNotifications(None, None) == 1
Esempio n. 9
0
def test_parseNotifications_OKArgs6(caplog):
    caplog.set_level(logging.DEBUG)
    nl = test_objects.icingaNotifGen(count=5, hostCount=3,
                                     serviceCount=2).getObj("class")
    o = ['test.ls.intra0: 2', 'test.ls.intra1: 2', 'test.ls.intra2: 1']
    assert parsing.parseNotifications(nl, "sms") == o
Esempio n. 10
0
def test_parseNotifications_OKArgs4(caplog):
    caplog.set_level(logging.DEBUG)
    nl = test_objects.icingaNotifGen(serviceCount=3).getObj("class")
    o = ["test.ls.intra: 2000x SERVICE"]
    assert parsing.parseNotifications(nl, "sms") == o
Esempio n. 11
0
def test_parseNotifications_OKArgs1(caplog):
    caplog.set_level(logging.DEBUG)
    nl = test_objects.icingaNotifGen().getObj("class")
    o = ["Host(UP): 2000x HOST"]
    assert parsing.parseNotifications(nl, "sms") == o
Esempio n. 12
0
def test_parseNotifications_BadArgs5():
    assert parsing.parseNotifications([1, 2, 3, 4, 5], "sms") == 1
Esempio n. 13
0
def test_parseNotifications_BadArgs4():
    assert parsing.parseNotifications(None, "sms") == 1
Esempio n. 14
0
def test_parseNotifications_BadArgs3():
    assert parsing.parseNotifications(None, "call") == 1
Esempio n. 15
0
def test_parseNotifications_BadArgs2():
    assert parsing.parseNotifications(123, 456) == 1