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

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