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

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