NewsBlur-viq/utils/monitor_disk_usage.py

33 lines
1.2 KiB
Python
Raw Normal View History

#!/srv/newsblur/venv/newsblur/bin/python
import sys
2015-02-11 17:37:41 -08:00
sys.path.append('/srv/newsblur')
import subprocess
import requests
import settings
import socket
def main():
df = subprocess.Popen(["df", "/"], stdout=subprocess.PIPE)
output = df.communicate()[0]
device, size, used, available, percent, mountpoint = output.split("\n")[1].split()
hostname = socket.gethostname()
2016-03-16 12:20:09 -07:00
percent = int(percent.strip('%'))
admin_email = settings.ADMINS[0][1]
2016-03-16 12:20:09 -07:00
if percent > 95:
requests.post(
"https://api.mailgun.net/v2/%s/messages" % settings.MAILGUN_SERVER_NAME,
auth=("api", settings.MAILGUN_ACCESS_KEY),
2020-06-08 15:11:42 -04:00
data={"from": "NewsBlur Disk Monitor: %s <admin@%s.newsblur.com>" % (hostname, hostname),
"to": [admin_email],
2016-03-16 12:20:09 -07:00
"subject": "%s hit %s%% disk usage!" % (hostname, percent),
"text": "Usage on %s: %s" % (hostname, output)})
2020-06-19 02:27:48 -04:00
print(" ---> Disk usage is NOT fine: %s / %s%% used" % (hostname, percent))
else:
2020-06-19 02:27:48 -04:00
print(" ---> Disk usage is fine: %s / %s%% used" % (hostname, percent))
if __name__ == '__main__':
main()