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

Last change on this file since 9198 was 9183, checked in by Daniela Dorner, 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#mc run process status
129Fcorsika=23
130Freflector=24
131Fcamera=25
132#again run process status
133FCompmux=26
134Fdowebplots=27
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) echo " checklock0=$checklock0 -> continue " ;;
185 1) echo " checklock0=$checklock0 -> file $lockfile exists"
186 date
187 $@
188 exit;;
189 *) echo " 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) echo "setstatus start"
219 starttime="Now()"
220 ;;
221 stop) case $check in
222 ok) echo "setstatus stop - ok"
223 statustime="Now()"
224 ;;
225 no) echo "setstatus stop - nothing new"
226 check="ok"
227 ;;
228 *) echo "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 *) echo "error -> exit"
243 printprocesslog "ERROR function evalstatus got wrong variable"
244 finish
245 ;;
246 esac
247}
248
249function getdbsetup()
250{
251 db=`grep Database $sqlrc | grep -v '#' | sed -e 's/Database: //' -e 's/ //g'`
252 pw=`grep Password $sqlrc | grep -v '#' | sed -e 's/Password: //' -e 's/ //g'`
253 us=`grep User $sqlrc | grep -v '#' | sed -e 's/User: //' -e 's/ //g'`
254 ho=`grep URL $sqlrc | grep -v '#' | sed -e 's/ //g' -e 's/URL:mysql:\/\///'`
255# echo "setup: "
256# echo " db: "$db
257# echo " pw: "$pw
258# echo " us: "$us
259# echo " ho: "$ho
260}
261
262function getstepinfo()
263{
264 table=`grep "$column:" $steps | sed -e "s/[.]$column://" -e 's/#//' -e 's/ //g'`
265 coltab=`grep "$column:" $steps | sed -e 's/://' -e 's/#//' -e 's/ //g'`
266 needs=`grep "$coltab[.]Needs:" $steps | sed -e "s/$coltab[.]Needs://"`
267 influences=`grep "$coltab[.]Influences:" $steps | sed -e "s/$coltab[.]Influences://"`
268 prims=( `grep "$table[.]Primary:" $steps | sed -e "s/$table[.]Primary://"` )
269# echo " column $column - table $table - coltab $coltab"
270# echo " needs: $needs"
271# echo " influences: $influences"
272# echo " prims: ${prims[@]}"
273}
274
275# function to get todolist
276function gettodo()
277{
278 process=
279 echo "getting todo..."
280 getdbsetup
281 getstepinfo
282 # get query
283 query=" select "${prims[0]}
284 for (( i=1 ; i < ${#prims[@]} ; i++ ))
285 do
286 query=$query", ${prims[$i]}"
287 done
288 query=$query" from $table where "
289 if ! echo $needs | grep '#' > /dev/null
290 then
291 for need in $needs
292 do
293 query=$query" not isnull($need) and"
294 done
295 fi
296 query=$query" isnull($column) "
297 query=$query" and isnull(fStartTime) and isnull(fFailedTime) and isnull(fProgramId) and isnull(fReturnCode) "
298 query=$query" order by fPriority desc "
299 if [ "$@ " != " " ]
300 then
301 query=$query" limit 0, $@ "
302 fi
303 echo " QUERY: "$query
304 if ! process=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
305 then
306 echo "ERROR could not query processes from db -> exit"
307 printprocesslog "ERROR could not query processes from db (program: $program, function gettodo)"
308 finish
309 fi
310
311 if [ "$process" = "" ]
312 then
313 echo " => nothing to do"
314 finish
315 else
316 primaries=( $process )
317 num=`expr ${#primaries[@]} / ${#prims[@]} `
318 fi
319}
320
321
322# function to get the number of processes which still have to be done
323function getstatus()
324{
325 numproc=
326 getdbsetup
327 getstepinfo
328 # get query
329 query=" select count(*) from $table where "
330 if ! echo $needs | grep '#' > /dev/null
331 then
332 for need in $needs
333 do
334 query=$query" not isnull($need) and"
335 done
336 fi
337 query=$query" isnull($column) "
338 query=$query" and isnull(fStartTime) and isnull(fFailedTime) and isnull(fProgramId) and isnull(fReturnCode) "
339 query=$query" group by $column "
340# echo " QUERY: "$query
341 if ! numproc=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
342 then
343 echo "ERROR could not query number of todo proceses from db -> continue"
344 printprocesslog "ERROR could not query number of processes from db (program: $program, function getstatus)"
345 cont
346 fi
347}
348
349# function to set status of a process in the db
350function setstatus()
351{
352 if [ "$column" = "no" ]
353 then
354 return
355 fi
356 resetstatusvalues
357 evalstatus $@
358 getdbsetup
359 getstepinfo
360 # get query
361 reset=`grep "$coltab[.]Reset:" $steps | sed -e "s/$coltab[.]Reset://" -e 's/ //g'`
362 if [ "$reset" = "no" ]
363 then
364 echo "YOU CAN'T RESET $column for ${primaries[$s+$s]}!!!"
365 printprocesslog "ERROR you can't reset $column for ${primaries[$s+$s]}"
366 finish
367 fi
368 query=" update $table set $column=$statustime"
369 if ! echo $influences | grep '#' > /dev/null
370 then
371 for influence in $influences
372 do
373 query=$query", $influence=NULL"
374 done
375 fi
376 if ! [ "$starttime" = "noreset" ]
377 then
378 query=$query", fStartTime=$starttime"
379 fi
380 query=$query", fFailedTime=$failedtime, fProgramId=$programid, fReturnCode=$returncode "
381 query=$query" where "
382 if [ "$s" = "" ]
383 then
384 s=0
385 fi
386 query=$query" ${prims[0]}='${primaries[$s*${#prims[@]}]}'"
387 for (( i=1 ; i < ${#prims[@]} ; i++ ))
388 do
389 query=$query" and ${prims[$i]}='${primaries[$s*${#prims[@]}+$i]}' "
390 done
391 echo " QUERY: "$query
392 if ! mysql -s -u $us --password=$pw --host=$ho $db -e " $query "
393 then
394 echo "ERROR could not insert status into db -> exit"
395 printprocesslog "ERROR could not set status in db (program: $program, function setstatus)"
396 finish
397 fi
398
399}
400
401# function to send a mysql query
402function sendquery()
403{
404 getdbsetup
405 if ! val=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
406 then
407 printprocesslog "ERROR could not query db (program: $program, function sendquery)"
408 return 1
409 fi
410 if [ "$val" = "NULL" ]
411 then
412 val=
413 fi
414 echo $val
415 return 0
416}
417
Note: See TracBrowser for help on using the repository browser.