• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang gosnmp.GoSNMP类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/soniah/gosnmp.GoSNMP的典型用法代码示例。如果您正苦于以下问题:Golang GoSNMP类的具体用法?Golang GoSNMP怎么用?Golang GoSNMP使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了GoSNMP类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: bulkStats

func bulkStats(snmp *gosnmp.GoSNMP, cfg *SnmpConfig) error {
	now := time.Now()
	if cfg == nil {
		log.Fatal("cfg is nil")
	}
	if cfg.Influx == nil {
		log.Fatal("influx cfg is nil")
	}
	bps := cfg.Influx.BP()
	addPacket := func(pdu gosnmp.SnmpPDU) error {
		val := bulkPoint(cfg, pdu)
		if val != nil && val.value != nil {
			pt := makePoint(cfg.Host, val, now)
			bps.Points = append(bps.Points, pt)
		}
		return nil
	}
	for i := 0; i < len(cfg.oids); i += 1 {
		cfg.incRequests()
		if err := snmp.BulkWalk(cfg.oids[i], addPacket); err != nil {
			errLog("SNMP (%s) get error: %s\n", cfg.Host, err)
			cfg.incErrors()
			cfg.LastError = now
			return err
		}
	}
	cfg.Influx.Send(bps)
	return nil
}
开发者ID:qindj,项目名称:influxsnmp,代码行数:29,代码来源:snmp.go


示例2: getDiskUsage

func getDiskUsage(snmp g.GoSNMP) map[int64]float64 {
	result, err := snmp.GetBulk([]string{".1.3.6.1.2.1.25.2.3.1.1"}, 64, 64)
	if err != nil {
		log.Fatalf("Get() err: %v", err)
	}
	log.Println(result.Variables)
	return map[int64]float64{}
}
开发者ID:gr4y,项目名称:synology-graphite,代码行数:8,代码来源:disk_plugin.go


示例3: getTemperatures

func getTemperatures(snmp g.GoSNMP) map[int]float64 {
	result, err := snmp.Get([]string{".1.3.6.1.4.1.6574.2.1.1.6.0", ".1.3.6.1.4.1.6574.2.1.1.6.1"})
	if err != nil {
		log.Fatalf("Get() err: %v", err)
	}
	temps := map[int]float64{}
	for i, variable := range result.Variables {
		temps[i] = float64(variable.Value.(int))
	}
	return temps
}
开发者ID:gr4y,项目名称:synology-graphite,代码行数:11,代码来源:disk_plugin.go


示例4: FetchData

func (p LoadPlugin) FetchData(snmp g.GoSNMP) map[string]float64 {
	result, err := snmp.Get([]string{".1.3.6.1.4.1.2021.10.1.5.1", ".1.3.6.1.4.1.2021.10.1.5.2", ".1.3.6.1.4.1.2021.10.1.5.3"})
	if err != nil {
		log.Fatalf("Get() err: %v", err)
	}
	return map[string]float64{
		"load.shortterm": float64(result.Variables[0].Value.(int)) / 100,
		"load.midterm":   float64(result.Variables[1].Value.(int)) / 100,
		"load.longterm":  float64(result.Variables[2].Value.(int)) / 100,
	}
}
开发者ID:gr4y,项目名称:synology-graphite,代码行数:11,代码来源:load_plugin.go


示例5: snmpStats

func snmpStats(snmp *gosnmp.GoSNMP, cfg *SnmpConfig) error {
	now := time.Now()
	if cfg == nil {
		log.Fatal("cfg is nil")
	}
	if cfg.Influx == nil {
		log.Fatal("influx cfg is nil")
	}
	bps := cfg.Influx.BP()
	// we can only get 'maxOids' worth of snmp requests at a time
	for i := 0; i < len(cfg.oids); i += maxOids {
		end := i + maxOids
		if end > len(cfg.oids) {
			end = len(cfg.oids)
		}
		cfg.incRequests()
		pkt, err := snmp.Get(cfg.oids[i:end])
		if err != nil {
			errLog("SNMP (%s) get error: %s\n", cfg.Host, err)
			cfg.incErrors()
			cfg.LastError = now
			return err
		}
		cfg.incGets()
		if verbose {
			log.Println("SNMP GET CNT:", len(pkt.Variables))
		}
		//log.Println("PKT", pkt)
		for _, pdu := range pkt.Variables {
			val := getPoint(cfg, pdu)
			if val == nil {
				continue
			}
			if val.value == nil {
				continue
			}
			//log.Println("VAL", val)
			pt := makePoint(cfg.Host, val, now)
			//log.Println("PT", pt)
			bps.Points = append(bps.Points, pt)

		}
	}
	cfg.Influx.Send(bps)
	return nil
}
开发者ID:qindj,项目名称:influxsnmp,代码行数:46,代码来源:snmp.go


示例6: sysInterfaces

func sysInterfaces(snmp *gosnmp.GoSNMP) (map[int]sysInterface, error) {

	w, err := snmp.WalkAll(".1.3.6.1.2.1.2.2.1")
	if err != nil {
		return nil, err
	}

	ids := make(map[int]sysInterface)
	for _, v := range w {
		switch v.Type {
		case gosnmp.OctetString:
			switch {
			case strings.HasPrefix(v.Name, ".1.3.6.1.2.1.2.2.1.2"):
				ids[index(v.Name)] = sysInterface{Name: string(v.Value.([]byte))}
			}
		}
	}
	for _, v := range w {
		i, ok := ids[index(v.Name)]
		if !ok {
			continue
		}
		switch v.Type {
		case gosnmp.Integer:
			switch {
			case strings.HasPrefix(v.Name, ".1.3.6.1.2.1.2.2.1.3"):
				i.Type = v.Value.(int)
			}
		case gosnmp.Counter32:
			switch {
			case strings.HasPrefix(v.Name, ".1.3.6.1.2.1.2.2.1.10"):
				if n := (uint32)(v.Value.(uint)); true {
					i.RX = &n
				}
			case strings.HasPrefix(v.Name, ".1.3.6.1.2.1.2.2.1.16"):
				if n := (uint32)(v.Value.(uint)); true {
					i.TX = &n
				}
			}
		}
		ids[index(v.Name)] = i
	}

	return ids, nil
}
开发者ID:ozym,项目名称:dmc,代码行数:45,代码来源:snmp.go


示例7: getTable

func getTable(g *gosnmp.GoSNMP, oid string) {
	results, err := g.WalkAll(oid)
	if err != nil {
		log.Fatalf("Get() err: %v", err)
	}

	for _, variable := range results {
		fmt.Printf("oid: %s ", variable.Name[len(oid)+1:])

		// the Value of each variable returned by Get() implements
		// interface{}. You could do a type switch...
		switch variable.Type {
		case gosnmp.OctetString:
			fmt.Printf("string: %s\n", string(variable.Value.([]byte)))
		default:
			// ... or often you're just interested in numeric values.
			// ToBigInt() will return the Value as a BigInt, for plugging
			// into your calculations.
			fmt.Printf("number: %d\n", gosnmp.ToBigInt(variable.Value))
		}
	}
}
开发者ID:wathiede,项目名称:snmp_exporter,代码行数:22,代码来源:main.go


示例8: sysObjectID

func sysObjectID(snmp *gosnmp.GoSNMP) (*string, error) {
	r, err := snmp.Get([]string{
		".1.3.6.1.2.1.1.2.0",
	})
	if r == nil || err != nil {
		return nil, err
	}

	var oid *string
	for _, v := range r.Variables {
		switch v.Type {
		case gosnmp.ObjectIdentifier:
			switch v.Name {
			case ".1.3.6.1.2.1.1.2.0":
				if a := v.Value.(string); a != "" {
					oid = &a
				}
			}
		}
	}

	return oid, nil
}
开发者ID:ozym,项目名称:dmc,代码行数:23,代码来源:snmp.go


示例9: FetchData

func (p CPUPlugin) FetchData(snmp g.GoSNMP) map[string]float64 {
	oids := []string{
		".1.3.6.1.4.1.2021.11.50.0",
		".1.3.6.1.4.1.2021.11.51.0",
		".1.3.6.1.4.1.2021.11.52.0",
		".1.3.6.1.4.1.2021.11.53.0",
		".1.3.6.1.4.1.2021.11.54.0",
		".1.3.6.1.4.1.2021.11.55.0",
		".1.3.6.1.4.1.2021.11.56.0",
	}
	result, err := snmp.Get(oids)
	if err != nil {
		log.Fatalf("Get() err: %v", err)
	}
	return map[string]float64{
		"cpu-0.cpu-user":      float64(result.Variables[0].Value.(uint)),
		"cpu-0.cpu-nice":      float64(result.Variables[1].Value.(uint)),
		"cpu-0.cpu-system":    float64(result.Variables[2].Value.(uint)),
		"cpu-0.cpu-idle":      float64(result.Variables[3].Value.(uint)),
		"cpu-0.cpu-wait":      float64(result.Variables[4].Value.(uint)),
		"cpu-0.cpu-kernel":    float64(result.Variables[5].Value.(uint)),
		"cpu-0.cpu-interrupt": float64(result.Variables[6].Value.(uint)),
	}
}
开发者ID:gr4y,项目名称:synology-graphite,代码行数:24,代码来源:cpu_plugin.go


示例10: configureSNMP

// configureSNMP sets the various version and auth settings.
func (c Module) configureSNMP(g *gosnmp.GoSNMP) {
	switch c.Version {
	case 1:
		g.Version = gosnmp.Version1
	case 2:
		g.Version = gosnmp.Version2c
	case 3:
		g.Version = gosnmp.Version3
	}
	g.Community = c.Auth.Community

	// v3 security settings.
	g.SecurityModel = gosnmp.UserSecurityModel
	switch c.Auth.SecurityLevel {
	case "noAuthNoPriv":
		g.MsgFlags = gosnmp.NoAuthNoPriv
	case "authNoPriv":
		g.MsgFlags = gosnmp.AuthNoPriv
	case "authPriv":
		g.MsgFlags = gosnmp.AuthPriv
	}
	usm := &gosnmp.UsmSecurityParameters{
		UserName:                 c.Auth.Username,
		AuthenticationPassphrase: c.Auth.Password,
		PrivacyPassphrase:        c.Auth.PrivPassword,
	}
	switch c.Auth.AuthProtocol {
	case "SHA":
		usm.AuthenticationProtocol = gosnmp.SHA
	case "MD5":
		usm.AuthenticationProtocol = gosnmp.MD5
	}
	switch c.Auth.PrivProtocol {
	case "DES":
		usm.PrivacyProtocol = gosnmp.DES
	case "AES":
		usm.PrivacyProtocol = gosnmp.AES
	}
	g.SecurityParameters = usm
}
开发者ID:RichiH,项目名称:snmp_exporter,代码行数:41,代码来源:config.go


示例11:

)

var CmdFetchData = func(c *cli.Context) {
	diskStation := c.String("ds")
	interval := c.Duration("interval")
	prefix := c.String("prefix")

	carbonHost := c.String("carbon-host")
	carbonPort := c.String("carbon-port")
	carbonAddr := fmt.Sprintf("%s:%s", carbonHost, carbonPort)

	// SNMP Configuration
	snmp := g.GoSNMP{
		Target:    diskStation,
		Port:      161,
		Community: "public",
		Version:   g.Version2c,
		Timeout:   time.Duration(2) * time.Second,
	}

	// Plugins
	plugins := []p.Plugin{p.DiskPlugin{}, p.LoadPlugin{}, p.CPUPlugin{}} //, p.MemoryPlugin{}, p.NetworkPlugin{}}

	// Execute plugins every interval
	for now := range time.Tick(interval) {
		snmp.Connect()
		defer snmp.Conn.Close()
		for _, plugin := range plugins {
			data := plugin.FetchData(snmp)
			for key, value := range data {
				metric := fmt.Sprintf("%s.%s %v %d\n\r", prefix, key, value, now.Unix())
开发者ID:gr4y,项目名称:synology-graphite,代码行数:31,代码来源:fetch_data.go


示例12: ScrapeTarget

func ScrapeTarget(target string, config *Module) ([]gosnmp.SnmpPDU, error) {
	// Set the options.
	snmp := gosnmp.GoSNMP{}
	snmp.Retries = 3
	snmp.MaxRepetitions = 25
	snmp.Timeout = time.Second * 60

	snmp.Target = target
	snmp.Port = 161
	if host, port, err := net.SplitHostPort(target); err == nil {
		snmp.Target = host
		p, err := strconv.Atoi(port)
		if err != nil {
			return nil, fmt.Errorf("Error converting port number to int for target %s: %s", target, err)
		}
		snmp.Port = uint16(p)
	}

	// Configure auth.
	config.configureSNMP(&snmp)

	// Do the actual walk.
	err := snmp.Connect()
	if err != nil {
		return nil, fmt.Errorf("Error connecting to target %s: %s", target, err)
	}
	defer snmp.Conn.Close()

	result := []gosnmp.SnmpPDU{}
	for _, subtree := range config.Walk {
		var pdus []gosnmp.SnmpPDU
		if snmp.Version == gosnmp.Version1 {
			pdus, err = snmp.WalkAll(subtree)
		} else {
			pdus, err = snmp.BulkWalkAll(subtree)
		}
		if err != nil {
			return nil, fmt.Errorf("Error walking target %s: %s", snmp.Target, err)
		}
		result = append(result, pdus...)
	}
	return result, nil
}
开发者ID:RichiH,项目名称:snmp_exporter,代码行数:43,代码来源:collector.go



注:本文中的github.com/soniah/gosnmp.GoSNMP类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang cluster.DistanceMatrix类代码示例发布时间:2022-05-28
下一篇:
Golang ses.New函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap