Skip to content

Instantly share code, notes, and snippets.

@Sperryfreak01
Created March 27, 2014 04:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sperryfreak01/9800222 to your computer and use it in GitHub Desktop.
Save Sperryfreak01/9800222 to your computer and use it in GitHub Desktop.
# Copyright 2014 Matt Lovett
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import urllib
import urlparse
import os
import urllib2
import subprocess
import json
app_token = 'abc123def456' #Create a pushover app for this script (https://pushover.net/apps/build)
user_token = 'q1w2e3r4t5y6 #Your pushover user key goes here, you need a pushover account (https://pushover.net)
class PushoverError(Exception): pass
def pushover(**kwargs):
PUSHOVER_API = "https://api.pushover.net/1/"
assert 'message' in kwargs
if not 'token' in kwargs:
kwargs['token'] = os.environ['PUSHOVER_TOKEN']
if not 'user' in kwargs:
kwargs['user'] = os.environ['PUSHOVER_USER']
url = urlparse.urljoin(PUSHOVER_API, "messages.json")
data = urllib.urlencode(kwargs)
req = urllib2.Request(url, data)
try:
response = urllib2.urlopen(req)
output = response.read()
data = json.loads(output)
except urllib2.HTTPError, httperror:
raise PushoverError(httperror)
if data['status'] != 1:
raise PushoverError(output)
try:
greyJSON = subprocess.check_output(["/usr/bin/greyhole" , "-s" , "-j"])
decoded_data = json.loads(greyJSON)
for drive, stats in decoded_data.items():
if drive == 'Total':
for type, value in stats.items():
if type == 'potential_available_space':
PAS = value/1048576
elif type == 'total_space':
TS = value/1048576
elif type == 'used_space':
US = value/1048576
elif type == 'free_space':
FS = value/1048576
FSpercent=( float(FS) / float(TS)) * 100
FSpercent=round(FSpercent,2)
pushover(message='Greyhole has ' + str(FSpercent) + '% freespace available.\n' +
'( ' + str(FS) + 'GB of ' + str(TS) + 'GB )\n' + str(PAS) + 'GB potential free space'
, token=app_token, user=user_token)
except subprocess.CalledProcessError, errMSG:
print(errMSG.output)
except OSError as errMSG:
print(errMSG)
@Sperryfreak01
Copy link
Author

This script was written with Python 2.7 in mind, YMMV on different versions. I add it to cron.daily to get a daily report but you can add it to cron however suits you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment