source: trunk/DataCheck/Sourcefile.sh@ 13232

Last change on this file since 13232 was 13036, checked in by Daniela Dorner, 13 years ago
moved crontab files to cron directory
  • Property svn:executable set to *
File size: 13.9 KB
Line 
1#!/bin/bash
2
3# data paths
4auxdata=/data00/fact-construction/aux
5rawdata=/data00/fact-construction/raw
6rawdata_for_sed=$(printf "%s\n" "$rawdata" | sed 's/[][\.*^$(){}?+|/]/\\&/g')
7ziprawdata=/loc_data/zipraw
8ziprawdata_for_sed=$(printf "%s\n" "$ziprawdata" | sed 's/[][\.*^$(){}?+|/]/\\&/g')
9
10# setup to use ftools
11export HEADAS=/opt/heasoft-6.11/x86_64-unknown-linux-gnu-libc2.13-0/
12export HEADASPROMPT=/dev/null
13
14# software versions
15#export factpath=/home/fact/SW.automatic.processing/FACT++.2012.01.25
16export mars=~/Mars.von.Thomas.2012.03.07
17
18export AUTOMATIONSETUP="isdc.fact"
19#export SOURCEFILEPATH=`dirname/datacenter/scripts
20
21#source $SOURCEFILEPATH/sourcefile
22
23# Author(s): Daniela Dorner 05/2005 <mailto:dorner@astro.uni-wuerzburg.de>
24#
25# Copyright: MAGIC Software Development, 2000-2010
26#
27#
28# ========================================================================
29#
30# This a resource file for the scripts, in which the standard paths and
31# functions, which are needed more often are stored.
32# Only constant variables are stored here, changing variables are stored
33# in datacenter/scripts/setup
34#
35
36# to treat aliases in bash-script correctly
37shopt -s expand_aliases
38
39# check if script has been started with absolute path
40if ! dirname $0 | grep -E '^/' >/dev/null 2>&1
41then
42 echo "Please start your script with an absolute path."
43 exit
44fi
45
46if [ "$AUTOMATIONSETUP" = "" ]
47then
48 echo "Please set the environment variable \$AUTOMATIONSETUP."
49 exit
50fi
51
52if [ "$SOURCEFILEPATH" = "" ]
53then
54 export SOURCEFILEPATH=`dirname $0`
55fi
56if [ "$SCRIPTNAME" = "" ]
57then
58 SCRIPTNAME=`basename $0`
59fi
60
61source $SOURCEFILEPATH/../Setup/setup.$AUTOMATIONSETUP
62
63datetime=`date +%F-%H-%M-%S`
64
65
66# function to make sure that a directory is made
67function makedir()
68{
69 if [ ! -d $@ ]
70 then
71 mkdir -pv $@
72 if [ ! -d $@ ]
73 then
74 if ! [ "$processlog" = "" ]
75 then
76 echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "$SCRIPTNAME"["$$"] ERROR could not make dir "$@ >> $processlog
77 else
78 echo "could not make dir "$@
79 fi
80 if ls $lockfile >/dev/null 2>&1
81 then
82 rm -v $lockfile
83 fi
84 exit
85 fi
86 fi
87}
88
89# logging paths for runlogs and processlog
90runlogpath=$logpath/run/`date +%Y/%m/%d`
91processlogpath=$logpath/processlog
92makedir $runlogpath
93makedir $processlogpath
94processlog=$processlogpath/process`date +%F`.log
95
96makedir $lockpath
97
98
99# function to provide proper logging in a single logfile ($processlog)
100function printprocesslog
101{
102 makedir $processlogpath
103 echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "$SCRIPTNAME"["$$"] "$@ >> $processlog
104}
105
106# function to exit a script properly
107function finish()
108{
109 if ! [ "$lockfile" = "" ] && ls $lockfile >/dev/null 2>&1
110 then
111 printprocesslog "DEBUG " `rm -v $lockfile`
112 fi
113 printprocesslog "DEBUG finished "$SOURCEFILEPATH"/"$SCRIPTNAME
114 exit
115}
116
117
118# set checkvalue to ok at the beginning of the scripts
119check="ok"
120
121# setup for jobmanager:
122# log files (can't be defined in script itself, as script can run longer
123# than one day
124jmerrorlog=$runlogpath/jobmanager-`whoami`-$HOSTNAME-$AUTOMATIONSETUP-`date +%F`-error.log
125jmscriptlog=$runlogpath/jobmanager-`whoami`-$HOSTNAME-$AUTOMATIONSETUP-`date +%F`.log
126
127# check if rc-files are available
128if ! ls $steps >/dev/null
129then
130 echo "Can't find steps.rc ($steps)"
131 finish
132fi
133if ! ls $sqlrc >/dev/null
134then
135 echo "Can't find sql.rc ($sqlrc)"
136 finish
137fi
138
139# resetting values for jobmanager
140pno=0
141totalpno=0
142running=0
143queued=0
144runningscript=0
145queuedscript=0
146stillinqueue=0
147
148
149# alias (we cannot check the beginning of the line due to
150# color codes in filldotraw.C)
151alias 'intgrep'='grep -E -o \\\(int\\\)[0-9]+$ | grep -E -o [0-9]+'
152
153
154# in the following the functions, which are needed by several scripts, are
155# defined
156
157# function to check if a process is already locked
158# command line option can be used to execute something, e.g. 'continue'
159function checklock()
160{
161 if ! echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "$SCRIPTNAME"["$$"] "`uname -a` > $lockfile 2>/dev/null
162 then
163 printprocesslog "WARN lockfile $lockfile exists"
164 $@
165 exit
166 else
167 printprocesslog "DEBUG created lockfile $lockfile"
168 fi
169}
170
171# print the current status values
172function printstatusvalues()
173{
174 echo "the current values are:"
175 echo " starttime=$starttime"
176 echo " stoptime=$stoptime"
177 echo " availtime=$availtime"
178 echo " returncode=$returncode"
179 echo "-- check: -$check-"
180 echo ""
181}
182
183# get the db-setup from the sql.rc
184function getdbsetup()
185{
186 db=`grep Database $sqlrc | grep -v '#' | sed -e 's/Database: //' -e 's/ //g'`
187 pw=`grep Password $sqlrc | grep -v '#' | sed -e 's/Password: //' -e 's/ //g'`
188 us=`grep User $sqlrc | grep -v '#' | sed -e 's/User: //' -e 's/ //g'`
189 ho=`grep URL $sqlrc | grep -v '#' | sed -e 's/ //g' -e 's/URL:mysql:\/\///'`
190# echo "setup: "
191# echo " db: "$db
192# echo " pw: "$pw
193# echo " us: "$us
194# echo " ho: "$ho
195}
196
197# function to send a mysql query
198function sendquery()
199{
200 getdbsetup
201 printprocesslog "DEBUG sendquery QUERY: "$query
202 if ! val=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
203 then
204 printprocesslog "ERROR could not query DB "$db" on host "$ho" with user "$us
205 #printprocesslog "ERROR could not query db (program: $program, function sendquery)"
206 #return 1 #why???
207 finish
208 fi
209 if [ "$val" = "NULL" ]
210 then
211 val=
212 fi
213 echo $val
214 return 0
215}
216
217# function to get information from the setupfile $steps
218function getfromsetup()
219{
220 grep $1"[.]"$2":" $steps | grep -v '#' | sed -e "s/$1[.]$2://"
221}
222
223# function to get the needed information from the dependencies-file steps.rc
224function getstepinfo()
225{
226 getdbsetup
227 needs=( `getfromsetup $step "Needs"` )
228 noderestricted=`getfromsetup $step "NodeRestricted"`
229 prims=( `getfromsetup $step "Primaries"` )
230 maintable=`getfromsetup $step "MainTable" | sed -e "s/\ //g"`
231# echo " maintable: "$maintable
232# echo " needs: "${needs[@]}
233# echo " noderestricted: "$noderestricted
234# echo " prims: "${prims[@]}
235}
236
237# function to get the joins needed for the get/set status queries
238function getalljoins()
239{
240 # add table
241 query=$query" "$maintable"Status"
242 # add special join
243 query=$query" "`getfromsetup $maintable "SpecialJoin"`
244 # add join for step unless step is the same as maintable
245 if ! [ "$step" = "$maintable" ]
246 then
247 query=$query" LEFT JOIN "$step"Status USING("${prims[@]}") "
248 fi
249 # add joins for influences or needs
250 for otherstep in ${othersteps[@]}
251 do
252 if ! [ "$otherstep" = "$maintable" ]
253 then
254 query=$query" LEFT JOIN "$otherstep"Status USING("`getfromsetup $otherstep "Primaries"`") "
255 fi
256 done
257}
258
259# function to create the middle part of a query
260# which is identical for the functions getstatus() and gettodo()
261function getstatusquery()
262{
263 # add from which table the information is queried
264 query=$query" FROM "
265 othersteps=${needs[@]}
266 getalljoins
267 # add condition
268 query=$query" WHERE "
269 # add condition for step, i.e. step is not yet done
270 query=$query" ISNULL("$step"Status.fStartTime) "
271 query=$query" AND ISNULL("$step"Status.fStopTime) "
272 query=$query" AND ISNULL("$step"Status.fAvailable) "
273 query=$query" AND ISNULL("$step"Status.fReturnCode) "
274 # add requirement for production host in case it is needed
275 if [ "$1 " != " " ]
276 then
277 query=$query" AND fProductionHostKEY=$2 "
278 fi
279 if ! echo $query | grep UPDATE >/dev/null 2>&1
280 then
281 query=$query" GROUP BY "${prims[@]}
282 fi
283 # add condition for needs, i.e. that step is done
284 for (( k=0 ; k < ${#needs[@]} ; k++ ))
285 do
286 if [ $k -eq 0 ]
287 then
288 query=$query" HAVING "
289 else
290 query=$query" AND "
291 fi
292 query=$query" COUNT(*)=COUNT(IF("
293 query=$query" NOT ISNULL("${needs[$k]}"Status.fStartTime) "
294 query=$query" AND NOT ISNULL("${needs[$k]}"Status.fStopTime) "
295 query=$query" AND NOT ISNULL("${needs[$k]}"Status.fAvailable) "
296 query=$query" AND ISNULL("${needs[$k]}"Status.fReturnCode) "
297 query=$query" , 1, NULL)) "
298 done
299}
300
301# function to get todolist
302# returns the next or the list of next steps
303function gettodo()
304{
305 # reset the variable for the number of the next step
306 process=
307 printprocesslog "DEBUG getting todo for step $step..."
308 getstepinfo
309 # get query
310 query=" SELECT "${prims[@]}
311 getstatusquery $2
312 # order by priority to the the number of the next step to be done
313 query=$query" ORDER BY "$step"Status.fPriority desc "
314 # add limitation in case only one or a limited number of
315 # processes should be executed
316 if [ "$1 " != " " ]
317 then
318 query=$query" limit 0, $1 "
319 fi
320 # print query
321 printprocesslog "DEBUG gettodo for step $step QUERY: "$query
322 # execute query
323 process=`sendquery`
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 # get numbers of next step from mysql result
330 if [ "$process" = "" ]
331 then
332 printprocesslog "DEBUG => nothing to do"
333 finish
334 else
335 primaries=( $process )
336 num=`expr ${#primaries[@]} / ${#prims[@]} `
337 fi
338}
339
340# function to get the number of processes which still have to be done
341function getstatus()
342{
343 # reset the variable for the number of steps to be done
344 numproc=0
345 getstepinfo
346 # get query
347 query=" SELECT "${prims[@]}
348 getstatusquery $1
349 # print query
350 printprocesslog "DEBUG getstatus for step $step QUERY: "$query
351 # execute query
352 #numproc=`sendquery `#cannot be done with sendquery, because of row counting
353 if ! numproc=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query " | wc -l`
354 then
355 printprocesslog "ERROR could not query number of processes from db (program: $program, function getstatus)"
356 echo `date +%F\ %T`" ERROR could not query number of processes from db (program: $program, function getstatus)"
357 continue
358 fi
359}
360
361# function to set status of a process in the db
362function setstatus()
363{
364 # remark:
365 # this function does not include the 'Default' flag
366 # for resetting steps
367
368 # for dowebplots (there are steps which have no entry in the DB)
369 if [ "$step" = "no" ]
370 then
371 return
372 fi
373
374 # reset status values
375 starttime=NULL
376 stoptime=NULL
377 availtime=NULL
378 returncode=NULL
379 # evaluate the status values
380 case $@ in
381 start) printprocesslog "DEBUG setstatus start"
382 starttime="Now()"
383 ;;
384 stop) case $check in
385 ok) printprocesslog "DEBUB setstatus stop - ok"
386 starttime=noreset
387 stoptime="Now()"
388 if [ "$processingsite" = "$storagesite" ]
389 then
390 availtime="Now()"
391 fi
392 ;;
393 no) printprocesslog "DEBUG setstatus stop - nothing new"
394 check="ok"
395 ;;
396 *) printprocesslog "DEBUG setstatus stop - failed"
397 starttime=noreset
398 stoptime="Now()"
399 if [ "$processingsite" = "$storagesite" ]
400 then
401 availtime="Now()"
402 fi
403 if [ "$check" == "" ]
404 then
405 returncode=1
406 else
407 returncode=$check
408 fi
409 check="ok"
410 ;;
411 esac
412 ;;
413 *) printprocesslog "ERROR function setstatus got wrong variable"
414 finish
415 ;;
416 esac
417
418 # get
419 getstepinfo
420
421 # get the influences from the steps.rc by evaluating the needs of all steps
422 othersteps=`grep $step $steps | grep -v '#' | grep "Needs" | grep -v "$step[.]Needs" | cut -d'.' -f1`
423
424 # get query
425 query=" UPDATE "
426 getalljoins
427 # set the status values according to the new status of the step
428 query=$query" SET "
429 if ! [ "$starttime" = "noreset" ]
430 then
431 query=$query" "$step"Status.fStartTime=$starttime, "
432 fi
433 query=$query" "$step"Status.fStopTime=$stoptime, "$step"Status.fAvailable=$availtime"
434 query=$query", "$step"Status.fReturnCode=$returncode , "$step"Status.fProcessingSiteKEY=$sitekey "
435 # set also the status values of the influenced steps
436 for otherstep in $othersteps
437 do
438 query=$query", "$otherstep"Status.fStartTime=NULL "
439 query=$query", "$otherstep"Status.fStopTime=NULL "
440 query=$query", "$otherstep"Status.fAvailable=NULL "
441 query=$query", "$otherstep"Status.fReturnCode=NULL "
442 query=$query", "$otherstep"Status.fProcessingSiteKEY=NULL "
443 done
444 # give the condition for which step the status values have to be set
445 query=$query" WHERE "
446 if [ "$s" = "" ]
447 then
448 s=0
449 fi
450 query=$query" "$step"Status."`echo ${prims[0]} | sed -e 's/,//g'`"='${primaries[$s*${#prims[@]}]}'"
451 for (( j=1 ; j < ${#prims[@]} ; j++ ))
452 do
453 query=$query" AND "$step"Status."`echo ${prims[$j]} | sed -e 's/,//g'`"='${primaries[$s*${#prims[@]}+$j]}' "
454 done
455 # add additional query to allow for locking in db
456 if [ "$1" = "start" ]
457 then
458 query=$query" AND ISNULL("$step"Status.fStartTime) "
459 fi
460 # add row count to know how many rows have been changed
461 query=$query"; SELECT ROW_COUNT();"
462 # print query
463 printprocesslog "DEBUG setstatus for step $step QUERY: "$query
464 # execute query
465 numchanged=`sendquery`
466 #if ! numchanged=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
467 #then
468 # printprocesslog "ERROR could not set status in db (program: $program, function setstatus)"
469 # finish
470 #fi
471 if [ $numchanged -gt 0 ]
472 then
473 printprocesslog "INFO successful set of status in DB."
474 else
475 # should not happen
476 printprocesslog "ERROR status in DB was already set by another process "
477 fi
478}
479
480
Note: See TracBrowser for help on using the repository browser.