This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrunner.py
56 lines (40 loc) · 1.51 KB
/
runner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# BGPalerter
# Copyright (C) 2019 Massimo Candela <https://massimocandela.com>
#
# Licensed under BSD 3-Clause License. See LICENSE for more details.
import yaml
import smtplib
from bgpalerter import BGPalerter
import os
from email.mime.text import MIMEText
config = yaml.safe_load(open("config.yml", "r").read())
to_be_monitored = {}
for file_name in config.get("monitored-prefixes-files"):
print("Loading prefixes from " + file_name)
pointer = open(file_name, "r")
input_list = yaml.safe_load(pointer.read())
for item in input_list.keys():
to_be_monitored[item] = input_list[item]
def send_to_slack(message):
command = "curl -X POST -H 'Content-type: application/json' --data '{\"text\": \"" + message + "\"}' " + \
config.get("slack-web-hook")
os.system(command)
def send_email(message):
email_from = config.get("sender-notifications-email")
email_to = config.get("notified-emails")
msg = MIMEText(message)
msg['Subject'] = 'BGP alert'
msg['From'] = email_from
msg['To'] = ", ".join(email_to)
server = smtplib.SMTP('localhost')
server.sendmail(email_from, email_to, msg.as_string())
server.quit()
send_to_slack("Starting to monitor...")
# send_email("Starting to monitor...")
# change the way you want to be notified below
alerter = BGPalerter(config)
alerter.on("hijack", send_to_slack)
alerter.on("low-visibility", send_to_slack)
alerter.on("difference", send_to_slack)
# alerter.on("heartbeat", send_to_slack)
alerter.monitor(to_be_monitored)