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
Struggling writing custom plugins #206
Comments
nice you are trying it! So, here are a few rules:
If you can share your plugin, I can have a look. |
Here is the plugin I use for my tests:
May it be python not precise enough ? |
ok, it was a bit strange (I am not a python expert really - actually this was my first python program). #!/usr/bin/python3 -u
# use -u at shebang to disable buffering
# http://stackoverflow.com/questions/107705/disable-output-buffering
import sys, errno, time, random, argparse
sys.stdin.close()
parser = argparse.ArgumentParser(description='my super duper netdata module')
parser.add_argument('update_every', type=int, nargs='?', help='update frequency in seconds')
args = parser.parse_args()
# internal defaults for the command line arguments
update_every = 1
# evaluate the command line arguments
if args.update_every != None:
update_every = args.update_every
# various preparations
update_every *= 1000
get_millis = lambda: int(round(time.time() * 1000))
# generate the charts
try:
sys.stdout.write('CHART example.tferreira tferreira "Netdata Issue 206" "my unit" "my family" "my category" line 100000 %s\n' % int(update_every / 1000))
sys.stdout.write('DIMENSION value1 "random number 1" absolute 1 1\n')
sys.stdout.flush()
except IOError as e:
sys.stderr.write('Failed to send data to netdata\n')
sys.exit(0)
# the main loop
count = 0
last_run = next_run = now = get_millis()
while True:
if next_run <= now:
count += 1
# DO DATA COLLECTION HERE
value1 = random.randint(0, 1000)
# debugging to know it is working
# stderr is going to /var/log/netdata/error.log
# don't enable on production
#sys.stderr.write('collecting data, iteration No %s\n' % count)
#sys.stderr.flush()
# get the current time again
# data collection may be too slow
now = get_millis()
# find the time for the next run
while next_run <= now:
next_run += update_every
# calculate dt = the time we took
# since the last run
dt = now - last_run
last_run = now
# on the first iteration, don't set dt
# allowing netdata to align itself
if count == 1:
dt = 0
# send the values to netdata
try:
sys.stdout.write('BEGIN example.tferreira %s\n' % (dt * 1000))
sys.stdout.write('SET value1 = %s\n' % value1)
sys.stdout.write('END\n')
sys.stdout.flush()
except IOError as e:
sys.stderr.write('Failed to send data to netdata\n')
sys.exit(0)
# sleep 1/10 of update_every
time.sleep(update_every / 1000 / 10)
now = get_millis() Pay attention to While trying to find out what is happening, I replaced all Here is a screenshot of it running: |
Wow, this is awesome! Everything is working perfectly smooth with the -u option. I'm using Python for a few years now, and I never heard about this kind of buffering when working outside of a console. Thanks a lot for finding this. |
Hello, Putting it into "/usr/libexec/netdata/plugins.d/pythonscript.chart.sh" ? How can I go about enabling it? Thanks _EDIT_ Tom |
sys.stdout.write('CHART example.tferreira tferreira "Netdata Issue 206" "my unit" "my family" "my category" line 100000 %s\n' % int(update_every / 1000)) |
You want to add an application path, where? |
Hello, Thanks for this explanation it helped me understand better how plugins work. However, I'm trying to use the plugin above placing it on /usr/libexec/netdata/plugins.d/pythonscript.plugin as @barretttom suggest and adding example.tferreira to my index.html custom file but I get the following error: I've tried placing the example.chart.py from the example here and that works. Any help? |
I had it working!! I've removed the shebang, renamed the file to ferreira.charts.py and moved it to /usr/libexec/netdata/python.d |
This issue has been mentioned on the Netdata Community. There might be relevant details there: https://community.netdata.cloud/t/writing-a-custom-python-plugin/869/1 |
* cleanups1 * cleanups2 * cleanups3 * minor
Hi!
I'm really excited on monitoring all my microservices on netdata, but I am encountering some issues when writing my own plugins.
Indeed, to have a chart updating every second, I need to print data to stdout at least every 10ms (and thus having a high CPU usage). The more I increase this value, the more blank gaps I will have on the lines.
Also, if I don't add the CHART and DIMENSION lines to this output everytime, along with the BEGIN/SET/END ones, it also increase these gaps.
By the way I am using the pseudo code found here, without any other modifications nor adding data collection that would make the script sleep:
https://github.com/firehol/netdata/wiki/External-Plugins#writing-plugins-properly
Any idea of what I may be doing wrong ?
The text was updated successfully, but these errors were encountered: