File: stats_haproxy.sh

package info (click to toggle)
haproxy 1.7.5-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 8,364 kB
  • ctags: 8,494
  • sloc: ansic: 92,976; xml: 1,754; sh: 1,227; python: 1,005; makefile: 831; perl: 550
file content (78 lines) | stat: -rw-r--r-- 1,713 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/bash

## contrib by prizee.com

socket='/var/run/haproxy.stat'

if ! type socat >/dev/null 2>&1 ; then
    echo "can't find socat in PATH" 1>&2
    exit 1
fi

printUsage ()
{
    echo -e "Usage : $(basename $0) [options] -s section
--section -s section\t: section to use ( --list format)
Options :
--socket -S [socket]\t: socket to use (default: /var/run/haproxy.stat)
--list -l\t\t: print available sections
--help -h\t\t: print this  message"
}

getRawStat ()
{
    if [ ! -S $socket ] ; then
	echo "$socket socket unavailable" 1>&2
	exit 1
    fi

    if ! printf "show stat\n" | socat unix-connect:${socket} stdio | grep -v "^#" ; then
	echo "cannot read $socket" 1>&2
	exit 1
    fi
}

getStat ()
{
    stats=$(getRawStat | grep $1 | awk -F "," '{print $5" "$8}')
    export cumul=$(echo $stats | cut -d " " -f2)
    export current=$(echo $stats | cut -d " " -f1)
}

showList ()
{
    getRawStat | awk -F "," '{print $1","$2}'
}

set -- `getopt -u -l socket:,section:,list,help -- s:S:lh "$@"`

while true ; do
    case $1 in
	--socket|-S) socket=$2 ; shift 2 ;;
	--section|-s) section=$2 ; shift 2 ;;
	--help|-h) printUsage ; exit 0 ;;
	--list|-l) showList ; exit 0 ;;
	--) break ;;
    esac
done

if [ "$section" = "" ] ; then
    echo "section not specified, run '$(basename $0) --list' to know available sections" 1>&2
    printUsage
    exit 1
fi

cpt=0
totalrate=0
while true ; do
    getStat $section
    if [ "$cpt" -gt "0" ] ; then
	sessionrate=$(($cumul-$oldcumul))
	totalrate=$(($totalrate+$sessionrate))
	averagerate=$(($totalrate/$cpt))
	printf "$sessionrate sessions/s (avg: $averagerate )\t$current concurrent sessions\n"
    fi
    oldcumul=$cumul
    sleep 1
    cpt=$(($cpt+1))
done