Skip to content

Commit

Permalink
snmp插件增加up指标 (#590)
Browse files Browse the repository at this point in the history
* 增加whois插件,用于探测域名到期时间

* 增加初始化函数

* snmp增加up指标

* Update instances.go

---------

Co-authored-by: kongfei605 <kongfei605@gmail.com>
  • Loading branch information
tianyanli and kongfei605 committed Aug 9, 2023
1 parent faf8a99 commit 8a6090a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions inputs/snmp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ forked from [telegraf/snmp](https://github.com/influxdata/telegraf/tree/master/p

目前只修改了netsnmp的部分 ,配置中为了兼容,保留了path参数。

snmp_up代表设备是否存活,1 存活 0不存活,依赖ICMP

配置示例
```
[[instances]]
Expand Down
48 changes: 48 additions & 0 deletions inputs/snmp/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package snmp
import (
"fmt"
"log"
"net"
"sync"
"time"

"github.com/freedomkk-qfeng/go-fastping"
"github.com/gosnmp/gosnmp"

"flashcat.cloud/categraf/config"
Expand Down Expand Up @@ -106,6 +109,18 @@ func (ins *Instance) Gather(slist *types.SampleList) {
log.Printf("agent %s ins: gathering table %s error: %s", agent, t.Name, err)
}
}
//进行icmp探测

up := Ping(gs.Host(), 300)
fields := map[string]interface{}{
"icmp_up": up,
}
tags := map[string]string{
ins.AgentHostTag: gs.Host(),
}

slist.PushSamples(inputName, fields, tags)

}(i, agent)
}
wg.Wait()
Expand Down Expand Up @@ -189,3 +204,36 @@ func (ins *Instance) getConnection(idx int) (snmpConnection, error) {

return gs, nil
}

func Ping(ip string, timeout int) float64 {
rtt, _ := fastPingRtt(ip, timeout)
if rtt == -1 {
return 0
}
return 1
}

func fastPingRtt(ip string, timeout int) (float64, error) {
var rt float64
rt = -1
p := fastping.NewPinger()
ra, err := net.ResolveIPAddr("ip4:icmp", ip)
if err != nil {
return -1, err
}
p.AddIPAddr(ra)
p.OnRecv = func(addr *net.IPAddr, rtt time.Duration) {
rt = float64(rtt.Nanoseconds()) / 1000000.0
//fmt.Printf("IP Addr: %s receive, RTT: %v\n", addr.String(), rtt)
}
p.OnIdle = func() {
//fmt.Println("finish")
}
p.MaxRTT = time.Millisecond * time.Duration(timeout)
err = p.Run()
if err != nil {
return -1, err
}

return rt, err
}

0 comments on commit 8a6090a

Please sign in to comment.