-
Notifications
You must be signed in to change notification settings - Fork 43
Overhaul and update InfluxDB engine #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,41 +18,15 @@ | |
# with this program; if not, write to the Free Software Foundation, Inc., | ||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
|
||
try: | ||
import json | ||
except ImportError: | ||
import simplejson as json # pylint: disable=F0401 | ||
|
||
import requests | ||
from requests.exceptions import RequestException | ||
from requests.exceptions import RequestException, HTTPError | ||
|
||
from pyrobase.parts import Bunch | ||
from pyrocore import error | ||
from pyrocore import config as config_ini | ||
from pyrocore.util import fmt, xmlrpc, pymagic, stats | ||
|
||
|
||
def _flux_engine_data(engine): | ||
""" Return rTorrent data set for pushing to InfluxDB. | ||
""" | ||
data = stats.engine_data(engine) | ||
|
||
# Make it flat | ||
data["up_rate"] = data["upload"][0] | ||
data["up_limit"] = data["upload"][1] | ||
data["down_rate"] = data["download"][0] | ||
data["down_limit"] = data["download"][1] | ||
data["version"] = data["versions"][0] | ||
views = data["views"] | ||
|
||
del data["upload"] | ||
del data["download"] | ||
del data["versions"] | ||
del data["views"] | ||
|
||
return data, views | ||
|
||
|
||
class EngineStats(object): | ||
""" rTorrent connection statistics logger. | ||
""" | ||
|
@@ -97,63 +71,61 @@ def __init__(self, config=None): | |
def _influxdb_url(self): | ||
""" Return REST API URL to access time series. | ||
""" | ||
url = "{0}/db/{1}/series".format(self.influxdb.url.rstrip('/'), self.config.dbname) | ||
url = "{0}/write?db={1}".format(self.influxdb.url.rstrip('/'), self.config.dbname) | ||
|
||
if self.influxdb.user and self.influxdb.password: | ||
url += "?u={0}&p={1}".format(self.influxdb.user, self.influxdb.password) | ||
|
||
return url | ||
|
||
def _influxdb_data(self): | ||
""" Return statitics data formatted according to InfluxDB's line protocol | ||
""" | ||
datastr = '' | ||
|
||
try: | ||
proxy = config_ini.engine.open() | ||
hostname = proxy.system.hostname() | ||
pid = proxy.system.pid() | ||
data = stats.engine_data(config_ini.engine) | ||
views = data['views'] | ||
del data['views'] | ||
datastr = u"{0}stat,hostname={1},pid={2} ".format( | ||
self.config.series_prefix, hostname, pid) | ||
datastr += ','.join(['='.join([k, str(v)]) for k, v in data.items()]) + '\n' | ||
for view_name, values in views.items(): | ||
vstr = u"{0}view,hostname={1},pid={2},name={3} ".format( | ||
self.config.series_prefix, hostname, pid, view_name) | ||
vstr += ','.join(['='.join([k, str(v)]) for k, v in values.items()]) | ||
datastr += vstr + "\n" | ||
except (error.LoggableError, xmlrpc.ERRORS), exc: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. … as exc: (other places, too) |
||
self.LOG.warn("InfluxDB stats: {0}".format(exc)) | ||
return datastr | ||
|
||
def _push_data(self): | ||
""" Push stats data to InfluxDB. | ||
""" | ||
if not (self.config.series or self.config.series_host): | ||
self.LOG.info("Misconfigured InfluxDB job, neither 'series' nor 'series_host' is set!") | ||
return | ||
|
||
# Assemble data | ||
fluxdata = [] | ||
|
||
if self.config.series: | ||
try: | ||
config_ini.engine.open() | ||
data, views = _flux_engine_data(config_ini.engine) | ||
fluxdata.append(dict( | ||
name=self.config.series, | ||
columns=data.keys(), | ||
points=[data.values()] | ||
)) | ||
fluxdata.append(dict( | ||
name=self.config.series + '_views', | ||
columns=views.keys(), | ||
points=[views.values()] | ||
)) | ||
except (error.LoggableError, xmlrpc.ERRORS), exc: | ||
self.LOG.warn("InfluxDB stats: {0}".format(exc)) | ||
|
||
# if self.config.series_host: | ||
# fluxdata.append(dict( | ||
# name = self.config.series_host, | ||
# columns = .keys(), | ||
# points = [.values()] | ||
# )) | ||
|
||
if not fluxdata: | ||
datastr = self._influxdb_data() | ||
|
||
if not datastr: | ||
self.LOG.debug("InfluxDB stats: no data (previous errors?)") | ||
return | ||
|
||
# Encode into InfluxDB data packet | ||
fluxurl = self._influxdb_url() | ||
fluxjson = json.dumps(fluxdata) | ||
self.LOG.debug("POST to {0} with {1}".format(fluxurl.split('?')[0], fluxjson)) | ||
self.LOG.debug("POST to {0} with {1}".format(fluxurl.split('?')[0], datastr)) | ||
|
||
# Push it! | ||
try: | ||
# TODO: Use a session | ||
requests.post(fluxurl, data=fluxjson, timeout=self.influxdb.timeout) | ||
r = requests.post(fluxurl, data=datastr, timeout=self.influxdb.timeout) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use |
||
r.raise_for_status() | ||
except RequestException, exc: | ||
self.LOG.info("InfluxDB POST error: {0}".format(exc)) | ||
self.LOG.warn("InfluxDB POST error: {0}".format(exc)) | ||
except HTTPError, exc: | ||
self.LOG.warn("InfluxDB POST HTTP error {0}: Response: {1}".format( | ||
str(r.status_code), r.content)) | ||
|
||
|
||
def run(self): | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/?/&/