(self, section=None)
Request stats from HAProxy Server
Request stats from HAProxy Server
Request stats from HAProxy Server
def http_get_csv_data(self, section=None):
        """
        Request stats from HAProxy Server
        """
        metrics = []
        req = urllib2.Request(self._get_config_value(section, 'url'))
        try:
            handle = urllib2.urlopen(req)
            return handle.readlines()
        except Exception as e:
            if not hasattr(e, 'code') or e.code != 401:
                self.log.error("Error retrieving HAProxy stats. %s", e)
                return metrics

        # get the www-authenticate line from the headers
        # which has the authentication scheme and realm in it
        authline = e.headers['www-authenticate']

        # this regular expression is used to extract scheme and realm
        authre = (r'''(?:\s*www-authenticate\s*:)?\s*''' +
                  '''(\w*)\s+realm=['"]([^'"]+)['"]''')
        authobj = re.compile(authre, re.IGNORECASE)
        matchobj = authobj.match(authline)
        if not matchobj:
            # if the authline isn't matched by the regular expression
            # then something is wrong
            self.log.error('The authentication header is malformed.')
            return metrics

        scheme = matchobj.group(1)
        # here we've extracted the scheme
        # and the realm from the header
        if scheme.lower() != 'basic':
            self.log.error('Invalid authentication scheme.')
            return metrics

        base64string = base64.encodestring(
            '%s:%s' % (self._get_config_value(section, 'user'),
                       self._get_config_value(section, 'pass')))[:-1]
        authheader = 'Basic %s' % base64string
        req.add_header("Authorization", authheader)
        try:
            handle = urllib2.urlopen(req)
            metrics = handle.readlines()
            return metrics
        except IOError as e:
            # here we shouldn't fail if the USER/PASS is right
            self.log.error("Error retrieving HAProxy stats. " +
                           "(Invalid username or password?) %s", e)
            return metrics
def http_get_csv_data ( self , section = None ) : metrics = [ ] req = urllib2 . Request ( self . _get_config_value ( section , 'url' ) ) try : handle = urllib2 . urlopen ( req ) return handle . readlines ( ) except Exception as e : if not hasattr ( e , 'code' ) or e . code != 401 : self . log . error ( "Error retrieving HAProxy stats. %s" , e ) return metrics # get the www-authenticate line from the headers # which has the authentication scheme and realm in it authline = e . headers [ 'www-authenticate' ] # this regular expression is used to extract scheme and realm authre = ( r'''(?:\s*www-authenticate\s*:)?\s*''' + '''(\w*)\s+realm=['"]([^'"]+)['"]''' ) authobj = re . compile ( authre , re . IGNORECASE ) matchobj = authobj . match ( authline ) if not matchobj : # if the authline isn't matched by the regular expression # then something is wrong self . log . error ( 'The authentication header is malformed.' ) return metrics scheme = matchobj . group ( 1 ) # here we've extracted the scheme # and the realm from the header if scheme . lower ( ) != 'basic' : self . log . error ( 'Invalid authentication scheme.' ) return metrics base64string = base64 . encodestring ( '%s:%s' % ( self . _get_config_value ( section , 'user' ) , self . _get_config_value ( section , 'pass' ) ) ) [ : - 1 ] authheader = 'Basic %s' % base64string req . add_header ( "Authorization" , authheader ) try : handle = urllib2 . urlopen ( req ) metrics = handle . readlines ( ) return metrics except IOError as e : # here we shouldn't fail if the USER/PASS is right self . log . error ( "Error retrieving HAProxy stats. " + "(Invalid username or password?) %s" , e ) return metrics