File: //lib/python2.7/site-packages/lap/bacula_error_full.py
#!/usr/bin/python
from backupmanager import BackupAPI
import socket
import json
from datetime import datetime,timedelta
import argparse
import sys,os
import time
from random import randrange
import pwd, grp
auth = BackupAPI()
client = socket.gethostname().split('.')[0]
OK = 0
CRITICAL = 2
WARNING = 1
cache_file = "/tmp/bacula_full.cache"
cache_time = 10700 + randrange(100)
def user_cache():
try:
uid, gid = pwd.getpwnam('check-mk').pw_uid, grp.getgrnam('check-mk').gr_gid
if uid and gid:
os.chown(cache_file, uid, gid)
except KeyError:
pass
def cegonha(client, full_json):
if full_json['status'] in ["IN_PRODUCTION","SPARE_INSTALATION","SPARE_MAINTAINCE","PRE_PRODUCTION","MAINTENANCE"] and full_json['flag'] == True:
return True
else:
return False
def __run__(params):
lista = []
if os.path.isfile(cache_file) and (time.time() - os.stat(cache_file).st_mtime) < cache_time and \
os.stat(cache_file).st_size > 0:
data = json.loads(open(cache_file).read())
if data:
return [OK, "Cache " + data]
else:
return [CRITICAL, "Cache " + data]
else:
with open(cache_file, 'w') as f:
user_cache()
try:
full_json = auth.login('cegonha', client)
if cegonha(client, full_json):
jobs = auth.login('rotina', client)
if jobs:
for job in jobs:
error = auth.login('lasterror', job)
full_error = error['full_error']
if full_error:
auth.login('reviewed', job + "/full")
lista.append({job: full_error})
if lista:
return [CRITICAL, "Client %s with erros in the jobs %s " % (client, lista)]
else:
msg = "Backup OK"
f.write(json.dumps(msg))
return [OK, msg]
else:
msg = "OK: Client %s does not exist in backupmanager" % client
f.write(json.dumps(msg))
return [OK, msg]
else:
msg = "Machine flag false or desactived"
f.write(json.dumps(msg))
return [OK, msg]
except Exception, e:
return [WARNING, "WARNING: API Connection refused"]