-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbart-logger
executable file
·128 lines (104 loc) · 4.28 KB
/
bart-logger
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python
#
# Executable for generating usage records.
# Part of the SGAS Batch system Reporting Tool (BaRT).
#
# Author: Henrik Thostrup Jensen <[email protected]>
# Author: Magnus Jonsson <[email protected]>
# Author: Erik Edelmann <[email protected]>
# Copyright: Nordic Data Grid Facility (2009, 2010),
# Nordic e-Infrastructure Collaboration (2018)
import os
import sys
import traceback
import logging
import re
from bart import config, common
from bart.config import BartConfig
from bart.config import BartMapFile
LOG_FORMAT = "%(asctime)s [%(levelname)s] %(message)s"
def main():
# start with command line parsing and various setups
parser = config.getParser()
options, args = parser.parse_args()
# read config file
cfg = None
try:
cfg = BartConfig(options.config)
except Exception as e:
sys.stderr.write("Can't read config file: %s\n" % e)
sys.exit(1)
# Log level
if options.debug:
loglevel = logging.DEBUG
else:
loglevel = cfg.getLoglevel()
# open logfile
logfile = options.logfile
if logfile is None:
logfile = cfg.getConfigValue(config.SECTION_COMMON, config.LOGFILE, config.DEFAULT_LOG_FILE)
logging.basicConfig(filename=logfile, format=LOG_FORMAT, level=loglevel)
stderr_level = cfg.getConfigValue(config.SECTION_COMMON, config.STDERR_LEVEL, config.DEFAULT_STDERR_LEVEL)
if stderr_level:
stderr_handler = logging.StreamHandler()
stderr_handler.setLevel(stderr_level)
logging.getLogger().addHandler(stderr_handler)
# Load user map file
user_map = BartMapFile()
try:
user_map_file = cfg.getConfigValue(config.SECTION_COMMON, config.USERMAP, config.DEFAULT_USERMAP_FILE)
user_map.load(user_map_file)
except IOError:
logging.error('IOError while attempting to read user map at %s (missing file?)' % user_map_file)
# Load vo map file
vo_map = BartMapFile()
try:
vo_map_file = cfg.getConfigValue(config.SECTION_COMMON, config.VOMAP, config.DEFAULT_VOMAP_FILE)
default_vo = cfg.getConfigValue(config.SECTION_COMMON, config.DEFAULTVO, None)
vo_map.load(vo_map_file, default_vo)
except IOError:
logging.error('IOError while attempting to read vo map at %s (missing file?)' % vo_map_file)
# get hostname, if missing from config use host fqdn
hostname = cfg.getConfigValue(config.SECTION_COMMON, config.HOSTNAME)
if hostname is None:
import socket
hostname = socket.getfqdn()
lrmsObj = None
for section in cfg.sections():
if section in ['common','logger']:
continue
# Handle lrms sections
try:
# Load lrms module
csection = re.sub("^([a-z])",lambda x: x.group(0).upper(),section)
logging.info("Module %s loading" % section)
# import lrms module
lrmsModule = __import__('bart.%s' % section,globals(),locals(),[csection])
# Validate cfg options for lrms module
cfg.validate(section,lrmsModule)
# Create lrms class
lrmsClass = getattr(lrmsModule,csection)
# Instansiate lrms object
lrmsObj = lrmsClass(cfg)
# read state file
common.readGeneratorState(lrmsObj)
# create URs
lrmsObj.generateUsageRecords(hostname, user_map, vo_map)
# write state file
common.writeGeneratorState(lrmsObj)
# log missing user mappings
suppress_usermap_info = cfg.getConfigValueBool(config.SECTION_COMMON,config.SUPPRESS_USERMAP_INFO,config.DEFAULT_SUPPRESS_USERMAP_INFO)
if not suppress_usermap_info and lrmsObj.missing_user_mappings:
users = ','.join(lrmsObj.missing_user_mappings)
logging.warning('Missing user mapping for the following users: %s' % users)
logging.info('Module %s done' % section)
except Exception as e:
logging.error('Got exception while generating usage records:')
logging.exception(e)
traceback.print_exc(file=sys.stderr)
sys.exit(3)
if lrmsObj is None:
logging.error('No lrms section found in config')
sys.exit(3)
if __name__ == '__main__':
main()