def _openURL3(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.dictInterfaceData['status'] = 0
self.dictInterfaceData['msg'] = 'Response status code from haproxy url is :' + str(response.status)
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)
python类install_opener()的实例源码
def readUrl(self,host,port,url,user,password):
error=False
tomcatUrl = "http://"+host+":"+str(port)+url
try:
pwdManager = urlconnection.HTTPPasswordMgrWithDefaultRealm()
pwdManager.add_password(None,tomcatUrl,user,password)
authHandler = urlconnection.HTTPBasicAuthHandler(pwdManager)
opener=urlconnection.build_opener(authHandler)
urlconnection.install_opener(opener)
req = urlconnection.Request(tomcatUrl)
handle = urlconnection.urlopen(req, None)
data = handle.read()
except HTTPError as e:
if(e.code==401):
data="ERROR: Unauthorized user. Does not have permissions. %s" %(e)
elif(e.code==403):
data="ERROR: Forbidden, yours credentials are not correct. %s" %(e)
else:
data="ERROR: The server couldn\'t fulfill the request. %s" %(e)
error=True
except URLError as e:
data = 'ERROR: We failed to reach a server. Reason: %s' %(e.reason)
error = True
except socket.timeout as e:
data = 'ERROR: Timeout error'
error = True
except socket.error as e:
data = "ERROR: Unable to connect with host "+self.host+":"+self.port
error = True
except:
data = "ERROR: Unexpected error: %s"%(sys.exc_info()[0])
error = True
return data,error
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)
def readUrl(self,host,port,url,user,password):
error=False
tomcatUrl = "http://"+host+":"+str(port)+url
try:
pwdManager = urlconnection.HTTPPasswordMgrWithDefaultRealm()
pwdManager.add_password(None,tomcatUrl,user,password)
authHandler = urlconnection.HTTPBasicAuthHandler(pwdManager)
opener=urlconnection.build_opener(authHandler)
urlconnection.install_opener(opener)
req = urlconnection.Request(tomcatUrl)
handle = urlconnection.urlopen(req, None)
data = handle.read()
except HTTPError as e:
if(e.code==401):
data="ERROR: Unauthorized user. Does not have permissions. %s" %(e)
elif(e.code==403):
data="ERROR: Forbidden, yours credentials are not correct. %s" %(e)
else:
data="ERROR: The server couldn\'t fulfill the request. %s" %(e)
error=True
except URLError as e:
data = 'ERROR: We failed to reach a server. Reason: %s' %(e.reason)
error = True
except socket.timeout as e:
data = 'ERROR: Timeout error'
error = True
except socket.error as e:
data = "ERROR: Unable to connect with host "+self.host+":"+self.port
error = True
except:
data = "ERROR: Unexpected error: %s"%(sys.exc_info()[0])
error = True
return data,error
def metricCollector():
data = {}
#defaults
data['plugin_version'] = PLUGIN_VERSION
data['heartbeat_required']=HEARTBEAT
data['units']=METRICS_UNITS
URL = "http://"+COUCHDB_HOST+":"+COUCHDB_PORT+COUCHDB_STATS_URI
try:
if COUCHDB_USERNAME and COUCHDB_PASSWORD:
password_mgr = connector.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(REALM, URL, COUCHDB_USERNAME, COUCHDB_PASSWORD)
auth_handler = connector.HTTPBasicAuthHandler(password_mgr)
opener = connector.build_opener(auth_handler)
connector.install_opener(opener)
response = connector.urlopen(URL, timeout=10)
byte_responseData = response.read()
str_responseData = byte_responseData.decode('UTF-8')
couch_dict = json.loads(str_responseData)
for attribute, attribute_value in couch_dict.items():
for metric, val in attribute_value.items():
if 'current' in val and val['current'] is not None:
if metric in METRICS_KEY_VS_NAME:
metric = METRICS_KEY_VS_NAME[metric]
data[metric]=val['current']
except Exception as e:
data['status']=0
data['msg']=str(e)
return data
def metricCollector():
data = {}
data['plugin_version'] = PLUGIN_VERSION
data['heartbeat_required'] = HEARTBEAT
data['units'] = METRICS_UNITS
URL = 'http://%s:%s/api/jolokia/read/org.apache.activemq:type=Broker,brokerName=localhost' % (ACTIVEMQ_HOST, ACTIVEMQ_PORT)
try:
if ACTIVEMQ_USERNAME and ACTIVEMQ_PASSWORD:
password_mgr = connector.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(REALM, URL, ACTIVEMQ_USERNAME, ACTIVEMQ_PASSWORD)
auth_handler = connector.HTTPBasicAuthHandler(password_mgr)
opener = connector.build_opener(auth_handler)
connector.install_opener(opener)
response = connector.urlopen(URL, timeout=10)
byte_responseData = response.read()
str_responseData = byte_responseData.decode('UTF-8')
json_data = json.loads(str_responseData)
total_message_count = json_data['value']['TotalMessageCount']
total_connections_count = json_data['value']['TotalConnectionsCount']
total_consumer_count = json_data['value']['TotalConsumerCount']
total_producer_count = json_data['value']['TotalProducerCount']
data['total_message_count'] = total_message_count
data['total_connections_count'] = total_connections_count
data['total_consumer_count'] = total_consumer_count
data['total_producer_count'] = total_producer_count
except Exception as e:
data['status'] = 0
data['msg'] = str(e)
return data
def metricCollector():
data = {}
#defaults
data['plugin_version'] = PLUGIN_VERSION
data['heartbeat_required']=HEARTBEAT
data['units']=METRICS_UNITS
URL = "http://"+RIAK_HOST+":"+RIAK_PORT+"/"+RIAK_STATS_URI
try:
if RIAK_USERNAME and RIAK_PASSWORD:
password_mgr = connector.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(REALM, URL, RIAK_USERNAME, RIAK_PASSWORD)
auth_handler = connector.HTTPBasicAuthHandler(password_mgr)
opener = connector.build_opener(auth_handler)
connector.install_opener(opener)
response = connector.urlopen(URL, timeout=10)
byte_responseData = response.read()
str_responseData = byte_responseData.decode('UTF-8')
riak_dict = json.loads(str_responseData)
for metric in riak_dict:
if metric in METRICS_TO_BE_PUSHED_TO_SERVER:
value=riak_dict[metric]
if metric in BYTES_TO_MB_LIST:
value=convertBytesToMB(value)
data[metric]=value
except Exception as e:
data['status']=0
data['msg']=str(e)
return data
def readUrl(self,host,port,url,user,password):
error=False
tomcatUrl = "http://"+host+":"+str(port)+url
try:
pwdManager = urlconnection.HTTPPasswordMgrWithDefaultRealm()
pwdManager.add_password(None,tomcatUrl,user,password)
authHandler = urlconnection.HTTPBasicAuthHandler(pwdManager)
opener=urlconnection.build_opener(authHandler)
urlconnection.install_opener(opener)
req = urlconnection.Request(tomcatUrl)
handle = urlconnection.urlopen(req, None)
data = handle.read()
except HTTPError as e:
if(e.code==401):
data="ERROR: Unauthorized user. Does not have permissions. %s" %(e)
elif(e.code==403):
data="ERROR: Forbidden, yours credentials are not correct. %s" %(e)
else:
data="ERROR: The server couldn\'t fulfill the request. %s" %(e)
error=True
except URLError as e:
data = 'ERROR: We failed to reach a server. Reason: %s' %(e.reason)
error = True
except socket.timeout as e:
data = 'ERROR: Timeout error'
error = True
except socket.error as e:
data = "ERROR: Unable to connect with host "+self.host+":"+self.port
error = True
except:
data = "ERROR: Unexpected error: %s"%(sys.exc_info()[0])
error = True
return data,error
def getOverview(data):
try:
URL = RABBITMQ_SERVER+RABBITMQ_API_URI
if RABBITMQ_USERNAME and RABBITMQ_PASSWORD:
password_mgr = connector.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(REALM, URL, RABBITMQ_USERNAME, RABBITMQ_PASSWORD)
auth_handler = connector.HTTPBasicAuthHandler(password_mgr)
opener = connector.build_opener(auth_handler)
connector.install_opener(opener)
response = connector.urlopen(URL, timeout=10)
byte_responseData = response.read()
str_responseData = byte_responseData.decode('UTF-8')
rabbit_dict = json.loads(str_responseData)
if rabbit_dict:
if 'consumers' in rabbit_dict['object_totals']:
data['consumers']=rabbit_dict['object_totals']['consumers']
if 'queues' in rabbit_dict['object_totals']:
data['queues']=rabbit_dict['object_totals']['queues']
if 'exchanges' in rabbit_dict['object_totals']:
data['exchanges']=rabbit_dict['object_totals']['exchanges']
if 'channels' in rabbit_dict['object_totals']:
data['channels']=rabbit_dict['object_totals']['channels']
data['messages_ready']=rabbit_dict['queue_totals']['messages_ready']
data['messages_unack']=rabbit_dict['queue_totals']['messages_unacknowledged']
data['messages']=rabbit_dict['queue_totals']['messages']
data['messages_rate']=rabbit_dict['queue_totals']['messages_details']['rate']
data['messages_ready_rate']=rabbit_dict['queue_totals']['messages_ready_details']['rate']
data['messages_unack_rate']=rabbit_dict['queue_totals']['messages_unacknowledged_details']['rate']
if 'deliver_details' in rabbit_dict['message_stats']:
data['deliverrate']=rabbit_dict['message_stats']['deliver_details']['rate']
if 'ack_details' in rabbit_dict['message_stats']:
data['ackrate']=rabbit_dict['message_stats']['ack_details']['rate']
if 'publish_details' in rabbit_dict['message_stats']:
data['publishrate']=rabbit_dict['message_stats']['publish_details']['rate']
except Exception as e:
data['status']=0
data['msg']=str(e)
def getNodes(data):
try:
NODES_URL=RABBITMQ_SERVER+RABBITMQ_NODES_URI
if RABBITMQ_USERNAME and RABBITMQ_PASSWORD:
password_mgr = connector.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(REALM, NODES_URL, RABBITMQ_USERNAME, RABBITMQ_PASSWORD)
auth_handler = connector.HTTPBasicAuthHandler(password_mgr)
opener = connector.build_opener(auth_handler)
connector.install_opener(opener)
response = connector.urlopen(NODES_URL, timeout=10)
byte_responseData = response.read()
str_responseData = byte_responseData.decode('UTF-8')
rabbit_nodes_dict = json.loads(str_responseData)
nodes_dict=rabbit_nodes_dict[0]
if nodes_dict:
if 'mem_used' in nodes_dict:
value = convertBytesToMB(nodes_dict['mem_used'])
data['mem_used']=value
if 'fd_used' in nodes_dict:
data['fd_used']=nodes_dict['fd_used']
if 'run_queue' in nodes_dict:
data['run_queue']=nodes_dict['run_queue']
if 'sockets_used' in nodes_dict:
data['sockets_used']=nodes_dict['sockets_used']
if 'proc_used' in nodes_dict:
data['proc_used']=nodes_dict['proc_used']
if 'processors' in nodes_dict:
data['processors']=nodes_dict['processors']
if 'fd_total' in nodes_dict:
data['fd_total']=nodes_dict['fd_total']
if 'sockets_total' in nodes_dict:
data['sockets_total']=nodes_dict['sockets_total']
if 'disk_free_limit' in nodes_dict:
value=convertBytesToMB(nodes_dict['disk_free_limit'])
data['disk_free_limit']=value
if 'partitions' in nodes_dict:
partitions=nodes_dict['partitions']
data['partitions']=len(partitions)
except Exception as e:
data['status']=0
data['msg']=str(e)