1 |
corvo |
1.1 |
#!/usr/bin/python
|
2 |
|
|
|
3 |
|
|
"""
|
4 |
|
|
This is a simple test program for the 'apmon' Python module.
|
5 |
|
|
|
6 |
|
|
"""
|
7 |
|
|
|
8 |
|
|
import apmon
|
9 |
|
|
import time, sys, os
|
10 |
corvo |
1.2 |
from types import DictType
|
11 |
corvo |
1.1 |
|
12 |
corvo |
1.2 |
# Method for writing debug information in a file
|
13 |
corvo |
1.1 |
def logger(msg) :
|
14 |
|
|
msg = `msg`
|
15 |
|
|
if not msg.endswith('\n') :
|
16 |
|
|
msg = msg + '\n'
|
17 |
|
|
#print msg
|
18 |
corvo |
1.2 |
try :
|
19 |
|
|
fh = open('report.log','a')
|
20 |
|
|
fh.write(msg)
|
21 |
|
|
fh.close
|
22 |
|
|
except Exception, e :
|
23 |
|
|
pass
|
24 |
|
|
|
25 |
|
|
##############
|
26 |
|
|
## CONTEXT
|
27 |
|
|
|
28 |
|
|
# Global context for report
|
29 |
|
|
_context = {}
|
30 |
|
|
|
31 |
|
|
# Format envvar, context var name, context var default value
|
32 |
|
|
contextConf = {'MonitorID' : ('MonitorID', 'unknown'),
|
33 |
|
|
'MonitorJobID' : ('MonitorJobID', 'unknown'),
|
34 |
|
|
'MonitorLookupURL': ('MonitorLookupURL', 'http://lxgate35.cern.ch:40808/ApMonConf') }
|
35 |
|
|
|
36 |
|
|
def getContext(overload={}) :
|
37 |
|
|
if not isinstance(overload, DictType) :
|
38 |
|
|
overload = {}
|
39 |
|
|
if len(_context) == 0 :
|
40 |
|
|
for paramName in contextConf.keys() :
|
41 |
|
|
paramValue = None
|
42 |
|
|
if overload.has_key(paramName) :
|
43 |
|
|
paramValue = overload[paramName]
|
44 |
|
|
if paramValue is None :
|
45 |
|
|
envVar = contextConf[paramName][0]
|
46 |
|
|
paramValue = os.getenv(envVar)
|
47 |
|
|
if paramValue is None :
|
48 |
|
|
defaultValue = contextConf[paramName][1]
|
49 |
|
|
paramValue = defaultValue
|
50 |
|
|
_context[paramName] = paramValue
|
51 |
|
|
return _context
|
52 |
|
|
|
53 |
|
|
## /CONTEXT
|
54 |
|
|
##############
|
55 |
|
|
|
56 |
|
|
# Methods for handling the apmon instance
|
57 |
|
|
# apmonConf = {'sys_monitoring' : False, 'job_monitoring' : False, 'general_info': False}
|
58 |
|
|
|
59 |
|
|
def send(context, paramDict) :
|
60 |
|
|
task = context['MonitorID']
|
61 |
|
|
job = context['MonitorJobID']
|
62 |
|
|
apmonUrl = context['MonitorLookupURL']
|
63 |
|
|
logger("ApmReport: Creating ApMon with " + `apmonUrl`)
|
64 |
|
|
apm = apmon.ApMon(apmonUrl, apmon.Logger.WARNING)
|
65 |
|
|
logger("ApmReport: Destinations: " + `apm.destinations`)
|
66 |
|
|
#apm.enableBgMonitoring(False)
|
67 |
|
|
logger("ApmReport: Sending("+task+","+job+","+`paramDict`+")")
|
68 |
|
|
apm.sendParameters(task, job, paramDict)
|
69 |
|
|
time.sleep(1)
|
70 |
|
|
apm.free()
|
71 |
|
|
|
72 |
|
|
# Reading the input arguments
|
73 |
corvo |
1.1 |
|
74 |
corvo |
1.2 |
_jobid = None
|
75 |
|
|
jobidEnvList = ['EDG_WL_JOBID', 'GLITE_WMS_JOBID']
|
76 |
|
|
def setGridJobID(argValues=None,default='unknown') :
|
77 |
|
|
global _jobid
|
78 |
|
|
if argValues is not None and argValues.has_key('GridJobID') :
|
79 |
|
|
_jobid = argValues['GridJobID']
|
80 |
|
|
argValues.__delitem__('GridJobID')
|
81 |
|
|
if _jobid is None :
|
82 |
|
|
for jobidEnvCandidate in jobidEnvList :
|
83 |
|
|
jobidCandidate = os.getenv(jobidEnvCandidate)
|
84 |
|
|
if jobidCandidate is not None :
|
85 |
|
|
_jobid = jobidCandidate
|
86 |
|
|
break
|
87 |
|
|
if _jobid is None :
|
88 |
|
|
_jobid = default
|
89 |
|
|
|
90 |
|
|
def getGridJobID() :
|
91 |
|
|
if _jobid is None :
|
92 |
|
|
setGridJobID()
|
93 |
|
|
return _jobid
|
94 |
|
|
|
95 |
|
|
def getGridIdentity() :
|
96 |
|
|
userid = os.popen("grid-proxy-info -identity").read().strip()
|
97 |
|
|
return userid
|
98 |
|
|
|
99 |
|
|
# Simple filters (1-to-1 correspondance)
|
100 |
|
|
reportFilters = { 'getGridIdentity' : ('SyncGridName', getGridIdentity),
|
101 |
|
|
'SYNC' : ('SyncGridJobID', getGridJobID) }
|
102 |
corvo |
1.1 |
|
103 |
corvo |
1.2 |
# Complex filters (1-to-many relation)
|
104 |
|
|
reportCommands = {}
|
105 |
corvo |
1.1 |
|
106 |
corvo |
1.2 |
def readArgs(lines) :
|
107 |
|
|
argValues = {}
|
108 |
|
|
for line in lines :
|
109 |
|
|
paramName = 'unknown'
|
110 |
|
|
paramValue = 'unknown'
|
111 |
|
|
line = line.strip()
|
112 |
|
|
if line.find('=') != -1 :
|
113 |
|
|
split = line.split('=')
|
114 |
|
|
paramName = split[0]
|
115 |
|
|
paramValue = '='.join(split[1:])
|
116 |
|
|
else :
|
117 |
|
|
paramName = line
|
118 |
|
|
argValues[paramName] = paramValue
|
119 |
|
|
return argValues
|
120 |
|
|
|
121 |
|
|
def filterArgs(argValues) :
|
122 |
|
|
|
123 |
|
|
contextValues = {}
|
124 |
|
|
paramValues = {}
|
125 |
|
|
command = None
|
126 |
|
|
|
127 |
|
|
for paramName in argValues.keys() :
|
128 |
|
|
|
129 |
|
|
if paramName in reportFilters.keys() :
|
130 |
|
|
newParamName = reportFilters[paramName][0]
|
131 |
|
|
newParamFilter = reportFilters[paramName][1]
|
132 |
|
|
newParamValue = newParamFilter()
|
133 |
|
|
argValues[newParamName] = newParamValue
|
134 |
|
|
argValues.__delitem__(paramName)
|
135 |
|
|
|
136 |
|
|
elif paramName in reportCommands.keys() :
|
137 |
|
|
commandFilter = reportCommands[command]
|
138 |
|
|
commandFilter(argValues)
|
139 |
|
|
argValues.__delitem__(paramName)
|
140 |
|
|
|
141 |
|
|
for paramName in argValues.keys() :
|
142 |
|
|
paramValue = argValues[paramName]
|
143 |
|
|
if paramValue is not None :
|
144 |
|
|
if paramName in contextConf.keys() :
|
145 |
|
|
contextValues[paramName] = paramValue
|
146 |
|
|
else :
|
147 |
|
|
paramValues[paramName] = paramValue
|
148 |
|
|
else :
|
149 |
|
|
logger('Bad value for parameter :' + paramName)
|
150 |
|
|
|
151 |
|
|
return contextValues, paramValues
|
152 |
|
|
|
153 |
|
|
def report(args) :
|
154 |
|
|
argValues = readArgs(args)
|
155 |
|
|
setGridJobID(argValues)
|
156 |
|
|
contextArgs, paramArgs = filterArgs(argValues)
|
157 |
|
|
logger('context : ' + `contextArgs`)
|
158 |
|
|
logger('params : ' + `paramArgs`)
|
159 |
|
|
context = getContext(contextArgs)
|
160 |
|
|
send(context, paramArgs)
|
161 |
|
|
print "Parameters sent to Dashboard."
|
162 |
|
|
|
163 |
corvo |
1.1 |
##
|
164 |
|
|
## MAIN PROGRAM
|
165 |
|
|
##
|
166 |
|
|
if __name__ == '__main__' :
|
167 |
|
|
args = sys.argv[1:]
|
168 |
corvo |
1.2 |
if len(args) > 0 and args[0] == 'TEST' :
|
169 |
|
|
apm = apmon.ApMon('http://lxgate35.cern.ch:40808/ApMonConf', apmon.Logger.WARNING)
|
170 |
|
|
apm.sendParameters('Test', 'testjob_0_' + getGridJobID(), {'test':'0'})
|
171 |
|
|
apm.free()
|
172 |
corvo |
1.1 |
sys.exit(0)
|
173 |
corvo |
1.2 |
report(args)
|
174 |
corvo |
1.1 |
sys.exit(0)
|