File: //usr/lib/python2.7/site-packages/lap/mybackup.py
import ConfigParser
import MySQLdb
import os.path
import subprocess
import time
def __run__(params):
conn = None
# if gracetime, ignore the check
now = time.localtime()
gracetime = int(params.get('gracetime'))
if now.tm_hour < gracetime:
return [0, 'Backup em execucao - checagem livre das 0h as %dh' % gracetime]
try:
cp = ConfigParser.ConfigParser()
cp.readfp(open("/etc/locaweb/mybackup/mybackup.conf"))
timeout = 2
conn = MySQLdb.connect(host=cp.get('mybackup', 'host'),
user=cp.get('mybackup', 'user'),
passwd=cp.get('mybackup', 'password'),
db=cp.get('mybackup', 'dbname'),
connect_timeout=timeout)
cursor = conn.cursor()
try:
cursor.execute("""select count(1)
from mybackup_catalog
where created_at=date(now()) and db!='information_schema'""")
total_db = cursor.fetchone()[0]
if total_db == 0:
return [2, 'CRITICAL: Catalogo de backup nao encontrado']
except Exception, e:
return [2, 'CRITICAL: Catalogo de backup nao encontrado']
cursor.execute("""select count(1)
from mybackup_catalog
where (st_backup = 'ERROR' or st_backup is null)
and created_at=date(now()) and db!='information_schema'""")
backup_count = cursor.fetchone()[0]
cursor.execute("""select count(1)
from mybackup_catalog
where (st_compress = 'ERROR' or st_compress is null)
and created_at=date(now()) and db!='information_schema'""")
tarfile_count = cursor.fetchone()[0]
if (backup_count == 0 and tarfile_count == 0):
return [0, 'OK: backup completed']
# not completed, what happened?
# running?
# not sure if mybackup do the right thing with the pidfile
if os.path.exists('/usr/sbin/lsof'):
lsof_command = '/usr/sbin/lsof'
elif os.path.exists('/usr/bin/lsof'):
lsof_command = '/usr/bin/lsof'
else:
return [2, 'CRITICAL: lsof not found']
devnull = open('/dev/null', 'rw')
message = 'CRITICAL: Backup incompleto Total de Erros: %d'
for p in ['mybackup', 'myinnodb']:
lsof = subprocess.Popen([lsof_command, '/var/lock/{0}'.format(p)],
stdin=devnull, stdout=devnull, stderr=devnull)
ret = lsof.wait()
if ret == 0:
message = 'CRITICAL: Backup incompleto Total de Erros: %d - backup ainda em execucao'
break
return [2, message % backup_count]
except Exception, e:
return [2, 'CRITICAL: {0}'.format(str(e))]
finally:
try:
if conn:
conn.close()
except:
# finally clause, don't bother about errors here
pass