| 1 | #!/bin/sh
|
|---|
| 2 | #
|
|---|
| 3 | # ========================================================================
|
|---|
| 4 | #
|
|---|
| 5 | # *
|
|---|
| 6 | # * This file is part of MARS, the MAGIC Analysis and Reconstruction
|
|---|
| 7 | # * Software. It is distributed to you in the hope that it can be a useful
|
|---|
| 8 | # * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
|
|---|
| 9 | # * It is distributed WITHOUT ANY WARRANTY.
|
|---|
| 10 | # *
|
|---|
| 11 | # * Permission to use, copy, modify and distribute this software and its
|
|---|
| 12 | # * documentation for any purpose is hereby granted without fee,
|
|---|
| 13 | # * provided that the above copyright notice appear in all copies and
|
|---|
| 14 | # * that both that copyright notice and this permission notice appear
|
|---|
| 15 | # * in supporting documentation. It is provided "as is" without express
|
|---|
| 16 | # * or implied warranty.
|
|---|
| 17 | # *
|
|---|
| 18 | #
|
|---|
| 19 | #
|
|---|
| 20 | # Author(s): Daniela Dorner 05/2005 <mailto:dorner@astro.uni-wuerzburg.de>
|
|---|
| 21 | #
|
|---|
| 22 | # Copyright: MAGIC Software Development, 2000-2007
|
|---|
| 23 | #
|
|---|
| 24 | #
|
|---|
| 25 | # ========================================================================
|
|---|
| 26 | #
|
|---|
| 27 | # This a resource file for the scripts, in which the standard paths and
|
|---|
| 28 | # functions, which are needed more often are stored.
|
|---|
| 29 | # Only constant variables are stored here, changing variables are stored
|
|---|
| 30 | # in datacenter/scripts/setup
|
|---|
| 31 | #
|
|---|
| 32 |
|
|---|
| 33 | # check if script has been started with absolute path
|
|---|
| 34 | if ! dirname $0 | grep -E '^/' >/dev/null 2>&1
|
|---|
| 35 | then
|
|---|
| 36 | echo "Please start your script with an absolute path."
|
|---|
| 37 | exit
|
|---|
| 38 | fi
|
|---|
| 39 |
|
|---|
| 40 | if [ "$AUTOMATIONSETUP" = "" ]
|
|---|
| 41 | then
|
|---|
| 42 | echo "Please set the environment variable \$AUTOMATIONSETUP."
|
|---|
| 43 | exit
|
|---|
| 44 | fi
|
|---|
| 45 |
|
|---|
| 46 | source `dirname $0`/setup.$AUTOMATIONSETUP
|
|---|
| 47 |
|
|---|
| 48 | datetime=`date +%F-%H-%M-%S`
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 | # function to make sure that a directory is made
|
|---|
| 52 | function makedir()
|
|---|
| 53 | {
|
|---|
| 54 | if [ ! -d $@ ]
|
|---|
| 55 | then
|
|---|
| 56 | mkdir -pv $@
|
|---|
| 57 | if [ ! -d $@ ]
|
|---|
| 58 | then
|
|---|
| 59 | if ! [ "$processlog" = "" ]
|
|---|
| 60 | then
|
|---|
| 61 | echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "`basename $0`"["$$"] could not make dir "$@ >> $processlog
|
|---|
| 62 | else
|
|---|
| 63 | echo "could not make dir "$@
|
|---|
| 64 | fi
|
|---|
| 65 | if ls $lockfile >/dev/null 2>&1
|
|---|
| 66 | then
|
|---|
| 67 | rm -v $lockfile
|
|---|
| 68 | fi
|
|---|
| 69 | exit
|
|---|
| 70 | fi
|
|---|
| 71 | fi
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | # logging paths for runlogs and processlog
|
|---|
| 75 | runlogpath=$logpath/run/`date +%Y/%m/%d`
|
|---|
| 76 | processlogpath=$logpath/processlog
|
|---|
| 77 | makedir $runlogpath
|
|---|
| 78 | makedir $processlogpath
|
|---|
| 79 | processlog=$processlogpath/process`date +%F`.log
|
|---|
| 80 |
|
|---|
| 81 | makedir $lockpath
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 | # function to provide proper logging in a single logfile ($processlog)
|
|---|
| 85 | function printprocesslog
|
|---|
| 86 | {
|
|---|
| 87 | makedir $processlogpath
|
|---|
| 88 | echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "`basename $0`"["$$"] "$@ >> $processlog
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | # function to exit a script properly
|
|---|
| 92 | function finish()
|
|---|
| 93 | {
|
|---|
| 94 | if ! [ "$lockfile" = "" ] && ls $lockfile >/dev/null 2>&1
|
|---|
| 95 | then
|
|---|
| 96 | printprocesslog "INFO " `rm -v $lockfile`
|
|---|
| 97 | fi
|
|---|
| 98 | printprocesslog "INFO finished $0"
|
|---|
| 99 | exit
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 | # set checkvalue to ok at the beginning of the scripts
|
|---|
| 104 | check="ok"
|
|---|
| 105 |
|
|---|
| 106 | #failed codes
|
|---|
| 107 | #sequence build status
|
|---|
| 108 | Fbuildsequ=1
|
|---|
| 109 | Fdoexcl=2
|
|---|
| 110 | #run process status
|
|---|
| 111 | Ftimecorr=3
|
|---|
| 112 | Ffillraw=4
|
|---|
| 113 | Fsinope=5
|
|---|
| 114 | Ffillsinope=6
|
|---|
| 115 | Fresetexcl=7
|
|---|
| 116 | #sequence process status
|
|---|
| 117 | Fwritesequfile=8
|
|---|
| 118 | Ffilesavail=9
|
|---|
| 119 | Fnoccfile=10
|
|---|
| 120 | Fnocacofile=11
|
|---|
| 121 | Fmerppcc=12
|
|---|
| 122 | Fmerppcaco=13
|
|---|
| 123 | Fcallisto=14
|
|---|
| 124 | Ffillcalib=15
|
|---|
| 125 | Ffillsignal=16
|
|---|
| 126 | Fstar=17
|
|---|
| 127 | Ffillstar=18
|
|---|
| 128 | #dataset process status
|
|---|
| 129 | Fwritedatasetfile=19
|
|---|
| 130 | Fstardone=20
|
|---|
| 131 | Fganymed=21
|
|---|
| 132 | Ffillganymed=22
|
|---|
| 133 | #again run process status
|
|---|
| 134 | FCompmux=26
|
|---|
| 135 | Fdowebplots=27
|
|---|
| 136 | #again mc run process status
|
|---|
| 137 | Fmccallisto=28
|
|---|
| 138 | Ffillmccalib=29
|
|---|
| 139 | Ffillmcsignal=30
|
|---|
| 140 | Fmcstar=31
|
|---|
| 141 | Ffillmcstar=32
|
|---|
| 142 | Fcorsikasimtel=33
|
|---|
| 143 | Fchimp=34
|
|---|
| 144 | Fchimpcp=35
|
|---|
| 145 | Fctastar=36
|
|---|
| 146 | Fctastarcp=37
|
|---|
| 147 | FctastereoA=38
|
|---|
| 148 | FctastereoB=39
|
|---|
| 149 | FctastereoC=40
|
|---|
| 150 | FctastereoD=41
|
|---|
| 151 | FctastereoE=42
|
|---|
| 152 | FctastereoF=43
|
|---|
| 153 | FctastereoG=44
|
|---|
| 154 | FctastereoH=45
|
|---|
| 155 | Fctastereocp=46
|
|---|
| 156 |
|
|---|
| 157 | # setup for jobmanager:
|
|---|
| 158 | # log files (can't be defined in script itself, as script can run longer
|
|---|
| 159 | # than one day
|
|---|
| 160 | jmerrorlog=$runlogpath/jobmanager-error`date +%F`.log
|
|---|
| 161 | jmscriptlog=$runlogpath/jobmanager`date +%F`.log
|
|---|
| 162 |
|
|---|
| 163 | # check if rc-files are available
|
|---|
| 164 | if ! ls $steps >/dev/null
|
|---|
| 165 | then
|
|---|
| 166 | echo "Can't find steps.rc ($steps)"
|
|---|
| 167 | finish
|
|---|
| 168 | fi
|
|---|
| 169 | if ! ls $sqlrc >/dev/null
|
|---|
| 170 | then
|
|---|
| 171 | echo "Can't find sql.rc ($sqlrc)"
|
|---|
| 172 | finish
|
|---|
| 173 | fi
|
|---|
| 174 |
|
|---|
| 175 | # resetting values
|
|---|
| 176 | pno=0
|
|---|
| 177 | totalpno=0
|
|---|
| 178 | running=0
|
|---|
| 179 | queued=0
|
|---|
| 180 | runningscript=0
|
|---|
| 181 | queuedscript=0
|
|---|
| 182 | stillinqueue=0
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 | # alias (we cannot check the beginning of the line due to
|
|---|
| 186 | # color codes in filldotraw.C)
|
|---|
| 187 | alias 'intgrep'='grep -E -o \\\(int\\\)[0-9]+$ | grep -E -o [0-9]+'
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 | # in the following the functions, which are needed by several scripts, are
|
|---|
| 191 | # defined
|
|---|
| 192 |
|
|---|
| 193 | # function to check if a process is already locked
|
|---|
| 194 | # command line option can be used to execute something, e.g. 'continue'
|
|---|
| 195 | function checklock()
|
|---|
| 196 | {
|
|---|
| 197 | if ! echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "`basename $0`"["$$"] "`uname -a` > $lockfile 2>/dev/null
|
|---|
| 198 | then
|
|---|
| 199 | printprocesslog "WARN lockfile $lockfile exists"
|
|---|
| 200 | $@
|
|---|
| 201 | exit
|
|---|
| 202 | else
|
|---|
| 203 | printprocesslog "INFO created lockfile $lockfile"
|
|---|
| 204 | fi
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | function resetstatusvalues()
|
|---|
| 208 | {
|
|---|
| 209 | statustime=NULL
|
|---|
| 210 | starttime=NULL
|
|---|
| 211 | returncode=NULL
|
|---|
| 212 | programid=NULL
|
|---|
| 213 | failedtime=NULL
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | function printstatusvalues()
|
|---|
| 217 | {
|
|---|
| 218 | echo "the current values are:"
|
|---|
| 219 | echo " statustime=$statustime"
|
|---|
| 220 | echo " starttime=$starttime"
|
|---|
| 221 | echo " returncode=$returncode"
|
|---|
| 222 | echo " programid=$programid"
|
|---|
| 223 | echo " failedtime=$failedtime"
|
|---|
| 224 | echo "-- check: -$check-"
|
|---|
| 225 | echo ""
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | # function evaluating the statusvalues
|
|---|
| 229 | function evalstatus()
|
|---|
| 230 | {
|
|---|
| 231 | case $@ in
|
|---|
| 232 | start) printprocesslog "INFO setstatus start"
|
|---|
| 233 | starttime="Now()"
|
|---|
| 234 | ;;
|
|---|
| 235 | stop) case $check in
|
|---|
| 236 | ok) printprocesslog "INFO setstatus stop - ok"
|
|---|
| 237 | statustime="Now()"
|
|---|
| 238 | ;;
|
|---|
| 239 | no) printprocesslog "INFO setstatus stop - nothing new"
|
|---|
| 240 | check="ok"
|
|---|
| 241 | ;;
|
|---|
| 242 | *) printprocesslog "INFO setstatus stop - failed"
|
|---|
| 243 | starttime=noreset
|
|---|
| 244 | if [ "$check" == "" ]
|
|---|
| 245 | then
|
|---|
| 246 | returncode=1
|
|---|
| 247 | else
|
|---|
| 248 | returncode=$check
|
|---|
| 249 | fi
|
|---|
| 250 | programid=$com
|
|---|
| 251 | failedtime="Now()"
|
|---|
| 252 | check="ok"
|
|---|
| 253 | ;;
|
|---|
| 254 | esac
|
|---|
| 255 | ;;
|
|---|
| 256 | *) printprocesslog "ERROR function evalstatus got wrong variable"
|
|---|
| 257 | finish
|
|---|
| 258 | ;;
|
|---|
| 259 | esac
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | function getdbsetup()
|
|---|
| 263 | {
|
|---|
| 264 | db=`grep Database $sqlrc | grep -v '#' | sed -e 's/Database: //' -e 's/ //g'`
|
|---|
| 265 | pw=`grep Password $sqlrc | grep -v '#' | sed -e 's/Password: //' -e 's/ //g'`
|
|---|
| 266 | us=`grep User $sqlrc | grep -v '#' | sed -e 's/User: //' -e 's/ //g'`
|
|---|
| 267 | ho=`grep URL $sqlrc | grep -v '#' | sed -e 's/ //g' -e 's/URL:mysql:\/\///'`
|
|---|
| 268 | # echo "setup: "
|
|---|
| 269 | # echo " db: "$db
|
|---|
| 270 | # echo " pw: "$pw
|
|---|
| 271 | # echo " us: "$us
|
|---|
| 272 | # echo " ho: "$ho
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | function getstepinfo()
|
|---|
| 276 | {
|
|---|
| 277 | getdbsetup
|
|---|
| 278 | table=`grep "$column:" $steps | sed -e "s/[.]$column://" -e 's/#//' -e 's/ //g'`
|
|---|
| 279 | coltab=`grep "$column:" $steps | sed -e 's/://' -e 's/#//' -e 's/ //g'`
|
|---|
| 280 | needs=`grep "$coltab[.]Needs:" $steps | sed -e "s/$coltab[.]Needs://"`
|
|---|
| 281 | noderestricted=`grep "$coltab[.]NodeRestricted:" $steps | sed -e "s/$coltab[.]NodeRestricted://" -e 's/ //g'`
|
|---|
| 282 | influences=`grep "$coltab[.]Influences:" $steps | sed -e "s/$coltab[.]Influences://"`
|
|---|
| 283 | prims=( `grep "$table[.]Primary:" $steps | sed -e "s/$table[.]Primary://"` )
|
|---|
| 284 | # echo " column $column - table $table - coltab $coltab"
|
|---|
| 285 | # echo " needs: $needs"
|
|---|
| 286 | # echo " influences: $influences"
|
|---|
| 287 | # echo " noderestricted: $noderestricted"
|
|---|
| 288 | # echo " prims: ${prims[@]}"
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | # function to get todolist
|
|---|
| 292 | function gettodo()
|
|---|
| 293 | {
|
|---|
| 294 | process=
|
|---|
| 295 | printprocesslog "INFO getting todo..."
|
|---|
| 296 | getstepinfo
|
|---|
| 297 | # get query
|
|---|
| 298 | query=" SELECT "${prims[0]}
|
|---|
| 299 | for (( i=1 ; i < ${#prims[@]} ; i++ ))
|
|---|
| 300 | do
|
|---|
| 301 | query=$query", ${prims[$i]}"
|
|---|
| 302 | done
|
|---|
| 303 | query=$query" FROM $table WHERE "
|
|---|
| 304 | if ! echo $needs | grep '#' > /dev/null
|
|---|
| 305 | then
|
|---|
| 306 | for need in $needs
|
|---|
| 307 | do
|
|---|
| 308 | query=$query" NOT ISNULL($need) AND"
|
|---|
| 309 | done
|
|---|
| 310 | fi
|
|---|
| 311 | query=$query" ISNULL($column) "
|
|---|
| 312 | if [ "$2 " != " " ]
|
|---|
| 313 | then
|
|---|
| 314 | query=$query" AND fProductionHostKEY=$2 "
|
|---|
| 315 | fi
|
|---|
| 316 | query=$query" AND ISNULL(fStartTime) AND ISNULL(fFailedTime) AND ISNULL(fProgramId) AND ISNULL(fReturnCode) "
|
|---|
| 317 | query=$query" ORDER BY fPriority desc "
|
|---|
| 318 | if [ "$1 " != " " ]
|
|---|
| 319 | then
|
|---|
| 320 | query=$query" limit 0, $1 "
|
|---|
| 321 | fi
|
|---|
| 322 | # echo " QUERY: "$query
|
|---|
| 323 | printprocesslog "INFO gettodo QUERY: "$query
|
|---|
| 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 |
|
|---|
| 330 | if [ "$process" = "" ]
|
|---|
| 331 | then
|
|---|
| 332 | printprocesslog "INFO => nothing to do"
|
|---|
| 333 | finish
|
|---|
| 334 | else
|
|---|
| 335 | primaries=( $process )
|
|---|
| 336 | num=`expr ${#primaries[@]} / ${#prims[@]} `
|
|---|
| 337 | fi
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 |
|
|---|
| 341 | # function to get the number of processes which still have to be done
|
|---|
| 342 | function getstatus()
|
|---|
| 343 | {
|
|---|
| 344 | numproc=
|
|---|
| 345 | getstepinfo
|
|---|
| 346 | # get query
|
|---|
| 347 | query=" SELECT COUNT(*) FROM $table WHERE "
|
|---|
| 348 | if ! echo $needs | grep '#' > /dev/null
|
|---|
| 349 | then
|
|---|
| 350 | for need in $needs
|
|---|
| 351 | do
|
|---|
| 352 | query=$query" NOT ISNULL($need) AND"
|
|---|
| 353 | done
|
|---|
| 354 | fi
|
|---|
| 355 | query=$query" ISNULL($column) "
|
|---|
| 356 | if [ "$1 " != " " ]
|
|---|
| 357 | then
|
|---|
| 358 | query=$query" AND fProductionHostKEY=$1 "
|
|---|
| 359 | fi
|
|---|
| 360 | query=$query" AND ISNULL(fStartTime) AND ISNULL(fFailedTime) AND ISNULL(fProgramId) AND ISNULL(fReturnCode) "
|
|---|
| 361 | query=$query" GROUP BY $column "
|
|---|
| 362 | # echo "QUERY: "$query
|
|---|
| 363 | printprocesslog "INFO getstatus QUERY: "$query
|
|---|
| 364 | if ! numproc=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
|
|---|
| 365 | then
|
|---|
| 366 | printprocesslog "ERROR could not query number of processes from db (program: $program, function getstatus)"
|
|---|
| 367 | echo `date +%F\ %T`" ERROR could not query number of processes from db (program: $program, function getstatus)"
|
|---|
| 368 | continue
|
|---|
| 369 | fi
|
|---|
| 370 | if [ "$numproc" = "" ]
|
|---|
| 371 | then
|
|---|
| 372 | numproc=0
|
|---|
| 373 | fi
|
|---|
| 374 | }
|
|---|
| 375 |
|
|---|
| 376 | # function to set status of a process in the db
|
|---|
| 377 | function setstatus()
|
|---|
| 378 | {
|
|---|
| 379 | if [ "$column" = "no" ]
|
|---|
| 380 | then
|
|---|
| 381 | return
|
|---|
| 382 | fi
|
|---|
| 383 | resetstatusvalues
|
|---|
| 384 | evalstatus $@
|
|---|
| 385 | getstepinfo
|
|---|
| 386 | # get query
|
|---|
| 387 | reset=`grep "$coltab[.]Reset:" $steps | sed -e "s/$coltab[.]Reset://" -e 's/ //g'`
|
|---|
| 388 | if [ "$reset" = "no" ]
|
|---|
| 389 | then
|
|---|
| 390 | printprocesslog "ERROR You cannot reset $column for ${primaries[$s+$s]}"
|
|---|
| 391 | finish
|
|---|
| 392 | fi
|
|---|
| 393 | query=" update $table set $column=$statustime"
|
|---|
| 394 | if ! echo $influences | grep '#' > /dev/null
|
|---|
| 395 | then
|
|---|
| 396 | for influence in $influences
|
|---|
| 397 | do
|
|---|
| 398 | query=$query", $influence=NULL"
|
|---|
| 399 | done
|
|---|
| 400 | fi
|
|---|
| 401 | if ! [ "$starttime" = "noreset" ]
|
|---|
| 402 | then
|
|---|
| 403 | query=$query", fStartTime=$starttime"
|
|---|
| 404 | fi
|
|---|
| 405 | query=$query", fFailedTime=$failedtime, fProgramId=$programid, fReturnCode=$returncode "
|
|---|
| 406 | query=$query" where "
|
|---|
| 407 | if [ "$s" = "" ]
|
|---|
| 408 | then
|
|---|
| 409 | s=0
|
|---|
| 410 | fi
|
|---|
| 411 | query=$query" ${prims[0]}='${primaries[$s*${#prims[@]}]}'"
|
|---|
| 412 | for (( i=1 ; i < ${#prims[@]} ; i++ ))
|
|---|
| 413 | do
|
|---|
| 414 | query=$query" AND ${prims[$i]}='${primaries[$s*${#prims[@]}+$i]}' "
|
|---|
| 415 | done
|
|---|
| 416 | # echo " QUERY: "$query
|
|---|
| 417 | printprocesslog "INFO setstatus QUERY: "$query
|
|---|
| 418 | if ! mysql -s -u $us --password=$pw --host=$ho $db -e " $query "
|
|---|
| 419 | then
|
|---|
| 420 | printprocesslog "ERROR could not set status in db (program: $program, function setstatus)"
|
|---|
| 421 | finish
|
|---|
| 422 | fi
|
|---|
| 423 |
|
|---|
| 424 | }
|
|---|
| 425 |
|
|---|
| 426 | # function to send a mysql query
|
|---|
| 427 | function sendquery()
|
|---|
| 428 | {
|
|---|
| 429 | getdbsetup
|
|---|
| 430 | printprocesslog "INFO sendquery QUERY: "$query
|
|---|
| 431 | if ! val=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
|
|---|
| 432 | then
|
|---|
| 433 | printprocesslog "ERROR could not query db (program: $program, function sendquery)"
|
|---|
| 434 | return 1
|
|---|
| 435 | fi
|
|---|
| 436 | if [ "$val" = "NULL" ]
|
|---|
| 437 | then
|
|---|
| 438 | val=
|
|---|
| 439 | fi
|
|---|
| 440 | echo $val
|
|---|
| 441 | return 0
|
|---|
| 442 | }
|
|---|
| 443 |
|
|---|