diff --git a/icinga-host-message.py b/icinga-host-message.py new file mode 100644 index 00000000..85f352c8 --- /dev/null +++ b/icinga-host-message.py @@ -0,0 +1,49 @@ +import os +import socket + +from matrix_bot.bot import Bot +from matrix_bot.message import MarkdownMessage +from matrix_bot.utils import run_async + +ROOM_ID = os.environ.get('MATRIX_ROOM_ID', + '!wDuJbnZXXNhbdPPcsW:gaja-group.com') + +HOSTADDRESS = os.environ.get('HOSTADDRESS', None) +HOSTADDRESS6 = os.environ.get('HOSTADDRESS6', None) +NOTIFICATIONAUTHORNAME = os.environ.get('NOTIFICATIONAUTHORNAME', None) +NOTIFICATIONCOMMENT = os.environ.get('NOTIFICATIONCOMMENT', None) +LONGDATETIME = os.environ.get('LONGDATETIME', None) +MAILFROM = os.environ.get('MAILFROM', None) +ICINGAWEB2URL = os.environ.get('ICINGAWEB2URL', None) +ICINGA2HOST = os.environ.get('ICINGA2HOST', socket.gethostname()) +HOSTNAME = os.environ.get('HOSTNAME', None) +HOSTDISPLAYNAME = os.environ.get('HOSTDISPLAYNAME', None) +HOSTOUTPUT = os.environ.get('HOSTOUTPUT', None) +RECIPIENT = os.environ.get('RECIPIENT', None) +HOSTSTATE = os.environ.get('HOSTSTATE', None) +NOTIFICATIONTYPE = os.environ.get('NOTIFICATIONTYPE', None) +VERBOSE = os.environ.get('VERBOSE', None) + + +def create_message() -> str: + message = (f'**HOST [{HOSTSTATE}]: {HOSTDISPLAYNAME} ({HOSTADDRESS})**' + f'\n\n{HOSTOUTPUT}') + if NOTIFICATIONCOMMENT: + message = f'{message}\n\nComment: {NOTIFICATIONCOMMENT}' + + return message + + +async def main() -> None: + bot = Bot() + client = await bot.login() + await bot.sync() + room = await bot.find_room_by_id(ROOM_ID) + msg = create_message() + message = MarkdownMessage(msg) + await message.send(client, room) + await bot.shutdown() + + +if __name__ == '__main__': + run_async(main()) diff --git a/icinga-service-message.py b/icinga-service-message.py new file mode 100644 index 00000000..27059607 --- /dev/null +++ b/icinga-service-message.py @@ -0,0 +1,53 @@ +import os +import socket + +from matrix_bot.bot import Bot +from matrix_bot.message import MarkdownMessage +from matrix_bot.utils import run_async + +ROOM_ID = os.environ.get('MATRIX_ROOM_ID', + '!wDuJbnZXXNhbdPPcsW:gaja-group.com') + +HOSTADDRESS = os.environ.get('HOSTADDRESS', None) +HOSTADDRESS6 = os.environ.get('HOSTADDRESS6', None) +NOTIFICATIONAUTHORNAME = os.environ.get('NOTIFICATIONAUTHORNAME', None) +NOTIFICATIONCOMMENT = os.environ.get('NOTIFICATIONCOMMENT', None) +LONGDATETIME = os.environ.get('LONGDATETIME', None) +MAILFROM = os.environ.get('MAILFROM', None) +ICINGAWEB2URL = os.environ.get('ICINGAWEB2URL', None) +ICINGA2HOST = os.environ.get('ICINGA2HOST', socket.gethostname()) +HOSTNAME = os.environ.get('HOSTNAME', None) +HOSTDISPLAYNAME = os.environ.get('HOSTDISPLAYNAME', None) + +SERVICENAME = os.environ.get('SERVICENAME', None) +SERVICEOUTPUT = os.environ.get('SERVICEOUTPUT', None) +SERVICESTATE = os.environ.get('SERVICESTATE', None) +SERVICEDISPLAYNAME = os.environ.get('SERVICEDISPLAYNAME', None) + +RECIPIENT = os.environ.get('RECIPIENT', None) +NOTIFICATIONTYPE = os.environ.get('NOTIFICATIONTYPE', None) +VERBOSE = os.environ.get('VERBOSE', None) + + +def create_message() -> str: + message = ( + f'**SERVICE [{SERVICESTATE}]: {SERVICENAME} on {HOSTDISPLAYNAME} ' + f'({HOSTADDRESS})**\n\n{SERVICEOUTPUT}') + if NOTIFICATIONCOMMENT: + message = f'{message}\n\nComment: {NOTIFICATIONCOMMENT}' + return message + + +async def main() -> None: + bot = Bot() + client = await bot.login() + await bot.sync() + room = await bot.find_room_by_id(ROOM_ID) + msg = create_message() + message = MarkdownMessage(msg) + await message.send(client, room) + await bot.shutdown() + + +if __name__ == '__main__': + run_async(main())