source: tags/Mars-V2.4/datacenter/scripts/sourcefile

Last change on this file was 9504, checked in by Daniela Dorner, 16 years ago
*** empty log message ***
  • Property svn:executable set to *
File size: 11.3 KB
Line 
1#!/bin/sh
2#
3# ========================================================================
4#
5# *
6# * This file is part of MARS, the MAGIC Analysis and Reconstruction
7# * Software. It is distributed to you in the hope that it can be a useful
8# * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
9# * It is distributed WITHOUT ANY WARRANTY.
10# *
11# * Permission to use, copy, modify and distribute this software and its
12# * documentation for any purpose is hereby granted without fee,
13# * provided that the above copyright notice appear in all copies and
14# * that both that copyright notice and this permission notice appear
15# * in supporting documentation. It is provided "as is" without express
16# * or implied warranty.
17# *
18#
19#
20# Author(s): Daniela Dorner 05/2005 <mailto:dorner@astro.uni-wuerzburg.de>
21#
22# Copyright: MAGIC Software Development, 2000-2007
23#
24#
25# ========================================================================
26#
27# This a resource file for the scripts, in which the standard paths and
28# functions, which are needed more often are stored.
29# Only constant variables are stored here, changing variables are stored
30# in datacenter/scripts/setup
31#
32
33# check if script has been started with absolute path
34if ! dirname $0 | grep -E '^/' >/dev/null 2>&1
35then
36 echo "Please start your script with an absolute path."
37 exit
38fi
39
40if [ "$AUTOMATIONSETUP" = "" ]
41then
42 echo "Please set the environment variable \$AUTOMATIONSETUP."
43 exit
44fi
45
46source `dirname $0`/setup.$AUTOMATIONSETUP
47
48datetime=`date +%F-%H-%M-%S`
49
50
51# function to make sure that a directory is made
52function makedir()
53{
54 if [ ! -d $@ ]
55 then
56 mkdir -pv $@
57 if [ ! -d $@ ]
58 then
59 if ! [ "$processlog" = "" ]
60 then
61 echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "`basename $0`"["$$"] could not make dir "$@ >> $processlog
62 else
63 echo "could not make dir "$@
64 fi
65 if ls $lockfile >/dev/null 2>&1
66 then
67 rm -v $lockfile
68 fi
69 exit
70 fi
71 fi
72}
73
74# logging paths for runlogs and processlog
75runlogpath=$logpath/run/`date +%Y/%m/%d`
76processlogpath=$logpath/processlog
77makedir $runlogpath
78makedir $processlogpath
79processlog=$processlogpath/process`date +%F`.log
80
81makedir $lockpath
82
83
84# function to provide proper logging in a single logfile ($processlog)
85function printprocesslog
86{
87 makedir $processlogpath
88 echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "`basename $0`"["$$"] "$@ >> $processlog
89}
90
91# function to exit a script properly
92function finish()
93{
94 if ! [ "$lockfile" = "" ] && ls $lockfile >/dev/null 2>&1
95 then
96 printprocesslog "INFO " `rm -v $lockfile`
97 fi
98 printprocesslog "INFO finished $0"
99 exit
100}
101
102
103# set checkvalue to ok at the beginning of the scripts
104check="ok"
105
106#failed codes
107#sequence build status
108Fbuildsequ=1
109Fdoexcl=2
110#run process status
111Ftimecorr=3
112Ffillraw=4
113Fsinope=5
114Ffillsinope=6
115Fresetexcl=7
116#sequence process status
117Fwritesequfile=8
118Ffilesavail=9
119Fnoccfile=10
120Fnocacofile=11
121Fmerppcc=12
122Fmerppcaco=13
123Fcallisto=14
124Ffillcalib=15
125Ffillsignal=16
126Fstar=17
127Ffillstar=18
128#dataset process status
129Fwritedatasetfile=19
130Fstardone=20
131Fganymed=21
132Ffillganymed=22
133#again run process status
134FCompmux=26
135Fdowebplots=27
136#again mc run process status
137Fmccallisto=28
138Ffillmccalib=29
139Ffillmcsignal=30
140Fmcstar=31
141Ffillmcstar=32
142Fcorsikasimtel=33
143Fchimp=34
144Fchimpcp=35
145Fctastar=36
146Fctastarcp=37
147FctastereoA=38
148FctastereoB=39
149FctastereoC=40
150FctastereoD=41
151FctastereoE=42
152FctastereoF=43
153FctastereoG=44
154FctastereoH=45
155Fctastereocp=46
156
157# setup for jobmanager:
158# log files (can't be defined in script itself, as script can run longer
159# than one day
160jmerrorlog=$runlogpath/jobmanager-error`date +%F`.log
161jmscriptlog=$runlogpath/jobmanager`date +%F`.log
162
163# check if rc-files are available
164if ! ls $steps >/dev/null
165then
166 echo "Can't find steps.rc ($steps)"
167 finish
168fi
169if ! ls $sqlrc >/dev/null
170then
171 echo "Can't find sql.rc ($sqlrc)"
172 finish
173fi
174
175# resetting values
176pno=0
177totalpno=0
178running=0
179queued=0
180runningscript=0
181queuedscript=0
182stillinqueue=0
183
184
185# alias (we cannot check the beginning of the line due to
186# color codes in filldotraw.C)
187alias 'intgrep'='grep -E -o \\\(int\\\)[0-9]+$ | grep -E -o [0-9]+'
188
189
190# in the following the functions, which are needed by several scripts, are
191# defined
192
193# function to check if a process is already locked
194# command line option can be used to execute something, e.g. 'continue'
195function checklock()
196{
197 if ! echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "`basename $0`"["$$"] "`uname -a` > $lockfile 2>/dev/null
198 then
199 printprocesslog "WARN lockfile $lockfile exists"
200 $@
201 exit
202 else
203 printprocesslog "INFO created lockfile $lockfile"
204 fi
205}
206
207function resetstatusvalues()
208{
209 statustime=NULL
210 starttime=NULL
211 returncode=NULL
212 programid=NULL
213 failedtime=NULL
214}
215
216function printstatusvalues()
217{
218 echo "the current values are:"
219 echo " statustime=$statustime"
220 echo " starttime=$starttime"
221 echo " returncode=$returncode"
222 echo " programid=$programid"
223 echo " failedtime=$failedtime"
224 echo "-- check: -$check-"
225 echo ""
226}
227
228# function evaluating the statusvalues
229function evalstatus()
230{
231 case $@ in
232 start) printprocesslog "INFO setstatus start"
233 starttime="Now()"
234 ;;
235 stop) case $check in
236 ok) printprocesslog "INFO setstatus stop - ok"
237 statustime="Now()"
238 ;;
239 no) printprocesslog "INFO setstatus stop - nothing new"
240 check="ok"
241 ;;
242 *) printprocesslog "INFO setstatus stop - failed"
243 starttime=noreset
244 if [ "$check" == "" ]
245 then
246 returncode=1
247 else
248 returncode=$check
249 fi
250 programid=$com
251 failedtime="Now()"
252 check="ok"
253 ;;
254 esac
255 ;;
256 *) printprocesslog "ERROR function evalstatus got wrong variable"
257 finish
258 ;;
259 esac
260}
261
262function getdbsetup()
263{
264 db=`grep Database $sqlrc | grep -v '#' | sed -e 's/Database: //' -e 's/ //g'`
265 pw=`grep Password $sqlrc | grep -v '#' | sed -e 's/Password: //' -e 's/ //g'`
266 us=`grep User $sqlrc | grep -v '#' | sed -e 's/User: //' -e 's/ //g'`
267 ho=`grep URL $sqlrc | grep -v '#' | sed -e 's/ //g' -e 's/URL:mysql:\/\///'`
268# echo "setup: "
269# echo " db: "$db
270# echo " pw: "$pw
271# echo " us: "$us
272# echo " ho: "$ho
273}
274
275function getstepinfo()
276{
277 getdbsetup
278 table=`grep "$column:" $steps | sed -e "s/[.]$column://" -e 's/#//' -e 's/ //g'`
279 coltab=`grep "$column:" $steps | sed -e 's/://' -e 's/#//' -e 's/ //g'`
280 needs=`grep "$coltab[.]Needs:" $steps | sed -e "s/$coltab[.]Needs://"`
281 noderestricted=`grep "$coltab[.]NodeRestricted:" $steps | sed -e "s/$coltab[.]NodeRestricted://" -e 's/ //g'`
282 influences=`grep "$coltab[.]Influences:" $steps | sed -e "s/$coltab[.]Influences://"`
283 prims=( `grep "$table[.]Primary:" $steps | sed -e "s/$table[.]Primary://"` )
284# echo " column $column - table $table - coltab $coltab"
285# echo " needs: $needs"
286# echo " influences: $influences"
287# echo " noderestricted: $noderestricted"
288# echo " prims: ${prims[@]}"
289}
290
291# function to get todolist
292function gettodo()
293{
294 process=
295 printprocesslog "INFO getting todo..."
296 getstepinfo
297 # get query
298 query=" SELECT "${prims[0]}
299 for (( i=1 ; i < ${#prims[@]} ; i++ ))
300 do
301 query=$query", ${prims[$i]}"
302 done
303 query=$query" FROM $table WHERE "
304 if ! echo $needs | grep '#' > /dev/null
305 then
306 for need in $needs
307 do
308 query=$query" NOT ISNULL($need) AND"
309 done
310 fi
311 query=$query" ISNULL($column) "
312 if [ "$2 " != " " ]
313 then
314 query=$query" AND fProductionHostKEY=$2 "
315 fi
316 query=$query" AND ISNULL(fStartTime) AND ISNULL(fFailedTime) AND ISNULL(fProgramId) AND ISNULL(fReturnCode) "
317 query=$query" ORDER BY fPriority desc "
318 if [ "$1 " != " " ]
319 then
320 query=$query" limit 0, $1 "
321 fi
322# echo " QUERY: "$query
323 printprocesslog "INFO gettodo QUERY: "$query
324 if ! process=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
325 then
326 printprocesslog "ERROR could not query processes from db (program: $program, function gettodo)"
327 finish
328 fi
329
330 if [ "$process" = "" ]
331 then
332 printprocesslog "INFO => nothing to do"
333 finish
334 else
335 primaries=( $process )
336 num=`expr ${#primaries[@]} / ${#prims[@]} `
337 fi
338}
339
340
341# function to get the number of processes which still have to be done
342function getstatus()
343{
344 numproc=
345 getstepinfo
346 # get query
347 query=" SELECT COUNT(*) FROM $table WHERE "
348 if ! echo $needs | grep '#' > /dev/null
349 then
350 for need in $needs
351 do
352 query=$query" NOT ISNULL($need) AND"
353 done
354 fi
355 query=$query" ISNULL($column) "
356 if [ "$1 " != " " ]
357 then
358 query=$query" AND fProductionHostKEY=$1 "
359 fi
360 query=$query" AND ISNULL(fStartTime) AND ISNULL(fFailedTime) AND ISNULL(fProgramId) AND ISNULL(fReturnCode) "
361 query=$query" GROUP BY $column "
362# echo "QUERY: "$query
363 printprocesslog "INFO getstatus QUERY: "$query
364 if ! numproc=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
365 then
366 printprocesslog "ERROR could not query number of processes from db (program: $program, function getstatus)"
367 echo `date +%F\ %T`" ERROR could not query number of processes from db (program: $program, function getstatus)"
368 continue
369 fi
370 if [ "$numproc" = "" ]
371 then
372 numproc=0
373 fi
374}
375
376# function to set status of a process in the db
377function setstatus()
378{
379 if [ "$column" = "no" ]
380 then
381 return
382 fi
383 resetstatusvalues
384 evalstatus $@
385 getstepinfo
386 # get query
387 reset=`grep "$coltab[.]Reset:" $steps | sed -e "s/$coltab[.]Reset://" -e 's/ //g'`
388 if [ "$reset" = "no" ]
389 then
390 printprocesslog "ERROR You cannot reset $column for ${primaries[$s+$s]}"
391 finish
392 fi
393 query=" update $table set $column=$statustime"
394 if ! echo $influences | grep '#' > /dev/null
395 then
396 for influence in $influences
397 do
398 query=$query", $influence=NULL"
399 done
400 fi
401 if ! [ "$starttime" = "noreset" ]
402 then
403 query=$query", fStartTime=$starttime"
404 fi
405 query=$query", fFailedTime=$failedtime, fProgramId=$programid, fReturnCode=$returncode "
406 query=$query" where "
407 if [ "$s" = "" ]
408 then
409 s=0
410 fi
411 query=$query" ${prims[0]}='${primaries[$s*${#prims[@]}]}'"
412 for (( i=1 ; i < ${#prims[@]} ; i++ ))
413 do
414 query=$query" AND ${prims[$i]}='${primaries[$s*${#prims[@]}+$i]}' "
415 done
416# echo " QUERY: "$query
417 printprocesslog "INFO setstatus QUERY: "$query
418 if ! mysql -s -u $us --password=$pw --host=$ho $db -e " $query "
419 then
420 printprocesslog "ERROR could not set status in db (program: $program, function setstatus)"
421 finish
422 fi
423
424}
425
426# function to send a mysql query
427function sendquery()
428{
429 getdbsetup
430 printprocesslog "INFO sendquery QUERY: "$query
431 if ! val=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
432 then
433 printprocesslog "ERROR could not query db (program: $program, function sendquery)"
434 return 1
435 fi
436 if [ "$val" = "NULL" ]
437 then
438 val=
439 fi
440 echo $val
441 return 0
442}
443
Note: See TracBrowser for help on using the repository browser.