Initial commit
This commit is contained in:
commit
43302b30d5
37
irc-command-line-client.py
Normal file
37
irc-command-line-client.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#! /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)
|
||||||
41
irc-netxms-log-server.py
Normal file
41
irc-netxms-log-server.py
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
##pip3 install irc##
|
||||||
|
from irc import client, connection
|
||||||
|
from socket import socket, AF_INET, SOCK_DGRAM
|
||||||
|
from threading import Thread
|
||||||
|
|
||||||
|
host = '0.0.0.0'
|
||||||
|
port = 7747
|
||||||
|
addr = (host,port)
|
||||||
|
|
||||||
|
irc_host = '10.1.1.4'
|
||||||
|
irc_port = 6667
|
||||||
|
irc_chat_name = '#notifications'
|
||||||
|
irc_bot_nickname = 'Netxms_Bot'
|
||||||
|
|
||||||
|
s = socket(AF_INET, SOCK_DGRAM) # Socket creation
|
||||||
|
s.bind(addr) # Bind socket to ip
|
||||||
|
|
||||||
|
def on_connect(connection, event):
|
||||||
|
connection.mode(irc_bot_nickname, '+B')
|
||||||
|
connection.join(irc_chat_name)
|
||||||
|
|
||||||
|
def on_join(connection, event):
|
||||||
|
connection.privmsg(irc_chat_name, 'Hello, friend')
|
||||||
|
|
||||||
|
def on_disconnect(connection, event):
|
||||||
|
exit()
|
||||||
|
|
||||||
|
reactor = client.Reactor()
|
||||||
|
irc = reactor.server()
|
||||||
|
irc.connect(irc_host, irc_port, irc_bot_nickname)
|
||||||
|
irc.add_global_handler("welcome", on_connect)
|
||||||
|
irc.add_global_handler("join", on_join)
|
||||||
|
irc.add_global_handler('disconnect', on_disconnect)
|
||||||
|
Thread(target=reactor.process_forever).start()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
message, addr = s.recvfrom(1024) # receive message
|
||||||
|
#print('client addr: ', addr)
|
||||||
|
#print('info: ', message.decode())
|
||||||
|
irc.privmsg(irc_chat_name, message.decode().strip())
|
||||||
|
s.close()
|
||||||
Loading…
x
Reference in New Issue
Block a user