Commit ccd9aaa6 authored by ale's avatar ale
Browse files

Merge branch 'xmpp' into 'master'

Add an XMPP probe

See merge request !1
parents 8b141fec 45d0866e
Pipeline #7986 passed with stages
in 1 minute and 47 seconds
...@@ -8,6 +8,7 @@ from ai_diagnostics.probes.webmail import probe_webmail ...@@ -8,6 +8,7 @@ from ai_diagnostics.probes.webmail import probe_webmail
from ai_diagnostics.probes.imap import probe_imap from ai_diagnostics.probes.imap import probe_imap
from ai_diagnostics.probes.smtp import * from ai_diagnostics.probes.smtp import *
from ai_diagnostics.probes.webdav import probe_webdav from ai_diagnostics.probes.webdav import probe_webdav
from ai_diagnostics.probes.xmpp import probe_xmpp
class Credentials(dict): class Credentials(dict):
......
import threading
import time
from ai_diagnostics import xmpp
TIMEOUT = 30
def probe_xmpp(ctx):
bot = xmpp.SendMsgBot(ctx['credentials']['username'],
ctx['credentials']['password'])
bot.register_plugin('xep_0030')
bot.register_plugin('xep_0199')
# We can't reliably time out the XMPP probe on certain failures,
# so we just shut down the client altogether after a timeout.
done = threading.Event()
def _timeout():
time.sleep(TIMEOUT)
if not done.isSet():
bot.disconnect()
threading.Thread(target=_timeout, daemon=True).start()
try:
ctx.log('connecting to %s:5222', ctx['server'])
if bot.connect((ctx['server'], 5222), reattempt=False):
ctx.log('connection successful, setting presence')
bot.process(block=True)
ctx.log('disconnected successfully')
else:
ctx.log('unable to connect')
finally:
done.set()
bot.disconnect()
from sleekxmpp import ClientXMPP
class SendMsgBot(ClientXMPP):
"""XMPP bot that gets online and optionally sends a message."""
def __init__(self, jid, password, recipient=None, msg=None):
super(SendMsgBot, self).__init__(jid, password)
self.auto_reconnect = False
self.reconnect_max_attempts = 0
self.session_timeout = 5
self.recipient = recipient
self.msg = msg
self.add_event_handler('session_start', self.start)
def start(self, event):
self.send_presence()
self.get_roster()
if self.recipient:
self.send_message(mto=self.recipient, mbody=self.msg)
self.disconnect()
...@@ -9,7 +9,8 @@ setup( ...@@ -9,7 +9,8 @@ setup(
author="Autistici/Inventati", author="Autistici/Inventati",
author_email="info@autistici.org", author_email="info@autistici.org",
url="https://git.autistici.org/ai3/diagnostics", url="https://git.autistici.org/ai3/diagnostics",
install_requires=["cheroot", "Flask", "Mechanicalsoup", "prometheus_client"], install_requires=["cheroot", "Flask", "Mechanicalsoup", "sleekxmpp",
"prometheus_client"],
packages=find_packages(), packages=find_packages(),
package_data={ package_data={
"ai_diagnostics": [ "ai_diagnostics": [
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment