Graph 或 GraphAll 使用内联或动态查询

如果你不想使用 Alert 变量,可以内联定义图形查询。

template graph.template {
    subject = ...

    body = `{{template "header" .}}

    <strong>Graph With Inline Query</strong>
    <div>{{.Graph "q(\"avg:300s-avg:os.mem.percent_free{host=specifichost}\", \"1d\", \"\")" "Free Memory in GB"}}</div>

    <strong>GraphAll with Inline Query</strong>
    <div>{{.GraphAll "q(\"avg:300s-avg:os.mem.percent_free{host=host1|host2|host3}\", \"1d\", \"\")" "All Systems Free Memory in GB"}}</div>

    `
}

有时,你可能希望通过组合一个或多个变量在模板本身中动态创建图形查询。例如,主机关闭警报可能希望使用 dst_host 标记包含 Bosun 已知主机 ping 指标。

template host.down {
    subject = ...

    body = `{{template "header" .}}

    <strong>Graph from one variable</strong>
    <div>{{printf "q(\"sum:bosun.ping.timeout{dst_host=%s}\", \"8h\", \"\")" (.Group.host) | .Graph}}</div>

    <strong>Graph from multiple variables</strong>
    <div>{{printf "q(\"sum:%s{host=%s,anothertag=%s}\", \"8h\", \"\")" "some.metric.name" .Group.host "anothervalue" | .Graph}}</div>
    `
}

当该主机触发警报时,printf 语句将生成 q("sum:bosun.ping.timeout{dst_host=alerthostname}", "8h", ""),然后使用该语句在通知中创建图形。