Search code examples
dashboardgrafana

grafana scripted influxdb


I am using grafana 4 and influxDB.

I need to show a graph of say CPU usage for a certain host by building the parameters in the URL like this

http://my_grafana:3000/dashboard/script/scripted.js?name=CPULoad&host=ussd1

i am trying to use scripted dashboards for this but i cannot figure out how to tell scripted.js where to look for the data of CPULoad.

can anyone give me some pointers?

regards,

Martin


Solution

  • Well I found out how it works, but I have to say it is wierd that it is not documented anywhere and it involves a little modification to the source code...

    A little bit of context first

    I have a influxdb database called "Nagios". Inside this database, I have several series. a show series in influxdb shows the following

    > 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
    

    The structure of the data in series CPULoad is like this

    > select * from "nagios.CPULoad" limit 1
    name: nagios.CPULoad
    time                hostname     load1 load15 load5 state
    ----                --------     ----- ------ ----- -----
    1487867813000000000 cbba.storage 0     0      0     OK
    

    My URL to scripted.js is as follows:

    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
    

    The SQL I want grafana scripted.js to build is as follows

    SELECT mean("load1") FROM "nagios.CPULoad" WHERE "hostname" = 'ussd3' AND $timeFilter GROUP BY time($interval) fill(null)
    

    The code to build inside scripted.js involves modifying the "targets" parameter in dashboard.rows structure, and it turns out to be like this (i found this out after going through the code)

        targets: [
          {
            "measurement": "nagios." + ARGS.name,
            "metric": ARGS.name,
            "tags": {
                "hostname": {
                      operator: "=" ,
                      value: ARGS.hostname
                    }
            },
            "select": [[{
                        type: "field",
                        params: [ARGS.field]
                    }, {
                        type: "mean",
                        params: []
                    }]],
          },
        ],
    

    Now, I dont know why, but I had to modify the code in order for the key "hostname" to be taken into account. In function renderTagCondition which I copy here for convenience

    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
    }
    

    the returned value

    d + '"' + a.key + '" ' + e + " " + f
    

    seems to be wrong... It should be

    d + '"' + b + '" ' + e + " " + f
    

    since b carries "hostname"

    After all this, calling the URL I mentioned at the beginning it all worked out pretty well