source: trunk/MagicSoft/Mars/datacenter/scripts/sourcefile@ 8078

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