-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
add IPFS plugin #761
add IPFS plugin #761
Conversation
@paulfantom is this the right way to query multiple URLs? anything else to improve? |
For python guide lines @paulfantom will review. |
|
if len(self.url) == 0: | ||
self.baseurl = "http://localhost:5001/" | ||
else: | ||
self.baseurl = self.url |
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.
Those lines won't have any meaning since self.url
will be overridden when python.d.plugin
executes check
method (from UrlService).
This is something to be done in
That's because you don't use def check(self):
if UrlService.check(self):
return True
if len(self.url) == 0:
self.baseurl = "http://localhost:5001/"
else:
self.baseurl = self.url
test = self._get_data()
if test is None or len(test) == 0:
return False
else:
return True Some quick explanation, how it works (since documentation is non-existent): And every
And if you want to get something from configuration, you need to parse |
I hope my explanations are good enough. |
peers = self._get_peers() | ||
bandwidth_in, bandwidth_out = self._get_bandwidth() | ||
|
||
return {'peers': peers, 'in': bandwidth_in, 'out': bandwidth_out} |
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.
This shouldn't return None
as dictionary value. If dimension has None
value it shouldn't be returned.
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.
should it just return None
then?
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.
It should check if peers
and bandwidths are None
.
If peers
is None
, but bandwidths have some data, it should return {'in': bandwidth_in, 'out': bandwidth_out}
. if bandwidth_in
is None
but other have some data it should return {'peers': peers, 'out': bandwidth_out}
. And so on.
None
should be returned if there is no data in any dimension.
Also this will be the first service to query multiple URLs. |
Thanks for the explanations! I added your
leads to i added a
so that don't work :( |
def check(self):
print(self.url)
if len(self.url) == 0: with this you overrode gathering data from configuration, so object won't have |
My proposed changes:
if len(self.url) == 0:
self.baseurl = "http://localhost:5001/"
else:
self.baseurl = self.url
peers = self._get_peers()
bandwidth_in, bandwidth_out = self._get_bandwidth()
Basically it should look something like this: # -*- coding: utf-8 -*-
# Description: IPFS netdata python.d module
# Authors: Pawel Krupa (paulfantom), davidak
from base import UrlService
import json
# default module values (can be overridden per job in `config`)
# update_every = 2
priority = 60000
retries = 60
# default job configuration (overridden by python.d.plugin)
# config = {'local': {
# 'update_every': update_every,
# 'retries': retries,
# 'priority': priority,
# 'url': 'http://localhost:5001'
# }}
# charts order (can be overridden if you want less charts, or different order)
ORDER = ['bandwidth', 'peers']
CHARTS = {
'bandwidth': {
'options': [None, 'IPFS Bandwidth', 'kbits/s', 'Bandwidth', 'ipfs.bandwidth', 'line'],
'lines': [
["in", None, "absolute", 8, 1000],
["out", None, "absolute", -8, 1000]
]},
'peers': {
'options': [None, 'IPFS Peers', 'peers', 'Peers', 'ipfs.peers', 'line'],
'lines': [
["peers", None, 'absolute']
]}
}
class Service(UrlService):
def __init__(self, configuration=None, name=None):
UrlService.__init__(self, configuration=configuration, name=name)
self.order = ORDER
self.definitions = CHARTS
def _get_bandwidth(self):
"""
Format data received from http request
:return: int, int
"""
self.url = self.baseurl + "/api/v0/stats/bw"
try:
raw = self._get_raw_data()
except AttributeError:
return None
try:
parsed = json.loads(raw)
bw_in = int(parsed['RateIn'])
bw_out = int(parsed['RateOut'])
except:
return None
return bw_in, bw_out
def _get_peers(self):
"""
Format data received from http request
:return: int
"""
self.url = self.baseurl + "/api/v0/swarm/peers"
try:
raw = self._get_raw_data()
except AttributeError:
return None
try:
parsed = json.loads(raw)
peers = len(parsed['Strings'])
except:
return None
return peers
def _get_data(self):
"""
Get data from API
:return: dict
"""
try:
peers = self._get_peers()
bandwidth_in, bandwidth_out = self._get_bandwidth()
except:
return None
data = {}
if peers is None:
data['peers'] = peers
if bandwidth_in is not None and bandwidth_out is not None:
data['in'] = bandwidth_in
data['out'] = bandwidth_out
if len(data) == 0:
return None
return data
def check(self):
if UrlService.check(self):
return True
if len(self.url) == 0:
self.baseurl = "http://localhost:5001"
else:
self.baseurl = self.url
test = self._get_data()
if test is None or len(test) == 0:
return False
else:
return True or like this: # -*- coding: utf-8 -*-
# Description: IPFS netdata python.d module
# Authors: Pawel Krupa (paulfantom), davidak
from base import UrlService
import json
# default module values (can be overridden per job in `config`)
# update_every = 2
priority = 60000
retries = 60
# default job configuration (overridden by python.d.plugin)
# config = {'local': {
# 'update_every': update_every,
# 'retries': retries,
# 'priority': priority,
# 'url': 'http://localhost:5001'
# }}
# charts order (can be overridden if you want less charts, or different order)
ORDER = ['bandwidth', 'peers']
CHARTS = {
'bandwidth': {
'options': [None, 'IPFS Bandwidth', 'kbits/s', 'Bandwidth', 'ipfs.bandwidth', 'line'],
'lines': [
["in", None, "absolute", 8, 1000],
["out", None, "absolute", -8, 1000]
]},
'peers': {
'options': [None, 'IPFS Peers', 'peers', 'Peers', 'ipfs.peers', 'line'],
'lines': [
["peers", None, 'absolute']
]}
}
class Service(UrlService):
def __init__(self, configuration=None, name=None):
UrlService.__init__(self, configuration=configuration, name=name)
try:
self.baseurl = str(self.configuration['url'])
except (KeyError, TypeError):
self.baseurl = "http://localhost:5001"
self.order = ORDER
self.definitions = CHARTS
def _get_bandwidth(self):
"""
Format data received from http request
:return: int, int
"""
self.url = self.baseurl + "/api/v0/stats/bw"
try:
raw = self._get_raw_data()
except AttributeError:
return None
try:
parsed = json.loads(raw)
bw_in = int(parsed['RateIn'])
bw_out = int(parsed['RateOut'])
except:
return None
return bw_in, bw_out
def _get_peers(self):
"""
Format data received from http request
:return: int
"""
self.url = self.baseurl + "/api/v0/swarm/peers"
try:
raw = self._get_raw_data()
except AttributeError:
return None
try:
parsed = json.loads(raw)
peers = len(parsed['Strings'])
except:
return None
return peers
def _get_data(self):
"""
Get data from API
:return: dict
"""
try:
peers = self._get_peers()
bandwidth_in, bandwidth_out = self._get_bandwidth()
except:
return None
data = {}
if peers is None:
data['peers'] = peers
if bandwidth_in is not None and bandwidth_out is not None:
data['in'] = bandwidth_in
data['out'] = bandwidth_out
if len(data) == 0:
return None
return data Both are valid. Also what is returned by ipfs when you request |
the second makes it clearer where the url comes from, eg. now i can configure multiple checks!
that is no command. |
last thing is the name in the webui menu. it would be nice to have also all names should be displayed as i tried something like this for ipfs in be51843#diff-d23fe9001e474c96419c8f2519cbef67 @paulfantom can you do that? |
To display case 'postfix':
case 'ipfs':
case 'redis': and the second: 'redis': {
title: 'Redis',
info: undefined
},
'ipfs': {
title: 'ipfs',
info: undefined
},
'phpfpm': {
title: 'PHP-FPM',
info: undefined,
}, And that's all. Remember about refreshing page with Ctrl + F5. |
i have done exactly that, but in so it's ready to merge i thing. :) @paulfantom thanks again for your help! |
Ok, I think it is good. |
with Bandwidth and Peers chart