HAPROXY CFG TO CSV

De Wiki Clusterlab.com.br
Ir para navegação Ir para pesquisar

HOW TO

$ ./converter.sh haproxy.cfg

CODE

#!/bin/bash

function AJUDA() {
    echo "
    How to use
    $0 <haproxy.cfg>

    HAPROXY reserverd words:
    acl
    backend
    balance
    bind
    chroot
    daemon
    default_backend
    defaults
    frontend
    global
    group
    http-check
    http-request
    listen
    log
    maxconn
    mode
    nbproc
    option
    pidfile
    retries
    server
    stats
    timeout
    use_backend
    user
    "
}
function PARSER() {
    export TAG=$1
    case $1 in
        frontend)
            export CFG_FRONTEND=$2
            ;;
        bind)
            export CFG_FRONTEND_BIND=$(echo $2 | awk -F : '{ print $2} ')
            ;;
        default_backend)
            export CFG_FRONTEND_DEFAULT_BACK=$2
            ;;
        backend)
            export CFG_BACKEND=$2
            ;;
        server)
            if [ "$CFG_BACKEND_SERVER" == "0" ]
            then
                export CFG_BACKEND_SERVER="$2 $3"
            else
                export CFG_BACKEND_SERVER=$CFG_BACKEND_SERVER,"$2 $3"
            fi
            ;;
    esac
}
function CHECK_BEGIN() {
    if [ "$CFG_FRONTEND" != "0" ] && [[ "$1" == "frontend" ]]
    then
        echo 1
    else
        echo 0
    fi
}
function ZERO_ALL(){
    export CFG_FRONTEND=0
    export CFG_FRONTEND_BIND=0
    export CFG_FRONTEND_DEFAULT_BACK=0
    export CFG_BACKEND=0
    export CFG_BACKEND_SERVER=0
}
export CFG_FRONTEND=0
export CFG_FRONTEND_BIND=0
export CFG_FRONTEND_DEFAULT_BACK=0
export CFG_BACKEND=0
export CFG_BACKEND_SERVER=0
export END=0
if [ $# -ne 1 ]
then
    AJUDA
else
    echo "FRONTEND;PORT;BACKENDNAME;BACKEND;SERVERS"
    cat $1 | \
        sed -e "s/^ *//g"| \
        egrep -v "^#|^$" | \
        while read -r line
        do
            if [ $(CHECK_BEGIN $line) -eq 0 ]
            then
                PARSER $line
            else
                echo $CFG_FRONTEND\;$CFG_FRONTEND_BIND\;$CFG_FRONTEND_DEFAULT_BACK\;$CFG_BACKEND\;$CFG_BACKEND_SERVER
                ZERO_ALL
                PARSER $line
            fi
        done
fi