Created
September 9, 2017 09:59
-
-
Save alfredfrancis/a39e1a359b691b44ad74b78a087fb14c to your computer and use it in GitHub Desktop.
Nagios + IOT + Raspberry Pi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.*\'> (.+) <\/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