示例#1
0
 def metricCollector3(self):
     try:
         if (self._userName and self._userPass):
                 password_mgr = urlconnection.HTTPPasswordMgr()
                 password_mgr.add_password(self._realm, self._url, self._userName, self._userPass)
                 auth_handler = urlconnection.HTTPBasicAuthHandler(password_mgr)
                 opener = urlconnection.build_opener(auth_handler)
                 urlconnection.install_opener(opener)
         response = urlconnection.urlopen(self._url, timeout=10)
         if response.status == 200:
             byte_responseData = response.read()
             str_responseData = byte_responseData.decode('UTF-8')
             self._parseStats(str_responseData)
         else:
             self.dictApacheData['status'] = 0
             self.dictApacheData['msg'] = 'Error_code' + str(response.status)
     except HTTPError as e:
         self.dictApacheData['status'] = 0
         self.dictApacheData['msg'] = 'Error_code : HTTP Error ' + str(e.code)
     except URLError as e:
         self.dictApacheData['status'] = 0
         self.dictApacheData['msg'] = 'Error_code : URL Error ' + str(e.reason)
     except InvalidURL as e:
         self.dictApacheData['status'] = 0
         self.dictApacheData['msg'] = 'Error_code : Invalid URL'
     except Exception as e:
         self.dictApacheData['status'] = 0
         self.dictApacheData['msg'] = 'Exception occured in collecting data : ' + str(e)
示例#2
0
 def _openURL2(self):
     try:
         if (self._userName and self._userPass):
             password_mgr = urlconnection.HTTPPasswordMgr()
             password_mgr.add_password(self._realm, self._url, self._userName, self._userPass)
             auth_handler = urlconnection.HTTPBasicAuthHandler(password_mgr)
             opener = urlconnection.build_opener(auth_handler)
             urlconnection.install_opener(opener)
         response = urlconnection.urlopen(self._url, timeout=10)
         if (response.getcode() == 200):
             byte_responseData = response.read()
             str_responseData = byte_responseData.decode('UTF-8')
             self._parseStats(str_responseData)
         else:
             #self.dictInterfaceData['status'] = 0
             self.dictInterfaceData['msg'] = 'Response status code from haproxy url is :'  + str(response.getcode())
     except HTTPError as e:
         #self.dictInterfaceData['status'] = 0
         self.dictInterfaceData['msg'] ='Haproxy stats url has HTTP Error '+str(e.code)
     except URLError as e:
         #self.dictInterfaceData['status'] = 0
         self.dictInterfaceData['msg'] = 'Haproxy stats url has URL Error '+str(e.reason)
     except InvalidURL as e:
         #self.dictInterfaceData['status'] = 0
         self.dictInterfaceData['msg'] = 'Haproxy stats url is invalid URL'
     except Exception as e:
         #self.dictInterfaceData['status'] = 0
         self.dictInterfaceData['msg'] = 'Haproxy stats URL error : ' + str(e)
示例#3
0
 def _openURL2(self,str_URLsuffix):
     str_responseData = None
     url = None
     try:
         url = self._url + str_URLsuffix
         if (self._userName and self._userPass):
             password_mgr = urlconnection.HTTPPasswordMgr()
             password_mgr.add_password(self._realm, url, self._userName, self._userPass)
             auth_handler = urlconnection.HTTPBasicAuthHandler(password_mgr)
             opener = urlconnection.build_opener(auth_handler)
             urlconnection.install_opener(opener)
         response = urlconnection.urlopen(url, timeout = 5)
         if response.getcode() == 200:
             byte_responseData = response.read()
             str_responseData = byte_responseData.decode('UTF-8')
         else:
             self.dictEsPluginData['status'] = '0'
             self.dictEsPluginData['msg'] = 'Invalid response after opening URL : ' + str(response.getcode())
     except HTTPError as e:
         self.dictEsPluginData['status'] = '0'
         self.dictEsPluginData['msg'] ='HTTP Error '+str(e.code)
     except URLError as e:
         self.dictEsPluginData['status'] = '0'
         self.dictEsPluginData['msg'] = 'URL Error '+str(e.reason)
     except InvalidURL as e:
         self.dictEsPluginData['status'] = '0'
         self.dictEsPluginData['msg'] = 'Invalid URL'
     except Exception as e:
         self.dictEsPluginData['status'] = '0'
         self.dictEsPluginData['msg'] = 'Exception while opening stats url in python 2 : ' + str(e)
     finally:
         return str_responseData
示例#4
0
from urllib import request

url = 'https://www.myurl.com/'

# 「HTTPPasswordMgr」クラス作成する
passwd = request.HTTPPasswordMgr()
passwd.add_password(None, url, 'Username', 'Password')

# オプションを作成してインストール
opn = request.build_opener(passwd)
request.install_opener(opn)

# URLを開く
with request.urlopen(url) as response:
    html = response.read().decode('utf-8')
示例#5
0
def metricCollector():

    dictCounterValues = loadCounterValues()

    data = {}
    #defaults

    data['plugin_version'] = PLUGIN_VERSION

    data['heartbeat_required'] = HEARTBEAT

    import re

    PYTHON_MAJOR_VERSION = sys.version_info[0]

    if PYTHON_MAJOR_VERSION == 3:
        import urllib
        import urllib.request as urlconnection
        from urllib.error import URLError, HTTPError
        from http.client import InvalidURL
    elif PYTHON_MAJOR_VERSION == 2:
        import urllib2 as urlconnection
        from urllib2 import HTTPError, URLError
        from httplib import InvalidURL

    response = None

    try:

        url = NGINX_STATUS_URL
        if USERNAME and PASSWORD:
            password_mgr = urlconnection.HTTPPasswordMgr()
            password_mgr.add_password(None, url, USERNAME, PASSWORD)
            auth_handler = urlconnection.HTTPBasicAuthHandler(password_mgr)
            opener = urlconnection.build_opener(auth_handler)
            urlconnection.install_opener(opener)
        response = urlconnection.urlopen(url, timeout=10)
        output = response.read()
        active_con = re.search(r'Active connections:\s+(\d+)', output)
        if active_con:
            connection = int(active_con.group(1))
            data['active_connection'] = connection

        read_writes = re.search(
            r'Reading: (\d+)\s+Writing: (\d+)\s+Waiting: (\d+)', output)
        if read_writes:
            reading, writing, waiting = read_writes.groups()
            data['reading'] = reading
            data['writing'] = writing
            data['waiting'] = waiting

        conn = 0
        handled = 0
        requests = 0
        updateDict = {}
        per_s_connections = re.search(r'\s*(\d+)\s+(\d+)\s+(\d+)', output)
        if per_s_connections:
            conn = int(per_s_connections.group(1))
            handled = int(per_s_connections.group(2))
            requests = int(per_s_connections.group(3))

        if dictCounterValues:
            if 'request_per_s' in dictCounterValues:
                rps = dictCounterValues['request_per_s']
                data['request_per_s'] = (conn - rps) / TIME_INTERVAL
            if 'connection_opened' in dictCounterValues:
                conn_opened = dictCounterValues['connection_opened']
                data['connection_opened'] = (handled -
                                             conn_opened) / TIME_INTERVAL
            if 'connection_dropped' in dictCounterValues:
                conn_dropped = dictCounterValues['connection_dropped']
                data['connection_dropped'] = (requests -
                                              conn_dropped) / TIME_INTERVAL
        else:
            data['request_per_s'] = 0
            data['connection_opened'] = 0
            data['connection_dropped'] = 0

        updateDict['request_per_s'] = conn
        updateDict['connection_opened'] = handled
        updateDict['connection_dropped'] = requests

        updateCounterValues(updateDict)

    except HTTPError as e:
        data['status'] = 0
        data['msg'] = 'Error_code : HTTP Error ' + str(e.code)
    except URLError as e:
        data['status'] = 0
        data['msg'] = 'Error_code : URL Error ' + str(e.reason)
    except InvalidURL as e:
        data['status'] = 0
        data['msg'] = 'Error_code : Invalid URL'
    except Exception as e:
        data['status'] = 0
        data['msg'] = str(traceback.format_exc())

    return data