fixed the httpd conf issue with redirecting to /monitor/
[monitor.git] / monitor-server.init
index dc984ad..28a7df8 100644 (file)
@@ -231,6 +231,62 @@ EOF
        fi
 }
 
+function create_httpd_conf ()
+{
+       MONITOR_HTTP_CONF=/etc/httpd/conf.d/monitorweb.conf
+
+       if [ ! -f ${MONITOR_HTTP_CONF} ] ; then
+               # note: we need to either start this script before httpd, or always
+               # restart httpd, since there's no way to know beyond file's existence
+               # whether the values have changed or not.
+               WROTE_HTTP_CONFIG="true"
+       fi
+
+       # TODO: support HTTPS as well as port 80.  currently not specifying port
+       #               80 breaks https for other content on the myplc.
+       # TODO: make proxy port configurable.
+
+       cat <<EOF > ${MONITOR_HTTP_CONF}
+
+# NOTE: I've tried other means of redirection, including mod_rewrite, but did
+#      not have any success.  The means below is not idea, b/c it does not keep
+#      non-ssl session as non-ssl.  But it works.
+
+# NOTE: redirect path without trailing '/' to path with.  Favor SSL.
+Redirect /monitor https://${MONITOR_HOSTNAME}:${PLC_WWW_SSL_PORT}/monitor/
+
+# NOTE: this directive strips '/monitor/' from the requested path and pastes
+#       the remaining part to the end of the ProxyPass url below.  All TG urls
+#       should be relative to their current position, or the absolute path
+#       that includes /monitor/ at the beginning.  
+# TODO: make location configurable.
+<Location '/monitor/'>
+    #LogLevel debug
+    #Errorlog /var/log/httpd/monitorwebapp-error_log
+    #Customlog /var/log/httpd/monitorwebapp-access_log common
+
+    ProxyPass http://127.0.0.1:8080/
+    ProxyPassReverse http://127.0.0.1:8080/
+</Location>
+
+EOF
+}
+
+function start_tg_server ()
+{
+       pushd ${MONITORPATH}/web/MonitorWeb/
+       ./start-monitorweb.py ${MONITORPATH}/web/MonitorWeb/prod.cfg &> /var/log/monitorweb.log
+       popd
+}
+
+function stop_tg_server ()
+{
+        pid=$( cat /var/run/monitorweb.pid )
+        if [ -n "$pid" ] ; then
+                kill $pid
+        fi
+}
+
 function check_zab_server ()
 {
        ZABBIXCFG=/etc/zabbix
@@ -241,7 +297,7 @@ function check_zab_server ()
                    -e "s#DBName=.*#DBName=$ZABBIX_DB_NAME#g" \
                    -e "s#DBUser=.*#DBUser=$ZABBIX_DB_USER#g" \
                    -e "s#DBPassword=.*#DBPassword=$PLC_MONITOR_DBPASSWORD#g" \
-                   -e "s/.*ExternalScripts=.*/ExternalScripts=${MONITORPATH}\/zabbix/g" \
+                   -e "s#.*ExternalScripts=.*#ExternalScripts=${MONITORPATH}/zabbix#g" \
                    ${ZABBIXCFG}/zabbix_server.conf > $TMP_FILE
                cat $TMP_FILE > ${ZABBIXCFG}/zabbix_server.conf
        fi
@@ -308,6 +364,7 @@ case "$1" in
                if [ -n "$WROTE_PG_CONFIG" ] ; then
                        # NOTE: restart db to enable access by users granted above.
                        service plc restart postgresql
+                       service plc restart httpd
                fi
 
                check_zabbix_schema_and_data
@@ -316,6 +373,14 @@ case "$1" in
                # WRITE default /etc/monitor.conf
                check_monitor_conf
 
+               # create /etc/httpd/conf.d/monitorweb.conf
+               create_httpd_conf
+               if [ -n "$WROTE_HTTP_CONFIG" ] ; then
+                       # NOTE: restart web server to enable access web cfg
+                       service plc restart httpd
+               fi
+               start_tg_server
+
                # START zabbix services.  SETUP default config files.
                check_zab_server
                check_zab_agentd
@@ -344,6 +409,7 @@ case "$1" in
                MESSAGE=$"Stopping Monitor"
                dialog "$MESSAGE"
 
+               stop_tg_server
                service zabbix_server stop
                service zabbix_agentd stop
                # TODO: is there anything to stop?