source: trunk/DataCheck/Sourcefile.sh@ 18771

Last change on this file since 18771 was 18771, checked in by Daniela Dorner, 8 years ago
treat ratescan files specially in function check_file_avail
  • Property svn:executable set to *
File size: 20.3 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 startj) printprocesslog "DEBUG setstatus startj"
389 starttime="'1971-01-01 01:01:01'" # special recognizable datetime, i.e. "reserved by JobManager for processing"
390 ;;
391 stop) case $check in
392 ok) printprocesslog "DEBUB setstatus stop - ok"
393 starttime=noreset
394 stoptime="Now()"
395 if [ "$processingsite" = "$storagesite" ]
396 then
397 availtime="Now()"
398 fi
399 ;;
400 no) printprocesslog "DEBUG setstatus stop - nothing new"
401 check="ok"
402 ;;
403 *) printprocesslog "DEBUG setstatus stop - failed"
404 starttime=noreset
405 stoptime="Now()"
406 if [ "$processingsite" = "$storagesite" ]
407 then
408 availtime="Now()"
409 fi
410 if [ "$check" == "" ]
411 then
412 returncode=1
413 else
414 returncode=$check
415 fi
416 check="ok"
417 ;;
418 esac
419 ;;
420 *) printprocesslog "ERROR function setstatus got wrong variable"
421 finish
422 ;;
423 esac
424
425 # get
426 getstepinfo
427
428 # get the influences from the steps.rc by evaluating the needs of all steps
429 othersteps=`grep $step $steps | grep -v '#' | grep "Needs" | grep -v "$step[.]Needs" | cut -d'.' -f1`
430
431 # get query
432 query=" UPDATE "
433 getalljoins
434 # set the status values according to the new status of the step
435 query=$query" SET "
436 if ! [ "$starttime" = "noreset" ]
437 then
438 query=$query" "$step"Status.fStartTime=$starttime, "
439 fi
440 query=$query" "$step"Status.fStopTime=$stoptime, "$step"Status.fAvailable=$availtime"
441 query=$query", "$step"Status.fReturnCode=$returncode , "$step"Status.fProcessingSiteKEY=$sitekey "
442 # set also the status values of the influenced steps
443 for otherstep in $othersteps
444 do
445 query=$query", "$otherstep"Status.fStartTime=NULL "
446 query=$query", "$otherstep"Status.fStopTime=NULL "
447 query=$query", "$otherstep"Status.fAvailable=NULL "
448 query=$query", "$otherstep"Status.fReturnCode=NULL "
449 query=$query", "$otherstep"Status.fProcessingSiteKEY=NULL "
450 done
451 # give the condition for which step the status values have to be set
452 query=$query" WHERE "
453 if [ "$s" = "" ]
454 then
455 s=0
456 fi
457 query=$query" "$step"Status."`echo ${prims[0]} | sed -e 's/,//g'`"='${primaries[$s*${#prims[@]}]}'"
458 for (( j=1 ; j < ${#prims[@]} ; j++ ))
459 do
460 query=$query" AND "$step"Status."`echo ${prims[$j]} | sed -e 's/,//g'`"='${primaries[$s*${#prims[@]}+$j]}' "
461 done
462 # add additional query to allow for locking in db
463 if [ "$1" = "start" ]
464 then
465 # process job only if it is not yet being processed
466 # i.e. either StartTime NULL or '1971-01-01 01:01:01' (reserved by JobManager for processing)
467 query=$query" AND (ISNULL("$step"Status.fStartTime) OR "$step"Status.fStartTime='1971-01-01 01:01:01')"
468 fi
469 # add row count to know how many rows have been changed
470 query=$query"; SELECT ROW_COUNT();"
471 # print query
472 #printprocesslog "DEBUG setstatus for step $step QUERY: "$query
473 #echo "DEBUG setstatus for step $step QUERY: "$query
474 # execute query
475 numchanged=`sendquery`
476 # should not be needed anymore once finish does proper exit
477 printprocesslog "DEBUG numchanged "$numchanged
478 if [ "$numchanged" = "" ]
479 then
480 printprocesslog "DEBUG numchanged empty."
481 finish
482 fi
483 if [ $numchanged -gt 0 ]
484 then
485 printprocesslog "INFO successful set of status in DB."
486 #echo "INFO successful set of status in DB."
487 else
488 # action may be taken in script using $numchanged
489 printprocesslog "DEBUG status in DB was already set by another process "
490 #echo "ERROR status in DB was already set by another process "
491 fi
492}
493
494function getdates()
495{
496 case $1 in
497 # all dates
498 all)
499 printprocesslog "DEBUG getdates case 'all'"
500 dates=( `find $auxdata -mindepth 3 -type d | sort -r | sed "s/\${auxdata_for_sed}//g" | sed -e 's/^\///'` )
501 ;;
502 # certain date
503 [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])
504 d=`echo $1 | grep -o '[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]'`
505 range=`echo $1 | grep -o '[0-9][0-9][0-9]$'`
506 range=`echo $range | sed -e 's/^0//' -e 's/^0//'`
507 printprocesslog "DEBUG getdates - certain date "$d"."
508 dates=( $d )
509 if [ "$range" != "" ]
510 then
511 printprocesslog "DEBUG getdates - add the last "$range" days."
512 yy=`echo $d | cut -c 1-4`
513 mm=`echo $d | cut -c 6-7`
514 dd=`echo $d | cut -c 9-10`
515 for (( numdates=1 ; numdates <= $range ; numdates++ ))
516 do
517 numhours=`echo " 12 + ( $numdates - 1 ) * 24 " | bc -l`
518 dates=( ${dates[@]} `date +%Y/%m/%d --date=$yy/$mm/$dd"-"$numhours"hour"` )
519 done
520 fi
521 ;;
522 # certain number of dates (between 0 and 9999)
523 [1-9][0-9][0-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9]|[1-9])
524 # get last n nights
525 printprocesslog "DEBUG getdates - get the last "$1" days."
526 for (( numdates=1 ; numdates <= $1 ; numdates++ ))
527 do
528 numhours=`echo " 12 + ( $numdates - 1 ) * 24 " | bc -l`
529 dates=( ${dates[@]} `date +%Y/%m/%d --date="-"$numhours"hour"` )
530 done
531 # hour-dependent number of dates
532 if [ "$2" != "" ] && [ "$3" != "" ]
533 then
534 # get current hour
535 hour=`date +%k`
536 if [ $hour -le $2 ] || [ $hour -ge $3 ]
537 then
538 printprocesslog "DEBUG getdates - get the current night."
539 dates=( `date +%Y/%m/%d --date="-12hour"` )
540 fi
541 fi
542 ;;
543 # certain number of dates in the future (between 0 and 9)
544 +[1-9])
545 # get next n nights
546 printprocesslog "DEBUG getdates - get the next "$1" days (incl today)."
547 for (( numdates=1 ; numdates <= $1 ; numdates++ ))
548 do
549 numhours=`echo " 0 + ( $numdates - 1 ) * 24 " | bc -l`
550 dates=( ${dates[@]} `date +%Y/%m/%d --date="+"$numhours"hour"` )
551 done
552 # hour-dependent number of dates
553 if [ "$2" != "" ] && [ "$3" != "" ]
554 then
555 # get current hour
556 hour=`date +%k`
557 if [ $hour -le $2 ] || [ $hour -ge $3 ]
558 then
559 printprocesslog "DEBUG getdates - get the current night."
560 dates=( `date +%Y/%m/%d --date="-12hour"` )
561 fi
562 fi
563 ;;
564 *) # nothing valid given
565 echo "Please give valid option (YYYY/MM/DD[-RRR] or 1-9999 or +0-9)"
566 finish
567 ;;
568 esac
569}
570
571# missing: implement usage of this function in Fill*.sh
572function check_file_avail()
573{
574 if [ "$1" = "" ]
575 then
576 echo "Please give file name as first argument."
577 finish
578 fi
579 if [ "$transferdelay" = "" ]
580 then
581 # setup where immediate report of problem is needed, i.e. mainly LP
582 checknight=`date +%Y%m%d --date="-19HOUR"`
583 else
584 # any setup where transfer might cause a delay in arrival of files
585 checknight=`date +%Y%m%d --date="-${transferdelay}day"`
586 fi
587
588 printprocesslog "INFO checking availability of "$1
589 if ! [ -e $1 ]
590 then
591 # treat raw files differently than auxfiles
592 checkraw=`echo $1 | grep -E -o raw`
593 if [ "$checkraw" = "raw" ]
594 then
595 if [ $filenight -le $checknight ]
596 then
597 printprocesslog "WARN "$1" not found."
598 else
599 printprocesslog "INFO "$1" not found."
600 fi
601 return 1
602 fi
603 # only for aux files check of DB is needed
604 filenight=`basename $1 | cut -c 1-8`
605 # treat ratescan files
606 checkratescan=`echo $1 | grep -E -o RATE_SCAN_DATA`
607 if [ "$checkratescan" = "RATE_SCAN_DATA" ] && [ $filenight -gt 20130408 ]
608 then
609 #query="SELECT fMeasurementTypeKey from MeasurementType WHERE fMeasurementTypeName like 'Ratescan%'"
610 #ratescantypes=`sendquery`
611 query="SELECT COUNT(*) FROM Schedule WHERE fStart BETWEEN "$scheduletart" AND "$schedulestop" AND fMeasurementTypeKey IN (5,8) "
612 numratescans=`sendquery`
613 if [ $numratescans -gt 0 ]
614 then
615 printprocesslog "ERROR "$1" not found."
616 else
617 printprocesslog "INFO "$1" not found."
618 fi
619 return 1
620 fi
621 query="SELECT COUNT(*) FROM RunInfo WHERE fNight="$filenight" AND fRunTypeKey=1"
622 numdatruns=`sendquery`
623 # for some files it is more severe when they are missing
624 checkfile=`echo $1 | grep -E -o 'DRIVE'\|'RATES'`
625 # print INFO/WARN/ERROR depending on severity
626 if [ $numdatruns -eq 0 ]
627 then
628 # in case no data runs are available from this night, it's not a problem if files are missing
629 printprocesslog "INFO "$1" not found."
630 else
631 if [ "$checkfile" != "" ]
632 then
633 # drive and trigger-rates files are needed for analysis
634 #printprocesslog "ERROR "$1" not found."
635 if [ $filenight -le $checknight ]
636 then
637 printprocesslog "ERROR "$1" not found."
638 else
639 printprocesslog "WARN "$1" not found."
640 fi
641 else
642 # other aux-files would be good to have
643 #printprocesslog "WARN "$1" not found."
644 if [ $filenight -le $checknight ]
645 then
646 printprocesslog "WARN "$1" not found."
647 else
648 printprocesslog "INFO "$1" not found."
649 fi
650 fi
651 fi
652 return 1
653 fi
654
655 # file available
656 return 0
657}
658
Note: See TracBrowser for help on using the repository browser.