Read stat or info from the socket via perl

From: vmware vmware <vmwaretest09#gmail.com>
Date: Sat, 14 Feb 2009 22:53:11 +0100


Hi all,

I am trying to read the information (show info, show stat) from the socket of haproxy with a perl script in order to get a similar result when using the socat command. The problem is that I am not able to read something. I see also a similar example in the sources in the contrib folder, but somehow I don't get the point how to read and print this information.

This is my perl program.

Thanks for any help

###############################################

use strict;
use IO::Socket;
use lib "/usr/local/nagios/libexec";

my $sock = new IO::Socket::UNIX (

     LocalAddr => "/var/run/haproxy.socket",
     Type => SOCK_STREAM,
     Timeout => 1)

or die 'error on connectin.';

next if !$sock;

print $sock "show info\n";
# here i would like to read out the information from the socket
while(<$sock>){

        chomp;
        print;

# my @d = split('\t');
# print "info data: @d\n";
}check_disk_smb check_mrtgtraf check_spop check_dns check_nagios check_ssh check_dummy check_nntp check_ssmtp check_file_age check_nntps check_swap check_flexlm check_nrpe check_tcp check_ftp check_nt check_time check_hello check_ntp check_udp check_hello.pl check_ntp_peer check_ups check_hpjd check_ntp_time check_users check_http check_nwstat check_wave check_icmp check_oracle root#lobalix01:/usr/local/nagios/libexec# vi check_h check_hello check_hello.pl check_hpjd check_http root#lobalix01:/usr/local/nagios/libexec# vi check_h check_hello check_hello.pl check_hpjd check_http root#lobalix01:/usr/local/nagios/libexec# vi check_h check_hello check_hello.pl check_hpjd check_http root#lobalix01:/usr/local/nagios/libexec# vi check_hello check_hello check_hello.pl

root#lobalix01:/usr/local/nagios/libexec# vi check_hello root#lobalix01:/usr/local/nagios/libexec# vi test tests/ testSocket.pl
root#lobalix01:/usr/local/nagios/libexec# vi test tests/ testSocket.pl
root#lobalix01:/usr/local/nagios/libexec# vi testSocket.pl
check_apache        check_ifstatus      check_real
check_apache.pl     check_imap          check_rpc
check_apt           check_ircd          check_sensors
check_breeze        check_jabber        check_simap
check_by_ssh        check_load          check_smtp
check_clamd         check_log           check_snmp
check_cluster       check_mailq         check_spop
check_dhcp          check_mrtg          check_ssh
check_dig           check_mrtgtraf      check_ssmtp
check_disk          check_nagios        check_swap
check_disk_smb      check_nntp          check_tcp
check_dns           check_nntps         check_time
check_dummy         check_nrpe          check_udp
check_file_age      check_nt            check_ups
check_flexlm        check_ntp           check_users
check_ftp           check_ntp_peer      check_wave
check_hello         check_ntp_time      negate
check_hello.pl      check_nwstat        tests/
check_hpjd          check_oracle        testSocket.pl
check_http          check_overcr        urlize
check_icmp          check_ping          utils.pm
check_ide_smart     check_pop           utils.sh
check_ifoperstatus  check_procs

root#lobalix01:/usr/local/nagios/libexec# vi testSocket.pl root#lobalix01:/usr/local/nagios/libexec# vi check_h check_hello check_hello.pl check_hpjd check_http root#lobalix01:/usr/local/nagios/libexec# vi check_hello check_hello check_hello.pl
root#lobalix01:/usr/local/nagios/libexec# vi check_hello.pl
root#lobalix01:/usr/local/nagios/libexec# perl check_hello.pl
root#lobalix01:/usr/local/nagios/libexec# vi check_hello.pl
root#lobalix01:/usr/local/nagios/libexec# perl check_hello.pl
root#lobalix01:/usr/local/nagios/libexec# history | grep socat
  249 history | grep socat
  252 history | grep socat
  253 show stat | socat unix-connect:/var/run/haproxy.socket   254 "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  255  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  256  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  259  echo "show stat" | socat unix-connect:/var/run/haproxy.socket
stdioech
  260 echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio   261 socat unix-connect:/var/run/haproxy.socket stdio
  262  echo "show statt" | socat unix-connect:/var/run/haproxy.socket stdio
  263  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  270  echo "show stat" | socat unix-connect:/var/run/haproxy.socket
  271  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdiols
  272 socat -d -d READLINE,history=$HOME/.http_history \ TCP4:www.domain.org:www,crnl
  273 socat -d READLINE,history=$HOME/.http_history \ TCP4:www.domain.org: www,crnl
  274 socat -d -d READLINE,history=$HOME/.http_history \ TCP4:www.domain.org:www,crnl
  280 UserParameter=proxyconn,echo "show info" | /usr/local/bin/socat /var/lib/haproxy-stat stdio | grep CurrConns | awk '{ print $2 }'   281 echo "show info" | /usr/local/bin/socat /var/lib/haproxy-stat stdio | grep CurrConns | awk '{ print $2 }'
  282 history | grep socat
  283 echo "show info" | /usr/local/bin/socat /var/lib/haproxy-stat stdio | grep CurrConns | awk '{ print $2 }'
  284 echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio | grep VIAS10ix2 | awk -F, '{ print $2 " " $5 }'   401 history | grep socat
  402 echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio | grep VIAS10ix2 | awk -F, '{ print $2 " " $5 }'   404 echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio | grep VIAS10ix2 | awk -F, '{ print $2 " " $5 }'   405 echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio | grep VIAS10ix2 | awk -F, '{ print $2 " " $5 }'   407 echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio | grep VIAS10ix2 | awk -F, '{ print $2 " " $5 }'   411 echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio | grep VIAS10ix2 | awk -F, '{ print $2 " " $5 }'   412 socat unix-connect:/var/run/haproxy.socket stdio | grep VIAS10ix2 | awk -F, '{ print $2 " " $5 }'
  413 show stat socat unix-connect:/var/run/haproxy.socket stdio | grep VIAS10ix2 | awk -F, '{ print $2 " " $5 }'   414 "show stat" socat unix-connect:/var/run/haproxy.socket stdio | grep VIAS10ix2 | awk -F, '{ print $2 " " $5 }'   415 echo "show stat" socat unix-connect:/var/run/haproxy.socket stdio | grep VIAS10ix2 | awk -F, '{ print $2 " " $5 }'   416 echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio | grep VIAS10ix2 | awk -F, '{ print $2 " " $5 }'   417 echo "show info" | socat unix-connect:/var/run/haproxy.socket stdio | grep VIAS10ix2 | awk -F, '{ print $2 " " $5 }'
  418  echo "show info" | socat unix-connect:/var/run/haproxy.socket stdio
  419  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  420  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  421  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  422  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  423  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  424  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  429  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  430  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  431  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  432  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  510 history | grep socat
root#lobalix01:/usr/local/nagios/libexec# !431 echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
#
pxname,svname,qcur,qmax,scur,smax,slim,stot,bin,bout,dreq,dresp,ereq,econ,eresp,wretr,wredis,status,weight,act,bck,chkfail,chkdown,lastchg,downtime,qlimit,pid,iid,sid,throttle,lbtot,tracked,type,
iastesting,FRONTEND,,,0,22,2000,206036,17103180,665343376,0,0,0,,,,,OPEN,,,,,,,,,1,1,0,,,,0,
iastesting,VIAS10ix1,0,0,0,16,,103018,8551598,332681371,,0,,0,0,0,0,UP,1,1,0,0,0,82051,0,,1,1,1,,103018,,2,
iastesting,VIAS10ix2,0,0,0,16,,103018,8551582,332662005,,0,,0,0,0,0,UP,1,1,0,0,0,82051,0,,1,1,2,,103018,,2,
iastesting,BACKEND,0,0,0,19,2000,206036,17103180,665343376,0,0,,0,0,0,0,UP,2,2,0,,0,82051,0,,1,1,0,,206036,,1,
root#lobalix01:/usr/local/nagios/libexec# echo "show info" | socat unix-connect:/var/run/haproxy.socket stdio Name: HAProxy
Version: 1.3.15.7
Release_date: 2008/12/04
Nbproc: 1
Process_num: 1
Pid: 21157
Uptime: 0d 22h47m49s
Uptime_sec: 82069
Memmax_MB: 0
Ulimit-n: 8206
Maxsock: 8206
Maxconn: 4096
CurrConns: 4
root#lobalix01:/usr/local/nagios/libexec# vi
check_hello.pl                     root#lobalix01:/usr/local/nagios/libexec#
history | grep socat                    249  history | grep socat
  252 history | grep socat
  253 show stat | socat unix-connect:/var/run/haproxy.socket   254 "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  255  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  256  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  259  echo "show stat" | socat unix-connect:/var/run/haproxy.socket
stdioech
  260 echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio   261 socat unix-connect:/var/run/haproxy.socket stdio
  262  echo "show statt" | socat unix-connect:/var/run/haproxy.socket stdio
  263  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  270  echo "show stat" | socat unix-connect:/var/run/haproxy.socket
  271  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdiols
  272 socat -d -d READLINE,history=$HOME/.http_history \ TCP4:www.domain.org:www,crnl
  273 socat -d READLINE,history=$HOME/.http_history \ TCP4:www.domain.org: www,crnl
  274 socat -d -d READLINE,history=$HOME/.http_history \ TCP4:www.domain.org:www,crnl
  280 UserParameter=proxyconn,echo "show info" | /usr/local/bin/socat /var/lib/haproxy-stat stdio | grep CurrConns | awk '{ print $2 }'   281 echo "show info" | /usr/local/bin/socat /var/lib/haproxy-stat stdio | grep CurrConns | awk '{ print $2 }'
  282 history | grep socat
  283 echo "show info" | /usr/local/bin/socat /var/lib/haproxy-stat stdio | grep CurrConns | awk '{ print $2 }'
  284 echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio | grep VIAS10ix2 | awk -F, '{ print $2 " " $5 }'   401 history | grep socat
  402 echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio | grep VIAS10ix2 | awk -F, '{ print $2 " " $5 }'   404 echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio | grep VIAS10ix2 | awk -F, '{ print $2 " " $5 }'   405 echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio | grep VIAS10ix2 | awk -F, '{ print $2 " " $5 }'   407 echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio | grep VIAS10ix2 | awk -F, '{ print $2 " " $5 }'   411 echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio | grep VIAS10ix2 | awk -F, '{ print $2 " " $5 }'   412 socat unix-connect:/var/run/haproxy.socket stdio | grep VIAS10ix2 | awk -F, '{ print $2 " " $5 }'
  413 show stat socat unix-connect:/var/run/haproxy.socket stdio | grep VIAS10ix2 | awk -F, '{ print $2 " " $5 }'   414 "show stat" socat unix-connect:/var/run/haproxy.socket stdio | grep VIAS10ix2 | awk -F, '{ print $2 " " $5 }'   415 echo "show stat" socat unix-connect:/var/run/haproxy.socket stdio | grep VIAS10ix2 | awk -F, '{ print $2 " " $5 }'   416 echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio | grep VIAS10ix2 | awk -F, '{ print $2 " " $5 }'   417 echo "show info" | socat unix-connect:/var/run/haproxy.socket stdio | grep VIAS10ix2 | awk -F, '{ print $2 " " $5 }'
  418  echo "show info" | socat unix-connect:/var/run/haproxy.socket stdio
  419  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  420  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  421  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  422  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  423  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  424  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  429  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  430  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  431  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  432  echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio
  510 history | grep socat
  511 echo "show stat" | socat unix-connect:/var/run/haproxy.socket stdio   512 echo "show info" | socat unix-connect:/var/run/haproxy.socket stdio   514 history | grep socat
root#lobalix01:/usr/local/nagios/libexec# perl
check_hello.pl                   root#lobalix01:/usr/local/nagios/libexec#
vi check_hello.pl
root#lobalix01:/usr/local/nagios/libexec# perl check_hello.pl
root#lobalix01:/usr/local/nagios/libexec# vi check_hello.pl
root#lobalix01:/usr/local/nagios/libexec# perl check_hello.pl
root#lobalix01:/usr/local/nagios/libexec# vi check_hello.pl
use strict;
use IO::Socket;
#

use lib "/usr/local/nagios/libexec";
#

my $sock = new IO::Socket::UNIX (

     LocalAddr => "/var/run/haproxy.socket",
     Type => SOCK_STREAM,
     Timeout => 2)

or die 'error on connectin.';

next if !$sock;
# reading the information of show info and printing in stdout
print $sock "show info\n";
while(<$sock>){

        chomp;
        print;

# my @d = split('\t');
# print "daten: @d\n";

}
$sock->close;

###############################################
Received on 2009/02/14 22:53

This archive was generated by hypermail 2.2.0 : 2009/02/15 00:00 CET