Added commands for interacting with google's spreadsheets:
[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/commands/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 function send_mail ()
15 {
16     subject=$1
17     body=$2
18     mail -s "$subject" $exception_email <<EOF
19 $body
20 EOF
21 }
22
23
24 echo "#######################################"; echo "Running Monitor at $DATE"; echo "######################################"
25 echo "Performing API test"
26 API=$(${MONITOR_SCRIPT_ROOT}/tools/testapi.py)
27 if [ "$API" != "ok" ] ; then 
28         # NOTE: Do not try to run any commands if the API is obviously broken.
29         echo "API IS DOWN : "`date`
30     send_mail "API IS DOWN: canceled monitor run for `date`" "have a nice day..."
31         exit 1
32 fi
33
34 if [ -f $MONITOR_PID ] ; then 
35         if [ -z "$1" ] ; then 
36                 echo "KILLING Monitor"
37                 PID=`cat $MONITOR_PID`
38                 rm -f $MONITOR_PID
39                 if [ -z $PID ] ; then
40                         ${MONITOR_SCRIPT_ROOT}/tools/kill.cmd.sh $PID
41                         echo "done."
42                 else
43                         echo "No PID to be killed."
44                 fi
45         else 
46                 # skipping monitor
47                 echo "SKIPPING Monitor"
48                 exit
49         fi 
50 fi
51 echo $$ > $MONITOR_PID
52
53 # SETUP act_all database if it's not there.
54 if [ ! -f ${MONITOR_SCRIPT_ROOT}/actallsetup.flag ]; then
55         if ! python -c 'import database; database.dbLoad("act_all")' 2>/dev/null ; then 
56                 touch ${MONITOR_SCRIPT_ROOT}/actallsetup.flag
57         fi
58 fi
59
60
61 set +e
62 AGENT=`ps ax | grep ssh-agent | grep -v grep`
63 set -e
64 if [ -z "$AGENT" ] ; then
65         echo "starting ssh agent"
66         # if no agent is running, set it up.
67         ssh-agent > ${MONITOR_SCRIPT_ROOT}/agent.sh
68         source ${MONITOR_SCRIPT_ROOT}/agent.sh
69         ssh-add /etc/planetlab/myops_ssh_key.rsa
70         ssh-add /etc/planetlab/debug_ssh_key.rsa
71         ssh-add /etc/planetlab/root_ssh_key.rsa
72 fi
73 #TODO: should add a call to ssh-add -l to check if the keys are loaded or not.
74 source ${MONITOR_SCRIPT_ROOT}/agent.sh
75
76 # CHECK AGENT IS UP AND RUNNING
77 count=$( ssh-add -l | wc -l ) 
78 if [ $count -lt 3 ] ; then
79     send_mail "ssh-agent is not up and running." "Add keys before monitoring can continue"
80         exit
81 fi
82
83 ${MONITOR_SCRIPT_ROOT}/commands/syncwithplc.py $DATE || :
84 service plc restart monitor
85
86 echo "Performing FindAll Nodes"
87 #########################
88 # 1. FINDBAD NODES 
89 ${MONITOR_SCRIPT_ROOT}/commands/findall.py $DATE || :
90 ps ax | grep BatchMode | grep -v grep | awk '{print $1}' | xargs -r kill || :
91 # clean up stray 'locfg' processes that hang around inappropriately...
92 ps ax | grep locfg | grep -v grep | awk '{print $1}' | xargs -r kill || :
93
94
95 ${MONITOR_SCRIPT_ROOT}/commands/repair.py $DATE || :
96 ${MONITOR_SCRIPT_ROOT}/commands/policy.py $DATE || :
97 curl -s 'http://summer.cs.princeton.edu/status/tabulator.cgi?table=table_nodeview&formatcsv' > /var/lib/monitor/comon/$DATE.comon.csv || :
98
99 #${MONITOR_SCRIPT_ROOT}/statistics/add-google-record.py --email email  --password password --database MonitorStats --sheet NodeHistory `${MONITOR_SCRIPT_ROOT}/statistics/get-records.py nodes`
100 #${MONITOR_SCRIPT_ROOT}/statistics/add-google-record.py --email email --password password --database MonitorStats --sheet SiteHistory `${MONITOR_SCRIPT_ROOT}/statistics/get-records.py sites`
101
102 cp ${MONITOR_SCRIPT_ROOT}/monitor.log ${MONITOR_ARCHIVE_ROOT}/`date +%F-%H:%M`.monitor.log
103 service plc restart monitor || :
104 rm -f $MONITOR_PID
105
106 D=`date +%F-%H:%M`
107
108 # NOTE: check log for major sections.
109 wc=`grep -E "^(findbad|findbadpcu|nodebad|pcubad|sitebad|apply-policy)$" ${MONITOR_SCRIPT_ROOT}/monitor.log | wc -l`
110 if [[ $wc -ge 6 ]] ; then 
111     send_mail "A:finished monitor run for $SD at $D" "Thank you..."
112 else
113     send_mail "ERROR finished monitor run for $SD at $D" "Missing some sections:
114         $(grep -E "findbad|findbadpcu|nodebad|pcubad|sitebad|apply-policy" ${MONITOR_SCRIPT_ROOT}/monitor.log)"
115 fi
116