英文:
Bosun - Get IP address of host using .GetMeta
问题
我正在尝试创建一个用于处理通过HTTP发送的Bosun警报的(golang)结构体。它保存警报的详细信息,最重要的是关于相应主机的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字段中使用了[]string,因为我无法从以太网地址中分离出eth0 IP。
有什么方法可以解决这个问题吗?
编辑:
这是我得到的输出:
"ack":"http://localhost:8070/action?
key=alert.network%7Bhost%3D147210308125790%2CifName%3Dserver-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
}
英文:
I am trying to create a (golang) struct for handling bosun alerts sent over http. It holds alert details, most importantly, about the eth0
IP address of the corresponding host.
/* 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"`
}
The corresponding template looks as follows:
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 = ``
}
However, I get this error:
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
I am using a []string for IpMac field as I cannot isolate the eth0 IP from it's ethernet address.
Any way to do this?
EDIT:
This is the output I get:
"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"] }
This is the alert I have configured:
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
得分: 2
你确定所讨论的主机有一个名为eth0的设备(并且bosun已经索引了该元数据)吗?nil表示它找不到该条目。
以下是我使用的代码:
template test {
subject = {{ .GetMeta "" "addresses" (printf "host=%s,iface=eth0" .Group.host) }}
}
英文:
Are you sure that the host in question as an eth0 device (and bosun has indexed that metadata)? nil means it couldn't find the entry.
The following works for me:
template test {
subject = {{ .GetMeta "" "addresses" (printf "host=%s,iface=eth0" .Group.host) }}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论