Bosun - 使用.GetMeta获取主机的IP地址

时间:2016-09-06 15:38:58

标签: go bosun scollector

我正在尝试创建一个(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
}

1 个答案:

答案 0 :(得分:2)

您确定有问题的主机是eth0设备(并且bosun已将该元数据编入索引)吗?零意味着它无法找到该条目。

以下适用于我:

template test {
    subject = {{ .GetMeta "" "addresses" (printf "host=%s,iface=eth0" .Group.host) }}
}