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 "${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
|
---|
303 | function 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
|
---|
330 | function 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 | done
|
---|
371 | echo " QUERY: "$query
|
---|
372 | if ! mysql -s -u $us --password=$pw --host=$ho $db -e " $query "
|
---|
373 | then
|
---|
374 | echo "ERROR could not insert status into db -> exit"
|
---|
375 | printprocesslog "ERROR could not set status in db (program: $program, function setstatus)"
|
---|
376 | finish
|
---|
377 | fi
|
---|
378 |
|
---|
379 | }
|
---|
380 |
|
---|
381 | # function to send a mysql query
|
---|
382 | function sendquery()
|
---|
383 | {
|
---|
384 | getdbsetup
|
---|
385 | if ! val=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
|
---|
386 | then
|
---|
387 | printprocesslog "ERROR could not query db (program: $program, function sendquery)"
|
---|
388 | return 1
|
---|
389 | fi
|
---|
390 | if [ "$val" = "NULL" ]
|
---|
391 | then
|
---|
392 | val=
|
---|
393 | fi
|
---|
394 | echo $val
|
---|
395 | return 0
|
---|
396 | }
|
---|
397 |
|
---|