simple monitoring with xinetd

Whenever you are running servers behind load balancers you need to perform health checks for marking servers up and down. If you are lucky then those health checks are built into the application code itself but more often than not you need to hack something together on top of existing legacy application code. In those instances xinetd and a simple script can do the trick.

A recent example of such a hack was that we needed to monitor pgpool2 instances behind a load balancer. So I hacked together this script with some help and it performed the job admirably

#!/bin/bash
while read line;
do
  if [ $(echo $line | wc -c) -lt "3" ]; then
    break
  fi
done
echo HTTP/1.0 200 OK
echo "Content-Type: text/html; charset=utf-8"
echo "Connection: close"
psqlresponse=$(psql db user -H -h localhost -c "select 1")
response="<html><body>$psqlresponse</body></html>"
length=$(echo $response | wc -c)
echo "Content-Length: $length"
echo
echo $response

The other piece is the actual xinetd service configuration and that’s also pretty simple

service pgpoolcheck
{
  socket_type = stream
  flags = REUSE
  protocol = tcp
  wait = no
  user = root
  port = 9090
  server = /home/dkarapetyan/pgpool_check.sh
  disable = no
}