Grafana - mysql中没有数据时的间隙
Posted
技术标签:
【中文标题】Grafana - mysql中没有数据时的间隙【英文标题】:Grafana - gap when no data in mysql 【发布时间】:2018-09-03 17:11:10 【问题描述】:我创建了 mysql 数据库,其中包含有关 CPU、RAM、磁盘使用情况等的数据……我创建了图表,但我注意到当没有新数据时,会显示最后一个值。这意味着如果我使用了 50% 的 CPU 而不是关闭服务器,grafana 仍将显示 50% 的使用率。
这是我在 grafana 中的查询:
SELECT
UNIX_TIMESTAMP(date) as time_sec,
cpu as value,
'server' as metric
FROM server
这就是我在 monitoring.sh 中的查询的样子:
INSERT INTO database
VALUES ('$datetime', $cpu_percentage, $mem, $swap, $load1, $load5, $load15, $disk)
以值为例:
INSERT INTO database
VALUES ('2018-03-25 14:05:41', 9.69, 56.65, 0.00, 1.52, 1.72, 1.82, 20)
当没有新值出现时,我希望有一个间隙,因为例如服务器已关闭。
有没有办法配置 grafana 以达到这种状态?这可能吗?
我正在使用 Grafana v5.0.3。
已编辑:
"lines": true,
"linewidth": 1,
"links": [],
"nullPointMode": "null",
"percentage": false,
"pointradius": 0.5,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
"alias": "",
"format": "time_series",
"rawSql": "SELECT\n UNIX_TIMESTAMP(date) as time_sec,\n cpu as value,\n 'server' as metric\nFROM server\n\n",
"refId": "A"
,
"alias": "",
"format": "time_series",
"rawSql": "SELECT\n UNIX_TIMESTAMP(date) as time_sec,\n cpu as value,\n 'raspberrypi' as metric\nFROM raspberrypi\n\n",
"refId": "B"
,
"alias": "",
"format": "time_series",
"rawSql": "SELECT\n UNIX_TIMESTAMP(date) as time_sec,\n cpu as value,\n 'dellpc' as metric\nFROM dellpc\n\n",
"refId": "C"
],
"thresholds": [
"colorMode": "critical",
"fill": true,
"line": true,
"op": "gt",
"value": 70
],
"timeFrom": null,
"timeShift": null,
"title": "CPU usage",
"tooltip":
"shared": true,
"sort": 0,
"value_type": "individual"
,
"transparent": true,
"type": "graph",
"xaxis":
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
,
"yaxes": [
"format": "percent",
"label": "Usage",
"logBase": 1,
"max": null,
"min": null,
"show": true
,
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": false
]
,
"aliasColors":
"dellpc": "#0a50a1",
"raspberrypi": "#962d82",
"test": "#fce2de"
,
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": null,
"decimals": 2,
"fill": 1,
"gridPos":
"h": 11,
"w": 12,
"x": 12,
"y": 0
,
"id": 6,
"legend":
"alignAsTable": true,
"avg": true,
"current": true,
"hideEmpty": false,
"hideZero": false,
"max": true,
"min": true,
"show": true,
"total": false,
"values": true
,
【问题讨论】:
图表的指标设置是什么? 我在从 grafana 获得的 json 中添加了配置。 您是否尝试在面板配置中使用空值“已连接”?由于您没有空值,因此这将忽略间隙并连接非零值。 Grafana 中的默认设置是针对这种情况显示间隙,这就是 Null Value 字段的默认值为 null 的原因。你能显示你的图表的截图吗?此外,请注意您没有时间过滤查询 - 这与您的问题无关,但意味着您返回的数据比您在图表上查看的数据多。 【参考方案1】:更改:"nullPointMode": "null"
为 "nullPointMode": "null as zero"
【讨论】:
它不起作用,因为我在数据库表中没有空值。 你试过this回答吗?但不是'未找到'返回 '0' ?还要检查this issue,它说您的查询应该:>“查询需要按时间分组并为丢失的数据点返回 null/0”【参考方案2】:试试下面的查询
select $__timegroup(date,'24h',0) as time, cpu as value,'server' as metric
FROM server
group by $__timegroup(date,'24h',0)
【讨论】:
这个查询,添加了“Change: "nullPointMode": "null" to "nullPointMode": "null as zero"" 应该是公认的答案。以上是关于Grafana - mysql中没有数据时的间隙的主要内容,如果未能解决你的问题,请参考以下文章