好吧,我发现了它是如何工作的,但我不得不说它在任何地方都没有记录,而且它涉及到对源代码的一些修改......
先来一点上下文
我有一个名为“Nagios”的 influxdb 数据库。在这个数据库里面,我有几个系列。 influxdb 中的一个节目系列显示以下内容
> show series
key
---
nagios.CPULoad,hostname=cbba.storage,state=OK
nagios.CPULoad,hostname=ussd1,state=OK
nagios.CPULoad,hostname=ussd2,state=OK
nagios.CPULoad,hostname=ussd3,state=OK
nagios.CPULoad,hostname=ussd4,state=OK
系列CPULoad中的数据结构是这样的
> select * from "nagios.CPULoad" limit 1
name: nagios.CPULoad
time hostname load1 load15 load5 state
---- -------- ----- ------ ----- -----
1487867813000000000 cbba.storage 0 0 0 OK
我的 scripted.js 网址如下:
http://10.72.6.220:3000/dashboard/script/scripted.js?name=CPULoad&field=load1&hostname=ussd3
name indicates the series in influxDB I want to graph
field indicates which field to use
hostname indicates the host to choose
我要grafana scripted.js构建的SQL如下
SELECT mean("load1") FROM "nagios.CPULoad" WHERE "hostname" = 'ussd3' AND $timeFilter GROUP BY time($interval) fill(null)
在scripted.js内部构建的代码涉及到修改dashboard.rows结构中的“targets”参数,结果是这样的(我是通过代码才发现的)
targets: [
{
"measurement": "nagios." + ARGS.name,
"metric": ARGS.name,
"tags": {
"hostname": {
operator: "=" ,
value: ARGS.hostname
}
},
"select": [[{
type: "field",
params: [ARGS.field]
}, {
type: "mean",
params: []
}]],
},
],
现在,我不知道为什么,但我必须修改代码才能考虑到键“主机名”。在函数 renderTagCondition 中,为方便起见,我在此处复制
a.prototype.renderTagCondition = function(a, b, c) {
var d = ""
, e = a.operator
, f = a.value;
return b > 0 && (d = (a.condition || "AND") + " "),
e || (e = /^\/.*\/$/.test(f) ? "=~" : "="),
"=~" !== e && "!~" !== e ? (c && (f = this.templateSrv.replace(f, this.scopedVars)),
">" !== e && "<" !== e && (f = "'" + f.replace(/\\/g, "\\\\") + "'")) : c && (f = this.templateSrv.replace(f, this.scopedVars, "regex")),
d + '"' + a.key + '" ' + e + " " + f
}
返回值
d + '"' + a.key + '" ' + e + " " + f
好像错了……应该是
d + '"' + b + '" ' + e + " " + f
因为 b 带有“主机名”
毕竟,调用我一开始提到的 URL 一切都很好