File: //usr/lib/python2.7/site-packages/lap/postgresql_barman_backup.py
import subprocess
from dateutil.parser import parse
# check postgresql barman backup
# we can use it to monitor the backup
def __run__(params):
# gerando list de servidores
def list_server():
barman_list = subprocess.Popen(['sudo', 'barman', 'list-server'], stdout=subprocess.PIPE)
list_servers = barman_list.communicate()
return list_servers
# executando barman check
def check_server(host):
list_check_server = []
priority = 0
p = subprocess.Popen(['sudo', 'barman', 'check', host, '--nagios'], stdout=subprocess.PIPE)
list_check_server.append(p.communicate()[0])
for line in list_check_server:
if 'FAILED' in line:
if 'backup maximum age' in line:
priority = 3
else:
priority = 2
return priority
# executando barman list-backup
def list_backups(host):
hosts = ''
priority = 0
p = subprocess.Popen(['sudo', 'barman', 'list-backup', host], stdout=subprocess.PIPE)
a = p.communicate()[0]
# verificando se existe backup para o host
if a:
backup= a.splitlines()[0]
else:
hosts += host
priority = 2
return priority
hosts = list_server()
list_check_servers = []
for host in hosts[0].splitlines():
ret = check_server(host)
list_check_servers.append((ret, host))
#ret = list_backups(host)
#list_check_servers.append((ret, host))
errors = filter(lambda x: x[0] == 3, list_check_servers)
if errors:
servers = []
for _, host in errors:
if host not in servers:
servers.append(host)
return [2, 'Hosts com backup fora da retencao: {0}' .format(servers)]
errors = filter(lambda x: x[0] == 2, list_check_servers)
if errors:
servers = []
for _, host in errors:
if host not in servers:
servers.append(host)
return [2, 'Hosts com backup em execucao a mais de 1 dia: {0}' .format(servers)]
warnings = filter(lambda x: x[0] == 1, list_check_servers)
if warnings:
servers = []
for _, host in warnings:
if host not in servers:
servers.append(host)
return [1, 'Backup em execucao no host: {0}' .format(servers)]
return [0, 'Backup OK']
if __name__ == '__main__':
print __run__({})