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

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