source: people/joerg.steffens/technical/bareos/triggerjob/triggerjob.py@ 1269

Last change on this file since 1269 was 1240, checked in by joergs, on Mar 29, 2018 at 8:05:12 PM

prevent a problem when none prior jobs are found

  • Property svn:executable set to *
File size: 3.1 KB
Line 
1#!/usr/bin/env python
2
3from __future__ import print_function
4import argparse
5import bareos.bsock
6import logging
7import sys
8
9def get_job_names(director):
10 result=director.call('.jobs')['jobs']
11 jobs = [ job['name'] for job in result ]
12 return jobs
13
14def get_connected_clients(director):
15 result=director.call('status director')['client-connection']
16 clients = [ client['name'] for client in result ]
17 return clients
18
19def trigger(director, jobnames, clients):
20 for client in clients:
21 jobname = 'backup-{}'.format(client)
22 if not jobname in jobnames:
23 print('{} skipped, no matching job ({}) found'.format(client, jobname))
24 else:
25 jobs = director.call('list jobs client={} hours=24'.format(client))['jobs']
26 if jobs:
27 job = director.call('list jobs client={} hours=24 last'.format(client))['jobs'][0]
28 jobinfo = '{starttime}: jobid={jobid}, level={level}, status={jobstatus}'.format(**job)
29 print('{}: skipped, recent backups available ({})'.format(jobname, jobinfo))
30 else:
31 # TODO: check type=backup and failed
32 jobid = director.call('run {} yes'.format(jobname))['run']['jobid']
33 print('{}: backup triggered, jobid={}'.format(jobname, jobid))
34
35def getArguments():
36 parser = argparse.ArgumentParser(description='Console to Bareos Director.' )
37 parser.add_argument('-d', '--debug', action='store_true', help="enable debugging output")
38 parser.add_argument('--name', default="*UserAgent*", help="use this to access a specific Bareos director named console. Otherwise it connects to the default console (\"*UserAgent*\")")
39 parser.add_argument('-p', '--password', help="password to authenticate to a Bareos Director console", required=True)
40 parser.add_argument('--port', default=9101, help="Bareos Director network port")
41 parser.add_argument('--dirname', help="Bareos Director name")
42 parser.add_argument('address', nargs='?', default="localhost", help="Bareos Director network address")
43 args = parser.parse_args()
44 return args
45
46if __name__ == '__main__':
47 logging.basicConfig(format='%(levelname)s %(module)s.%(funcName)s: %(message)s', level=logging.INFO)
48 logger = logging.getLogger()
49
50 args=getArguments()
51 if args.debug:
52 logger.setLevel(logging.DEBUG)
53
54 try:
55 options = [ 'address', 'port', 'dirname', 'name' ]
56 parameter = {}
57 for i in options:
58 if hasattr(args, i) and getattr(args,i) != None:
59 logger.debug( "%s: %s" %(i, getattr(args,i)))
60 parameter[i] = getattr(args, i)
61 else:
62 logger.debug( '%s: ""' %(i))
63 logger.debug('options: %s' % (parameter))
64 password = bareos.bsock.Password(args.password)
65 parameter['password']=password
66 director = bareos.bsock.DirectorConsoleJson(**parameter)
67 except RuntimeError as e:
68 print(str(e))
69 sys.exit(1)
70 logger.debug( "authentication successful" )
71 jobs = get_job_names(director)
72 clients = get_connected_clients(director)
73 trigger(director, jobs, clients)
74
75 #director.interactive()
Note: See TracBrowser for help on using the repository browser.