Is it possible to create dynamic charts within the get_data() method of a python plugin? Using the below example plugin I’m able to create the dynamic charts at runtime:
# -*- coding: utf-8 -*-
# Description: example netdata python.d module
# Author: Put your name here (your github login)
# SPDX-License-Identifier: GPL-3.0-or-later
from random import SystemRandom
from bases.FrameworkServices.SimpleService import SimpleService
priority = 90000
ORDER = []
CHARTS = {}
class Service(SimpleService):
def __init__(self, configuration=None, name=None):
SimpleService.__init__(self, configuration=configuration, name=name)
self.order = ORDER
self.definitions = CHARTS
self.random = SystemRandom()
def check(self):
self.create_charts()
return True
def generate_data(self):
dummy_data = {
"my_chart_family0": ["my_chart_name0", "my_chart_name1"],
"my_chart_familyB": ["my_chart_nameA", "my_chart_nameB"]
}
return dummy_data
def get_data(self):
data = dict()
dummy_data = self.generate_data()
for cf in dummy_data:
# cf is the chart family
for cn in dummy_data.get(cf):
# cn is the chart name
for i in range(1, 4):
dimension_id = "{}.{}.{}".format(cf, cn, i)
if dimension_id not in self.charts[cn]:
self.charts[cn].add_dimension([dimension_id])
data[dimension_id] = self.random.randint(0, 100)
return data
def create_charts(self):
dummy_data = self.generate_data()
# Loop through the dummy data. Construct a chart and tie it to the chart family for grouping
for cf in dummy_data:
# cf is the chart family
for cn in dummy_data.get(cf):
# cn is the chart name
CHARTS.update({cn: {
"options": [],
'lines': []
}})
self.order.insert(0, cn)
self.definitions[cn] = {
'options': [None, 'Title', 'Unit', cf, cf, 'line'],
'lines': []
}
However, if I shift self.create_charts()
from the check()
method into get_data()
I get errors when trying to update the ORDER:
File "/usr/libexec/netdata/python.d/python_modules/bases/FrameworkServices/SimpleService.py", line 197, in run
updated = self.update(interval=since)
File "/usr/libexec/netdata/python.d/python_modules/bases/FrameworkServices/SimpleService.py", line 222, in update
data = self.get_data()
File "/usr/libexec/netdata/python.d/random.chart.py", line 33, in get_data
self.create_charts()
File "/usr/libexec/netdata/python.d/random.chart.py", line 65, in create_charts
self.order.insert(0, cn)
AttributeError: 'Service' object has no attribute 'order'
The purpose of me doing it within get_data()
is that I need to create charts as consumer_groups (kafka) come and go and by restricting it to check()
I’ll need to restart netdata every time a consumer_group is created in order to create the chart.