blob: 25f23d320dce0dfb2d51981adc05fb37c3373973 [file] [log] [blame]
Willy Tarreau240f9fd2007-12-02 14:11:36 +01001#!/bin/bash
2
3## contrib by prizee.com
4
5socket='/var/run/haproxy.stat'
6
7if ! type socat >/dev/null 2>&1 ; then
8 echo "can't find socat in PATH" 1>&2
9 exit 1
10fi
11
12printUsage ()
13{
14 echo -e "Usage : $(basename $0) [options] -s section
15--section -s section\t: section to use ( --list format)
16Options :
17--socket -S [socket]\t: socket to use (default: /var/run/haproxy.stat)
18--list -l\t\t: print available sections
19--help -h\t\t: print this message"
20}
21
22getRawStat ()
23{
24 if [ ! -S $socket ] ; then
25 echo "$socket socket unavailable" 1>&2
26 exit 1
27 fi
28
29 if ! printf "show stat\n" | socat unix-connect:${socket} stdio | grep -v "^#" ; then
30 echo "cannot read $socket" 1>&2
31 exit 1
32 fi
33}
34
35getStat ()
36{
37 stats=$(getRawStat | grep $1 | awk -F "," '{print $5" "$8}')
38 export cumul=$(echo $stats | cut -d " " -f2)
39 export current=$(echo $stats | cut -d " " -f1)
40}
41
42showList ()
43{
44 getRawStat | awk -F "," '{print $1","$2}'
45}
46
47set -- `getopt -u -l socket:,section:,list,help -- s:S:lh "$@"`
48
49while true ; do
50 case $1 in
51 --socket|-S) socket=$2 ; shift 2 ;;
52 --section|-s) section=$2 ; shift 2 ;;
53 --help|-h) printUsage ; exit 0 ;;
54 --list|-l) showList ; exit 0 ;;
55 --) break ;;
56 esac
57done
58
59if [ "$section" = "" ] ; then
60 echo "section not specified, run '$(basename $0) --list' to know available sections" 1>&2
61 printUsage
62 exit 1
63fi
64
65cpt=0
66totalrate=0
67while true ; do
68 getStat $section
69 if [ "$cpt" -gt "0" ] ; then
70 sessionrate=$(($cumul-$oldcumul))
71 totalrate=$(($totalrate+$sessionrate))
72 averagerate=$(($totalrate/$cpt))
73 printf "$sessionrate sessions/s (avg: $averagerate )\t$current concurrent sessions\n"
74 fi
75 oldcumul=$cumul
76 sleep 1
77 cpt=$(($cpt+1))
78done