# Creating dynamic charts within plugin get\_data() method

**URL:** <https://community.netdata.cloud/t/creating-dynamic-charts-within-plugin-get-data-method/957>\
**Category:** Help\
**Tags:** agent-collector, agent\
**Created:** [February 19, 2021, 4:02pm UTC](https://community.netdata.cloud/t/creating-dynamic-charts-within-plugin-get-data-method/957 "2021-02-19T16:02:33Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjtice](https://yyz1.discourse-cdn.com/flex029/user_avatar/community.netdata.cloud/mjtice/32/510_2.png) [@mjtice](https://community.netdata.cloud/u/mjtice)\
**Post date:** [February 19, 2021, 4:02pm UTC](https://community.netdata.cloud/t/creating-dynamic-charts-within-plugin-get-data-method/957/1 "2021-02-19T16:02:33Z")

</div>

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:

```nohighlight
  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.

---

<div class="post-metadata">

**Author:** ![andrewm4894](https://yyz1.discourse-cdn.com/flex029/user_avatar/community.netdata.cloud/andrewm4894/32/125_2.png) [@andrewm4894](https://community.netdata.cloud/u/andrewm4894)\
**Post date:** [February 21, 2021, 12:57pm UTC](https://community.netdata.cloud/t/creating-dynamic-charts-within-plugin-get-data-method/957/2 "2021-02-21T12:57:26Z")

</div>

Hello!

It sure is possible, I actually had a need to do something similar recently and did it like this

> <https://github.com/andrewm4894/netdata/blob/ab4864152040f4f4219c4ac106cc7b0015a4f7d1/collectors/python.d.plugin/aggregator/aggregator.chart.py#L46>

Basically the charts object has add\_chart() and add\_dimension() methods you can use.

Let me know if anything I can do to help.

---

<div class="post-metadata">

**Author:** ![andrewm4894](https://yyz1.discourse-cdn.com/flex029/user_avatar/community.netdata.cloud/andrewm4894/32/125_2.png) [@andrewm4894](https://community.netdata.cloud/u/andrewm4894)\
**Post date:** [February 21, 2021, 1:00pm UTC](https://community.netdata.cloud/t/creating-dynamic-charts-within-plugin-get-data-method/957/3 "2021-02-21T13:00:49Z")

</div>

You can also remove dimensions as needed in case useful, here is an example of that [netdata/alarms.chart.py at 292dea4e23fb381059fcc87da07b9c840ce2cb4e · netdata/netdata · GitHub](https://github.com/netdata/netdata/blob/292dea4e23fb381059fcc87da07b9c840ce2cb4e/collectors/python.d.plugin/alarms/alarms.chart.py#L71)

Your kafka use case sounds quite interesting, I’d love to hear more about it.

---

<div class="post-metadata">

**Author:** ![andrewm4894](https://yyz1.discourse-cdn.com/flex029/user_avatar/community.netdata.cloud/andrewm4894/32/125_2.png) [@andrewm4894](https://community.netdata.cloud/u/andrewm4894)\
**Post date:** [February 21, 2021, 1:09pm UTC](https://community.netdata.cloud/t/creating-dynamic-charts-within-plugin-get-data-method/957/4 "2021-02-21T13:09:43Z")

</div>

Adding link to this issue too as seem like a collector for kafka of some sort is something the community would find very useful.

> <https://github.com/netdata/netdata/issues/4819>
>
> We are willing to support monitoring \*\*\*\[Kafka\](https://kafka.apache.org/)\*\*\* wi…th netdata.
> 
> 
> To get started, we need a user that will act as a \[sponsor\](https://docs.netdata.cloud/contributing/#sponsor-a-part-of-netdata) during the implementation.
> He/she will assist us with the following:
> 
> - Specify the metrics required and how they should be presented in charts.
> - Specify the alarms that would make sense for each metric.
> - When the implementation passes QA, test the implementation in production.
> - Use the charts and alarms in their day to day work and provide additional feedback after the collector is delivered.
> 
> We would also appreciate getting votes :+1: for this issue from the community, to understand the interest in the particular collector.

---

<div class="post-metadata">

**Author:** ![andrewm4894](https://yyz1.discourse-cdn.com/flex029/user_avatar/community.netdata.cloud/andrewm4894/32/125_2.png) [@andrewm4894](https://community.netdata.cloud/u/andrewm4894)\
**Post date:** [February 21, 2021, 1:14pm UTC](https://community.netdata.cloud/t/creating-dynamic-charts-within-plugin-get-data-method/957/5 "2021-02-21T13:14:33Z")

</div>

@mjtice does it work if you just comment out the line that says self.order.insert()? Just wondering if it would work and then just order them itself behind the scenes as the charts get added?

---

<div class="post-metadata">

**Author:** ![mjtice](https://yyz1.discourse-cdn.com/flex029/user_avatar/community.netdata.cloud/mjtice/32/510_2.png) [@mjtice](https://community.netdata.cloud/u/mjtice)\
**Post date:** [February 22, 2021, 2:36pm UTC](https://community.netdata.cloud/t/creating-dynamic-charts-within-plugin-get-data-method/957/6 "2021-02-22T14:36:24Z")

</div>

Hi Andrew, it then fails on the next line:

```auto
self.definitions[cn] = {
                    'options': [None, 'Title', 'Unit', cf, cf, 'line'],
                    'lines': []
                }

```

```auto
AttributeError: 'Service' object has no attribute 'definitions'

```

---

<div class="post-metadata">

**Author:** ![ilyam8](https://yyz1.discourse-cdn.com/flex029/user_avatar/community.netdata.cloud/ilyam8/32/134_2.png) [@ilyam8](https://community.netdata.cloud/u/ilyam8)\
**Post date:** [February 22, 2021, 6:03pm UTC](https://community.netdata.cloud/t/creating-dynamic-charts-within-plugin-get-data-method/957/7 "2021-02-22T18:03:00Z")

</div>

`self.order` and `self.definitions` attributes are being deleted during `SimpleService.create()`

> <https://github.com/netdata/netdata/blob/e1f5a8db556265c9cebfb844ecd3887e559e3c0a/collectors/python.d.plugin/python_modules/bases/FrameworkServices/SimpleService.py#L174-L175>

If there are any dynamic/charts dimensions we need to use `self.charts` (populated with charts in `SimpleService.create()`)

- Dynamic charts (charts object):  
[netdata/charts.py at e1f5a8db556265c9cebfb844ecd3887e559e3c0a · netdata/netdata · GitHub](https://github.com/netdata/netdata/blob/e1f5a8db556265c9cebfb844ecd3887e559e3c0a/collectors/python.d.plugin/python_modules/bases/charts.py#L119-L137)

- Dynamic dimensions (chart object):

> <https://github.com/netdata/netdata/blob/e1f5a8db556265c9cebfb844ecd3887e559e3c0a/collectors/python.d.plugin/python_modules/bases/charts.py#L194-L225>

---

<div class="post-metadata">

**Author:** ![ilyam8](https://yyz1.discourse-cdn.com/flex029/user_avatar/community.netdata.cloud/ilyam8/32/134_2.png) [@ilyam8](https://community.netdata.cloud/u/ilyam8)\
**Post date:** [February 22, 2021, 6:09pm UTC](https://community.netdata.cloud/t/creating-dynamic-charts-within-plugin-get-data-method/957/8 "2021-02-22T18:09:56Z")

</div>

Think of `ORDER` and `CHARTS` (`self.order` and `self.definitions`) as raw data that is being used to create charts/chart objects during init. Later we don’t use them.

It is ~~a bit~~ confusing because python.d was refactored (new behaviour was added on top of existing, not instead of) at some point, but we had maintain backward compatibility to not break any custom collector.

Lack of good documentation is another problem which needs to be fixed.

---

<div class="post-metadata">

**Author:** ![mjtice](https://yyz1.discourse-cdn.com/flex029/user_avatar/community.netdata.cloud/mjtice/32/510_2.png) [@mjtice](https://community.netdata.cloud/u/mjtice)\
**Post date:** [February 22, 2021, 6:31pm UTC](https://community.netdata.cloud/t/creating-dynamic-charts-within-plugin-get-data-method/957/9 "2021-02-22T18:31:12Z")

</div>

I see. Thanks, @ilyam8

---

<div class="post-metadata">

**Author:** ![mjtice](https://yyz1.discourse-cdn.com/flex029/user_avatar/community.netdata.cloud/mjtice/32/510_2.png) [@mjtice](https://community.netdata.cloud/u/mjtice)\
**Post date:** [February 22, 2021, 6:32pm UTC](https://community.netdata.cloud/t/creating-dynamic-charts-within-plugin-get-data-method/957/10 "2021-02-22T18:32:29Z")

</div>

Thanks @andrewm4894 . I took this snippet of yours and integrated it into my script.

---

<div class="post-metadata">

**Author:** ![mjtice](https://yyz1.discourse-cdn.com/flex029/user_avatar/community.netdata.cloud/mjtice/32/510_2.png) [@mjtice](https://community.netdata.cloud/u/mjtice)\
**Post date:** [February 22, 2021, 6:34pm UTC](https://community.netdata.cloud/t/creating-dynamic-charts-within-plugin-get-data-method/957/11 "2021-02-22T18:34:47Z")

</div>

Essentially I’m just trying to track consumer\_lag (nothing more at this point).

I started writing the plugin using the kafka-python lib and creating admin/consumer connections. This ended up being a little too slow and cumbersome because I had to reconnect the consumers on each iteration.

I pivoted to just using the REST interface bundled with [GitHub - yahoo/CMAK: CMAK is a tool for managing Apache Kafka clusters](https://github.com/yahoo/CMAK). It makes for much shorter and easier-to-read code.

---

<div class="post-metadata">

**Author:** ![andrewm4894](https://yyz1.discourse-cdn.com/flex029/user_avatar/community.netdata.cloud/andrewm4894/32/125_2.png) [@andrewm4894](https://community.netdata.cloud/u/andrewm4894)\
**Post date:** [February 22, 2021, 10:21pm UTC](https://community.netdata.cloud/t/creating-dynamic-charts-within-plugin-get-data-method/957/12 "2021-02-22T22:21:57Z")

</div>

Cool - let us know how you get on - would be great to make a PR for whatever collector you build as i’m sure other kafka users (who are using CMAK) might find it very useful.

I’d be happy to try help any way i can.

---

<div class="post-metadata">

**Author:** ![OdysLam](https://yyz1.discourse-cdn.com/flex029/user_avatar/community.netdata.cloud/odyslam/32/98_2.png) [@OdysLam](https://community.netdata.cloud/u/OdysLam)\
**Post date:** [February 23, 2021, 1:44pm UTC](https://community.netdata.cloud/t/creating-dynamic-charts-within-plugin-get-data-method/957/13 "2021-02-23T13:44:57Z")

</div>

That sounds awesome @mjtice! Welcome to our community 🙂

Thanks @ilyam8 and @andrewm4894 for the tips. A new python collector guide is underway, hopefully, it will be out shortly!

In the meantime, @mjtice or any other user has any questions about creating a custom python collector, we are all ears!

Interestingly, although we have questions every now and then about our python collector, we don’t hear that often about `go.d` @ilyam8 . Lack of evidence is of-course not evidence of lack, but it’s an interesting observation.

Cheers!
