HEX
Server: Apache
System: Linux vpshost0650.publiccloud.com.br 4.4.79-grsec-1.lc.x86_64 #1 SMP Wed Aug 2 14:18:21 -03 2017 x86_64
User: bandeirantesbomb3 (10068)
PHP: 8.0.7
Disabled: apache_child_terminate,dl,escapeshellarg,escapeshellcmd,exec,link,mail,openlog,passthru,pcntl_alarm,pcntl_exec,pcntl_fork,pcntl_get_last_error,pcntl_getpriority,pcntl_setpriority,pcntl_signal,pcntl_signal_dispatch,pcntl_sigprocmask,pcntl_sigtimedwait,pcntl_sigwaitinfo,pcntl_strerror,pcntl_wait,pcntl_waitpid,pcntl_wexitstatus,pcntl_wifexited,pcntl_wifsignaled,pcntl_wifstopped,pcntl_wstopsig,pcntl_wtermsig,php_check_syntax,php_strip_whitespace,popen,proc_close,proc_open,shell_exec,symlink,system
Upload Files
File: //usr/lib/python2.7/site-packages/lap/bacula_schedule.py
#!/usr/bin/python

from backupmanager import BackupAPI
import socket
from datetime import datetime,timedelta, date
import argparse
import sys
import yaml
import os
import time
import json
from random import randrange
import pwd, grp

auth = BackupAPI()
client = socket.gethostname().split('.')[0]
OK = 0
CRITICAL = 2
WARNING = 1
cache_file = "/tmp/bacula_schedule.cache"
cache_time = 10850 + 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 schedule(job, hours):
    dic = {}
    jobs = auth.login('jobname', job)['jobs']
    date_now = date.today()
    date_time = datetime.today()
    hours = hours.split()[-1]
    if jobs:
        rotina = jobs[-1]
        starttimes = rotina['starttime']
        if starttimes == "None":
            return False
        last_date = datetime.strptime(starttimes, "%Y-%m-%d %H:%M:%S")
        scheduled = datetime.strptime(str(date_now) + " " + hours, "%Y-%m-%d %H:%M")


        if date_time < scheduled:
            if last_date.date() >= date_now - timedelta(days=1):
                pass
            else:
                dic[job] = str(scheduled)
        else:
            if last_date.date() >= date_now:
                pass
            else:
                dic[job] = str(scheduled)

    return dic


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):
                    client_json = auth.login('rotina', client)
                    if client_json:
                        for job in client_json:
                            jobjson = auth.login('job', job)
                            job_schedule = jobjson['schedule']
                            if jobjson and job_schedule:
                                schedules = auth.login('schedule', job_schedule)['run']
                                if schedules:
                                    run = schedule(job, schedules.splitlines()[0])
                                    if run:
                                        lista.append(run)
                        if lista:
                            return [CRITICAL,"CRITICAL: Jobs does not executed %s" % lista]
                
                        else:
                            msg = "All Jobs executed"
                            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:
                msg = "WARNING: %s" % e
                f.write(json.dumps(msg))
                return [WARNING, msg]