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 | # resetting values
|
---|
115 | pno=0
|
---|
116 | totalpno=0
|
---|
117 | running=0
|
---|
118 | queued=0
|
---|
119 | runningscript=0
|
---|
120 | queuedscript=0
|
---|
121 | stillinqueue=0
|
---|
122 |
|
---|
123 |
|
---|
124 | # alias (we cannot check the beginning of the line due to
|
---|
125 | # color codes in filldotraw.C)
|
---|
126 | alias '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 |
|
---|
131 | function printprocesslog
|
---|
132 | {
|
---|
133 | makedir $processlogpath
|
---|
134 | echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "`basename $0`"["$$"] "$@ >> $processlog
|
---|
135 | }
|
---|
136 |
|
---|
137 | # function to exit a script properly
|
---|
138 | function 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
|
---|
150 | function cont()
|
---|
151 | {
|
---|
152 | date
|
---|
153 | echo ""
|
---|
154 | continue
|
---|
155 | }
|
---|
156 |
|
---|
157 | # function to check if a process is already locked
|
---|
158 | function 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 |
|
---|
172 | function resetstatusvalues()
|
---|
173 | {
|
---|
174 | statustime=NULL
|
---|
175 | starttime=NULL
|
---|
176 | returncode=NULL
|
---|
177 | programid=NULL
|
---|
178 | failedtime=NULL
|
---|
179 | }
|
---|
180 |
|
---|
181 | function 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
|
---|
194 | function 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 |
|
---|
228 | function 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 | ho=`grep URL $mars/sql.rc | grep -v '#' | sed -e 's/ //g' -e 's/URL:mysql:\/\///'`
|
---|
234 | # echo "setup: "
|
---|
235 | # echo " db: "$db
|
---|
236 | # echo " pw: "$pw
|
---|
237 | # echo " us: "$us
|
---|
238 | # echo " ho: "$ho
|
---|
239 | }
|
---|
240 |
|
---|
241 | function 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
|
---|
255 | function 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=$ho $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
|
---|
307 | function getstatus()
|
---|
308 | {
|
---|
309 | numproc=
|
---|
310 | getdbsetup
|
---|
311 | getstepinfo
|
---|
312 | # get query
|
---|
313 | query=" select count(*) from $table where "
|
---|
314 | if ! echo $needs | grep '#' > /dev/null
|
---|
315 | then
|
---|
316 | for need in $needs
|
---|
317 | do
|
---|
318 | query=$query" not isnull($need) and"
|
---|
319 | done
|
---|
320 | fi
|
---|
321 | query=$query" isnull($column) "
|
---|
322 | query=$query" and isnull(fStartTime) and isnull(fFailedTime) and isnull(fProgramId) and isnull(fReturnCode) "
|
---|
323 | query=$query" group by $column "
|
---|
324 | # echo " QUERY: "$query
|
---|
325 | if ! numproc=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
|
---|
326 | then
|
---|
327 | echo "ERROR could not query number of todo proceses from db -> continue"
|
---|
328 | printprocesslog "ERROR could not query number of processes from db (program: $program, function getstatus)"
|
---|
329 | cont
|
---|
330 | fi
|
---|
331 | }
|
---|
332 |
|
---|
333 | # function to set status of a process in the db
|
---|
334 | function setstatus()
|
---|
335 | {
|
---|
336 | if [ "$column" = "no" ]
|
---|
337 | then
|
---|
338 | return
|
---|
339 | fi
|
---|
340 | resetstatusvalues
|
---|
341 | evalstatus $@
|
---|
342 | getdbsetup
|
---|
343 | getstepinfo
|
---|
344 | # get query
|
---|
345 | reset=`grep "$coltab[.]Reset:" $steps | sed -e "s/$coltab[.]Reset://" -e 's/ //g'`
|
---|
346 | if [ "$reset" = "no" ]
|
---|
347 | then
|
---|
348 | echo "YOU CAN'T RESET $column for ${primaries[$s+$s]}!!!"
|
---|
349 | printprocesslog "ERROR you can't reset $column for ${primaries[$s+$s]}"
|
---|
350 | finish
|
---|
351 | fi
|
---|
352 | query=" update $table set $column=$statustime"
|
---|
353 | if ! echo $influences | grep '#' > /dev/null
|
---|
354 | then
|
---|
355 | for influence in $influences
|
---|
356 | do
|
---|
357 | query=$query", $influence=NULL"
|
---|
358 | done
|
---|
359 | fi
|
---|
360 | if ! [ "$starttime" = "noreset" ]
|
---|
361 | then
|
---|
362 | query=$query", fStartTime=$starttime"
|
---|
363 | fi
|
---|
364 | query=$query", fFailedTime=$failedtime, fProgramId=$programid, fReturnCode=$returncode "
|
---|
365 | query=$query" where "
|
---|
366 | # query=$query" where $primary='$primvar'"
|
---|
367 | if [ "$s" = "" ]
|
---|
368 | then
|
---|
369 | s=0
|
---|
370 | fi
|
---|
371 | for (( i=0 ; i < ${#prims[@]} ; i++ ))
|
---|
372 | do
|
---|
373 | if [ $i -lt `expr ${#prims[@]} - 1` ]
|
---|
374 | then
|
---|
375 | query=$query" ${prims[$i]}='${primaries[$s*${#prims[@]}+$i]}' and "
|
---|
376 | else
|
---|
377 | query=$query" ${prims[$i]}='${primaries[$s*${#prims[@]}+$i]}' "
|
---|
378 | fi
|
---|
379 | done
|
---|
380 | echo " QUERY: "$query
|
---|
381 | if ! mysql -s -u $us --password=$pw --host=$ho $db -e " $query "
|
---|
382 | then
|
---|
383 | echo "ERROR could not insert status into db -> exit"
|
---|
384 | printprocesslog "ERROR could not set status in db (program: $program, function setstatus)"
|
---|
385 | finish
|
---|
386 | fi
|
---|
387 |
|
---|
388 | }
|
---|
389 |
|
---|
390 | # function to send a mysql query
|
---|
391 | function sendquery()
|
---|
392 | {
|
---|
393 | getdbsetup
|
---|
394 | if ! val=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
|
---|
395 | then
|
---|
396 | printprocesslog "ERROR could not query db (program: $program, function sendquery)"
|
---|
397 | return 1
|
---|
398 | fi
|
---|
399 | if [ "$val" = "NULL" ]
|
---|
400 | then
|
---|
401 | val=
|
---|
402 | fi
|
---|
403 | echo $val
|
---|
404 | return 0
|
---|
405 | }
|
---|
406 |
|
---|