示例#1
0
文件: snmp.go 项目: qindj/influxsnmp
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
}
示例#2
0
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{}
}
示例#3
0
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
}
示例#4
0
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,
	}
}
示例#5
0
文件: snmp.go 项目: qindj/influxsnmp
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
}
示例#6
0
文件: snmp.go 项目: ozym/dmc
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
}
示例#7
0
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))
		}
	}
}
示例#8
0
文件: snmp.go 项目: ozym/dmc
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
}
示例#9
0
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)),
	}
}
示例#10
0
// 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
}
示例#11
0
)

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())
示例#12
0
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
}