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

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