Hello,

the attached Python script can be used to reproduce the problem.
No external library is necessary.

Cheers,
Michele

'''
Created on Mar 15, 2012

@author: Michele Mazzucco <[email protected]>
'''

import sys, time, subprocess, string

# backend
backend = 'www'
# list of servers
servers_list = ['i-932dbef7', 'i-c368faa7', 'i-c968faad', 'i-cf68faab', 'i-c168faa5']


def test(sock='tmp/haproxy'):
    '''
        * param sock: path to the socket used by HAProxy
    '''
    enable = 'disable'
    while True:
        l = []
        for i in servers_list:
            tmp = '%s server %s/%s' % (enable, backend, i)
            l.append(tmp)
            
        command = ';'.join(l)
        
        #socat = 'echo "%s" | socat stdio %s' % (command, sock)
        p1 = subprocess.Popen(['echo', command], stdout=subprocess.PIPE, shell=False)
        p2 = subprocess.Popen(['socat', 'stdio', sock], stdin=p1.stdout, shell=False, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
        output = p2.communicate()
        if p2.returncode != 0:
            raise RuntimeError('Unable to issue command')
        
        #socat = 'echo "show stat" | socat stdio %s' % sock        
        # check
        p1 = subprocess.Popen(['echo', 'show stat'], stdout=subprocess.PIPE, shell=False)
        p2 = subprocess.Popen(['socat', 'stdio', sock], stdin=p1.stdout, shell=False, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
        output = p2.communicate()
        if p2.returncode != 0:
            raise RuntimeError('Unable to retrieve stats')
        
        
        res = output[0].split('\n')
        for line in res:
            if line.startswith(backend):
                tokens = string.split(line, ',')
                if tokens[1] in servers_list: # ignore BACKEND
                    if enable == 'disable' and tokens[17] != 'MAINT':
                        # see sec. 9.1
                        # http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
                        print 'Expected MAINT, got %s' % tokens[17]
                        print line
                        raise RuntimeError()
                    if enable == 'enable' and tokens[17] == 'MAINT':
                        print 'Expected not MAINT!'
                        print line
                        raise RuntimeError()
        
        
        if enable == 'enable':
            enable = 'disable'
        else:
            enable = 'enable'     
        time.sleep(0.1)     # the longer sleep is, the less likely the problem is likely to occur 
            
            
if __name__ == '__main__':
    sock = '/tmp/haproxy'
    if len(sys.argv) > 1:
        sock = sys.argv[1]
    test(sock)
        
        
        

On Mar 15, 2012, at 4:59 PM, Jonathan Matthews wrote:

> On 15 March 2012 08:21, Michele Mazzucco <[email protected]> wrote:
>> I don't think it's a problem related to the web interface -- socat behaves 
>> like my script, i.e., it fails to enable servers from time to time.
> 
> Michele -
> 
> I think the most useful thing you can do is to provide a socat
> invocation that causes the problem, and let the list know how
> frequently it's caused by that *exact* invocation.
> Or, to put it another way, I'm not entirely sure that we're not simply
> remote-debugging your script, without actually having a copy of it!
> 
> However, it's not really in-scope for anyone on list to fix your
> script, hence providing a simple command using very simple primitives
> (bash; socat; etc) that we can repeat until failure occurs will really
> help to clarify the failure you're observing. FWIW, I suggest you
> /don't/ provide the script :-)
> 
> All the best,
> Jonathan
> -- 
> Jonathan Matthews
> London, Oxford, UK
> http://www.jpluscplusm.com/contact.html

Reply via email to