source: trunk/DataCheck/Sourcefile.sh@ 18696

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