Просмотр исходного кода

support arguments in crashplan script

Jon Schipp 11 лет назад
Родитель
Сommit
d723d6c97a
2 измененных файлов с 67 добавлено и 27 удалено
  1. 7 4
      README.md
  2. 60 23
      check_crashplan_backup.py

+ 7 - 4
README.md

@@ -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

+ 60 - 23
check_crashplan_backup.py

@@ -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)