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

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