File: //usr/lib/python2.7/site-packages/lap/services_bacula.py
#!/usr/bin/python
import pexpect
import socket
from datetime import datetime, date, timedelta
from distutils.spawn import find_executable
import sys
import os
hostname = socket.gethostname()
bibbconsole = '/opt/bacula/bin/bconsole'
hpastday = datetime.now() - timedelta(hours=13)
bacula_format_date = hpastday.strftime('%Y-%m-%d')
def bconsole(command, pattern, timeout):
child = pexpect.spawn(bibbconsole)
child.sendline(command)
try:
child.expect(pattern, timeout=timeout)
child.close()
return True
except Exception,e:
return False
def bconsole_runjob(timeout):
now = datetime.now()
today18 = now.replace(hour=18, minute=0, second=0, microsecond=0)
today1805 = now.replace(hour=18, minute=0, second=0, microsecond=0)
if now > today18 and now < today1805:
try:
child = pexpect.spawn(bibbconsole)
child.sendline('run job=%s-%s-test yes' % (hostname, hostname))
child.expect('Job queued. JobId=', timeout=timeout)
child.close()
return True
except Exception,e:
return False
else:
return True
def bconsole_lastjob():
try:
child = pexpect.spawn(bibbconsole)
child.sendline('list jobs jobstatus=T jobtype=B order=DESC')
child.expect(bacula_format_date)
child.close()
return True
except Exception,e:
return False
def __run__(params):
if bconsole('m', '\*$', 300) == True or bconsole('status dir', '\*$', 300) == True and bconsole_runjob(300) == True and bconsole_lastjob() == True:
return [0, 'OK: bacula seems to be working properly']
else:
return [2, 'CRITICAL: bacula seems to be stoped']