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

huangapple go评论68阅读模式
英文:

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&amp;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:&quot;ack&quot;`
    AlertName    string   `json:&quot;alert_name&quot;`
    LastStatus   string   `json:&quot;last_status&quot;`
    IpMac        []string `json:&quot;ip,omitempty&quot;`
}

The corresponding template looks as follows:

template template.bosun_listener {
    subject = `{
        &quot;ack&quot;:&quot;{{.Ack}}&quot;,
        &quot;alert_name&quot;:&quot;{{.Alert.Name}}&quot;,
        &quot;last_status&quot;:&quot;{{.Last.Status}}&quot;,
        &quot;ip&quot;:{{ .GetMeta &quot;&quot; &quot;addresses&quot; (printf &quot;host=%s,iface=eth0&quot; .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 &quot;template.bosun_listener&quot; at 
&lt;.GetMeta&gt;: 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:

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

This is the alert I have configured:

alert alert.network {
    runEvery = 5
    $ip = &quot;&quot;
    template = template.bosun_listener
    $usage = avg(q(&quot;avg:rate:container.net.bytes{host=*,ifName=server*}&quot;, &quot;5m&quot;, &quot;&quot;))
    crit = $usage &gt; 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 &quot;&quot; &quot;addresses&quot; (printf &quot;host=%s,iface=eth0&quot; .Group.host) }}
}

huangapple
  • 本文由 发表于 2016年9月6日 23:38:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/39352921.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定