#!/usr/local/bin/python # # check_svn_backup: check liveness and integrity of a Subversion backup # Dan R. K. Ports # # Copyright (c) 2007-2008 Dan R. K. Ports # # Permission is hereby granted, free of charge, to any person # obtaining a copy of this software and associated documentation # files (the "Software"), to deal in the Software without # restriction, including without limitation the rights to use, # copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following # conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE. # # $Revision$ $Date$ import sys, os, re, popen2, optparse from datetime import datetime, timedelta from time import strptime SVNADMIN = "/usr/local/bin/svnadmin" SVNLOOK = "/usr/local/bin/svnlook" CHECK_BY_SSH = "/usr/local/libexec/nagios/check_by_ssh" CHECK_NRPE = "/usr/local/libexec/nagios/check_nrpe2" TIMEOUT=900 parser = optparse.OptionParser() parser.add_option("-H", "--host", dest="host", help="remote host to check", metavar="HOST") parser.add_option("-l", "--local-repos", dest="localrepos", help="local repository", metavar="PATH") parser.add_option("-s", "--remote-helper", dest="ssh_helper", help="path to remote helper plugin", metavar="PATH") parser.add_option("-n", "--nrpe-command", dest="nrpe_command", help="command to execute via nrpe", metavar="CMD") parser.add_option("-d", "--remote-dir", dest="remote_dir", help="path to remote backup directory", metavar="PATH") parser.add_option("-a", "--age", dest="days", type="int", help="acceptable backup age (days)", metavar="DAYS") (options, args) = parser.parse_args() if not ((options.ssh_helper != None) ^ (options.nrpe_command != None)): raise Exception("exactly one of -s or -n must be specified!") def youngest_revision_older_than(repos, age): proc = popen2.Popen4((SVNLOOK, "youngest", repos)) if (proc.wait() != 0): raise Exception("failed to svnlook youngest") youngest = int(proc.fromchild.readline()) print ("Youngest revision is " + str(youngest)) for r in range(youngest, 0, -1): proc = popen2.Popen4((SVNLOOK, "date", "-r", str(r), repos)) if (proc.wait() != 0): raise Exception("failed to svnlook date") revdate = " ".join(proc.fromchild.readline().rstrip().split(" ")[0:2]) print "Revision", str(r), "committed", revdate dt = datetime(*strptime(revdate, "%Y-%m-%d %H:%M:%S")[0:6]) if datetime.now() - dt > age: return r return 1 try: rev = youngest_revision_older_than(options.localrepos, timedelta(days=options.days)) if options.ssh_helper != None: os.execl(CHECK_BY_SSH, CHECK_BY_SSH, "-H", options.host, "-t", str(TIMEOUT), "-C", (options.ssh_helper + " -d " + options.remote_dir + " -o " + str(rev))) else: print (CHECK_NRPE, CHECK_NRPE, "-H", options.host, "-u", "-t", str(TIMEOUT), "-c", options.nrpe_command, "-a", options.remote_dir, str(rev)) os.execl(CHECK_NRPE, CHECK_NRPE, "-H", options.host, "-u", "-t", str(TIMEOUT), "-c", options.nrpe_command, "-a", options.remote_dir, str(rev)) except Exception, e: print "CRIT: ", str(e) sys.exit(2)