0

My Question is : How Can I Setup a http service with xinetd

Shell Script (pgsqlchk) is as follows:

#!/bin/bash
# This script checks if a postgres server is healthy running on localhost. It will return:
# "HTTP/1.x 200 OK\r" (if postgres is running smoothly)
# - OR -
# "HTTP/1.x 500 Internal Server Error\r" (else)
# The purpose of this script is make haproxy capable of monitoring postgres properly
# It is recommended that a low-privileged postgres  user is created to be used by this script.
# For eg. create  user healthchkusr login password 'hc321';


PGSQL_HOST="localhost"
PGSQL_PORT="5432"
PGSQL_DATABASE="postgres"
PGSQL_USERNAME="postgres_user"
export PGPASSWORD="postgres_password"
TMP_FILE="/tmp/pgsqlchk.out"
ERR_FILE="/tmp/pgsqlchk.err"

return_ok()
{
    echo -ne "HTTP/1.0 200 OK\r\n"
    echo -ne "Content-Type: text/html\r\n"
    echo -ne "Content-Length: 47\r\n"
    echo -ne "\r\n"
    echo -ne "<html><body>My DB is running with read write mode.</body></html>\r\n"
    echo -ne "\r\n"
    exit 0
}

return_fail()
{
    echo -ne "HTTP/1.0 503 Service Unavailable\r\n"
    echo -ne "Content-Type: text/html\r\n"
    echo -ne "Content-Length: 46\r\n"
    echo -ne "\r\n"
    echo -ne "<html><body>My DB is running with read mode.</body></html>\r\n"
    echo -ne "\r\n"
    exit 1
}


# We perform a simple query that should return a few results


VALUE=`/usr/lib/postgresql/14/bin/psql -t -h localhost -U postgres -p 5432 -c "select pg_is_in_recovery()" 2> /dev/null`
# Check the output. If it is not empty then everything is fine and we return something. Else, we just do not return anything.

echo "$VALUE"
if [ "$VALUE" == " t" ]
then
        return_fail

elif [ "$VALUE" == " f" ]
then
        return_ok
else
        return_fail
fi

Below is my xinetd service file

service pgsqlchk
{
        flags           = REUSE
        socket_type     = stream
        port            = 23267
        wait            = no
        user            = nobody
        server          = /opt/pgsqlchk
        log_on_failure  += USERID
        disable         = no
        only_from       = 0.0.0.0/0
        per_source      = UNLIMITED
}

And I am getting below output

curl --verbose http://localhost:23267
*   Trying 127.0.0.1:23267...
* Connected to localhost (127.0.0.1) port 23267 (#0)
> GET / HTTP/1.1
> Host: localhost:23267
> User-Agent: curl/7.81.0
> Accept: */*
>
* Received HTTP/0.9 when not allowed
* Closing connection 0
curl: (1) Received HTTP/0.9 when not allowed

I am getting curl: (1) Received HTTP/0.9 when not allowed, and hence HA PROXY is not able to send requests to this Server

My Question is : How Can I Setup a http service with xinetd

vaibhav singhal
  • 883
  • 8
  • 9

0 Answers0