irc-netxms-notifications/irc-command-line-client.py
2023-10-30 21:46:56 +03:00

38 lines
1.3 KiB
Python

#! /usr/bin/env python3
import socket
import sys
from irccodes import colored
from re import sub, IGNORECASE
def gen_highlight(pattern, color, padding=' ', end=''):
return (pattern, colored(r'\1', color, padding=padding)+end)
server = ("10.1.1.4", 7747)
highlights = (
#gen_highlight('( (?:\d+.){3}\d)', 'light blue'), # Highlight IP address
gen_highlight('(down|unreachable|not responding)', 'light red', ''),
gen_highlight('(up)', 'light green', ''),
gen_highlight('(ICMP|SNMP)', 'cyan', ''), # Highlight protocols
gen_highlight('(\S+)( Node)', 'lightblue', end=r'\2') # Highlight node names
)
level = sys.argv[1]
text = ' '.join(sys.argv[2:])
# Translate log level
if level == '0': level = colored('Normal', 'Grey')
elif level == '1': level = colored('Warning', 'Orange')
elif level == '2': level = colored('Minor', 'Orange')
elif level == '3': level = colored('Major', 'Light Red')
elif level == '4': level = colored('Critical', 'Light Red')
# Apply highlights
message = '{}: {}'.format(level, text)
for highlight in highlights:
message = sub(*highlight, message, flags=IGNORECASE)
# Create a UDP socket and send data
socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM).sendto(message.encode(), server)