source: trunk/DataCheck/Sourcefile.sh@ 18763

Last change on this file since 18763 was 18763, checked in by Daniela Dorner, 8 years ago
handle empty $numchanged
  • Property svn:executable set to *
File size: 17.0 KB
Line 
1#!/bin/bash
2
3# to treat aliases in bash-script correctly
4shopt -s expand_aliases
5
6# check if script has been started with absolute path
7if ! dirname $0 | grep -E '^/' >/dev/null 2>&1
8then
9 echo "Please start your script with an absolute path."
10 exit
11fi
12
13if [ "$AUTOMATIONSETUP" = "" ]
14then
15 echo "Please set the environment variable \$AUTOMATIONSETUP."
16 exit
17fi
18
19if [ "$SOURCEFILEPATH" = "" ]
20then
21 export SOURCEFILEPATH=`dirname $0`
22fi
23if [ "$SCRIPTNAME" = "" ]
24then
25 SCRIPTNAME=`basename $0`
26fi
27
28source $SOURCEFILEPATH/../Setup/setup.$AUTOMATIONSETUP
29
30if [ "$mars" = "" ]
31then
32 echo "Please set the path for MARS."
33 exit
34fi
35
36datetime=`date +%F-%H-%M-%S`
37
38
39# function to make sure that a directory is made
40function makedir()
41{
42 if [ ! -d $@ ]
43 then
44 if [ "$processlog" = "" ] || [ "$logfile" = "" ]
45 then
46 mkdir -p $@
47 else
48 mkdir -pv $@
49 fi
50 if [ ! -d $@ ]
51 then
52 if ! [ "$processlog" = "" ]
53 then
54 echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "$SCRIPTNAME"["$$"] ERROR could not make dir "$@ >> $processlog
55 else
56 echo "could not make dir "$@
57 fi
58 if ls $lockfile >/dev/null 2>&1
59 then
60 rm -v $lockfile
61 fi
62 exit
63 fi
64 fi
65}
66
67# logging paths for runlogs and processlog
68runlogpath=$logpath/run/`date +%Y/%m/%d`
69processlogpath=$logpath/processlog
70makedir $runlogpath
71makedir $processlogpath
72processlog=$processlogpath/process`date +%F`.log
73
74makedir $lockpath
75
76
77# function to provide proper logging in a single logfile ($processlog)
78function printprocesslog
79{
80 makedir $processlogpath
81 echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "$SCRIPTNAME"["$$"] "$@ >> $processlog
82}
83
84# function to exit a script properly
85function finish()
86{
87 if ! [ "$lockfile" = "" ] && ls $lockfile >/dev/null 2>&1
88 then
89 printprocesslog "DEBUG " `rm -v $lockfile`
90 fi
91 printprocesslog "DEBUG finished "$SOURCEFILEPATH"/"$SCRIPTNAME
92 # fixme: handle different cases - sometimes exit of script is needed, sometimes only continue
93 exit
94}
95
96
97# set checkvalue to ok at the beginning of the scripts
98check="ok"
99
100# setup for jobmanager:
101# log files (can't be defined in script itself, as script can run longer
102# than one day
103jmerrorlog=$runlogpath/jobmanager-`whoami`-$HOSTNAME-$AUTOMATIONSETUP-`date +%F`-error.log
104jmscriptlog=$runlogpath/jobmanager-`whoami`-$HOSTNAME-$AUTOMATIONSETUP-`date +%F`.log
105
106# check if rc-files are available
107if ! ls $steps >/dev/null
108then
109 echo "Can't find steps.rc ($steps)"
110 finish
111fi
112if ! ls $sqlrc >/dev/null
113then
114 echo "Can't find sql.rc ($sqlrc)"
115 finish
116fi
117
118# resetting values for jobmanager
119pno=0
120totalpno=0
121running=0
122queued=0
123runningscript=0
124queuedscript=0
125stillinqueue=0
126
127
128# alias (we cannot check the beginning of the line due to
129# color codes in filldotraw.C)
130alias 'intgrep'='grep -E -o \\\("(int|Bool_t)"\\\)[0-9]+$ | grep -E -o [0-9]+'
131
132
133# in the following the functions, which are needed by several scripts, are
134# defined
135
136# function to check if a process is already locked
137# command line option can be used to execute something, e.g. 'continue'
138function checklock()
139{
140 if ! echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "$SCRIPTNAME"["$$"] "`uname -a` > $lockfile 2>/dev/null
141 then
142 if find $lockfile -amin -5
143 then
144 printprocesslog "INFO lockfile $lockfile exists"
145 else
146 printprocesslog "WARN lockfile $lockfile exists"
147 fi
148 $@
149 exit
150 else
151 printprocesslog "DEBUG created lockfile $lockfile"
152 fi
153}
154
155# print the current status values
156function printstatusvalues()
157{
158 echo "the current values are:"
159 echo " starttime=$starttime"
160 echo " stoptime=$stoptime"
161 echo " availtime=$availtime"
162 echo " returncode=$returncode"
163 echo "-- check: -$check-"
164 echo ""
165}
166
167# get the db-setup from the sql.rc
168function getdbsetup()
169{
170 db=`grep Database $sqlrc | grep -v '#' | sed -e 's/Database: //' -e 's/ //g'`
171 pw="--password="`grep Password $sqlrc | grep -v '#' | sed -e 's/Password: //' -e 's/ //g'`
172 us=`grep User $sqlrc | grep -v '#' | sed -e 's/User: //' -e 's/ //g'`
173 ho=`grep URL $sqlrc | grep -v '#' | sed -e 's/ //g' -e 's/URL:mysql:\/\///'`
174 if [ "$sqlpw" != "" ]
175 then
176 sqlpwfile="--defaults-file=$sqlpw"
177 pw=""
178 fi
179# echo "setup: "
180# echo " db: "$db
181# echo " pw: "$pw
182# echo " us: "$us
183# echo " ho: "$ho
184}
185
186# function to send a mysql query
187function sendquery()
188{
189 getdbsetup
190 printprocesslog "DEBUG sendquery QUERY: "$query
191 val=`mysql $sqlpwfile -s -u $us $pw --host=$ho $db -e " $query " 2>&1`
192 checkmysql=`echo $?`
193 if [ $checkmysql -gt 0 ]
194 then
195 printprocesslog "ERROR could not query DB "$db" on host "$ho" with user "$us" (program: $program, function sendquery) "
196 printprocesslog "MYSQLERROR [returncode: "$checkmysql"] "$val
197 error=`echo $val | grep -E -o 'ERROR [1-9]{1,4}' | grep -E -o '[1-9]{1,4}'`
198 # here possible reaction to mysql error
199 val=
200 finish
201 fi
202 if [ "$val" = "NULL" ]
203 then
204 val=
205 fi
206 echo $val
207 return 0
208}
209
210# function to get information from the setupfile $steps
211function getfromsetup()
212{
213 grep $1"[.]"$2":" $steps | grep -v '#' | sed -e "s/$1[.]$2://"
214}
215
216# function to get the needed information from the dependencies-file steps.rc
217function getstepinfo()
218{
219 getdbsetup
220 needs=( `getfromsetup $step "Needs"` )
221 noderestricted=`getfromsetup $step "NodeRestricted"`
222 prims=( `getfromsetup $step "Primaries"` )
223 maintable=`getfromsetup $step "MainTable" | sed -e "s/\ //g"`
224 sort=`getfromsetup $step "SortDirection" | sed -e "s/\ //g"`
225# echo " maintable: "$maintable
226# echo " needs: "${needs[@]}
227# echo " noderestricted: "$noderestricted
228# echo " prims: "${prims[@]}
229}
230
231# function to get the joins needed for the get/set status queries
232function getalljoins()
233{
234 # add table
235 query=$query" "$maintable"Status"
236 # add special join
237 query=$query" "`getfromsetup $maintable "SpecialJoin"`
238 # add join for step unless step is the same as maintable
239 if ! [ "$step" = "$maintable" ]
240 then
241 query=$query" LEFT JOIN "$step"Status USING("${prims[@]}") "
242 fi
243 # add joins for influences or needs
244 for otherstep in ${othersteps[@]}
245 do
246 if ! [ "$otherstep" = "$maintable" ]
247 then
248 query=$query" LEFT JOIN "$otherstep"Status USING("`getfromsetup $otherstep "Primaries"`") "
249 fi
250 done
251}
252
253# function to create the middle part of a query
254# which is identical for the functions getstatus() and gettodo()
255function getstatusquery()
256{
257 # add from which table the information is queried
258 query=$query" FROM "
259 othersteps=${needs[@]}
260 getalljoins
261 # add condition
262 query=$query" WHERE "
263 # add condition for step, i.e. step is not yet done
264 query=$query" ISNULL("$step"Status.fStartTime) "
265 query=$query" AND ISNULL("$step"Status.fStopTime) "
266 query=$query" AND ISNULL("$step"Status.fAvailable) "
267 query=$query" AND ISNULL("$step"Status.fReturnCode) "
268 # add requirement for production host in case it is needed
269 if [ "$1 " != " " ]
270 then
271 query=$query" AND fProductionHostKEY=$2 "
272 fi
273 query=$query`getfromsetup $step "SpecialWhere"`
274 # add condition for needs, i.e. that step is done
275 for (( k=0 ; k < ${#needs[@]} ; k++ ))
276 do
277# if [ $k -eq 0 ]
278# then
279# query=$query" HAVING "
280# else
281 query=$query" AND "
282# fi
283# query=$query" COUNT(*)=COUNT(IF("
284 query=$query" NOT ISNULL("${needs[$k]}"Status.fStartTime) "
285 query=$query" AND NOT ISNULL("${needs[$k]}"Status.fStopTime) "
286 query=$query" AND NOT ISNULL("${needs[$k]}"Status.fAvailable) "
287 query=$query" AND ISNULL("${needs[$k]}"Status.fReturnCode) "
288# query=$query" , 1, NULL)) "
289 done
290# if ! echo $query | grep UPDATE >/dev/null 2>&1
291# then
292# query=$query" GROUP BY "${prims[@]}
293# fi
294}
295
296# function to get todolist
297# returns the next or the list of next steps
298function gettodo()
299{
300 # reset the variable for the number of the next step
301 process=
302 printprocesslog "DEBUG getting todo for step $step..."
303 getstepinfo
304 # get query
305 query=" SELECT "${prims[@]}
306 getstatusquery $2
307 # order by priority to the the number of the next step to be done
308 query=$query" ORDER BY "$step"Status.fPriority "
309 if [ "$sort" = "" ]
310 then
311 query=$query" DESC "
312 else
313 query=$query" "$sort
314 fi
315 # add limitation in case only one or a limited number of
316 # processes should be executed
317 if [ "$1 " != " " ]
318 then
319 query=$query" limit 0, $1 "
320 fi
321 # print query
322 #printprocesslog "DEBUG gettodo for step $step QUERY: "$query
323 # execute query
324 process=`sendquery`
325 #if ! process=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
326 #then
327 # printprocesslog "ERROR could not query processes from db (program: $program, function gettodo)"
328 # finish
329 #fi
330 # get numbers of next step from mysql result
331 if [ "$process" = "" ]
332 then
333 printprocesslog "DEBUG => nothing to do"
334 finish
335 else
336 primaries=( $process )
337 num=`expr ${#primaries[@]} / ${#prims[@]} `
338 fi
339}
340
341# function to get the number of processes which still have to be done
342function getstatus()
343{
344 printprocesslog "DEBUG getstatus for step "$step
345 # reset the variable for the number of steps to be done
346 numproc=0
347 getstepinfo
348 # get query
349 query=" SELECT "${prims[@]}
350 getstatusquery $1
351 # print query
352 #printprocesslog "DEBUG getstatus for step $step QUERY: "$query
353 # execute query
354 #numproc=`sendquery `#cannot be done with sendquery, because of row counting
355 if ! numproc=`mysql $sqlpwfile -s -u $us $pw --host=$ho $db -e " $query " | wc -l`
356 then
357 printprocesslog "ERROR could not query number of processes from db (program: $program, function getstatus)"
358 echo `date +%F\ %T`" ERROR could not query number of processes from db (program: $program, function getstatus)"
359 continue
360 fi
361}
362
363# function to set status of a process in the db
364function setstatus()
365{
366 # remark:
367 # this function does not include the 'Default' flag
368 # for resetting steps
369
370 # for dowebplots (there are steps which have no entry in the DB)
371 if [ "$step" = "no" ]
372 then
373 return
374 fi
375
376 printprocesslog "DEBUG setstatus for step "$step
377
378 # reset status values
379 starttime=NULL
380 stoptime=NULL
381 availtime=NULL
382 returncode=NULL
383 # evaluate the status values
384 case $@ in
385 start) printprocesslog "DEBUG setstatus start"
386 starttime="Now()"
387 ;;
388 stop) case $check in
389 ok) printprocesslog "DEBUB setstatus stop - ok"
390 starttime=noreset
391 stoptime="Now()"
392 if [ "$processingsite" = "$storagesite" ]
393 then
394 availtime="Now()"
395 fi
396 ;;
397 no) printprocesslog "DEBUG setstatus stop - nothing new"
398 check="ok"
399 ;;
400 *) printprocesslog "DEBUG setstatus stop - failed"
401 starttime=noreset
402 stoptime="Now()"
403 if [ "$processingsite" = "$storagesite" ]
404 then
405 availtime="Now()"
406 fi
407 if [ "$check" == "" ]
408 then
409 returncode=1
410 else
411 returncode=$check
412 fi
413 check="ok"
414 ;;
415 esac
416 ;;
417 *) printprocesslog "ERROR function setstatus got wrong variable"
418 finish
419 ;;
420 esac
421
422 # get
423 getstepinfo
424
425 # get the influences from the steps.rc by evaluating the needs of all steps
426 othersteps=`grep $step $steps | grep -v '#' | grep "Needs" | grep -v "$step[.]Needs" | cut -d'.' -f1`
427
428 # get query
429 query=" UPDATE "
430 getalljoins
431 # set the status values according to the new status of the step
432 query=$query" SET "
433 if ! [ "$starttime" = "noreset" ]
434 then
435 query=$query" "$step"Status.fStartTime=$starttime, "
436 fi
437 query=$query" "$step"Status.fStopTime=$stoptime, "$step"Status.fAvailable=$availtime"
438 query=$query", "$step"Status.fReturnCode=$returncode , "$step"Status.fProcessingSiteKEY=$sitekey "
439 # set also the status values of the influenced steps
440 for otherstep in $othersteps
441 do
442 query=$query", "$otherstep"Status.fStartTime=NULL "
443 query=$query", "$otherstep"Status.fStopTime=NULL "
444 query=$query", "$otherstep"Status.fAvailable=NULL "
445 query=$query", "$otherstep"Status.fReturnCode=NULL "
446 query=$query", "$otherstep"Status.fProcessingSiteKEY=NULL "
447 done
448 # give the condition for which step the status values have to be set
449 query=$query" WHERE "
450 if [ "$s" = "" ]
451 then
452 s=0
453 fi
454 query=$query" "$step"Status."`echo ${prims[0]} | sed -e 's/,//g'`"='${primaries[$s*${#prims[@]}]}'"
455 for (( j=1 ; j < ${#prims[@]} ; j++ ))
456 do
457 query=$query" AND "$step"Status."`echo ${prims[$j]} | sed -e 's/,//g'`"='${primaries[$s*${#prims[@]}+$j]}' "
458 done
459 # add additional query to allow for locking in db
460 if [ "$1" = "start" ]
461 then
462 query=$query" AND ISNULL("$step"Status.fStartTime) "
463 fi
464 # add row count to know how many rows have been changed
465 query=$query"; SELECT ROW_COUNT();"
466 # print query
467 #printprocesslog "DEBUG setstatus for step $step QUERY: "$query
468 #echo "DEBUG setstatus for step $step QUERY: "$query
469 # execute query
470 numchanged=`sendquery`
471 # should not be needed anymore once finish does proper exit
472 printprocesslog "DEBUG numchanged "$numchanged
473 if [ "$numchanged" = "" ]
474 then
475 printprocesslog "DEBUG numchanged empty."
476 finish
477 fi
478 if [ $numchanged -gt 0 ]
479 then
480 printprocesslog "INFO successful set of status in DB."
481 #echo "INFO successful set of status in DB."
482 else
483 # action may be taken in script using $numchanged
484 printprocesslog "DEBUG status in DB was already set by another process "
485 #echo "ERROR status in DB was already set by another process "
486 fi
487}
488
489function getdates()
490{
491 case $1 in
492 # all dates
493 all)
494 printprocesslog "DEBUG getdates case 'all'"
495 dates=( `find $auxdata -mindepth 3 -type d | sort -r | sed "s/\${auxdata_for_sed}//g" | sed -e 's/^\///'` )
496 ;;
497 # certain date
498 [0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]|[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]-[0-9][0-9][0-9])
499 d=`echo $1 | grep -o '[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]'`
500 range=`echo $1 | grep -o '[0-9][0-9][0-9]$'`
501 range=`echo $range | sed -e 's/^0//' -e 's/^0//'`
502 printprocesslog "DEBUG getdates - certain date "$d"."
503 dates=( $d )
504 if [ "$range" != "" ]
505 then
506 printprocesslog "DEBUG getdates - add the last "$range" days."
507 yy=`echo $d | cut -c 1-4`
508 mm=`echo $d | cut -c 6-7`
509 dd=`echo $d | cut -c 9-10`
510 for (( numdates=1 ; numdates <= $range ; numdates++ ))
511 do
512 numhours=`echo " 12 + ( $numdates - 1 ) * 24 " | bc -l`
513 dates=( ${dates[@]} `date +%Y/%m/%d --date=$yy/$mm/$dd"-"$numhours"hour"` )
514 done
515 fi
516 ;;
517 # certain number of dates (between 0 and 9999)
518 [1-9][0-9][0-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9]|[1-9])
519 # get last n nights
520 printprocesslog "DEBUG getdates - get the last "$1" days."
521 for (( numdates=1 ; numdates <= $1 ; numdates++ ))
522 do
523 numhours=`echo " 12 + ( $numdates - 1 ) * 24 " | bc -l`
524 dates=( ${dates[@]} `date +%Y/%m/%d --date="-"$numhours"hour"` )
525 done
526 # hour-dependent number of dates
527 if [ "$2" != "" ] && [ "$3" != "" ]
528 then
529 # get current hour
530 hour=`date +%k`
531 if [ $hour -le $2 ] || [ $hour -ge $3 ]
532 then
533 printprocesslog "DEBUG getdates - get the current night."
534 dates=( `date +%Y/%m/%d --date="-12hour"` )
535 fi
536 fi
537 ;;
538 # certain number of dates in the future (between 0 and 9)
539 +[1-9])
540 # get next n nights
541 printprocesslog "DEBUG getdates - get the next "$1" days (incl today)."
542 for (( numdates=1 ; numdates <= $1 ; numdates++ ))
543 do
544 numhours=`echo " 0 + ( $numdates - 1 ) * 24 " | bc -l`
545 dates=( ${dates[@]} `date +%Y/%m/%d --date="+"$numhours"hour"` )
546 done
547 # hour-dependent number of dates
548 if [ "$2" != "" ] && [ "$3" != "" ]
549 then
550 # get current hour
551 hour=`date +%k`
552 if [ $hour -le $2 ] || [ $hour -ge $3 ]
553 then
554 printprocesslog "DEBUG getdates - get the current night."
555 dates=( `date +%Y/%m/%d --date="-12hour"` )
556 fi
557 fi
558 ;;
559 *) # nothing valid given
560 echo "Please give valid option (YYYY/MM/DD[-RRR] or 1-9999 or +0-9)"
561 finish
562 ;;
563 esac
564}
565
Note: See TracBrowser for help on using the repository browser.