Skip to content

Commit

Permalink
support arguments in crashplan script
Browse files Browse the repository at this point in the history
  • Loading branch information
jonschipp committed Aug 27, 2015
1 parent 8a0ad10 commit d723d6c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 27 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ one cannot simply update the scripts with malicious code.

**check_rsyslog.sh** - Check for rsyslog disk queue buffers (Find when logs are buffered)

**check_crashplan_backup.py** - Check latest backup times for crashplan server (uses API), notifies if backups hasn't been completed in 48 hours.
1. Set hostname in url variable in script to crashplan server
2. Add credentials to text file which script will read: `printf 'user = admin\npassword = ChangeMe\n' > /root/crashplan-credentials-for-nagios.txt`
3. Use sudo when exucuting script with nagios
**check_crashplan_backup.py** - Check latest backup times for crashplan server (uses API), notifies if backups hasn't been completed in 48 hours by default
1. Add credentials to text file which script will read: `printf 'user = admin@company.com\npassword = ChangeMe\n' > /root/crashplan-credentials-for-nagios.txt`
2. Use
a. Check all hosts: `check_crashplan_backup.py /root/crashplan_creds.txt crashplan.company.com:4285`
b. Check single host: `check_crashplan_backup.py /root/crashplan_creds.txt crashplan.company.com:4285 server1.company.com`
3. Use sudo when exucuting script with nagios if you put credentials in a file not readable by nagios user
4. You can edit the max_backup_time variable in the script to adjust the max number of days w/o backup for critical
83 changes: 60 additions & 23 deletions check_crashplan_backup.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,84 @@
#!/usr/bin/env python
import requests
import sys
import datetime
import requests
import json
import imp

filename = '/root/crashplan-credentials-for-nagios.txt'
# replace host with your crashplan server
url = 'https://crashplan.company.com:4285/api/DeviceBackupReport?active=true&srtKey=lastConnectedDate'

# Nagios exit codes
nagios_ok = 0
nagios_warning = 1
nagios_critical = 2
nagios_unknown = 3

# Two arguments are required
if len(sys.argv) < 3:
print 'usage: %s <creds_file> <host:port> [deviceName]' % sys.argv[0]
exit(nagios_unknown)

filename = sys.argv[1]
url = 'https://' + sys.argv[2] + '/api/DeviceBackupReport?active=true&srtKey=lastConnectedDate'
critical = 0
single_result = 0
max_backup_time = 2 # notify if backup is older than x days
status = 0

if len(sys.argv) == 4:
host = sys.argv[3]
else:
host = 0

# Open file
try:
f = open(filename, "r")
global data
creds = imp.load_source('data', '', f)
f.close()
except IOError:
print "Could not open credential file! Does it exist?"
print "Could not open file! Does it exist?"
exit(nagios_unknown)

global data
creds = imp.load_source('data', '', f)
f.close()
def backup_check(device, orig_time):
global status
if time < critical_days:
print "CRITICAL: %s: LastCompleteBackup: %s" % (device, orig_time)
status = nagios_critical

def format_time(entry, orig_time):
global time
time = datetime.datetime.strptime(orig_time, "%b %d, %Y %I:%M:%S %p")

def check_all_backup():
for entry in data["data"]:
device = entry["deviceName"]
orig_time = entry["lastCompletedBackupDate"]
if orig_time is None:
continue
format_time(entry, orig_time)
backup_check(device, orig_time)

def check_host_backup():
for entry in data["data"]:
device = entry["deviceName"]
if device == host:
orig_time = entry["lastCompletedBackupDate"]
format_time(entry, orig_time)
backup_check(device, orig_time)

# Make API request
r = requests.get(url, auth=(creds.user, creds.password))
r.raise_for_status()

data = r.json()
two_days_ago = datetime.datetime.now() - datetime.timedelta(days=2)
critical_days = datetime.datetime.now() - datetime.timedelta(days=max_backup_time)

for entry in data["data"]:
device = entry["deviceName"]
orig_time = entry["lastCompletedBackupDate"]
if orig_time is None:
continue
time = datetime.datetime.strptime(orig_time, "%b %d, %Y %I:%M:%S %p")
if time < two_days_ago:
print "CRITICAL: %s: LastCompleteBackup: %s" % (device, orig_time)
critical = 1
if host == 0:
check_all_backup()
else:
check_host_backup()

if critical == 1:
if status == nagios_critical:
exit(nagios_critical)

print "OK: All backups have been completed recently"
exit(nagios_ok)
else:
print "OK: All backups have been completed recently"
exit(nagios_ok)

0 comments on commit d723d6c

Please sign in to comment.