Initial commit
This commit is contained in:
commit
531734ea15
70
irc_monitor.py
Normal file
70
irc_monitor.py
Normal file
@ -0,0 +1,70 @@
|
||||
## pip3 install irc##
|
||||
from irc import client, connection
|
||||
from time import time
|
||||
from socket import socket
|
||||
from configparser import ConfigParser
|
||||
from time import sleep
|
||||
|
||||
config = ConfigParser()
|
||||
# Defaults
|
||||
config['Global'] = {'check_delay': '5 * 60', # every 5 min
|
||||
'IRC_host': '127.1', 'IRC_port': '6667', 'IRC_bot_nickname': 'bot', 'IRC_admin_nickname': '',
|
||||
'IRC_chat_name': '#main', 'attempts': '', 'attempts_delay': '', }
|
||||
|
||||
config.read('monitor.conf')
|
||||
|
||||
def check(host, port):
|
||||
s = socket()
|
||||
s.settimeout(1)
|
||||
for _ in range(int(config['Global']['attempts'])):
|
||||
if not s.connect_ex((host, int(port))): return True
|
||||
sleep(int(config['Global']['attempts_delay']))
|
||||
return False
|
||||
|
||||
#def on_connect(connection, event):
|
||||
# connection.mode(bot_nickname, '+x')
|
||||
# connection.join(IRC_chat_name)
|
||||
|
||||
def connect():
|
||||
reactor = client.Reactor()
|
||||
irc = reactor.server()
|
||||
irc.connect(config['Global']['IRC_host'], int(config['Global']['IRC_port']), config['Global']['IRC_bot_nickname'])
|
||||
reactor.process_once(timeout=30)
|
||||
irc.join(config['Global']['IRC_chat_name'])
|
||||
reactor.process_once(timeout=30)
|
||||
#irc.privmsg(config['Global']['IRC_chat_name'], 'Bot started')
|
||||
print('Connected')
|
||||
return irc, reactor
|
||||
|
||||
|
||||
hosts = config._sections
|
||||
check_delay = eval(config['Global']['check_delay'])
|
||||
|
||||
last_check = 0
|
||||
irc, reactor = connect()
|
||||
connection_states = {}
|
||||
for host, services in hosts.items():
|
||||
connection_states[host] = {}
|
||||
for service in services:
|
||||
connection_states[host][service] = True
|
||||
|
||||
try:
|
||||
while True:
|
||||
if time() - last_check >= check_delay:
|
||||
#print('Checking')
|
||||
for host, services in hosts.items():
|
||||
if host == 'Global': continue
|
||||
for service_name, service_port in services.items():
|
||||
result = check(host, service_port)
|
||||
if connection_states[host][service_name] != result:
|
||||
if result:
|
||||
irc.privmsg(config['Global']['IRC_chat_name'], '{} on {} is up'
|
||||
.format(service_name, host))
|
||||
else:
|
||||
irc.privmsg(config['Global']['IRC_chat_name'], '{}:{} on {} is down!'
|
||||
.format(config['Global']['IRC_admin_nickname'], service_name, host))
|
||||
connection_states[host][service_name] = result
|
||||
last_check = time()
|
||||
reactor.process_once(timeout=1)
|
||||
except KeyboardInterrupt:
|
||||
irc.disconnect()
|
||||
14
irc_monitor.service
Normal file
14
irc_monitor.service
Normal file
@ -0,0 +1,14 @@
|
||||
[Unit]
|
||||
Description=IRC Devices availaibility check
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
StartLimitIntervalSec=0[Service]
|
||||
Type=simple
|
||||
Restart=always
|
||||
RestartSec=1
|
||||
User=bridgebot
|
||||
ExecStart=python3 /irc_monitor/irc_monitor.py
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
19
monitor.conf
Normal file
19
monitor.conf
Normal file
@ -0,0 +1,19 @@
|
||||
[Global]
|
||||
IRC_host = 10.200.201.4
|
||||
IRC_port = 6697
|
||||
IRC_bot_nickname = GandCMon
|
||||
IRC_admin_nickname = GandC
|
||||
IRC_chat_name = #notifications
|
||||
check_delay = 60*5
|
||||
attempts = 7
|
||||
attempts_delay = 2
|
||||
|
||||
[gandc.icu]
|
||||
Https = 443
|
||||
Xmpp = 5222
|
||||
TurnServer = 5349
|
||||
I2pd = 20235
|
||||
|
||||
[localhost]
|
||||
Mysql:3306
|
||||
SSh:22222
|
||||
Loading…
x
Reference in New Issue
Block a user