1

我正在尝试创建一个(golang)结构来处理通过 http 发送的 bosun 警报。它保存警报详细信息,最重要的是,有关eth0相应主机的 IP 地址。

/* This struct is defined as per the notification defined in bosun config */
type BosunAlert struct {
    AckUrl       string   `json:"ack"`
    AlertName    string   `json:"alert_name"`
    LastStatus   string   `json:"last_status"`
    IpMac        []string `json:"ip,omitempty"`
}

对应的模板如下所示:

template template.bosun_listener {
    subject = `{
        "ack":"{{.Ack}}",
        "alert_name":"{{.Alert.Name}}",
        "last_status":"{{.Last.Status}}",
        "ip":{{ .GetMeta "" "addresses" (printf "host=%s,iface=eth0" .Group.host) }}
    }`
    body = ``
}

但是,我收到此错误:

info: check.go:305: alert.network{host=147210308125790,ifName=server-1609}:
template: template.bosun_listener:6:12: executing "template.bosun_listener" at 
<.GetMeta>: error calling GetMeta: redigo: nil returned

我在 IpMac 字段中使用 [] 字符串,因为我无法将 eth0 IP 与其以太网地址隔离开来。

有什么办法可以做到这一点?

编辑:这是我得到的输出:

"ack":"http://localhost:8070/action?
key=alert.network%7Bhost%3D147210308125790%2CifName%3server-1609%7D&type=ack",
"alert_name":"alert.network", "last_status":"critical", "ip":
["172.31.40.31/20","fe80::61:adff:feb1:1f5b/64"] }

这是我配置的警报:

alert alert.network {
    runEvery = 5
    $ip = ""
    template = template.bosun_listener
    $usage = avg(q("avg:rate:container.net.bytes{host=*,ifName=server*}", "5m", ""))
    crit = $usage > 100
    critNotification = notification.test
}
4

1 回答 1

2

您确定有问题的主机是 eth0 设备(并且 bosun 已索引该元数据)吗?nil 表示找不到条目。

以下对我有用:

template test {
    subject = {{ .GetMeta "" "addresses" (printf "host=%s,iface=eth0" .Group.host) }}
}
于 2016-09-06T16:08:59.620 回答