Willy Tarreau | 240f9fd | 2007-12-02 14:11:36 +0100 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | ## contrib by prizee.com |
| 4 | |
| 5 | socket='/var/run/haproxy.stat' |
| 6 | |
| 7 | if ! type socat >/dev/null 2>&1 ; then |
| 8 | echo "can't find socat in PATH" 1>&2 |
| 9 | exit 1 |
| 10 | fi |
| 11 | |
| 12 | printUsage () |
| 13 | { |
| 14 | echo -e "Usage : $(basename $0) [options] -s section |
| 15 | --section -s section\t: section to use ( --list format) |
| 16 | Options : |
| 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 | |
| 22 | getRawStat () |
| 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 | |
| 35 | getStat () |
| 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 | |
| 42 | showList () |
| 43 | { |
| 44 | getRawStat | awk -F "," '{print $1","$2}' |
| 45 | } |
| 46 | |
| 47 | set -- `getopt -u -l socket:,section:,list,help -- s:S:lh "$@"` |
| 48 | |
| 49 | while 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 |
| 57 | done |
| 58 | |
| 59 | if [ "$section" = "" ] ; then |
| 60 | echo "section not specified, run '$(basename $0) --list' to know available sections" 1>&2 |
| 61 | printUsage |
| 62 | exit 1 |
| 63 | fi |
| 64 | |
| 65 | cpt=0 |
| 66 | totalrate=0 |
| 67 | while 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)) |
| 78 | done |