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

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