source: trunk/DataCheck/Sourcefile.sh@ 18779

Last change on this file since 18779 was 18774, checked in by Daniela Dorner, 8 years ago
repeat query in case of deadlock
  • Property svn:executable set to *
File size: 21.1 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
38if [ "$transferdelay" = "" ]
39then
40 # setup where immediate report of problem is needed, i.e. mainly LP
41 checknight=`date +%Y%m%d --date="-19HOUR"`
42else
43 # any setup where transfer might cause a delay in arrival of files
44 checknight=`date +%Y%m%d --date="-${transferdelay}day"`
45fi
46
47# function to make sure that a directory is made
48function makedir()
49{
50 if [ ! -d $@ ]
51 then
52 if [ "$processlog" = "" ] || [ "$logfile" = "" ]
53 then
54 mkdir -p $@
55 else
56 mkdir -pv $@
57 fi
58 if [ ! -d $@ ]
59 then
60 if ! [ "$processlog" = "" ]
61 then
62 echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "$SCRIPTNAME"["$$"] ERROR could not make dir "$@ >> $processlog
63 else
64 echo "could not make dir "$@
65 fi
66 if ls $lockfile >/dev/null 2>&1
67 then
68 rm -v $lockfile
69 fi
70 exit
71 fi
72 fi
73}
74
75# logging paths for runlogs and processlog
76runlogpath=$logpath/run/`date +%Y/%m/%d`
77processlogpath=$logpath/processlog
78makedir $runlogpath
79makedir $processlogpath
80processlog=$processlogpath/process`date +%F`.log
81
82makedir $lockpath
83
84
85# function to provide proper logging in a single logfile ($processlog)
86function printprocesslog
87{
88 makedir $processlogpath
89 echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "$SCRIPTNAME"["$$"] "$@ >> $processlog
90}
91
92# function to exit a script properly
93function finish()
94{
95 if ! [ "$lockfile" = "" ] && ls $lockfile >/dev/null 2>&1
96 then
97 printprocesslog "DEBUG " `rm -v $lockfile`
98 fi
99 printprocesslog "DEBUG finished "$SOURCEFILEPATH"/"$SCRIPTNAME
100 # fixme: handle different cases - sometimes exit of script is needed, sometimes only continue
101 exit
102}
103
104
105# set checkvalue to ok at the beginning of the scripts
106check="ok"
107
108# setup for jobmanager:
109# log files (can't be defined in script itself, as script can run longer
110# than one day
111jmerrorlog=$runlogpath/jobmanager-`whoami`-$HOSTNAME-$AUTOMATIONSETUP-`date +%F`-error.log
112jmscriptlog=$runlogpath/jobmanager-`whoami`-$HOSTNAME-$AUTOMATIONSETUP-`date +%F`.log
113
114# check if rc-files are available
115if ! ls $steps >/dev/null
116then
117 echo "Can't find steps.rc ($steps)"
118 finish
119fi
120if ! ls $sqlrc >/dev/null
121then
122 echo "Can't find sql.rc ($sqlrc)"
123 finish
124fi
125
126# resetting values for jobmanager
127pno=0
128totalpno=0
129running=0
130queued=0
131runningscript=0
132queuedscript=0
133stillinqueue=0
134
135
136# alias (we cannot check the beginning of the line due to
137# color codes in filldotraw.C)
138alias 'intgrep'='grep -E -o \\\("(int|Bool_t)"\\\)[0-9]+$ | grep -E -o [0-9]+'
139
140
141# in the following the functions, which are needed by several scripts, are
142# defined
143
144# function to check if a process is already locked
145# command line option can be used to execute something, e.g. 'continue'
146function checklock()
147{
148 if ! echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "$SCRIPTNAME"["$$"] "`uname -a` > $lockfile 2>/dev/null
149 then
150 if find $lockfile -amin -5
151 then
152 printprocesslog "INFO lockfile $lockfile exists"
153 else
154 printprocesslog "WARN lockfile $lockfile exists"
155 fi
156 $@
157 exit
158 else
159 printprocesslog "DEBUG created lockfile $lockfile"
160 fi
161}
162
163# print the current status values
164function printstatusvalues()
165{
166 echo "the current values are:"
167 echo " starttime=$starttime"
168 echo " stoptime=$stoptime"
169 echo " availtime=$availtime"
170 echo " returncode=$returncode"
171 echo "-- check: -$check-"
172 echo ""
173}
174
175# get the db-setup from the sql.rc
176function getdbsetup()
177{
178 db=`grep Database $sqlrc | grep -v '#' | sed -e 's/Database: //' -e 's/ //g'`
179 pw="--password="`grep Password $sqlrc | grep -v '#' | sed -e 's/Password: //' -e 's/ //g'`
180 us=`grep User $sqlrc | grep -v '#' | sed -e 's/User: //' -e 's/ //g'`
181 ho=`grep URL $sqlrc | grep -v '#' | sed -e 's/ //g' -e 's/URL:mysql:\/\///'`
182 if [ "$sqlpw" != "" ]
183 then
184 sqlpwfile="--defaults-file=$sqlpw"
185 pw=""
186 fi
187# echo "setup: "
188# echo " db: "$db
189# echo " pw: "$pw
190# echo " us: "$us
191# echo " ho: "$ho
192}
193
194# function to send a mysql query
195function sendquery()
196{
197 getdbsetup
198 printprocesslog "DEBUG sendquery QUERY: "$query
199 val=`mysql $sqlpwfile -s -u $us $pw --host=$ho $db -e " $query " 2>&1`
200 checkmysql=`echo $?`
201 if [ $checkmysql -gt 0 ]
202 then
203 printprocesslog "ERROR could not query DB "$db" on host "$ho" with user "$us" (program: $program, function sendquery) "
204 printprocesslog "MYSQLERROR [returncode: "$checkmysql"] "$val
205 error=`echo $val | grep -E -o 'ERROR [1-9]{1,4}' | grep -E -o '[1-9]{1,4}'`
206 # here possible reaction to mysql error
207 if [ $error -eq 1213 ]
208 then
209 printprocesslog "WARN Deadlock found. Should resend query."
210 printprocesslog "DEBUG sendquery QUERY: [2nd try] "$query
211 val=`mysql $sqlpwfile -s -u $us $pw --host=$ho $db -e " $query " 2>&1`
212 checkmysql2=`echo $?`
213 if [ $checkmysql2 -gt 0 ]
214 then
215 printprocesslog "ERROR could not query DB "$db" on host "$ho" with user "$us" (program: $program, function sendquery) [2nd try]"
216 printprocesslog "MYSQLERROR [returncode: "$checkmysql"] "$val" [2nd try]"
217 error2=`echo $val | grep -E -o 'ERROR [1-9]{1,4}' | grep -E -o '[1-9]{1,4}'`
218 val=
219 finish
220 else
221 if [ "$val" = "NULL" ]
222 then
223 val=
224 fi
225 echo $val
226 return 0
227 fi
228 fi
229 val=
230 finish
231 fi
232 if [ "$val" = "NULL" ]
233 then
234 val=
235 fi
236 echo $val
237 return 0
238}
239
240# function to get information from the setupfile $steps
241function getfromsetup()
242{
243 grep $1"[.]"$2":" $steps | grep -v '#' | sed -e "s/$1[.]$2://"
244}
245
246# function to get the needed information from the dependencies-file steps.rc
247function getstepinfo()
248{
249 getdbsetup
250 needs=( `getfromsetup $step "Needs"` )
251 noderestricted=`getfromsetup $step "NodeRestricted"`
252 prims=( `getfromsetup $step "Primaries"` )
253 maintable=`getfromsetup $step "MainTable" | sed -e "s/\ //g"`
254 sort=`getfromsetup $step "SortDirection" | sed -e "s/\ //g"`
255# echo " maintable: "$maintable
256# echo " needs: "${needs[@]}
257# echo " noderestricted: "$noderestricted
258# echo " prims: "${prims[@]}
259}
260
261# function to get the joins needed for the get/set status queries
262function getalljoins()
263{
264 # add table
265 query=$query" "$maintable"Status"
266 # add special join
267 query=$query" "`getfromsetup $maintable "SpecialJoin"`
268 # add join for step unless step is the same as maintable
269 if ! [ "$step" = "$maintable" ]
270 then
271 query=$query" LEFT JOIN "$step"Status USING("${prims[@]}") "
272 fi
273 # add joins for influences or needs
274 for otherstep in ${othersteps[@]}
275 do
276 if ! [ "$otherstep" = "$maintable" ]
277 then
278 query=$query" LEFT JOIN "$otherstep"Status USING("`getfromsetup $otherstep "Primaries"`") "
279 fi
280 done
281}
282
283# function to create the middle part of a query
284# which is identical for the functions getstatus() and gettodo()
285function getstatusquery()
286{
287 # add from which table the information is queried
288 query=$query" FROM "
289 othersteps=${needs[@]}
290 getalljoins
291 # add condition
292 query=$query" WHERE "
293 # add condition for step, i.e. step is not yet done
294 query=$query" ISNULL("$step"Status.fStartTime) "
295 query=$query" AND ISNULL("$step"Status.fStopTime) "
296 query=$query" AND ISNULL("$step"Status.fAvailable) "
297 query=$query" AND ISNULL("$step"Status.fReturnCode) "
298 # add requirement for production host in case it is needed
299 if [ "$1 " != " " ]
300 then
301 query=$query" AND fProductionHostKEY=$2 "
302 fi
303 query=$query`getfromsetup $step "SpecialWhere"`
304 # add condition for needs, i.e. that step is done
305 for (( k=0 ; k < ${#needs[@]} ; k++ ))
306 do
307# if [ $k -eq 0 ]
308# then
309# query=$query" HAVING "
310# else
311 query=$query" AND "
312# fi
313# query=$query" COUNT(*)=COUNT(IF("
314 query=$query" NOT ISNULL("${needs[$k]}"Status.fStartTime) "
315 query=$query" AND NOT ISNULL("${needs[$k]}"Status.fStopTime) "
316 query=$query" AND NOT ISNULL("${needs[$k]}"Status.fAvailable) "
317 query=$query" AND ISNULL("${needs[$k]}"Status.fReturnCode) "
318# query=$query" , 1, NULL)) "
319 done
320# if ! echo $query | grep UPDATE >/dev/null 2>&1
321# then
322# query=$query" GROUP BY "${prims[@]}
323# fi
324}
325
326# function to get todolist
327# returns the next or the list of next steps
328function gettodo()
329{
330 # reset the variable for the number of the next step
331 process=
332 printprocesslog "DEBUG getting todo for step $step..."
333 getstepinfo
334 # get query
335 query=" SELECT "${prims[@]}
336 getstatusquery $2
337 # order by priority to the the number of the next step to be done
338 query=$query" ORDER BY "$step"Status.fPriority "
339 if [ "$sort" = "" ]
340 then
341 query=$query" DESC "
342 else
343 query=$query" "$sort
344 fi
345 # add limitation in case only one or a limited number of
346 # processes should be executed
347 if [ "$1 " != " " ]
348 then
349 query=$query" limit 0, $1 "
350 fi
351 # print query
352 #printprocesslog "DEBUG gettodo for step $step QUERY: "$query
353 # execute query
354 process=`sendquery`
355 #if ! process=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
356 #then
357 # printprocesslog "ERROR could not query processes from db (program: $program, function gettodo)"
358 # finish
359 #fi
360 # get numbers of next step from mysql result
361 if [ "$process" = "" ]
362 then
363 printprocesslog "DEBUG => nothing to do"
364 finish
365 else
366 primaries=( $process )
367 num=`expr ${#primaries[@]} / ${#prims[@]} `
368 fi
369}
370
371# function to get the number of processes which still have to be done
372function getstatus()
373{
374 printprocesslog "DEBUG getstatus for step "$step
375 # reset the variable for the number of steps to be done
376 numproc=0
377 getstepinfo
378 # get query
379 query=" SELECT "${prims[@]}
380 getstatusquery $1
381 # print query
382 #printprocesslog "DEBUG getstatus for step $step QUERY: "$query
383 # execute query
384 #numproc=`sendquery `#cannot be done with sendquery, because of row counting
385 if ! numproc=`mysql $sqlpwfile -s -u $us $pw --host=$ho $db -e " $query " | wc -l`
386 then
387 printprocesslog "ERROR could not query number of processes from db (program: $program, function getstatus)"
388 echo `date +%F\ %T`" ERROR could not query number of processes from db (program: $program, function getstatus)"
389 continue
390 fi
391}
392
393# function to set status of a process in the db
394function setstatus()
395{
396 # remark:
397 # this function does not include the 'Default' flag
398 # for resetting steps
399
400 # for dowebplots (there are steps which have no entry in the DB)
401 if [ "$step" = "no" ]
402 then
403 return
404 fi
405
406 printprocesslog "DEBUG setstatus for step "$step
407
408 # reset status values
409 starttime=NULL
410 stoptime=NULL
411 availtime=NULL
412 returncode=NULL
413 # evaluate the status values
414 case $@ in
415 start) printprocesslog "DEBUG setstatus start"
416 starttime="Now()"
417 ;;
418 startj) printprocesslog "DEBUG setstatus startj"
419 starttime="'1971-01-01 01:01:01'" # special recognizable datetime, i.e. "reserved by JobManager for processing"
420 ;;
421 stop) case $check in
422 ok) printprocesslog "DEBUB setstatus stop - ok"
423 starttime=noreset
424 stoptime="Now()"
425 if [ "$processingsite" = "$storagesite" ]
426 then
427 availtime="Now()"
428 fi
429 ;;
430 no) printprocesslog "DEBUG setstatus stop - nothing new"
431 check="ok"
432 ;;
433 *) printprocesslog "DEBUG setstatus stop - failed"
434 starttime=noreset
435 stoptime="Now()"
436 if [ "$processingsite" = "$storagesite" ]
437 then
438 availtime="Now()"
439 fi
440 if [ "$check" == "" ]
441 then
442 returncode=1
443 else
444 returncode=$check
445 fi
446 check="ok"
447 ;;
448 esac
449 ;;
450 *) printprocesslog "ERROR function setstatus got wrong variable"
451 finish
452 ;;
453 esac
454
455 # get
456 getstepinfo
457
458 # get the influences from the steps.rc by evaluating the needs of all steps
459 othersteps=`grep $step $steps | grep -v '#' | grep "Needs" | grep -v "$step[.]Needs" | cut -d'.' -f1`
460
461 # get query
462 query=" UPDATE "
463 getalljoins
464 # set the status values according to the new status of the step
465 query=$query" SET "
466 if ! [ "$starttime" = "noreset" ]
467 then
468 query=$query" "$step"Status.fStartTime=$starttime, "
469 fi
470 query=$query" "$step"Status.fStopTime=$stoptime, "$step"Status.fAvailable=$availtime"
471 query=$query", "$step"Status.fReturnCode=$returncode , "$step"Status.fProcessingSiteKEY=$sitekey "
472 # set also the status values of the influenced steps
473 for otherstep in $othersteps
474 do
475 query=$query", "$otherstep"Status.fStartTime=NULL "
476 query=$query", "$otherstep"Status.fStopTime=NULL "
477 query=$query", "$otherstep"Status.fAvailable=NULL "
478 query=$query", "$otherstep"Status.fReturnCode=NULL "
479 query=$query", "$otherstep"Status.fProcessingSiteKEY=NULL "
480 done
481 # give the condition for which step the status values have to be set
482 query=$query" WHERE "
483 if [ "$s" = "" ]
484 then
485 s=0
486 fi
487 query=$query" "$step"Status."`echo ${prims[0]} | sed -e 's/,//g'`"='${primaries[$s*${#prims[@]}]}'"
488 for (( j=1 ; j < ${#prims[@]} ; j++ ))
489 do
490 query=$query" AND "$step"Status."`echo ${prims[$j]} | sed -e 's/,//g'`"='${primaries[$s*${#prims[@]}+$j]}' "
491 done
492 # add additional query to allow for locking in db
493 if [ "$1" = "start" ]
494 then
495 # process job only if it is not yet being processed
496 # i.e. either StartTime NULL or '1971-01-01 01:01:01' (reserved by JobManager for processing)
497 query=$query" AND (ISNULL("$step"Status.fStartTime) OR "$step"Status.fStartTime='1971-01-01 01:01:01')"
498 fi
499 # add row count to know how many rows have been changed
500 query=$query"; SELECT ROW_COUNT();"
501 # print query
502 #printprocesslog "DEBUG setstatus for step $step QUERY: "$query
503 #echo "DEBUG setstatus for step $step QUERY: "$query
504 # execute query
505 numchanged=`sendquery`
506 # should not be needed anymore once finish does proper exit
507 printprocesslog "DEBUG numchanged "$numchanged
508 if [ "$numchanged" = "" ]
509 then
510 printprocesslog "DEBUG numchanged empty."
511 finish
512 fi
513 if [ $numchanged -gt 0 ]
514 then
515 printprocesslog "INFO successful set of status in DB."
516 #echo "INFO successful set of status in DB."
517 else
518 # action may be taken in script using $numchanged
519 printprocesslog "DEBUG status in DB was already set by another process "
520 #echo "ERROR status in DB was already set by another process "
521 fi
522}
523
524function getdates()
525{
526 case $1 in
527 # all dates
528 all)
529 printprocesslog "DEBUG getdates case 'all'"
530 dates=( `find $auxdata -mindepth 3 -type d | sort -r | sed "s/\${auxdata_for_sed}//g" | sed -e 's/^\///'` )
531 ;;
532 # certain date
533 [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])
534 d=`echo $1 | grep -o '[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]'`
535 range=`echo $1 | grep -o '[0-9][0-9][0-9]$'`
536 range=`echo $range | sed -e 's/^0//' -e 's/^0//'`
537 printprocesslog "DEBUG getdates - certain date "$d"."
538 dates=( $d )
539 if [ "$range" != "" ]
540 then
541 printprocesslog "DEBUG getdates - add the last "$range" days."
542 yy=`echo $d | cut -c 1-4`
543 mm=`echo $d | cut -c 6-7`
544 dd=`echo $d | cut -c 9-10`
545 for (( numdates=1 ; numdates <= $range ; numdates++ ))
546 do
547 numhours=`echo " 12 + ( $numdates - 1 ) * 24 " | bc -l`
548 dates=( ${dates[@]} `date +%Y/%m/%d --date=$yy/$mm/$dd"-"$numhours"hour"` )
549 done
550 fi
551 ;;
552 # certain number of dates (between 0 and 9999)
553 [1-9][0-9][0-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9]|[1-9])
554 # get last n nights
555 printprocesslog "DEBUG getdates - get the last "$1" days."
556 for (( numdates=1 ; numdates <= $1 ; numdates++ ))
557 do
558 numhours=`echo " 12 + ( $numdates - 1 ) * 24 " | bc -l`
559 dates=( ${dates[@]} `date +%Y/%m/%d --date="-"$numhours"hour"` )
560 done
561 # hour-dependent number of dates
562 if [ "$2" != "" ] && [ "$3" != "" ]
563 then
564 # get current hour
565 hour=`date +%k`
566 if [ $hour -le $2 ] || [ $hour -ge $3 ]
567 then
568 printprocesslog "DEBUG getdates - get the current night."
569 dates=( `date +%Y/%m/%d --date="-12hour"` )
570 fi
571 fi
572 ;;
573 # certain number of dates in the future (between 0 and 9)
574 +[1-9])
575 # get next n nights
576 printprocesslog "DEBUG getdates - get the next "$1" days (incl today)."
577 for (( numdates=1 ; numdates <= $1 ; numdates++ ))
578 do
579 numhours=`echo " 0 + ( $numdates - 1 ) * 24 " | bc -l`
580 dates=( ${dates[@]} `date +%Y/%m/%d --date="+"$numhours"hour"` )
581 done
582 # hour-dependent number of dates
583 if [ "$2" != "" ] && [ "$3" != "" ]
584 then
585 # get current hour
586 hour=`date +%k`
587 if [ $hour -le $2 ] || [ $hour -ge $3 ]
588 then
589 printprocesslog "DEBUG getdates - get the current night."
590 dates=( `date +%Y/%m/%d --date="-12hour"` )
591 fi
592 fi
593 ;;
594 *) # nothing valid given
595 echo "Please give valid option (YYYY/MM/DD[-RRR] or 1-9999 or +0-9)"
596 finish
597 ;;
598 esac
599}
600
601# missing: implement usage of this function in Fill*.sh
602function check_file_avail()
603{
604 if [ "$1" = "" ]
605 then
606 echo "Please give file name as first argument."
607 finish
608 fi
609
610 printprocesslog "INFO checking availability of "$1
611 if ! [ -e $1 ]
612 then
613 # treat raw files differently than auxfiles
614 checkraw=`echo $1 | grep -E -o raw`
615 if [ "$checkraw" = "raw" ]
616 then
617 if [ $filenight -le $checknight ]
618 then
619 printprocesslog "WARN "$1" not found."
620 else
621 printprocesslog "INFO "$1" not found."
622 fi
623 return 1
624 fi
625 # only for aux files check of DB is needed
626 filenight=`basename $1 | cut -c 1-8`
627 # treat ratescan files
628 checkratescan=`echo $1 | grep -E -o RATE_SCAN_DATA`
629 if [ "$checkratescan" = "RATE_SCAN_DATA" ] && [ $filenight -gt 20130408 ]
630 then
631 #query="SELECT fMeasurementTypeKey from MeasurementType WHERE fMeasurementTypeName like 'Ratescan%'"
632 #ratescantypes=`sendquery`
633 query="SELECT COUNT(*) FROM Schedule WHERE fStart BETWEEN "$scheduletart" AND "$schedulestop" AND fMeasurementTypeKey IN (5,8) "
634 numratescans=`sendquery`
635 if [ $numratescans -gt 0 ]
636 then
637 printprocesslog "ERROR "$1" not found."
638 else
639 printprocesslog "INFO "$1" not found."
640 fi
641 return 1
642 fi
643 query="SELECT COUNT(*) FROM RunInfo WHERE fNight="$filenight" AND fRunTypeKey=1"
644 numdatruns=`sendquery`
645 # for some files it is more severe when they are missing
646 checkfile=`echo $1 | grep -E -o 'DRIVE'\|'RATES'`
647 # print INFO/WARN/ERROR depending on severity
648 if [ $numdatruns -eq 0 ]
649 then
650 # in case no data runs are available from this night, it's not a problem if files are missing
651 printprocesslog "INFO "$1" not found."
652 else
653 if [ "$checkfile" != "" ]
654 then
655 # drive and trigger-rates files are needed for analysis
656 #printprocesslog "ERROR "$1" not found."
657 if [ $filenight -le $checknight ]
658 then
659 printprocesslog "ERROR "$1" not found."
660 else
661 printprocesslog "WARN "$1" not found."
662 fi
663 else
664 # other aux-files would be good to have
665 #printprocesslog "WARN "$1" not found."
666 if [ $filenight -le $checknight ]
667 then
668 printprocesslog "WARN "$1" not found."
669 else
670 printprocesslog "INFO "$1" not found."
671 fi
672 fi
673 fi
674 return 1
675 fi
676
677 # file available
678 return 0
679}
680
Note: See TracBrowser for help on using the repository browser.