I wanted to show an easy way to automate alerting ISPs in case your link/connection/router is down.
As mentioned in a previous post, with LibreNMS you can easily manage the monitoring of a network. One of the features of LibreNMS is to send alerts to your email when something fails.
The logical next step is to open a ticket with the Internet Service Providers (ISPs) to check their connection (if you did NOT buy an expensive service that provides monitoring from ISPs).
But, why do this manually ? (or have a NOC 1st line team do it)
Here is where automation comes into place.
Automation Flow
So lets you already configured LibreNMS to send alerts to email and that you receive an alert in an email called: noc@example.com
If that is the case, then this could be a possible automation Flow:
- LibreNMS generates Alert ->
- Alert arrives to noc@example.com ->
- Script parses the alert ->
- Script sends automatically an email to the ISPs (with users in CC) ->
- Email is deleted (so that when the script runs again, we won’t spam the ISPs)
Script
import requests
import ipaddress
import exchangelib as E
import sys
import email
import requests
import json
import re
import time
librenms_api = "http://IP_ADDRESS/api/v0"
TOKEN = "TOKEN"
exchange_username = "domain\\username"
exchange_password = "username_password"
exchange_server = "webmail.example.com"
exchange_smtp_address = "username@example.com"
exchange_cc_recipients= ['noc@example.com','helpdesk@example.com']
email_ISP1 = ['email1@isp1.com','email2@isp1.com']
email_ISP2 = ['email1@isp2.com','email2@isp2.com']
email_ISP3 = ['email1@isp3.com','email2@isp3.com']
headers = {'User-Agent': 'LibreNMS',
'X-Auth-Token': TOKEN
}
We are going to run over the script now and what everything means so that you can easily use/change it.
First we import all our needed libraries.
We then define the following: