Files
monasca-agent/monasca_agent/common/emitter.py
Gary Hessler b1de7db1f5 Removing subprocess call from system metrics classes
Changed the IO, Disk and Network classes to not use a subprocess.
They now use psutil to get the metrics.  Also, changed the linux
system metrics classes to subclass the AgentCheck class instead
of the old-style Check class.  Added additional configuration
and changed monasca-setup to support that. Fixed some Python
2.6 incompatible string formatting issues.

Change-Id: I1f8b65bf48e48e2c598aa4950c194fbae2f9e337
2015-02-25 10:58:43 -07:00

58 lines
1.8 KiB
Python

from hashlib import md5
import json
import urllib2
from monasca_agent.common.metrics import Measurement
def post_headers(payload):
return {
'User-Agent': 'Mon/Agent',
'Content-Type': 'application/json',
'Accept': 'text/html, */*',
'Content-MD5': md5(payload).hexdigest()
}
def http_emitter(message, log, url):
"""Send payload
"""
log.debug('http_emitter: attempting postback to ' + url)
# Post back the data
partial_payload = []
for measurement in message:
if not isinstance(measurement, Measurement):
log.error('Data was not in the form of a monasca_agent.common.metrics.Measurement')
continue
# Measurements need their __dict__ encoded to avoid being expressed as a tuple
partial_payload.append(measurement.__dict__)
payload = json.dumps(partial_payload)
url = "%s/intake" % url
headers = post_headers(payload)
try:
# Make sure no proxy is autodetected for this localhost connection
proxy_handler = urllib2.ProxyHandler({})
# Should this be installed as the default opener and reused?
opener = urllib2.build_opener(proxy_handler)
request = urllib2.Request(url, payload, headers)
response = None
try:
response = opener.open(request)
log.debug('http_emitter: postback response: ' + str(response.read()))
except Exception as exc:
log.error("""Forwarder at {0} is down or not responding...
Error is {1}
Please restart the monasca-agent.""".format(url, repr(exc)))
finally:
if response:
response.close()
except urllib2.HTTPError as e:
if e.code == 202:
log.debug("http payload accepted")
else:
raise