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

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