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 | webpath=/www/htdocs/datacenter
|
---|
40 |
|
---|
41 | datetime=`date +%F-%H-%M-%S`
|
---|
42 |
|
---|
43 |
|
---|
44 | # function to make sure that a directory is made
|
---|
45 | function 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
|
---|
64 | runlogpath=$logpath/run/`date +%Y/%m/%d`
|
---|
65 | processlogpath=$logpath/processlog
|
---|
66 | makedir $runlogpath
|
---|
67 | makedir $processlogpath
|
---|
68 | processlog=$processlogpath/process`date +%F`.log
|
---|
69 |
|
---|
70 | makedir $lockpath
|
---|
71 | makedir $listpath
|
---|
72 |
|
---|
73 | # set checkvalue to ok at the beginning of the scripts
|
---|
74 | check="ok"
|
---|
75 |
|
---|
76 | #failed codes
|
---|
77 | #sequence build status
|
---|
78 | Fbuildsequ=1
|
---|
79 | Fdoexcl=2
|
---|
80 | #run process status
|
---|
81 | Ftimecorr=3
|
---|
82 | Ffillraw=4
|
---|
83 | Fsinope=5
|
---|
84 | Ffillsinope=6
|
---|
85 | Fresetexcl=7
|
---|
86 | #sequence process status
|
---|
87 | Fwritesequfile=8
|
---|
88 | Ffilesavail=9
|
---|
89 | Fnoccfile=10
|
---|
90 | Fnocacofile=11
|
---|
91 | Fmerppcc=12
|
---|
92 | Fmerppcaco=13
|
---|
93 | Fcallisto=14
|
---|
94 | Ffillcalib=15
|
---|
95 | Ffillsignal=16
|
---|
96 | Fstar=17
|
---|
97 | Ffillstar=18
|
---|
98 | #dataset process status
|
---|
99 | Fwritedatasetfile=19
|
---|
100 | Fstardone=20
|
---|
101 | Fganymed=21
|
---|
102 | Ffillganymed=22
|
---|
103 | #mc run process status
|
---|
104 | Fcorsika=23
|
---|
105 | Freflector=24
|
---|
106 | Fcamera=25
|
---|
107 | #again run process status
|
---|
108 | FCompmux=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
|
---|
113 | jmerrorlog=$runlogpath/jobmanager-error`date +%F`.log
|
---|
114 | jmscriptlog=$runlogpath/jobmanager`date +%F`.log
|
---|
115 | steps=$mars/resources/steps.rc
|
---|
116 | # resetting values
|
---|
117 | pno=0
|
---|
118 | totalpno=0
|
---|
119 | running=0
|
---|
120 | queued=0
|
---|
121 | runningscript=0
|
---|
122 | queuedscript=0
|
---|
123 | stillinqueue=0
|
---|
124 |
|
---|
125 |
|
---|
126 | # alias (we cannot check the beginning of the line due to
|
---|
127 | # color codes in filldotraw.C)
|
---|
128 | alias '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 |
|
---|
133 | function printprocesslog
|
---|
134 | {
|
---|
135 | makedir $processlogpath
|
---|
136 | echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "`basename $0`"["$$"] "$@ >> $processlog
|
---|
137 | }
|
---|
138 |
|
---|
139 | # function to exit a script properly
|
---|
140 | function 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
|
---|
152 | function cont()
|
---|
153 | {
|
---|
154 | date
|
---|
155 | echo ""
|
---|
156 | continue
|
---|
157 | }
|
---|
158 |
|
---|
159 | # function to check if a process is already locked
|
---|
160 | function 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 |
|
---|
174 | function resetstatusvalues()
|
---|
175 | {
|
---|
176 | statustime=NULL
|
---|
177 | starttime=NULL
|
---|
178 | returncode=NULL
|
---|
179 | programid=NULL
|
---|
180 | failedtime=NULL
|
---|
181 | }
|
---|
182 |
|
---|
183 | function 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
|
---|
196 | function 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 |
|
---|
230 | function 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 |
|
---|
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=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
|
---|
307 | function 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
|
---|
336 | function 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
|
---|
385 | function 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 |
|
---|