Skip to content

Instantly share code, notes, and snippets.

@miticojo
Created June 12, 2016 07:24
Show Gist options
  • Save miticojo/77ab3326ae23ac814fa2b3a2e9e10ab6 to your computer and use it in GitHub Desktop.
Save miticojo/77ab3326ae23ac814fa2b3a2e9e10ab6 to your computer and use it in GitHub Desktop.
Check if drive is still mounted on a linux system and send notification through Slack
#!/usr/bin/python
__author__ = 'Giorgio Crivellari'
__version__ = "0.1"
import os
import sys
import socket
import urllib2
def check_mount(mountpoint):
if os.path.ismount(mountpoint):
return True
return False
def help():
print "Usage: monit-fs <mount-point> <slack-community> <slack-token> <slack-channel>"
print "\nExample:\n" \
"monit-fs.py /var/test apptothecloud XXXXXXXXXXX '#stack'"
sys.exit(0)
def public_ip():
ret = urllib2.urlopen('https://enabledns.com/ip')
return ret.read()
def slackmsg(mp):
msg = "ALERT: problem occurred on machine %s (%s) - mount point %s is not mounted" % \
(socket.gethostname().upper(), public_ip(), mp)
return msg
def slackbot(community, token, channel, msg):
uri = "https://%s.slack.com/services/hooks/slackbot?token=%s&channel=%%23%s" % (community, token, channel)
req = urllib2.Request(uri, msg)
response = urllib2.urlopen(req)
result = response.read()
if result == "ok":
return True
return False
def main():
if len(sys.argv) != 5:
help()
mp = sys.argv[1]
scommunity = sys.argv[2]
stoken = sys.argv[3]
schannel = sys.argv[4]
if '#' in schannel:
schannel = schannel[1:]
if not check_mount(mp):
slackbot(scommunity, stoken, schannel, slackmsg(mp))
sys.exit(2)
sys.exit(0)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment