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

Last change on this file since 9164 was 9164, 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
114sqlrc=$mars/sql.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 ! [ "$lockfile" = "" ] && ls $lockfile >/dev/null 2>&1
142 then
143 rm -v $lockfile
144 fi
145 date
146 printprocesslog "INFO finished $0"
147 exit
148}
149
150# function to do continue in a loop and produce according logging
151function cont()
152{
153 date
154 echo ""
155 continue
156}
157
158# function to check if a process is already locked
159function checklock()
160{
161 date > $lockfile
162 checklock0=$?
163 case $checklock0 in
164 0) echo " checklock0=$checklock0 -> continue " ;;
165 1) echo " checklock0=$checklock0 -> file $lockfile exists"
166 date
167 $@
168 exit;;
169 *) echo " checklock0=$checklock0 -> something went completely wrong" ;;
170 esac
171}
172
173function resetstatusvalues()
174{
175 statustime=NULL
176 starttime=NULL
177 returncode=NULL
178 programid=NULL
179 failedtime=NULL
180}
181
182function printstatusvalues()
183{
184 echo "the current values are:"
185 echo " statustime=$statustime"
186 echo " starttime=$starttime"
187 echo " returncode=$returncode"
188 echo " programid=$programid"
189 echo " failedtime=$failedtime"
190 echo "-- check: -$check-"
191 echo ""
192}
193
194# function evaluating the statusvalues
195function evalstatus()
196{
197 case $@ in
198 start) echo "setstatus start"
199 starttime="Now()"
200 ;;
201 stop) case $check in
202 ok) echo "setstatus stop - ok"
203 statustime="Now()"
204 ;;
205 no) echo "setstatus stop - nothing new"
206 check="ok"
207 ;;
208 *) echo "setstatus stop - failed"
209 starttime=noreset
210 if [ "$check" == "" ]
211 then
212 returncode=1
213 else
214 returncode=$check
215 fi
216 programid=$com
217 failedtime="Now()"
218 check="ok"
219 ;;
220 esac
221 ;;
222 *) echo "error -> exit"
223 printprocesslog "ERROR function evalstatus got wrong variable"
224 finish
225 ;;
226 esac
227}
228
229function getdbsetup()
230{
231 db=`grep Database $sqlrc | grep -v '#' | sed -e 's/Database: //' -e 's/ //g'`
232 pw=`grep Password $sqlrc | grep -v '#' | sed -e 's/Password: //' -e 's/ //g'`
233 us=`grep User $sqlrc | grep -v '#' | sed -e 's/User: //' -e 's/ //g'`
234 ho=`grep URL $sqlrc | grep -v '#' | sed -e 's/ //g' -e 's/URL:mysql:\/\///'`
235# echo "setup: "
236# echo " db: "$db
237# echo " pw: "$pw
238# echo " us: "$us
239# echo " ho: "$ho
240}
241
242function getstepinfo()
243{
244 table=`grep "$column:" $steps | sed -e "s/[.]$column://" -e 's/#//' -e 's/ //g'`
245 coltab=`grep "$column:" $steps | sed -e 's/://' -e 's/#//' -e 's/ //g'`
246 needs=`grep "$coltab[.]Needs:" $steps | sed -e "s/$coltab[.]Needs://"`
247 influences=`grep "$coltab[.]Influences:" $steps | sed -e "s/$coltab[.]Influences://"`
248 prims=( `grep "$table[.]Primary:" $steps | sed -e "s/$table[.]Primary://"` )
249# echo " column $column - table $table - coltab $coltab"
250# echo " needs: $needs"
251# echo " influences: $influences"
252# echo " prims: ${prims[@]}"
253}
254
255# function to get todolist
256function gettodo()
257{
258 process=
259 echo "getting todo..."
260 getdbsetup
261 getstepinfo
262 # get query
263 query=" select "${prims[0]}
264 for (( i=1 ; i < ${#prims[@]} ; i++ ))
265 do
266 query=$query", ${prims[$i]}"
267 done
268 query=$query" from $table where "
269 if ! echo $needs | grep '#' > /dev/null
270 then
271 for need in $needs
272 do
273 query=$query" not isnull($need) and"
274 done
275 fi
276 query=$query" isnull($column) "
277 query=$query" and isnull(fStartTime) and isnull(fFailedTime) and isnull(fProgramId) and isnull(fReturnCode) "
278 query=$query" order by fPriority desc "
279 if [ "$@ " != " " ]
280 then
281 query=$query" limit 0, $@ "
282 fi
283 echo " QUERY: "$query
284 if ! process=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
285 then
286 echo "ERROR could not query processes from db -> exit"
287 printprocesslog "ERROR could not query processes from db (program: $program, function gettodo)"
288 finish
289 fi
290
291 if [ "$process" = "" ]
292 then
293 echo " => nothing to do"
294 finish
295 else
296 primaries=( $process )
297 num=`expr ${#primaries[@]} / ${#prims[@]} `
298 fi
299}
300
301
302# function to get the number of processes which still have to be done
303function getstatus()
304{
305 numproc=
306 getdbsetup
307 getstepinfo
308 # get query
309 query=" select count(*) from $table where "
310 if ! echo $needs | grep '#' > /dev/null
311 then
312 for need in $needs
313 do
314 query=$query" not isnull($need) and"
315 done
316 fi
317 query=$query" isnull($column) "
318 query=$query" and isnull(fStartTime) and isnull(fFailedTime) and isnull(fProgramId) and isnull(fReturnCode) "
319 query=$query" group by $column "
320# echo " QUERY: "$query
321 if ! numproc=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
322 then
323 echo "ERROR could not query number of todo proceses from db -> continue"
324 printprocesslog "ERROR could not query number of processes from db (program: $program, function getstatus)"
325 cont
326 fi
327}
328
329# function to set status of a process in the db
330function setstatus()
331{
332 if [ "$column" = "no" ]
333 then
334 return
335 fi
336 resetstatusvalues
337 evalstatus $@
338 getdbsetup
339 getstepinfo
340 # get query
341 reset=`grep "$coltab[.]Reset:" $steps | sed -e "s/$coltab[.]Reset://" -e 's/ //g'`
342 if [ "$reset" = "no" ]
343 then
344 echo "YOU CAN'T RESET $column for ${primaries[$s+$s]}!!!"
345 printprocesslog "ERROR you can't reset $column for ${primaries[$s+$s]}"
346 finish
347 fi
348 query=" update $table set $column=$statustime"
349 if ! echo $influences | grep '#' > /dev/null
350 then
351 for influence in $influences
352 do
353 query=$query", $influence=NULL"
354 done
355 fi
356 if ! [ "$starttime" = "noreset" ]
357 then
358 query=$query", fStartTime=$starttime"
359 fi
360 query=$query", fFailedTime=$failedtime, fProgramId=$programid, fReturnCode=$returncode "
361 query=$query" where "
362 if [ "$s" = "" ]
363 then
364 s=0
365 fi
366 query=$query" ${prims[0]}='${primaries[$s*${#prims[@]}]}'"
367 for (( i=1 ; i < ${#prims[@]} ; i++ ))
368 do
369 query=$query" and ${prims[$i]}='${primaries[$s*${#prims[@]}+$i]}' "
370 fi
371 done
372 echo " QUERY: "$query
373 if ! mysql -s -u $us --password=$pw --host=$ho $db -e " $query "
374 then
375 echo "ERROR could not insert status into db -> exit"
376 printprocesslog "ERROR could not set status in db (program: $program, function setstatus)"
377 finish
378 fi
379
380}
381
382# function to send a mysql query
383function sendquery()
384{
385 getdbsetup
386 if ! val=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
387 then
388 printprocesslog "ERROR could not query db (program: $program, function sendquery)"
389 return 1
390 fi
391 if [ "$val" = "NULL" ]
392 then
393 val=
394 fi
395 echo $val
396 return 0
397}
398
Note: See TracBrowser for help on using the repository browser.