| 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 | source `dirname $0`/setup
|
|---|
| 41 |
|
|---|
| 42 | mars=`dirname $0 | sed -e 's/\/datacenter\/scripts//'`
|
|---|
| 43 | macrospath=$mars/datacenter/macros
|
|---|
| 44 | scriptspath=$mars/datacenter/scripts
|
|---|
| 45 |
|
|---|
| 46 | datetime=`date +%F-%H-%M-%S`
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | # function to make sure that a directory is made
|
|---|
| 50 | function makedir()
|
|---|
| 51 | {
|
|---|
| 52 | if [ ! -d $@ ]
|
|---|
| 53 | then
|
|---|
| 54 | mkdir -pv $@
|
|---|
| 55 | if [ ! -d $@ ]
|
|---|
| 56 | then
|
|---|
| 57 | echo "could not make dir "$@
|
|---|
| 58 | if ls $lockfile >/dev/null 2>&1
|
|---|
| 59 | then
|
|---|
| 60 | rm -v $lockfile
|
|---|
| 61 | fi
|
|---|
| 62 | date
|
|---|
| 63 | exit
|
|---|
| 64 | fi
|
|---|
| 65 | fi
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | # logging paths for runlogs and processlog
|
|---|
| 69 | runlogpath=$logpath/run/`date +%Y/%m/%d`
|
|---|
| 70 | processlogpath=$logpath/processlog
|
|---|
| 71 | makedir $runlogpath
|
|---|
| 72 | makedir $processlogpath
|
|---|
| 73 | processlog=$processlogpath/process`date +%F`.log
|
|---|
| 74 |
|
|---|
| 75 | makedir $lockpath
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 | # function to provide proper logging in a single logfile ($processlog)
|
|---|
| 79 | function printprocesslog
|
|---|
| 80 | {
|
|---|
| 81 | makedir $processlogpath
|
|---|
| 82 | echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "`basename $0`"["$$"] "$@ >> $processlog
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | # function to exit a script properly
|
|---|
| 86 | function finish()
|
|---|
| 87 | {
|
|---|
| 88 | if ! [ "$lockfile" = "" ] && ls $lockfile >/dev/null 2>&1
|
|---|
| 89 | then
|
|---|
| 90 | rm -v $lockfile
|
|---|
| 91 | fi
|
|---|
| 92 | date
|
|---|
| 93 | printprocesslog "INFO finished $0"
|
|---|
| 94 | exit
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 | # set checkvalue to ok at the beginning of the scripts
|
|---|
| 99 | check="ok"
|
|---|
| 100 |
|
|---|
| 101 | #failed codes
|
|---|
| 102 | #sequence build status
|
|---|
| 103 | Fbuildsequ=1
|
|---|
| 104 | Fdoexcl=2
|
|---|
| 105 | #run process status
|
|---|
| 106 | Ftimecorr=3
|
|---|
| 107 | Ffillraw=4
|
|---|
| 108 | Fsinope=5
|
|---|
| 109 | Ffillsinope=6
|
|---|
| 110 | Fresetexcl=7
|
|---|
| 111 | #sequence process status
|
|---|
| 112 | Fwritesequfile=8
|
|---|
| 113 | Ffilesavail=9
|
|---|
| 114 | Fnoccfile=10
|
|---|
| 115 | Fnocacofile=11
|
|---|
| 116 | Fmerppcc=12
|
|---|
| 117 | Fmerppcaco=13
|
|---|
| 118 | Fcallisto=14
|
|---|
| 119 | Ffillcalib=15
|
|---|
| 120 | Ffillsignal=16
|
|---|
| 121 | Fstar=17
|
|---|
| 122 | Ffillstar=18
|
|---|
| 123 | #dataset process status
|
|---|
| 124 | Fwritedatasetfile=19
|
|---|
| 125 | Fstardone=20
|
|---|
| 126 | Fganymed=21
|
|---|
| 127 | Ffillganymed=22
|
|---|
| 128 | #mc run process status
|
|---|
| 129 | Fcorsika=23
|
|---|
| 130 | Freflector=24
|
|---|
| 131 | Fcamera=25
|
|---|
| 132 | #again run process status
|
|---|
| 133 | FCompmux=26
|
|---|
| 134 | Fdowebplots=27
|
|---|
| 135 |
|
|---|
| 136 | # setup for jobmanager:
|
|---|
| 137 | # log files (can't be defined in script itself, as script can run longer
|
|---|
| 138 | # than one day
|
|---|
| 139 | jmerrorlog=$runlogpath/jobmanager-error`date +%F`.log
|
|---|
| 140 | jmscriptlog=$runlogpath/jobmanager`date +%F`.log
|
|---|
| 141 | steps=$mars/resources/steps.rc
|
|---|
| 142 | if ! ls $steps >/dev/null
|
|---|
| 143 | then
|
|---|
| 144 | echo "Can't find steps.rc ($steps)"
|
|---|
| 145 | finish
|
|---|
| 146 | fi
|
|---|
| 147 | sqlrc=$mars/sql.rc
|
|---|
| 148 | if ! ls $sqlrc >/dev/null
|
|---|
| 149 | then
|
|---|
| 150 | echo "Can't find sql.rc ($sqlrc)"
|
|---|
| 151 | finish
|
|---|
| 152 | fi
|
|---|
| 153 | # resetting values
|
|---|
| 154 | pno=0
|
|---|
| 155 | totalpno=0
|
|---|
| 156 | running=0
|
|---|
| 157 | queued=0
|
|---|
| 158 | runningscript=0
|
|---|
| 159 | queuedscript=0
|
|---|
| 160 | stillinqueue=0
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 | # alias (we cannot check the beginning of the line due to
|
|---|
| 164 | # color codes in filldotraw.C)
|
|---|
| 165 | alias 'intgrep'='grep -E -o \\\(int\\\)[0-9]+$ | grep -E -o [0-9]+'
|
|---|
| 166 |
|
|---|
| 167 | # in the following the functions, which are needed by several scripts, are
|
|---|
| 168 | # defined
|
|---|
| 169 |
|
|---|
| 170 | # function to do continue in a loop and produce according logging
|
|---|
| 171 | function cont()
|
|---|
| 172 | {
|
|---|
| 173 | date
|
|---|
| 174 | echo ""
|
|---|
| 175 | continue
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | # function to check if a process is already locked
|
|---|
| 179 | function checklock()
|
|---|
| 180 | {
|
|---|
| 181 | date > $lockfile
|
|---|
| 182 | checklock0=$?
|
|---|
| 183 | case $checklock0 in
|
|---|
| 184 | 0) echo " checklock0=$checklock0 -> continue " ;;
|
|---|
| 185 | 1) echo " checklock0=$checklock0 -> file $lockfile exists"
|
|---|
| 186 | date
|
|---|
| 187 | $@
|
|---|
| 188 | exit;;
|
|---|
| 189 | *) echo " checklock0=$checklock0 -> something went completely wrong" ;;
|
|---|
| 190 | esac
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | function resetstatusvalues()
|
|---|
| 194 | {
|
|---|
| 195 | statustime=NULL
|
|---|
| 196 | starttime=NULL
|
|---|
| 197 | returncode=NULL
|
|---|
| 198 | programid=NULL
|
|---|
| 199 | failedtime=NULL
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | function printstatusvalues()
|
|---|
| 203 | {
|
|---|
| 204 | echo "the current values are:"
|
|---|
| 205 | echo " statustime=$statustime"
|
|---|
| 206 | echo " starttime=$starttime"
|
|---|
| 207 | echo " returncode=$returncode"
|
|---|
| 208 | echo " programid=$programid"
|
|---|
| 209 | echo " failedtime=$failedtime"
|
|---|
| 210 | echo "-- check: -$check-"
|
|---|
| 211 | echo ""
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | # function evaluating the statusvalues
|
|---|
| 215 | function evalstatus()
|
|---|
| 216 | {
|
|---|
| 217 | case $@ in
|
|---|
| 218 | start) echo "setstatus start"
|
|---|
| 219 | starttime="Now()"
|
|---|
| 220 | ;;
|
|---|
| 221 | stop) case $check in
|
|---|
| 222 | ok) echo "setstatus stop - ok"
|
|---|
| 223 | statustime="Now()"
|
|---|
| 224 | ;;
|
|---|
| 225 | no) echo "setstatus stop - nothing new"
|
|---|
| 226 | check="ok"
|
|---|
| 227 | ;;
|
|---|
| 228 | *) echo "setstatus stop - failed"
|
|---|
| 229 | starttime=noreset
|
|---|
| 230 | if [ "$check" == "" ]
|
|---|
| 231 | then
|
|---|
| 232 | returncode=1
|
|---|
| 233 | else
|
|---|
| 234 | returncode=$check
|
|---|
| 235 | fi
|
|---|
| 236 | programid=$com
|
|---|
| 237 | failedtime="Now()"
|
|---|
| 238 | check="ok"
|
|---|
| 239 | ;;
|
|---|
| 240 | esac
|
|---|
| 241 | ;;
|
|---|
| 242 | *) echo "error -> exit"
|
|---|
| 243 | printprocesslog "ERROR function evalstatus got wrong variable"
|
|---|
| 244 | finish
|
|---|
| 245 | ;;
|
|---|
| 246 | esac
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | function getdbsetup()
|
|---|
| 250 | {
|
|---|
| 251 | db=`grep Database $sqlrc | grep -v '#' | sed -e 's/Database: //' -e 's/ //g'`
|
|---|
| 252 | pw=`grep Password $sqlrc | grep -v '#' | sed -e 's/Password: //' -e 's/ //g'`
|
|---|
| 253 | us=`grep User $sqlrc | grep -v '#' | sed -e 's/User: //' -e 's/ //g'`
|
|---|
| 254 | ho=`grep URL $sqlrc | grep -v '#' | sed -e 's/ //g' -e 's/URL:mysql:\/\///'`
|
|---|
| 255 | # echo "setup: "
|
|---|
| 256 | # echo " db: "$db
|
|---|
| 257 | # echo " pw: "$pw
|
|---|
| 258 | # echo " us: "$us
|
|---|
| 259 | # echo " ho: "$ho
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | function getstepinfo()
|
|---|
| 263 | {
|
|---|
| 264 | table=`grep "$column:" $steps | sed -e "s/[.]$column://" -e 's/#//' -e 's/ //g'`
|
|---|
| 265 | coltab=`grep "$column:" $steps | sed -e 's/://' -e 's/#//' -e 's/ //g'`
|
|---|
| 266 | needs=`grep "$coltab[.]Needs:" $steps | sed -e "s/$coltab[.]Needs://"`
|
|---|
| 267 | influences=`grep "$coltab[.]Influences:" $steps | sed -e "s/$coltab[.]Influences://"`
|
|---|
| 268 | prims=( `grep "$table[.]Primary:" $steps | sed -e "s/$table[.]Primary://"` )
|
|---|
| 269 | # echo " column $column - table $table - coltab $coltab"
|
|---|
| 270 | # echo " needs: $needs"
|
|---|
| 271 | # echo " influences: $influences"
|
|---|
| 272 | # echo " prims: ${prims[@]}"
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | # function to get todolist
|
|---|
| 276 | function gettodo()
|
|---|
| 277 | {
|
|---|
| 278 | process=
|
|---|
| 279 | echo "getting todo..."
|
|---|
| 280 | getdbsetup
|
|---|
| 281 | getstepinfo
|
|---|
| 282 | # get query
|
|---|
| 283 | query=" select "${prims[0]}
|
|---|
| 284 | for (( i=1 ; i < ${#prims[@]} ; i++ ))
|
|---|
| 285 | do
|
|---|
| 286 | query=$query", ${prims[$i]}"
|
|---|
| 287 | done
|
|---|
| 288 | query=$query" from $table where "
|
|---|
| 289 | if ! echo $needs | grep '#' > /dev/null
|
|---|
| 290 | then
|
|---|
| 291 | for need in $needs
|
|---|
| 292 | do
|
|---|
| 293 | query=$query" not isnull($need) and"
|
|---|
| 294 | done
|
|---|
| 295 | fi
|
|---|
| 296 | query=$query" isnull($column) "
|
|---|
| 297 | query=$query" and isnull(fStartTime) and isnull(fFailedTime) and isnull(fProgramId) and isnull(fReturnCode) "
|
|---|
| 298 | query=$query" order by fPriority desc "
|
|---|
| 299 | if [ "$@ " != " " ]
|
|---|
| 300 | then
|
|---|
| 301 | query=$query" limit 0, $@ "
|
|---|
| 302 | fi
|
|---|
| 303 | echo " QUERY: "$query
|
|---|
| 304 | if ! process=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
|
|---|
| 305 | then
|
|---|
| 306 | echo "ERROR could not query processes from db -> exit"
|
|---|
| 307 | printprocesslog "ERROR could not query processes from db (program: $program, function gettodo)"
|
|---|
| 308 | finish
|
|---|
| 309 | fi
|
|---|
| 310 |
|
|---|
| 311 | if [ "$process" = "" ]
|
|---|
| 312 | then
|
|---|
| 313 | echo " => nothing to do"
|
|---|
| 314 | finish
|
|---|
| 315 | else
|
|---|
| 316 | primaries=( $process )
|
|---|
| 317 | num=`expr ${#primaries[@]} / ${#prims[@]} `
|
|---|
| 318 | fi
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 |
|
|---|
| 322 | # function to get the number of processes which still have to be done
|
|---|
| 323 | function getstatus()
|
|---|
| 324 | {
|
|---|
| 325 | numproc=
|
|---|
| 326 | getdbsetup
|
|---|
| 327 | getstepinfo
|
|---|
| 328 | # get query
|
|---|
| 329 | query=" select count(*) from $table where "
|
|---|
| 330 | if ! echo $needs | grep '#' > /dev/null
|
|---|
| 331 | then
|
|---|
| 332 | for need in $needs
|
|---|
| 333 | do
|
|---|
| 334 | query=$query" not isnull($need) and"
|
|---|
| 335 | done
|
|---|
| 336 | fi
|
|---|
| 337 | query=$query" isnull($column) "
|
|---|
| 338 | query=$query" and isnull(fStartTime) and isnull(fFailedTime) and isnull(fProgramId) and isnull(fReturnCode) "
|
|---|
| 339 | query=$query" group by $column "
|
|---|
| 340 | # echo " QUERY: "$query
|
|---|
| 341 | if ! numproc=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
|
|---|
| 342 | then
|
|---|
| 343 | echo "ERROR could not query number of todo proceses from db -> continue"
|
|---|
| 344 | printprocesslog "ERROR could not query number of processes from db (program: $program, function getstatus)"
|
|---|
| 345 | cont
|
|---|
| 346 | fi
|
|---|
| 347 | }
|
|---|
| 348 |
|
|---|
| 349 | # function to set status of a process in the db
|
|---|
| 350 | function setstatus()
|
|---|
| 351 | {
|
|---|
| 352 | if [ "$column" = "no" ]
|
|---|
| 353 | then
|
|---|
| 354 | return
|
|---|
| 355 | fi
|
|---|
| 356 | resetstatusvalues
|
|---|
| 357 | evalstatus $@
|
|---|
| 358 | getdbsetup
|
|---|
| 359 | getstepinfo
|
|---|
| 360 | # get query
|
|---|
| 361 | reset=`grep "$coltab[.]Reset:" $steps | sed -e "s/$coltab[.]Reset://" -e 's/ //g'`
|
|---|
| 362 | if [ "$reset" = "no" ]
|
|---|
| 363 | then
|
|---|
| 364 | echo "YOU CAN'T RESET $column for ${primaries[$s+$s]}!!!"
|
|---|
| 365 | printprocesslog "ERROR you can't reset $column for ${primaries[$s+$s]}"
|
|---|
| 366 | finish
|
|---|
| 367 | fi
|
|---|
| 368 | query=" update $table set $column=$statustime"
|
|---|
| 369 | if ! echo $influences | grep '#' > /dev/null
|
|---|
| 370 | then
|
|---|
| 371 | for influence in $influences
|
|---|
| 372 | do
|
|---|
| 373 | query=$query", $influence=NULL"
|
|---|
| 374 | done
|
|---|
| 375 | fi
|
|---|
| 376 | if ! [ "$starttime" = "noreset" ]
|
|---|
| 377 | then
|
|---|
| 378 | query=$query", fStartTime=$starttime"
|
|---|
| 379 | fi
|
|---|
| 380 | query=$query", fFailedTime=$failedtime, fProgramId=$programid, fReturnCode=$returncode "
|
|---|
| 381 | query=$query" where "
|
|---|
| 382 | if [ "$s" = "" ]
|
|---|
| 383 | then
|
|---|
| 384 | s=0
|
|---|
| 385 | fi
|
|---|
| 386 | query=$query" ${prims[0]}='${primaries[$s*${#prims[@]}]}'"
|
|---|
| 387 | for (( i=1 ; i < ${#prims[@]} ; i++ ))
|
|---|
| 388 | do
|
|---|
| 389 | query=$query" and ${prims[$i]}='${primaries[$s*${#prims[@]}+$i]}' "
|
|---|
| 390 | done
|
|---|
| 391 | echo " QUERY: "$query
|
|---|
| 392 | if ! mysql -s -u $us --password=$pw --host=$ho $db -e " $query "
|
|---|
| 393 | then
|
|---|
| 394 | echo "ERROR could not insert status into db -> exit"
|
|---|
| 395 | printprocesslog "ERROR could not set status in db (program: $program, function setstatus)"
|
|---|
| 396 | finish
|
|---|
| 397 | fi
|
|---|
| 398 |
|
|---|
| 399 | }
|
|---|
| 400 |
|
|---|
| 401 | # function to send a mysql query
|
|---|
| 402 | function sendquery()
|
|---|
| 403 | {
|
|---|
| 404 | getdbsetup
|
|---|
| 405 | if ! val=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
|
|---|
| 406 | then
|
|---|
| 407 | printprocesslog "ERROR could not query db (program: $program, function sendquery)"
|
|---|
| 408 | return 1
|
|---|
| 409 | fi
|
|---|
| 410 | if [ "$val" = "NULL" ]
|
|---|
| 411 | then
|
|---|
| 412 | val=
|
|---|
| 413 | fi
|
|---|
| 414 | echo $val
|
|---|
| 415 | return 0
|
|---|
| 416 | }
|
|---|
| 417 |
|
|---|