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

Last change on this file since 8482 was 8482, checked in by Daniela Dorner, 17 years ago
*** empty log message ***
  • Property svn:executable set to *
File size: 11.0 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-2007
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 if ! [ "$todofile" = "" ] && ls $todofile >/dev/null 2>&1
136 then
137 rm -v $todofile
138 fi
139 if ls $lockfile >/dev/null 2>&1
140 then
141 rm -v $lockfile
142 fi
143 date
144 printprocesslog "INFO finished $0"
145 exit
146}
147
148# function to do continue in a loop and produce according logging
149function cont()
150{
151 date
152 echo ""
153 continue
154}
155
156# function to check if a process is already locked
157function checklock()
158{
159 date > $lockfile
160 checklock0=$?
161 case $checklock0 in
162 0) echo " checklock0=$checklock0 -> continue " ;;
163 1) echo " checklock0=$checklock0 -> file $lockfile exists"
164 date
165 $@
166 exit;;
167 *) echo " checklock0=$checklock0 -> something went completely wrong" ;;
168 esac
169}
170
171function resetstatusvalues()
172{
173 statustime=NULL
174 starttime=NULL
175 returncode=NULL
176 failedcode=NULL
177 failedcodeadd=NULL
178 failedtime=NULL
179}
180
181function printstatusvalues()
182{
183 echo "the current values are:"
184 echo " statustime=$statustime"
185 echo " starttime=$starttime"
186 echo " returncode=$returncode"
187 echo " failedcode=$failedcode"
188 echo " failedcodeadd=$failedcodeadd"
189 echo " failedtime=$failedtime"
190 echo "-- check: -$check-"
191 echo ""
192}
193
194# function evaluating the statusvalues
195function evalstatus()
196{
197 case $@ in
198 start) echo "setstatus start"
199 starttime="Now()"
200 ;;
201 stop) case $check in
202 ok) echo "setstatus stop - ok"
203 statustime="Now()"
204 ;;
205 no) echo "setstatus stop - nothing new"
206 check="ok"
207 ;;
208 *) echo "setstatus stop - failed"
209 starttime=noreset
210 if [ "$check" == "" ]
211 then
212 returncode=1
213 else
214 returncode=$check
215 fi
216 failedcode=$com
217 if ! [ "$comadd" = "" ]
218 then
219 failedcodeadd=$comadd
220 fi
221 failedtime="Now()"
222 check="ok"
223 ;;
224 esac
225 ;;
226 *) echo "error -> exit"
227 printprocesslog "ERROR function evalstatus got wrong variable"
228 finish
229 ;;
230 esac
231}
232
233function getdbsetup()
234{
235 db=`grep Database $mars/sql.rc | grep -v '#' | sed -e 's/Database: //' -e 's/ //g'`
236 pw=`grep Password $mars/sql.rc | grep -v '#' | sed -e 's/Password: //' -e 's/ //g'`
237 us=`grep User $mars/sql.rc | grep -v '#' | sed -e 's/User: //' -e 's/ //g'`
238# echo "setup: "
239# echo " db: "$db
240# echo " pw: "$pw
241# echo " us: "$us
242}
243
244function getstepinfo()
245{
246 table=`grep "$column:" $steps | sed -e "s/[.]$column://" -e 's/#//' -e 's/ //g'`
247 coltab=`grep "$column:" $steps | sed -e 's/://' -e 's/#//' -e 's/ //g'`
248 needs=`grep "$coltab[.]Needs:" $steps | sed -e "s/$coltab[.]Needs://"`
249 influences=`grep "$coltab[.]Influences:" $steps | sed -e "s/$coltab[.]Influences://"`
250 primary=`grep "$table[.]Primary:" $steps | sed -e "s/$table[.]Primary://"`
251# echo " column $column - table $table - coltab $coltab"
252# echo " needs: $needs"
253# echo " influences: $influences"
254# echo " primary: $primary"
255}
256
257# function to get todolist
258function getdolist()
259{
260 echo "getting todolist..."
261 getdbsetup
262 getstepinfo
263 # get query
264 query=" select $primary from $table where "
265 if ! echo $needs | grep '#' > /dev/null
266 then
267 for need in $needs
268 do
269 query=$query" not isnull($need) and"
270 done
271 fi
272 query=$query" isnull($column) "
273 query=$query" and isnull(fStartTime) and isnull(fFailedTime) and isnull(fFailedCode) and isnull(fFailedCodeAdd) and isnull(fReturnCode) "
274 query=$query" order by $primary desc "
275# echo " QUERY: "$query
276 if ! process=`mysql -s -u $us --password=$pw --host=vela $db -e " $query "`
277 then
278 echo "ERROR could not query processes from db -> exit"
279 printprocesslog "ERROR could not query processes from db (program: $program, function getdolist)"
280 finish
281 fi
282
283 if [ "$process" = "" ]
284 then
285 echo " => nothing to do"
286 finish
287 else
288 todofile=$listpath/ToDo-$table-$column.txt
289
290 if ls $todofile > /dev/null 2>&1
291 then
292 echo "$todofile exists already"
293 printprocesslog "WARN $todofile exists already (program: $program, function getdolist)"
294 finish
295 fi
296 echo "found processes, writing todofile..."
297 echo $process > $todofile
298 fi
299}
300
301# function to get todo (process)
302function gettodo()
303{
304 process=
305 echo "getting todo..."
306 getdbsetup
307 getstepinfo
308 # get query
309 query=" select $primary from $table where "
310 if ! echo $needs | grep '#' > /dev/null
311 then
312 for need in $needs
313 do
314 query=$query" not isnull($need) and"
315 done
316 fi
317 query=$query" isnull($column) "
318 query=$query" and isnull(fStartTime) and isnull(fFailedTime) and isnull(fFailedCode) and isnull(fFailedCodeAdd) and isnull(fReturnCode) "
319 query=$query" order by $primary desc "
320 query=$query" limit 0, 1 "
321# echo " QUERY: "$query
322 if ! process=`mysql -s -u $us --password=$pw --host=vela $db -e " $query "`
323 then
324 echo "ERROR could not query process from db -> exit"
325 printprocesslog "ERROR could not query process from db (program: $program, function gettodo)"
326 finish
327 fi
328
329 if [ "$process" = "" ]
330 then
331 echo " => nothing to do -> exit"
332 finish
333 fi
334}
335
336# function to get the number of processes which still have to be done
337function getstatus()
338{
339 numproc=
340# echo "getting status..."
341 getdbsetup
342 column=${scriptscolname[$i]}
343 getstepinfo
344 # get query
345 query=" select count(*) 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" group by $column "
356# echo " QUERY: "$query
357 numproc=0
358 if ! numproc=`mysql -s -u $us --password=$pw --host=vela $db -e " $query "`
359 then
360 echo "ERROR could not query number of todo proceses from db -> continue"
361 printprocesslog "ERROR could not query number of processes from db (program: $program, function getstatus)"
362 cont
363 fi
364}
365
366# function to set status of a process in the db
367function setstatus()
368{
369 resetstatusvalues
370 evalstatus $@
371 getdbsetup
372 getstepinfo
373 # get query
374 reset=`grep "$coltab[.]Reset:" $steps | sed -e "s/$coltab[.]Reset://" -e 's/ //g'`
375 if [ "$reset" = "no" ]
376 then
377 echo "YOU CAN'T RESET $column for $primvar!!!"
378 printprocesslog "ERROR you can't reset $column for $primvar"
379 finish
380 fi
381 query=" update $table set $column=$statustime"
382 if ! echo $influences | grep '#' > /dev/null
383 then
384 for influence in $influences
385 do
386 query=$query", $influence=NULL"
387 done
388 fi
389 if ! [ "$starttime" = "noreset" ]
390 then
391 query=$query", fStartTime=$starttime"
392 fi
393 query=$query", fFailedTime=$failedtime, fFailedCode=$failedcode, fFailedCodeAdd=$failedcodeadd, fReturnCode=$returncode "
394 query=$query" where $primary='$primvar'"
395 echo " QUERY: "$query
396 if ! mysql -s -u $us --password=$pw --host=vela $db -e " $query "
397 then
398 echo "ERROR could not insert status into db -> exit"
399 printprocesslog "ERROR could not set status in db (program: $program, function setstatus)"
400 finish
401 fi
402
403}
404
405# function to send a mysql query
406function sendquery()
407{
408 getdbsetup
409 if ! val=`mysql -s -u $us --password=$pw --host=vela $db -e " $query "`
410 then
411 printprocesslog "ERROR could not query db (program: $program, function sendquery)"
412 return 1
413 fi
414 if [ "$val" = "NULL" ]
415 then
416 val=
417 fi
418 echo $val
419 return 0
420}
421
Note: See TracBrowser for help on using the repository browser.