Last active
February 16, 2017 21:15
-
-
Save dpryan79/e30fb98513b8da5d5140d3eb7fe49501 to your computer and use it in GitHub Desktop.
This is an only partially tested script to check that various NFS servers are still working and post messages to slack accordingly.
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
#!/usr/bin/env python3 | |
import urllib.request | |
import json | |
from subprocess import Popen | |
from time import sleep | |
def checkUp(path, timeout): | |
p = Popen(['ls', path]) | |
try: | |
rc = p.wait(timeout) | |
except: | |
return False | |
if rc is None: | |
# Still running! Kill it! | |
p.terminate() | |
return False | |
if rc < 0: | |
return False | |
return True | |
def sendMessage(content): | |
# CHANGE THIS TO SOMETHING APPROPRIATE! | |
url = "https://hooks.slack.com/services/T000000000/B000000000/XXXXXXXXXXXXXXXXXX" | |
data = json.dumps({"text": content, | |
"username": "nfs-bot", | |
"channel": "#general"}) | |
opener = urllib.request.build_opener() | |
req = urllib.request.Request(url, data=bytes(data, "ascii"), headers={'Content-Type': 'application/json'}) | |
resp = opener.open(req) | |
def iterateStatus(sleepTime, timeout): | |
""" | |
A list of NFS servers with a mount point associated with each. | |
The 3rd field is a placeholder for the state, which is | |
initially 'None', but can change to 'up' or 'down'. | |
""" | |
l = [["Void", "/data/processing3", None], | |
["Valid", "/data/galaxy2", None], | |
["solsys4", "/data/processing", None]] | |
while True: | |
for _ in l: | |
if checkUp(_[1], timeout) is False: | |
# NFS is down, only send a message if it was up/unknown | |
if _[2] != "down": | |
_[2] = "down" | |
sendMessage("{} is down, time for beer!".format(_[0])) | |
else: | |
# NFS is up, don't post if this was already the case | |
if _[2] is None: | |
_[2] = "up" | |
sendMessage("Monitoring NFS server {} according to accessibility of {}.".format(_[0], _[1])) | |
elif _[2] == "down": | |
_[2] = "up" | |
sendMessage("{} is risen!".format(_[0])) | |
sleep(sleepTime) | |
# Check every 5 minutes (60*5), wait up to 15 seconds for a mount point to respond | |
iterateStatus(60*5, 15) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment