帶有可選圖表的通用模板

在首次建立新警報時使用通用模板通常更快,而在需要顯示更多資訊時僅對模板進行專門化。以下模板將顯示具有數值,自定義格式和描述字串的主題,然後顯示最多包含兩個圖形的主體。如果未指定圖形變數,則它將列出警報中使用的計算。通用模板還使用警報的名稱來生成主題(用空格替換點)並在使用它們之前檢查變數以防止錯誤。

#See Embedded Templates and CSS Styles example for header template
template header { ... } 

template computation {
    body = `
    <p><strong>Computation</strong>
    <table>
        {{range .Computations}}
            <tr><td><a href="{{$.Expr .Text}}">{{.Text}}</a></td><td>{{.Value}}</td></tr>
        {{end}}
    </table></p>`
}

template generic_template {
    subject = {{.Last.Status}}: {{replace .Alert.Name "." " " -1}}: {{if .Alert.Vars.value}}{{if .Alert.Vars.value_format}}{{.Eval .Alert.Vars.value | printf .Alert.Vars.value_format}}{{else}}{{.Eval .Alert.Vars.value | printf "%.1f"}}{{end}}{{end}}{{if .Alert.Vars.value_string}}{{.Alert.Vars.value_string}}{{end}}{{if .Group.host}} on {{.Group.host}}{{end}}
    
    body = `{{template "header" .}}

    {{if or .Alert.Vars.generic_graph .Alert.Vars.generic_graph_all}}
        <strong>Graph</strong>
        {{if and .Alert.Vars.graph_unit .Alert.Vars.generic_graph}}
            <div>{{.Graph .Alert.Vars.generic_graph .Alert.Vars.graph_unit}}</div>
        {{else if .Alert.Vars.generic_graph}}
            <div>{{.Graph .Alert.Vars.generic_graph}}</div>
        {{end}}
        {{if and .Alert.Vars.graph_unit2 .Alert.Vars.generic_graph2}}
            <div>{{.Graph .Alert.Vars.generic_graph2 .Alert.Vars.graph_unit2}}</div>
        {{else if .Alert.Vars.generic_graph2}}
            <div>{{.Graph .Alert.Vars.generic_graph2}}</div>
        {{end}}
        {{if and .Alert.Vars.generic_graph_all .Alert.Vars.graph_unit}}
            <div>{{.GraphAll .Alert.Vars.generic_graph_all .Alert.Vars.graph_unit}}</div>
        {{else if .Alert.Vars.generic_graph_all}}
            <div>{{.GraphAll .Alert.Vars.generic_graph_all}}</div>
        {{end}}
        {{if and .Alert.Vars.generic_graph_all2 .Alert.Vars.graph_unit2}}
            <div>{{.GraphAll .Alert.Vars.generic_graph_all2 .Alert.Vars.graph_unit2}}</div>
        {{else if .Alert.Vars.generic_graph_all2}}
            <div>{{.GraphAll .Alert.Vars.generic_graph_all2}}</div>
        {{end}}
    {{else}}
        {{template "computation" .}}
    {{end}}`
}

alert puppet.last.run {
    template = generic_template
    $timethreshold = 60
    $timegraph = 24h
    $notes = Checks if puppet has not run in at least ${timethreshold} minutes. Doesn't include hosts which have puppet disabled.
    
    $generic_graph = q("sum:300s-max:puppet.last_run{host=*}", "$timegraph", "") / 60
    $graph_unit = Minutes since Last Puppet Run
    $generic_graph2 = q("sum:300s-max:puppet.disabled{host=*}", "$timegraph", "")
    $graph_unit2 = Puppet Disabled=1 Enabled=0
    
    $value = last(q("sum:puppet.last_run{host=*}", "6h", "")) / 60
    $value_format = It has been %.0f
    $value_string = ` minutes since last run`
    $disabled = max(q("sum:puppet.disabled{host=*}", "60m", ""))
    warn = ($value > $timethreshold) && ! $disabled
    warnNotification = default
    runEvery = 15
}

這將產生一個主題,如“警告:傀儡上次執行:自上次執行 co-lb04 後已經過了 62 分鐘”,幷包含該主機的 last_run 和禁用圖表。如果要繪製查詢的所有結果而不是匹配的標記集,可以使用 $ generic_graph_all$ generic_graph_all2 作為變數名稱。