xmppnotify.py 4.36 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# pip3 install dnspython

import argparse
import configparser
from slixmpp import ClientXMPP
from socket import gethostname


class XMPPNotify(ClientXMPP):
    def __init__(self, jid, password, recipient, msg):
        ClientXMPP.__init__(self, jid, password)
        self.recipient = recipient
        self.msg = msg
        self.add_event_handler("session_start", self.session_start)
        # self.add_event_handler("message", self.message)

    def session_start(self, event):
        self.send_presence()
        self.get_roster()
        self.send_message(mto=self.recipient, mbody=self.msg)
        self.disconnect()

    def message(self, msg):
        pass


def build_message(args):
    if args.servicename:
        # service
32
        message = """[{notificationtype}] {servicedisplayname} on {hostdisplayname} is {servicestate}!
33

34
Info: {serviceoutput}
35

36
37
When: {longdatetime}
Ref: {hostname}!{servicename}
38
Monitoring host: {monitoringhostname}\
39
""".format(
40
            notificationtype=args.notificationtype,
41
42
43
44
45
46
47
48
49
50
            monitoringhostname=gethostname(),
            servicedisplayname=args.servicedisplayname,
            hostdisplayname=args.hostdisplayname,
            servicestate=args.state,
            serviceoutput=args.output,
            longdatetime=args.longdatetime,
            servicename=args.servicename,
            hostname=args.hostname
        )
    else:
51
        message = """[{notificationtype}] {hostdisplayname} is {hoststate}!
52

53
Info: {hostoutput}
54

55
56
When: {longdatetime}
Ref: {hostname}
57
Monitoring host: {monitoringhostname}\
58
""".format(
59
            notificationtype=args.notificationtype,
60
61
62
63
64
65
66
67
68
            monitoringhostname=gethostname(),
            hostdisplayname=args.hostdisplayname,
            hoststate=args.state,
            hostoutput=args.output,
            longdatetime=args.longdatetime,
            hostname=args.hostname
        )

    if args.hostaddress:
Christian Tacke's avatar
Christian Tacke committed
69
        message += "\nIPv4: {}".format(args.hostaddress)
70
71

    if args.hostaddress6:
Christian Tacke's avatar
Christian Tacke committed
72
        message += "\nIPv6: {}".format(args.hostaddress6)
73
74

    if args.notificationcomment:
Christian Tacke's avatar
Christian Tacke committed
75
76
        message += """\n
Comment by {notificationauthorname}
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
 {notificationcomment}
""".format(
            notificationauthorname=args.notificationauthorname,
            notificationcomment=args.notificationcomment
        )

    if args.icingaweb2url and args.servicename:
        message += "\n" + args.icingaweb2url + \
            "/monitoring/service/show?host={hostname}&service={servicename}" \
            .format(
                hostname=args.hostname,
                servicename=args.servicename
            )
    elif args.icingaweb2url:
        message += "\n" + args.icingaweb2url + \
            "/monitoring/host/show?host={hostname}" \
            .format(
                hostname=args.hostname,
            )

    return message


100
def build_argparser():
101
    parser = argparse.ArgumentParser(description='XMPP Notifications')
102

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    # required
    parser.add_argument('-d', '--longdatetime')
    parser.add_argument('-e', '--servicename')  # service only
    parser.add_argument('-l', '--hostname')
    parser.add_argument('-n', '--hostdisplayname')
    parser.add_argument('-o', '--output')  # host or service output
    parser.add_argument('-r', '--jid')
    parser.add_argument('-s', '--state')  # host or service state
    parser.add_argument('-u', '--servicedisplayname')  # service only
    parser.add_argument('-t', '--notificationtype')

    # optional
    parser.add_argument('-4', '--hostaddress')
    parser.add_argument('-6', '--hostaddress6')
    parser.add_argument('-b', '--notificationauthorname')
    parser.add_argument('-c', '--notificationcomment')
    parser.add_argument('-i', '--icingaweb2url')

121
122
123
124
125
126
127
128
129
130
    return parser


if __name__ == '__main__':
    config = configparser.RawConfigParser()
    config.read('/etc/xmppnotify.cfg')
    jid = config.get('Account', 'jid')
    password = config.get('Account', 'password')

    parser = build_argparser()
131
132
133
134
135
136
137
138
139
140
141
142
    args = parser.parse_args()
    message = build_message(args)

    xmpp = XMPPNotify(jid, password, args.jid, message)
    xmpp.register_plugin('xep_0030')  # Service Discovery
    xmpp.register_plugin('xep_0004')  # Data Forms
    xmpp.register_plugin('xep_0060')  # PubSub
    xmpp.register_plugin('xep_0199')  # XMPP Ping

    # Connect to the XMPP server and start processing XMPP stanzas.
    xmpp.connect()
    xmpp.process(forever=False)