##
#
# Vaisala software source code file
#
# Copyright (c) Vaisala Oyj 2015. All rights reserved.
#
##
check process vaisala-radarsw-webapp matching "/usr/vaisala/radarsw/webapp/lib/webapp.jar"
   group radarsw
   start program = "/sbin/service vaisala-radarsw-webapp start"
   stop  program = "/sbin/service vaisala-radarsw-webapp stop"

   # Application stack health check
   if failed host localhost port 34080
      protocol HTTP request "/health" then restart

   # Static file serving health check
   if failed host localhost port 34080
      protocol HTTP request "/fire/fire.nocache.js" then restart

   if 10 restarts within 10 cycles then timeout

   depends on postgres

check process vaisala-radarsw-geoserver matching "/srv/vaisala/radarsw/geoserver"
   group geoserver
   start program = "/sbin/service vaisala-radarsw-geoserver start"
   stop  program = "/sbin/service vaisala-radarsw-geoserver stop"

   # Check if GeoServer Web UI is accessible
   if failed host localhost port 34180
      protocol HTTP request "/geoserver" then restart

   # Check if GeoServer renders a map image in WGS 84 projection
   if failed host localhost port 34180
      protocol HTTP request "/geoserver/vaisala/wms?LAYERS=vai_base&STYLES=&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&SRS=EPSG%3A4326&BBOX=22.6318359375,36.0791015625,37.1337890625,43.330078125&WIDTH=660&HEIGHT=330" for 3 cycles then restart

   # Test if GWC returns an image in Web Mercator projection
   if failed host localhost port 34180
      protocol HTTP request "/geoserver/gwc/service/wms?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&FORMAT=image%2Fpng&TRANSPARENT=true&LAYERS=vaisala%3Avai_full_en&WIDTH=256&HEIGHT=256&SRS=EPSG%3A3857&STYLES=&BBOX=4696291.0171874985%2C4383204.949375%2C5009377.084999998%2C4696291.017187499" for 3 cycles then restart

   if 10 restarts within 10 cycles then timeout

   # We don't want geoserver to depend on postgres because webapp also depends on postgres. That would mean that
   # the webapp would also be restarted when geoserver is restarted.

check process postgres matching "postgres"
   group postgres
   start program = "/sbin/service postgresql-9.4 start"
   stop  program = "/sbin/service postgresql-9.4 stop"

   if failed unixsocket /var/run/postgresql/.s.PGSQL.5432 protocol pgsql
      then restart

   if failed host 127.0.0.1 port 5432 protocol pgsql then restart

   if 5 restarts within 5 cycles then timeout


check process haproxy with pidfile /var/run/haproxy.pid
   group haproxy
   start program = "/sbin/service haproxy start"
   stop  program = "/sbin/service haproxy stop"

   if failed host localhost port 80
      protocol HTTP request "/" then restart

   if failed port 443 with timeout 15 seconds then restart

   if 5 restarts within 5 cycles then timeout

   depends on vaisala-radarsw-webapp

