hope I got the merge wright...
[monitor.git] / automate-default.sh
1 #!/bin/bash
2
3 export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
4
5 # NOTE: Must be an absolute path to guarantee it is read.
6 INSTALLPATH=/usr/share/monitor/
7 $INSTALLPATH/shconfig.py >  $INSTALLPATH/monitorconfig.sh
8 source $INSTALLPATH/monitorconfig.sh
9 cd ${MONITOR_SCRIPT_ROOT}
10 set -e
11 DATE=`date +%Y-%m-%d-%T`
12 MONITOR_PID="${MONITOR_SCRIPT_ROOT}/SKIP"
13
14 echo "#######################################"; echo "Running Monitor at $DATE"; echo "######################################"
15 echo "Performing API test"
16 API=$(./testapi.py)
17 if [ "$API" != "ok" ] ; then 
18         # NOTE: Do not try to run any commands if the API is obviously broken.
19         echo "API IS DOWN : "`date`
20         exit 1
21 fi
22
23 if [ -f $MONITOR_PID ] ; then 
24         if [ -z "$1" ] ; then 
25                 echo "KILLING Monitor"
26                 PID=`cat $MONITOR_PID`
27                 rm -f $MONITOR_PID
28                 if [ -z $PID ] ; then
29                         ${MONITOR_SCRIPT_ROOT}/tools/kill.cmd.sh $PID
30                         echo "done."
31                 else
32                         echo "No PID to be killed."
33                 fi
34         else 
35                 # skipping monitor
36                 echo "SKIPPING Monitor"
37                 exit
38         fi 
39 fi
40 echo $$ > $MONITOR_PID
41
42 # SETUP act_all database if it's not there.
43 if [ ! -f ${MONITOR_SCRIPT_ROOT}/actallsetup.flag ]; then
44         if ! python -c 'import database; database.dbLoad("act_all")' 2>/dev/null ; then 
45                 touch ${MONITOR_SCRIPT_ROOT}/actallsetup.flag
46         fi
47 fi
48
49
50 set +e
51 AGENT=`ps ax | grep ssh-agent | grep -v grep`
52 set -e
53 if [ -z "$AGENT" ] ; then
54         echo "starting ssh agent"
55         # if no agent is running, set it up.
56         ssh-agent > ${MONITOR_SCRIPT_ROOT}/agent.sh
57         source ${MONITOR_SCRIPT_ROOT}/agent.sh
58         ssh-add /etc/planetlab/myops_ssh_key.rsa
59         ssh-add /etc/planetlab/debug_ssh_key.rsa
60         ssh-add /etc/planetlab/root_ssh_key.rsa
61 fi
62 #TODO: should add a call to ssh-add -l to check if the keys are loaded or not.
63 source ${MONITOR_SCRIPT_ROOT}/agent.sh
64
65 ${MONITOR_SCRIPT_ROOT}/tools/syncwithplc.py $DATE || :
66 service plc restart monitor
67
68 echo "Performing FindAll Nodes"
69 #########################
70 # 1. FINDBAD NODES 
71 ${MONITOR_SCRIPT_ROOT}/commands/findall.py $DATE || :
72 ps ax | grep BatchMode | grep -v grep | awk '{print $1}' | xargs -r kill || :
73 # clean up stray 'locfg' processes that hang around inappropriately...
74 ps ax | grep locfg | grep -v grep | awk '{print $1}' | xargs -r kill || :
75
76
77 ${MONITOR_SCRIPT_ROOT}/policy.py $DATE || :
78 curl -s 'http://summer.cs.princeton.edu/status/tabulator.cgi?table=table_nodeview&formatcsv' > /var/lib/monitor/comon/$DATE.comon.csv || :
79
80 cp ${MONITOR_SCRIPT_ROOT}/monitor.log ${MONITOR_ARCHIVE_ROOT}/`date +%F-%H:%M`.monitor.log
81 service plc restart monitor || :
82 rm -f $MONITOR_PID