github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/acceptancetests/repository/trusty/haproxy/files/metrics/haproxy_to_statsd.sh (about)

     1  #!/bin/bash
     2  # haproxy-to-stasd: query haproxy CSV status page, transform it
     3  # to stdout suitable for statsd
     4  #
     5  # Author: JuanJo Ciarlante <jjo@canonical.com>
     6  # Copyright 2012, Canonical Ltd.
     7  # License: GPLv3
     8  set -u
     9  PREFIX=${1:?missing statsd node prefix, e.g.: production.host.${HOSTNAME}.haproxy.stats}
    10  PERIOD=${2:?missing period, e.g.: 10min}
    11  HOSTPORT=${3:?missing haproxy hostport, e.g.: localhost:10000}
    12  HTTPAUTH=${4:?missing httpauth, e.g.: user:pass}
    13  
    14  TSTAMP="$(date +%s)"
    15  
    16  # Filter only numeric metrics, cleanup to be only <var> <value>
    17  get_metrics() {
    18      curl -s "http://${HTTPAUTH}@${HOSTPORT}/;csv;norefresh"| \
    19          awk -v FS=, '/^#/{ for(i=1;i<NF;i++) fieldname[i]=$i;}
    20  /^[^#]/{ for(i=3;i<NF;i++) if(length($i) > 0) printf("%s.%s.%s %d\n", $1, $2, fieldname[i], $i);}'
    21  }
    22  
    23  # Add statsd-isms to <var> <value> lines
    24  statsdify() {
    25      sed -r "s/([^ ]+) (.*)/${PREFIX}.\1.${PERIOD}:\2|g/"
    26  }
    27  get_metrics | statsdify