merge from:
[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="$HOME/monitor/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 AGENT=`ps ax | grep ssh-agent | grep -v grep`
45 if [ -z "$AGENT" ] ; then
46         echo "starting ssh agent"
47         # if no agent is running, set it up.
48         ssh-agent > ${MONITOR_SCRIPT_ROOT}/agent.sh
49         source ${MONITOR_SCRIPT_ROOT}/agent.sh
50         ssh-add /etc/planetlab/debug_ssh_key.rsa
51         ssh-add /etc/planetlab/root_ssh_key.rsa
52 fi
53 #TODO: should add a call to ssh-add -l to check if the keys are loaded or not.
54 source ${MONITOR_SCRIPT_ROOT}/agent.sh
55
56
57 echo "Performing Findbad Nodes"
58 #########################
59 # 1. FINDBAD NODES 
60 rm -f ${MONITOR_DATA_ROOT}/production.findbad2.pkl
61 ${MONITOR_SCRIPT_ROOT}/findbad.py --increment --cachenodes --debug=0 --dbname="findbad2" $DATE || :
62 cp ${MONITOR_DATA_ROOT}/production.findbad2.pkl ${MONITOR_DATA_ROOT}/production.findbad.pkl
63 ps ax | grep BatchMode | grep -v grep | awk '{print $1}' | xargs kill || :
64
65 echo "Performing Findbad PCUs"
66 #########################
67 # 2. FINDBAD PCUS
68 rm -f ${MONITOR_DATA_ROOT}/production.findbadpcus2.pkl
69 ${MONITOR_SCRIPT_ROOT}/findbadpcu.py --increment --refresh --debug=0 --dbname=findbadpcus2 $DATE || :
70 cp ${MONITOR_DATA_ROOT}/production.findbadpcus2.pkl ${MONITOR_DATA_ROOT}/production.findbadpcus.pkl
71 # clean up stray 'locfg' processes that hang around inappropriately...
72 ps ax | grep locfg | grep -v grep | awk '{print $1}' | xargs kill || :
73
74 echo "Generating web data"
75 # badcsv.txt
76 ${MONITOR_SCRIPT_ROOT}/printbadcsv.py  | grep -v loading | tr -d ' ' > badcsv.txt
77 cp badcsv.txt /plc/data/var/www/html/monitor/
78 ${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
79
80 echo "Performing uptime changes for sites, nodes, and pcus"
81 ########################
82 # 3. record last-changed for sites, nodes and pcus.
83 ${MONITOR_SCRIPT_ROOT}/sitebad.py --increment || :
84 ${MONITOR_SCRIPT_ROOT}/nodebad.py --increment || :
85 ${MONITOR_SCRIPT_ROOT}/pcubad.py --increment || :
86
87 echo "Converting pkl files to phpserial"
88 #########################
89 # 4. convert pkl to php serialize format.
90 ${MONITOR_SCRIPT_ROOT}/pkl2php.py -i findbadpcus2 -o findbadpcus
91 ${MONITOR_SCRIPT_ROOT}/pkl2php.py -i act_all -o act_all
92 ${MONITOR_SCRIPT_ROOT}/pkl2php.py -i plcdb_hn2lb -o plcdb_hn2lb
93 ${MONITOR_SCRIPT_ROOT}/pkl2php.py -i findbad -o findbadnodes
94 ${MONITOR_SCRIPT_ROOT}/pkl2php.py -i ad_dbTickets -o ad_dbTickets
95 ${MONITOR_SCRIPT_ROOT}/pkl2php.py -i idTickets -o idTickets
96
97 echo "Archiving pkl files"
98 #########################
99 # Archive pkl files.
100 for f in findbad act_all findbadpcus l_plcnodes site_persistflags node_persistflags pcu_persistflags ; do 
101         cp ${MONITOR_DATA_ROOT}/production.$f.pkl ${MONITOR_ARCHIVE_ROOT}/`date +%F-%H:%M`.production.$f.pkl
102 done
103
104 echo "Running grouprins on all dbg nodes"
105 ############################
106 # 5. Check if there are any nodes in dbg state.  Clean up afterward.
107 ${MONITOR_SCRIPT_ROOT}/grouprins.py --mail=1 \
108         --nodeselect 'state=DEBUG&&boot_state=(rins|dbg|boot)' \
109         --stopselect 'state=BOOT&&kernel=2.6.22.19-vs2.3.0.34.9.planetlab' \
110         --reboot || :
111 ${MONITOR_SCRIPT_ROOT}/findbad.py --increment --cachenodes --debug=0 --dbname="findbad" --nodeselect 'state=DEBUG&&boot_state=dbg||state=DEBUG&&boot_state=boot' || :
112
113 echo "Collecting RT database dump"
114 ##########################
115 # 6. cache the RT db locally.
116 python ${MONITOR_SCRIPT_ROOT}/rt.py
117
118 rm -f $MONITOR_PID