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

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