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

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