check_crashplan_backup.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python
  2. # Author: Jon Schipp <jonschipp@gmail.com, jschipp@illinois.edu>
  3. import sys
  4. import datetime
  5. import requests
  6. import json
  7. import imp
  8. import argparse
  9. # Nagios exit codes
  10. nagios_ok = 0
  11. nagios_warning = 1
  12. nagios_critical = 2
  13. nagios_unknown = 3
  14. parser = argparse.ArgumentParser(description='Last Crashplan backup check')
  15. parser.add_argument("-s", "--skip", type=str, help="Hosts to skip from backup check")
  16. parser.add_argument("-d", "--device", type=str, help="Specify deviceName to check (def: all)")
  17. parser.add_argument("-H", "--host", type=str, help="<host:port> e.g. crashplan.company.org:443", required=True)
  18. parser.add_argument("-f", "--filename", type=str, help="Filename that contains credentials", required=True)
  19. args = parser.parse_args()
  20. filename = args.filename
  21. url = 'https://' + args.host + '/api/DeviceBackupReport?active=true&srtKey=lastConnectedDate'
  22. critical = 0
  23. single_result = 0
  24. max_backup_time = 2 # notify if backup is older than x days
  25. status = 0
  26. if args.skip:
  27. skip = args.skip.split(",")
  28. else:
  29. skip = "wutthehelld000d!"
  30. if args.device:
  31. host = args.device
  32. else:
  33. host = 0
  34. # Open file
  35. try:
  36. with open(filename, 'r') as f:
  37. creds = json.load(f)
  38. user = creds["user"]
  39. password = creds["password"]
  40. except IOError:
  41. print "Could not open file! Does it exist? Is it valid JSON?"
  42. exit(nagios_unknown)
  43. def backup_check(device, orig_time):
  44. global status
  45. if time < critical_days:
  46. print "CRITICAL: %s: LastCompleteBackup: %s" % (device, orig_time)
  47. status = nagios_critical
  48. def format_time(entry, orig_time):
  49. global time
  50. time = datetime.datetime.strptime(orig_time, "%b %d, %Y %I:%M:%S %p")
  51. def check_all_backup(skip):
  52. for entry in data["data"]:
  53. device = entry["deviceName"]
  54. orig_time = entry["lastCompletedBackupDate"]
  55. if device in skip:
  56. continue
  57. if orig_time is None:
  58. continue
  59. format_time(entry, orig_time)
  60. backup_check(device, orig_time)
  61. def check_host_backup():
  62. for entry in data["data"]:
  63. device = entry["deviceName"]
  64. if device == host:
  65. orig_time = entry["lastCompletedBackupDate"]
  66. format_time(entry, orig_time)
  67. backup_check(device, orig_time)
  68. # Make API request
  69. r = requests.get(url, auth=(user, password))
  70. r.raise_for_status()
  71. data = r.json()
  72. critical_days = datetime.datetime.now() - datetime.timedelta(days=max_backup_time)
  73. if host == 0:
  74. check_all_backup(skip)
  75. else:
  76. check_host_backup()
  77. if status == nagios_critical:
  78. exit(nagios_critical)
  79. else:
  80. print "OK: All backups have been completed recently"
  81. exit(nagios_ok)