Skip to content

Instantly share code, notes, and snippets.

@jyotishp
Created May 18, 2018 18:48
Show Gist options
  • Save jyotishp/887f859d5d1dfd57f6bb30af4622a9ca to your computer and use it in GitHub Desktop.
Save jyotishp/887f859d5d1dfd57f6bb30af4622a9ca to your computer and use it in GitHub Desktop.
Script to scrape Nagios and update Cachet status page
#!/usr/bin/env python
# Import required modules
import os
import requests
import time
import getpass
from bs4 import BeautifulSoup
import re
# Set some useful variables
nagios_username = input('Nagios Username: ')
nagios_password = getpass.getpass('Nagios Password: ')
api_key = input('Cachet API key: ') # This is cachet API key
poll_interval = 2 * 60 # In seconds
# We will check only for these services
# Add service name and corresponding status code
# Status code description:
# 1 = Operational
# 2 = Performance Issues
# 3 = Partial Outage
# 4 = Major Outage
scores = {
'CPU Load': 2,
'Mem Usage': 2,
'DNS': 4,
'HTTP': 3,
'HTTPS': 3,
'SMTP': 4,
'SMTPS': 4,
'POP3': 4,
'POP3S': 4,
'IMAP': 4,
'IMAPS': 4,
'Postfix Email Queue': 2,
'Squid': 4
}
# Get list of all added components in Cachet
def getServersList():
response = requests.get('https://status.iiit.ac.in/api/v1/components')
response = response.json()
response = response['data']
return response
# Ping for given host
# We use this as a fallback mechanism
def checkHost(host):
if os.system("ping -c 1 " + host + " 2>/dev/null 1>/dev/null") is 0:
return 1
return 4
# Scrape and nagios
def checkNagios(service_name):
# Start with status code and update based on severity
status_code = 1
# Authenticate Nagios session
nagios_session = requests.Session()
nagios_session.auth = (nagios_username, nagios_password)
nagios_session.post('https://nagios.iiit.ac.in')
# Get host details and extract available services on the host
nagios_page = nagios_session.get('https://nagios.iiit.ac.in/nagios/cgi-bin//status.cgi?host=' + service_name + '&limit=0')
host_table = BeautifulSoup(nagios_page.text, 'html.parser')
host_table = host_table.findAll('td', { 'class': 'statusEven', 'align': 'left' })
del host_table[0]
# Check if host is down
url = 'https://nagios.iiit.ac.in/nagios/cgi-bin//extinfo.cgi?type=1&host='
url += service_name
page = nagios_session.get(url)
if re.search('hostDOWN', page.text):
return 4
# Todo check for packet loss and return 3
# Check services
for service in host_table:
if service.text in scores:
url = 'https://nagios.iiit.ac.in/nagios/cgi-bin//extinfo.cgi?type=2&host='
url += service_name + '&service='
url += '+'.join(service_name.split())
page = nagios_session.get(url)
# We check only for critical status
if re.search('serviceCRITICAL', page.text):
tmp_code = scores[service.text]
status_code = tmp_code if status_code < tmp_code else status_code
return status_code
# Update cachet
# We monitor things based on the tag values of each component in cachet
# Two values are required in tag field:
# 1) service type 2) ip/host
# Right now, we just ping and return 1 or 4 code if there is nagios involved
# IMP: THERE SHOULD BE EITHER 'nagios,nagios_hostname' or 'ping,ip_or_domain' IN THE TAGS FIELD!
def updateMonitor(component):
if 'ping' in component['tags']:
del component['tags']['ping']
host = list(component['tags'].values())[0]
status_code = checkHost(host)
elif 'nagios' in component['tags']:
del component['tags']['nagios']
service_name = list(component['tags'])[0]
status_code = checkNagios(service_name)
else:
status_code = 4
headers = {
'X-Cachet-Token': api_key
}
payload = {
'status': status_code
}
url = 'https://status.iiit.ac.in/api/v1/components/' + str(component['id'])
requests.put(url, data = payload, headers = headers)
# Loop for all components in cachet
def monitor():
servers = getServersList()
for server in servers:
updateMonitor(server)
# Run the monitor forever
# For cron job, remove the below part and run monitor()
if __name__ == '__main__':
while True:
monitor()
time.sleep(poll_interval)
beautifulsoup4==4.6.0
certifi==2018.4.16
chardet==3.0.4
idna==2.6
requests==2.18.4
urllib3==1.22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment