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

Last change on this file since 8193 was 8193, 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
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 returncode=$check
227 failedcode=$com
228 if ! [ "$comadd" = "" ]
229 then
230 failedcodeadd=$comadd
231 fi
232 failedtime="Now()"
233 check="ok"
234 ;;
235 esac
236 ;;
237 *) echo "error -> exit"
238 printprocesslog "ERROR function evalstatus got wrong variable"
239 finish
240 ;;
241 esac
242}
243
244# function calling the macro to set the status, after a process has finished
245function setstatusroot()
246{
247 # set status values
248 resetstatusvalues
249 evalstatus $@
250
251# printstatusvalues
252 # set status
253 setstatuslogpath=$logpath/setstatus/$program/$var1
254 makedir $setstatuslogpath
255 setstatuslog=$setstatuslogpath/setstatus-$@-$program-$var2.log
256 checkstatus=`root -q -b $macrospath/setstatus.C+\("\"$var2\""\,"\"$table\""\,"\"$column\""\,"\"$statustime\""\,"\"$returncode\""\,"\"$failedcode\""\,"\"$failedcodeadd\""\,"\"$starttime\""\,"\"$failedtime\""\) | tee $setstatuslog | grep int | sed -e 's/(int)//'`
257 case $checkstatus in
258 1) echo " checkstatus=$checkstatus -> everything ok, status has been set";;
259 *) echo " checkstatus=$checkstatus -> ERROR -> step could not be set -> exit"
260 finish ;;
261 esac
262}
263
264function getdbsetup()
265{
266 db=`grep Database $mars/sql.rc | grep -v '#' | sed -e 's/Database: //' -e 's/ //g'`
267 pw=`grep Password $mars/sql.rc | grep -v '#' | sed -e 's/Password: //' -e 's/ //g'`
268 us=`grep User $mars/sql.rc | grep -v '#' | sed -e 's/User: //' -e 's/ //g'`
269# echo "setup: "
270# echo " db: "$db
271# echo " pw: "$pw
272# echo " us: "$us
273}
274
275function getstepinfo()
276{
277 table=`grep "$column:" $steps | sed -e "s/[.]$column://" -e 's/#//' -e 's/ //g'`
278 coltab=`grep "$column:" $steps | sed -e 's/://' -e 's/#//' -e 's/ //g'`
279 needs=`grep "$coltab[.]Needs:" $steps | sed -e "s/$coltab[.]Needs://"`
280 influences=`grep "$coltab[.]Influences:" $steps | sed -e "s/$coltab[.]Influences://"`
281 primary=`grep "$table[.]Primary:" $steps | sed -e "s/$table[.]Primary://"`
282# echo " column $column - table $table - coltab $coltab"
283# echo " needs: $needs"
284# echo " influences: $influences"
285# echo " primary: $primary"
286}
287
288# function to get todolist
289function getdolist()
290{
291 echo "getting todolist..."
292 getdbsetup
293 getstepinfo
294 # get query
295 query=" select $primary from $table where "
296 if ! echo $needs | grep '#' > /dev/null
297 then
298 for need in $needs
299 do
300 query=$query" not isnull($need) and"
301 done
302 fi
303 query=$query" isnull($column) "
304 query=$query" and isnull(fStartTime) and isnull(fFailedTime) and isnull(fFailedCode) and isnull(fFailedCodeAdd) and isnull(fReturnCode) "
305 query=$query" order by $primary desc "
306# echo " QUERY: "$query
307 if ! process=`mysql -s -u $us --password=$pw --host=hercules $db -e " $query "`
308 then
309 echo "ERROR could not query processes from db -> exit"
310 printprocesslog "ERROR could not query processes from db (program: $program, function getdolist)"
311 finish
312 fi
313
314 if [ "$process" = "" ]
315 then
316 echo " => nothing to do"
317 finish
318 else
319 todofile=$listpath/ToDo-$table-$column.txt
320
321 if ls $todofile > /dev/null 2>&1
322 then
323 echo "$todofile exists already"
324 printprocesslog "WARN $todofile exists already (program: $program, function getdolist)"
325 finish
326 fi
327 echo "found processes, writing todofile..."
328 echo $process > $todofile
329 fi
330}
331
332# function to get todo (process)
333function gettodo()
334{
335 process=
336 echo "getting todo..."
337 getdbsetup
338 getstepinfo
339 # get query
340 query=" select $primary from $table where "
341 if ! echo $needs | grep '#' > /dev/null
342 then
343 for need in $needs
344 do
345 query=$query" not isnull($need) and"
346 done
347 fi
348 query=$query" isnull($column) "
349 query=$query" and isnull(fStartTime) and isnull(fFailedTime) and isnull(fFailedCode) and isnull(fFailedCodeAdd) and isnull(fReturnCode) "
350 query=$query" order by $primary desc "
351 query=$query" limit 0, 1 "
352# echo " QUERY: "$query
353 if ! process=`mysql -s -u $us --password=$pw --host=hercules $db -e " $query "`
354 then
355 echo "ERROR could not query process from db -> exit"
356 printprocesslog "ERROR could not query process from db (program: $program, function gettodo)"
357 finish
358 fi
359
360 if [ "$process" = "" ]
361 then
362 echo " => nothing to do -> exit"
363 finish
364 fi
365}
366
367# function to get the number of processes which still have to be done
368function getstatus()
369{
370 numproc=
371# echo "getting status..."
372 getdbsetup
373 column=${scriptscolname[$i]}
374 getstepinfo
375 # get query
376 query=" select count(*) from $table where "
377 if ! echo $needs | grep '#' > /dev/null
378 then
379 for need in $needs
380 do
381 query=$query" not isnull($need) and"
382 done
383 fi
384 query=$query" isnull($column) "
385 query=$query" and isnull(fStartTime) and isnull(fFailedTime) and isnull(fFailedCode) and isnull(fFailedCodeAdd) and isnull(fReturnCode) "
386 query=$query" group by $column "
387# echo " QUERY: "$query
388 numproc=0
389 if ! numproc=`mysql -s -u $us --password=$pw --host=hercules $db -e " $query "`
390 then
391 echo "ERROR could not query number of todo proceses from db -> continue"
392 printprocesslog "ERROR could not query number of processes from db (program: $program, function getstatus)"
393 cont
394 fi
395}
396
397# function to set status of a process in the db
398function setstatus()
399{
400 resetstatusvalues
401 evalstatus $@
402 getdbsetup
403 getstepinfo
404 # get query
405 reset=`grep "$coltab[.]Reset:" $steps | sed -e "s/$coltab[.]Reset://" -e 's/ //g'`
406 if [ "$reset" = "no" ]
407 then
408 echo "YOU CAN'T RESET $column for $var2!!!"
409 printprocesslog "ERROR you can't reset $column for $var2"
410 finish
411 fi
412 query=" update $table set $column=$statustime"
413 if ! echo $influences | grep '#' > /dev/null
414 then
415 for influence in $influences
416 do
417 query=$query", $influence=NULL"
418 done
419 fi
420 if ! [ "$starttime" = "noreset" ]
421 then
422 query=$query", fStartTime=$starttime"
423 fi
424 query=$query", fFailedTime=$failedtime, fFailedCode=$failedcode, fFailedCodeAdd=$failedcodeadd, fReturnCode=$returncode "
425 query=$query" where $primary='$var2'"
426 echo " QUERY: "$query
427 if ! mysql -s -u $us --password=$pw --host=hercules $db -e " $query "
428 then
429 echo "ERROR could not insert status into db -> exit"
430 printprocesslog "ERROR could not set status in db (program: $program, function setstatus)"
431 finish
432 fi
433
434}
435
Note: See TracBrowser for help on using the repository browser.