-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmpp-notification.py
executable file
·148 lines (116 loc) · 4.46 KB
/
xmpp-notification.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python3
import slixmpp
import argparse
class SendMsgBot(slixmpp.ClientXMPP):
def __init__(self, recipient, msg, sender_jid, password):
super().__init__(sender_jid, password)
self.recipient = recipient
self.msg = msg
# Service Discovery
self.register_plugin('xep_0030')
# XMPP Ping
self.register_plugin('xep_0199')
self.add_event_handler('session_start', self.start)
def start(self, event):
self.send_presence()
self.get_roster()
self.send_message(mto=self.recipient, mbody=self.msg)
# Using wait=True ensures that the send queue will be
# emptied before ending the session.
self.disconnect(wait=True)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Send XMPP Notifications")
# Always necessary arguments
parser.add_argument('-r', '--recipient',
type=str,
help="The JID(s) of the recipient(s)",
nargs='+',
action='append',
required=True)
parser.add_argument('-f', '--sender',
type=str,
nargs=1,
help="The sender address and XMPP JID",
required=True)
parser.add_argument('-p', '--password',
type=str,
nargs=1,
help="XMPP login password",
required=True)
parser.add_argument('-D', '--notification_date',
type=str,
nargs=1,
help="The date of the notification",
required=True)
parser.add_argument('-H', '--hostname',
type=str,
nargs=1,
help="The respective host",
required=True)
parser.add_argument('-d', '--hostdisplayname',
type=str,
nargs=1,
help="Name of the host")
parser.add_argument('-t', '--notification_type',
nargs=1,
help="Type of notification",
required=True)
# Optional
parser.add_argument('-a', '--notification_author',
type=str,
nargs=1,
help="The service sending this")
parser.add_argument('-c', '--notification_Comment',
type=str,
nargs=1,
help="Some comment")
parser.add_argument('args',
nargs=argparse.REMAINDER)
parser.add_argument('-S', '--state',
type=str,
nargs=1,
required=True,
help="State of the host/service")
parser.add_argument('-O', '--output',
type=str,
nargs=1,
required=True,
help="Output of the host/service check")
parser.add_argument('-e', '--servicename',
type=str,
nargs=1,
required=False,
help="Name of the service in question")
parser.add_argument('-u', '--servicedisplayname',
type=str,
nargs=1,
help="Display name of the service")
args = parser.parse_args()
# Hostname
if args.hostdisplayname is None:
host = args.host[0]
else:
host = args.hostdisplayname[0]
# Date
date = args.notification_date[0]
# State
state = args.state[0]
# Output
output = args.output[0]
if args.servicename:
# Service notification
if args.servicedisplayname is None:
service = args.servicename[0]
else:
service = args.servicedisplayname[0]
title = f"Service {service} on {host} is {state}"
text = f"{title}\nOutput: {output}\nTime/Date: {date}"
else:
# Host notification
title = f"Host {host} is {state}"
text = f"{title}:\nOutput: {output}\nTime/Date: {date}"
# print(args)
for i in args.recipient:
xmpp = SendMsgBot(recipient=i[0], msg=text, sender_jid=args.sender[0], password=args.password[0])
xmpp.connect()
xmpp.process(forever=False)