/usr/share/arc/DGLog2XML.py is in nordugrid-arc-arex 5.3.0~rc1-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | #!/usr/bin/python
"""Usage: DGLog2XML.py [-h] [-d|debug] [-c inifile|--config=inifile]
-h,--help
Print this information
-d,--debug
enable debug printing (default: 0)
-c inifile,--config=inifile
Specify the ARC config (default: /etc/arc/service.ini)
"""
# Set to 1 to enable debug messages on stdout
debug_level = 0
def debug(msg):
"""Print debug messages. The global variable debug_level controls
the behavior. Currently only 0 and 1 is used
"""
global debug_level
if debug_level == 1:
print(msg)
def readlog(logfile):
"""Read whole logfile - deprecated"""
f = open(logfile)
oldlog = f.readlines()
f.close
newlog = []
for line in oldlog:
newlog.append(line.strip())
return newlog
def line2dict(line):
"""Convert a log line to a dictionary
It is assumed that lines are of the form attr1=val1 attr2=val2 ...
"""
import sys
fields = line.split(" ")
f = {}
for i in fields:
pair = i.split("=",1)
if len(pair) != 2:
print("""Error parsing field: "%s" in line:\n%s"""%(i,line))
sys.exit(1)
(attr,val) = i.split("=")
f[attr] = val
return f
def line2xml(line):
"""Convert a log line to XML - ignoring empty lines"""
line = line.strip()
if line == "":
return ""
else:
return dict2xml(line2dict(line))
def dict2xml(line):
"""Translation of log between log in dictionary format to xml"""
# This dictionary/arrayrepresents valid elements as well as the order
order = ['dt',
'event',
'bridge_id',
'job_id',
'job_id_bridge',
'status',
'application',
'input_grid_name',
]
emap = {
'dt' : 'dt',
'event' : 'event',
'job_id' : 'job_id',
'job_id_bridge' : 'job_id_bridge',
'status' : 'status',
'application' : 'application',
'input_grid_name' : 'input_grid_name',
'bridge_id' : 'output_grid_name',
}
# Fix date
#pos = line['dt'].find('_')
#line['dt'] = line['dt'][0:pos] + ' ' + line['dt'][pos+1:]
line['dt'] = line['dt'].replace('_',' ')
xml = ""
for key in order:
if key in line:
xml += " <%s>%s</%s>\n" %(emap[key],line[key],emap[key])
xml = " <metric_data>\n%s </metric_data>\n" %xml
return xml
def getstatus(status_file):
"""Read status file and return tuple of last log file name and index line"""
try:
f = open(status_file)
except:
debug("Problem reading %s"%status_file)
return log_start
for l in f.readlines():
# Valid lines starts with 2
if l[0] == '2':
result = l.strip().split(' ')
if len(result) == 2:
f.close()
return result
print("Badly formatted line: %s"%l)
f.close()
return ["",0]
def parseini(inifile):
"""Get required info from the ARC ini file (eg. /etc/arc/service.ini)"""
import os
import ConfigParser
config = ConfigParser.RawConfigParser()
config.read(inifile)
stage_path = config.get('common', 'dgbridge_stage_dir')
xmldir = os.path.join(stage_path,"monitor")
controldir = config.get('grid-manager', 'controldir')
logdir = os.path.join(controldir,"3gbridge_logs")
return xmldir,logdir
def writexml(timestamp,xmldir,xml):
"""Write the log report in XML"""
import os
xmlfile = os.path.join(xmldir,"report_%s.xml"%str(timestamp))
debug("Writing xml to %s"%xmlfile)
x = open(xmlfile,"w")
x.write(xml)
x.close()
return
def updatestatus(status_file,lastlog,lastline):
"""Update status file
The file is updated with every call even though the content does not change
"""
debug("Updating status %(lastlog)s %(lastline)s in %(status_file)s" %locals())
f = open(status_file,"w")
f.write("%s %s"%(lastlog,lastline))
f.close()
return
def main():
"""Main routine"""
import os,time,sys
import getopt
global debug_level
inifile = "/etc/arc/service.ini"
try:
opts, args = getopt.getopt(sys.argv[1:], "hc:d", ["help","config","debug"])
except getopt.error, msg:
print msg
print "for help use --help"
sys.exit(2)
for o, a in opts:
if o in ("-h", "--help"):
print __doc__
sys.exit(0)
if o in ("-c", "--config"):
inifile = a
if o in ("-d", "--debug"):
debug_level=1
xmldir,logdir = parseini(inifile)
if not os.path.isdir(xmldir):
debug("No such directory: %s"%xmldir)
debug("Please create %s such that xml log files can be produced"%xmldir)
sys.exit(1)
status_name = "status"
status_file = os.path.join(logdir,status_name)
xml = ""
entries = os.listdir(logdir)
entries.sort()
# Check for status file
log_start = ["",0]
if status_name in entries:
log_start = getstatus(status_file)
# Find index of the first logfile in entries
first_logfile = 0
if log_start[0] != "":
first_logfile = entries.index(log_start[0])
# Find relevant log files
for i in range(first_logfile,len(entries)):
e = entries[i]
# Only accept logs from this millenia
if e[0] == '2':
f = open(os.path.join(logdir,e))
n = 0
# If this is the first logfile skip lines
if log_start[0] == e and log_start[1] > 0:
for i in range(int(log_start[1])):
line = f.readline()
n = int(log_start[1])
line = f.readline()
if line != "":
xml += line2xml(line)
while line:
line = f.readline()
n += 1
xml += line2xml(line)
f.close()
lastlog = e
lastline = n
timestamp = int(time.mktime(time.localtime()))
if not xml == "":
xml = """<report timestamp="%s" timezone="UTC">\n"""% timestamp+xml+"""</report>"""
writexml(timestamp,xmldir,xml)
else:
xml = """"""
writexml(timestamp,xmldir,xml)
if 'lastlog' in locals() and 'lastline' in locals():
updatestatus(status_file,lastlog,lastline)
if __name__ == "__main__":
main()
|