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 | source `dirname $0`/setup
|
---|
34 |
|
---|
35 | mars=`dirname $0 | sed -e 's/\/datacenter\/scripts//'`
|
---|
36 | macrospath=$mars/datacenter/macros
|
---|
37 | scriptspath=$mars/datacenter/scripts
|
---|
38 |
|
---|
39 | datetime=`date +%F-%H-%M-%S`
|
---|
40 |
|
---|
41 |
|
---|
42 | # function to make sure that a directory is made
|
---|
43 | function 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
|
---|
62 | runlogpath=$logpath/run/`date +%Y/%m/%d`
|
---|
63 | processlogpath=$logpath/processlog
|
---|
64 | makedir $runlogpath
|
---|
65 | makedir $processlogpath
|
---|
66 | processlog=$processlogpath/process`date +%F`.log
|
---|
67 |
|
---|
68 | makedir $lockpath
|
---|
69 |
|
---|
70 | # set checkvalue to ok at the beginning of the scripts
|
---|
71 | check="ok"
|
---|
72 |
|
---|
73 | #failed codes
|
---|
74 | #sequence build status
|
---|
75 | Fbuildsequ=1
|
---|
76 | Fdoexcl=2
|
---|
77 | #run process status
|
---|
78 | Ftimecorr=3
|
---|
79 | Ffillraw=4
|
---|
80 | Fsinope=5
|
---|
81 | Ffillsinope=6
|
---|
82 | Fresetexcl=7
|
---|
83 | #sequence process status
|
---|
84 | Fwritesequfile=8
|
---|
85 | Ffilesavail=9
|
---|
86 | Fnoccfile=10
|
---|
87 | Fnocacofile=11
|
---|
88 | Fmerppcc=12
|
---|
89 | Fmerppcaco=13
|
---|
90 | Fcallisto=14
|
---|
91 | Ffillcalib=15
|
---|
92 | Ffillsignal=16
|
---|
93 | Fstar=17
|
---|
94 | Ffillstar=18
|
---|
95 | #dataset process status
|
---|
96 | Fwritedatasetfile=19
|
---|
97 | Fstardone=20
|
---|
98 | Fganymed=21
|
---|
99 | Ffillganymed=22
|
---|
100 | #mc run process status
|
---|
101 | Fcorsika=23
|
---|
102 | Freflector=24
|
---|
103 | Fcamera=25
|
---|
104 | #again run process status
|
---|
105 | FCompmux=26
|
---|
106 | Fdowebplots=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
|
---|
111 | jmerrorlog=$runlogpath/jobmanager-error`date +%F`.log
|
---|
112 | jmscriptlog=$runlogpath/jobmanager`date +%F`.log
|
---|
113 | steps=$mars/resources/steps.rc
|
---|
114 | sqlrc=$mars/sql.rc
|
---|
115 | # resetting values
|
---|
116 | pno=0
|
---|
117 | totalpno=0
|
---|
118 | running=0
|
---|
119 | queued=0
|
---|
120 | runningscript=0
|
---|
121 | queuedscript=0
|
---|
122 | stillinqueue=0
|
---|
123 |
|
---|
124 |
|
---|
125 | # alias (we cannot check the beginning of the line due to
|
---|
126 | # color codes in filldotraw.C)
|
---|
127 | alias '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 |
|
---|
132 | function printprocesslog
|
---|
133 | {
|
---|
134 | makedir $processlogpath
|
---|
135 | echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "`basename $0`"["$$"] "$@ >> $processlog
|
---|
136 | }
|
---|
137 |
|
---|
138 | # function to exit a script properly
|
---|
139 | function 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
|
---|
151 | function cont()
|
---|
152 | {
|
---|
153 | date
|
---|
154 | echo ""
|
---|
155 | continue
|
---|
156 | }
|
---|
157 |
|
---|
158 | # function to check if a process is already locked
|
---|
159 | function 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 |
|
---|
173 | function resetstatusvalues()
|
---|
174 | {
|
---|
175 | statustime=NULL
|
---|
176 | starttime=NULL
|
---|
177 | returncode=NULL
|
---|
178 | programid=NULL
|
---|
179 | failedtime=NULL
|
---|
180 | }
|
---|
181 |
|
---|
182 | function 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
|
---|
195 | function 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 |
|
---|
229 | function 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 |
|
---|
242 | function 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
|
---|
256 | function gettodo()
|
---|
257 | {
|
---|
258 | process=
|
---|
259 | echo "getting todo..."
|
---|
260 | getdbsetup
|
---|
261 | getstepinfo
|
---|
262 | # get query
|
---|
263 | query=" select "
|
---|
264 | for (( i=0 ; i < ${#prims[@]} ; i++ ))
|
---|
265 | do
|
---|
266 | if [ $i -lt `expr ${#prims[@]} - 1` ]
|
---|
267 | then
|
---|
268 | query=$query" ${prims[$i]}, "
|
---|
269 | else
|
---|
270 | query=$query" ${prims[$i]} "
|
---|
271 | fi
|
---|
272 | done
|
---|
273 | query=$query" from $table where "
|
---|
274 | if ! echo $needs | grep '#' > /dev/null
|
---|
275 | then
|
---|
276 | for need in $needs
|
---|
277 | do
|
---|
278 | query=$query" not isnull($need) and"
|
---|
279 | done
|
---|
280 | fi
|
---|
281 | query=$query" isnull($column) "
|
---|
282 | query=$query" and isnull(fStartTime) and isnull(fFailedTime) and isnull(fProgramId) and isnull(fReturnCode) "
|
---|
283 | query=$query" order by fPriority desc "
|
---|
284 | if [ "$@ " != " " ]
|
---|
285 | then
|
---|
286 | query=$query" limit 0, $@ "
|
---|
287 | fi
|
---|
288 | echo " QUERY: "$query
|
---|
289 | if ! process=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
|
---|
290 | then
|
---|
291 | echo "ERROR could not query processes from db -> exit"
|
---|
292 | printprocesslog "ERROR could not query processes from db (program: $program, function gettodo)"
|
---|
293 | finish
|
---|
294 | fi
|
---|
295 |
|
---|
296 | if [ "$process" = "" ]
|
---|
297 | then
|
---|
298 | echo " => nothing to do"
|
---|
299 | finish
|
---|
300 | else
|
---|
301 | primaries=( $process )
|
---|
302 | num=`expr ${#primaries[@]} / ${#prims[@]} `
|
---|
303 | fi
|
---|
304 | }
|
---|
305 |
|
---|
306 |
|
---|
307 | # function to get the number of processes which still have to be done
|
---|
308 | function getstatus()
|
---|
309 | {
|
---|
310 | numproc=
|
---|
311 | getdbsetup
|
---|
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 | if ! numproc=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
|
---|
327 | then
|
---|
328 | echo "ERROR could not query number of todo proceses from db -> continue"
|
---|
329 | printprocesslog "ERROR could not query number of processes from db (program: $program, function getstatus)"
|
---|
330 | cont
|
---|
331 | fi
|
---|
332 | }
|
---|
333 |
|
---|
334 | # function to set status of a process in the db
|
---|
335 | function setstatus()
|
---|
336 | {
|
---|
337 | if [ "$column" = "no" ]
|
---|
338 | then
|
---|
339 | return
|
---|
340 | fi
|
---|
341 | resetstatusvalues
|
---|
342 | evalstatus $@
|
---|
343 | getdbsetup
|
---|
344 | getstepinfo
|
---|
345 | # get query
|
---|
346 | reset=`grep "$coltab[.]Reset:" $steps | sed -e "s/$coltab[.]Reset://" -e 's/ //g'`
|
---|
347 | if [ "$reset" = "no" ]
|
---|
348 | then
|
---|
349 | echo "YOU CAN'T RESET $column for ${primaries[$s+$s]}!!!"
|
---|
350 | printprocesslog "ERROR you can't reset $column for ${primaries[$s+$s]}"
|
---|
351 | finish
|
---|
352 | fi
|
---|
353 | query=" update $table set $column=$statustime"
|
---|
354 | if ! echo $influences | grep '#' > /dev/null
|
---|
355 | then
|
---|
356 | for influence in $influences
|
---|
357 | do
|
---|
358 | query=$query", $influence=NULL"
|
---|
359 | done
|
---|
360 | fi
|
---|
361 | if ! [ "$starttime" = "noreset" ]
|
---|
362 | then
|
---|
363 | query=$query", fStartTime=$starttime"
|
---|
364 | fi
|
---|
365 | query=$query", fFailedTime=$failedtime, fProgramId=$programid, fReturnCode=$returncode "
|
---|
366 | query=$query" where "
|
---|
367 | # query=$query" where $primary='$primvar'"
|
---|
368 | if [ "$s" = "" ]
|
---|
369 | then
|
---|
370 | s=0
|
---|
371 | fi
|
---|
372 | for (( i=0 ; i < ${#prims[@]} ; i++ ))
|
---|
373 | do
|
---|
374 | if [ $i -lt `expr ${#prims[@]} - 1` ]
|
---|
375 | then
|
---|
376 | query=$query" ${prims[$i]}='${primaries[$s*${#prims[@]}+$i]}' and "
|
---|
377 | else
|
---|
378 | query=$query" ${prims[$i]}='${primaries[$s*${#prims[@]}+$i]}' "
|
---|
379 | fi
|
---|
380 | done
|
---|
381 | echo " QUERY: "$query
|
---|
382 | if ! mysql -s -u $us --password=$pw --host=$ho $db -e " $query "
|
---|
383 | then
|
---|
384 | echo "ERROR could not insert status into db -> exit"
|
---|
385 | printprocesslog "ERROR could not set status in db (program: $program, function setstatus)"
|
---|
386 | finish
|
---|
387 | fi
|
---|
388 |
|
---|
389 | }
|
---|
390 |
|
---|
391 | # function to send a mysql query
|
---|
392 | function sendquery()
|
---|
393 | {
|
---|
394 | getdbsetup
|
---|
395 | if ! val=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
|
---|
396 | then
|
---|
397 | printprocesslog "ERROR could not query db (program: $program, function sendquery)"
|
---|
398 | return 1
|
---|
399 | fi
|
---|
400 | if [ "$val" = "NULL" ]
|
---|
401 | then
|
---|
402 | val=
|
---|
403 | fi
|
---|
404 | echo $val
|
---|
405 | return 0
|
---|
406 | }
|
---|
407 |
|
---|