source: tags/Mars-V0.10.3/datacenter/scripts/sourcefile

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