Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Nagios + IOT + Raspberry Pi
import requests
import re
from requests.auth import HTTPBasicAuth
import time
import pyttsx
engine = pyttsx.init()
REFRESH_INTERVAL = 5
def get_status(server):
try:
result = requests.get('http://172.30.10.68/nagios/cgi-bin/extinfo.cgi?type=2&host={}.yomoney.com&service=Tomcat'.format(server),
auth=HTTPBasicAuth('username', 'password'), timeout=100)
pattern = re.compile(
r'<DIV CLASS=\'service.*\'>&nbsp;&nbsp;(.+)&nbsp;&nbsp;<\/DIV>')
result = pattern.findall(result.text)
except requests.exceptions.Timeout:
return "UNKNOWN"
return result[0]
bulb_state = {
"OK": "GREEN",
"CRITICAL": "RED",
"WARNING": "ORANGE"
}
print_color = {
"OK": '\033[92m',
"CRITICAL":'\033[91m',
"WARNING": '\033[93m',
"ENDC" : '\033[0m',
"HEAD": '\033[94m'
}
servers = {
"server id 1": {
"name": "server 1 name",
"status": "OK"
},
"server id 1": {
"name": "server 2 name",
"status": "OK"
}
}
while True:
for server in servers:
current_status = get_status(server)
if current_status not in servers[server]["status"]:
for n in [1, 2, 3]:
engine.say('{} is {}'.format(
servers[server]["name"], current_status))
time.sleep(1)
engine.runAndWait()
servers[server]["status"] = current_status
print("{} LED :\t{} {} {}".format(server,print_color[current_status], bulb_state[current_status],print_color["ENDC"]))
print("{}*****************************{}".format(print_color["HEAD"],print_color["ENDC"]))
time.sleep(REFRESH_INTERVAL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment