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

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