updates to improve generalization and auto-installation.
[monitor.git] / automate-default.sh
1 #!/bin/bash
2
3 # NOTE: Must be an absolute path to guarantee it is read.
4 INSTALLPATH=/usr/share/monitor-server/
5 # Generate an 'sh' style file full of variables in monitor.conf
6 $INSTALLPATH/shconfig.py >  $INSTALLPATH/monitorconfig.sh
7 source $INSTALLPATH/monitorconfig.sh
8 cd ${MONITOR_SCRIPT_ROOT}
9 set -e
10 DATE=`date +%Y-%m-%d-%T`
11 MONITOR_PID="${MONITOR_SCRIPT_ROOT}/SKIP"
12
13 echo "Performing API test"
14 API=$(./testapi.py)
15 if [ "$API" != "ok" ] ; then 
16         # NOTE: Do not try to run any commands if the API is obviously broken.
17         echo "API IS DOWN : "`date`
18         exit 1
19 fi
20
21 if [ -f $MONITOR_PID ] ; then 
22         if [ -z "$1" ] ; then 
23                 echo "KILLING Monitor"
24                 PID=`cat $MONITOR_PID`
25                 rm -f $MONITOR_PID
26                 ${MONITOR_SCRIPT_ROOT}/kill.cmd.sh $PID
27         else 
28                 # skipping monitor
29                 echo "SKIPPING Monitor"
30                 exit
31         fi 
32 fi
33 echo $$ > $MONITOR_PID
34
35 # SETUP act_all database if it's not there.
36 if [ ! -f ${MONITOR_SCRIPT_ROOT}/actallsetup.flag ]; then
37         if ! python -c 'import database; database.dbLoad("act_all")' 2>/dev/null ; then 
38                 python -c 'import database; database.dbDump("act_all", {})' 2>/dev/null ; then 
39                 touch ${MONITOR_SCRIPT_ROOT}/actallsetup.flag
40         fi
41 fi
42
43
44 set +e
45 AGENT=`ps ax | grep ssh-agent | grep -v grep`
46 set -e
47 if [ -z "$AGENT" ] ; then
48         echo "starting ssh agent"
49         # if no agent is running, set it up.
50         ssh-agent > ${MONITOR_SCRIPT_ROOT}/agent.sh
51         source ${MONITOR_SCRIPT_ROOT}/agent.sh
52         ssh-add /etc/planetlab/debug_ssh_key.rsa
53         ssh-add /etc/planetlab/root_ssh_key.rsa
54 fi
55 #TODO: should add a call to ssh-add -l to check if the keys are loaded or not.
56 source ${MONITOR_SCRIPT_ROOT}/agent.sh
57
58
59 echo "Performing Findbad Nodes"
60 #########################
61 # 1. FINDBAD NODES 
62 rm -f ${MONITOR_DATA_ROOT}/production.findbad2.pkl
63 ${MONITOR_SCRIPT_ROOT}/findbad.py --increment --cachenodes --debug=0 --dbname="findbad2" $DATE || :
64 cp ${MONITOR_DATA_ROOT}/production.findbad2.pkl ${MONITOR_DATA_ROOT}/production.findbad.pkl
65 ps ax | grep BatchMode | grep -v grep | awk '{print $1}' | xargs kill || :
66
67 echo "Performing Findbad PCUs"
68 #########################
69 # 2. FINDBAD PCUS
70 rm -f ${MONITOR_DATA_ROOT}/production.findbadpcus2.pkl
71 ${MONITOR_SCRIPT_ROOT}/findbadpcu.py --increment --refresh --debug=0 --dbname=findbadpcus2 $DATE || :
72 cp ${MONITOR_DATA_ROOT}/production.findbadpcus2.pkl ${MONITOR_DATA_ROOT}/production.findbadpcus.pkl
73 # clean up stray 'locfg' processes that hang around inappropriately...
74 ps ax | grep locfg | grep -v grep | awk '{print $1}' | xargs kill || :
75
76 #echo "Generating web data"
77 # badcsv.txt
78 #${MONITOR_SCRIPT_ROOT}/printbadcsv.py  | grep -v loading | tr -d ' ' > badcsv.txt
79 #cp badcsv.txt /plc/data/var/www/html/monitor/
80 #${MONITOR_SCRIPT_ROOT}/showlatlon.py | head -9 | awk 'BEGIN {print "<table>"} { print "<tr><td>", $0, "</td></tr>"} END{print "</table>"}'  | sed -e 's\|\</td><td>\g' > /plc/data/var/www/html/monitor/regions.html
81
82 echo "Performing uptime changes for sites, nodes, and pcus"
83 ########################
84 # 3. record last-changed for sites, nodes and pcus.
85 ${MONITOR_SCRIPT_ROOT}/sitebad.py --increment || :
86 ${MONITOR_SCRIPT_ROOT}/nodebad.py --increment || :
87 ${MONITOR_SCRIPT_ROOT}/pcubad.py --increment || :
88
89 echo "Converting pkl files to phpserial"
90 #########################
91 # 4. convert pkl to php serialize format.
92 ${MONITOR_SCRIPT_ROOT}/pkl2php.py -i findbadpcus2 -o findbadpcus
93 ${MONITOR_SCRIPT_ROOT}/pkl2php.py -i act_all -o act_all
94 ${MONITOR_SCRIPT_ROOT}/pkl2php.py -i plcdb_hn2lb -o plcdb_hn2lb
95 ${MONITOR_SCRIPT_ROOT}/pkl2php.py -i findbad -o findbadnodes
96 #${MONITOR_SCRIPT_ROOT}/pkl2php.py -i ad_dbTickets -o ad_dbTickets
97 #${MONITOR_SCRIPT_ROOT}/pkl2php.py -i idTickets -o idTickets
98
99 echo "Archiving pkl files"
100 #########################
101 # Archive pkl files.
102 for f in findbad act_all findbadpcus l_plcnodes site_persistflags node_persistflags pcu_persistflags ; do 
103         cp ${MONITOR_DATA_ROOT}/production.$f.pkl ${MONITOR_ARCHIVE_ROOT}/`date +%F-%H:%M`.production.$f.pkl
104 done
105
106 echo "Running grouprins on all dbg nodes"
107 ############################
108 # 5. Check if there are any nodes in dbg state.  Clean up afterward.
109 ${MONITOR_SCRIPT_ROOT}/grouprins.py --mail=1 --reboot --nodeselect 'state=DOWN&&boot_state=(boot|rins|dbg|diag)' --stopselect "state=BOOT" || :
110 ${MONITOR_SCRIPT_ROOT}/grouprins.py --mail=1 --reboot --nodeselect 'state=DEBUG&&boot_state=(rins|dbg|boot)' --stopselect 'state=BOOT' || :
111
112 cp ${MONITOR_SCRIPT_ROOT}/monitor.log ${MONITOR_ARCHIVE_ROOT}/`date +%F-%H:%M`.monitor.log
113 rm -f $MONITOR_PID