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", ""),然後使用該語句在通知中建立圖形。