Skip to content

Instantly share code, notes, and snippets.

@lyc8503
Last active July 14, 2024 13:43
Show Gist options
  • Save lyc8503/5698e5194001cbfc753dcdc2673f3afe to your computer and use it in GitHub Desktop.
Save lyc8503/5698e5194001cbfc753dcdc2673f3afe to your computer and use it in GitHub Desktop.
Netdata plugin for aggregating multiple metrics into one graph, mainly for custom dashboard
# -*- coding: utf-8 -*-
# Put this in /usr/libexec/netdata/python.d/
from bases.FrameworkServices.SimpleService import SimpleService
import requests
def fetch_latest():
return requests.get("http://192.168.1.10:19999/api/v1/allmetrics?filter=cgroup*&format=json").json()
NETDATA_UPDATE_EVERY = 1
priority = 1000
ORDER = [
'cgroup_cpu',
'cgroup_mem'
]
CHARTS = {
'cgroup_cpu': {
'options': ["cgroup_cpu", 'Containers & VMs CPU usage', 'percentage', 'cpu', 'cgroup.cpu', 'line'],
'lines': []
},
'cgroup_mem': {
'options': ["cgroup_mem", 'Containers & VMs memory usage', 'MiB', 'mem', 'cgroup.mem', 'line'],
'lines': []
},
}
class Service(SimpleService):
def __init__(self, configuration=None, name=None):
SimpleService.__init__(self, configuration=configuration, name=name)
self.order = ORDER
self.definitions = CHARTS
@staticmethod
def check():
return True
def get_data(self):
data = dict()
resp = fetch_latest()
for k in resp:
if k.endswith(".cpu"):
dim = k.replace("cgroup_", "")
if dim not in self.charts['cgroup_cpu']:
self.charts['cgroup_cpu'].add_dimension([dim, dim.replace(".cpu", ""), None, None, 100])
data[dim] = sum(map(lambda x: x["value"] * 100, resp[k]["dimensions"].values()))
if k.endswith(".mem"):
dim = k.replace("cgroup_", "")
if dim not in self.charts['cgroup_mem']:
self.charts['cgroup_mem'].add_dimension([dim, dim.replace(".mem", ""), None, None, 1000])
data[dim] = resp[k]["dimensions"]["anon"]["value"] * 1000
return data
# Also a dashboard.html
# Put it in /var/lib/netdata/www/mydashboard.html
"""
<!DOCTYPE html>
<html lang="en">
<head>
<title>HomeLab dashboard</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
</head>
<body>
Full dashboard => <a href="/v1/">v1</a> <a href="/">v2</a><br />
<div style="display: flex;">
<div data-netdata="system.cpu" data-dygraph-valuerange="[0, 100]" data-height="180"></div>
<div data-netdata="system.ram" data-height="180"></div>
</div>
<div style="display: flex;">
<div data-netdata="system.io" data-height="180"></div>
<div data-netdata="system.net" data-height="180"></div>
</div>
<div style="display: flex;">
<div data-netdata="aggregator.cgroup_cpu" data-height="180"></div>
<div data-netdata="aggregator.cgroup_mem" data-height="180"></div>
</div>
<div style="display: flex;">
<div data-netdata="miiot_power.power_usage" data-height="150"></div>
</div>
</body>
<script type="text/javascript" src="http://192.168.1.10:19999/dashboard.js"></script>
</html>
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment