Exemple #1
0
func main() {

	// Default is a pointer to a GoSNMP struct that contains sensible defaults
	// eg port 161, community public, etc
	g.Default.Target = "192.168.1.10"
	err := g.Default.Connect()
	if err != nil {
		log.Fatalf("Connect() err: %v", err)
	}
	defer g.Default.Conn.Close()

	oids := []string{"1.3.6.1.2.1.1.4.0", "1.3.6.1.2.1.1.7.0"}
	result, err2 := g.Default.Get(oids) // Get() accepts up to g.MAX_OIDS
	if err2 != nil {
		log.Fatalf("Get() err: %v", err2)
	}

	for i, variable := range result.Variables {
		fmt.Printf("%d: oid: %s ", i, variable.Name)

		// the Value of each variable returned by Get() implements
		// interface{}. You could do a type switch...
		switch variable.Type {
		case g.OctetString:
			fmt.Printf("string: %s\n", variable.Value.(string))
		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", g.ToBigInt(variable.Value))
		}
	}
}
Exemple #2
0
func printValue(pdu gosnmp.SnmpPDU) error {
	fmt.Printf("%s = ", pdu.Name)

	switch pdu.Type {
	case gosnmp.OctetString:
		fmt.Printf("STRING: %s\n", pdu.Value.(string))
	default:
		fmt.Printf("TYPE %d: %d\n", pdu.Type, gosnmp.ToBigInt(pdu.Value))
	}
	return nil
}
Exemple #3
0
func printValue(pdu gosnmp.SnmpPDU) error {
	fmt.Printf("%s = ", pdu.Name)
	dec := mahonia.NewDecoder("gbk")
	switch pdu.Type {
	case gosnmp.OctetString:
		b := pdu.Value.([]byte)
		_, date, _ := dec.Translate(b, true)
		fmt.Printf("STRING: %s\n", string(date))
	case gosnmp.TimeTicks:
		b := gosnmp.ToBigInt(pdu.Value)
		t, _ := strconv.Atoi(fmt.Sprintf("%d", b))
		str_time := time.Unix(int64(t), 0).Format("Mon Jan 2 15:04:05.000 2006 +0800")
		fmt.Printf("TimeTicks: %d %s\n", b, str_time)
	case gosnmp.IPAddress:
		b := pdu.Value.(string)
		fmt.Printf("IpAddress: %s\n", string(b))
	default:
		fmt.Printf("TYPE %d: %d\n", pdu.Type, gosnmp.ToBigInt(pdu.Value))
	}
	return nil
}
Exemple #4
0
func getData(h host, oids []string, c chan bool) {
	fmt.Printf("This is getData, polling device %s. Oids are:\n", h.Addr)
	//spew.Dump(oids)
	var result []g.SnmpPDU
	result = make([]g.SnmpPDU, 1)

	g.Default.Target = h.Addr
	g.Default.Version = 0x1
	g.Default.Timeout = time.Duration(2) * time.Second
	fmt.Printf("DEBUG: community is %s\n", g.Default.Community)
	fmt.Printf("DEBUG: version is %v\n", g.Default.Version)
	err := g.Default.Connect()
	if err != nil {
		log.Fatalf("gosnmp.Connect err: %v", err)
		fmt.Printf("gosnmp.Connect err: %v", err)
	}
	runtime.Gosched()
	defer g.Default.Conn.Close()
	for _, o := range oids {
		fmt.Printf("About to BulkWalkAll %s on host %s\n", o, h.Addr)
		result, err = g.Default.BulkWalkAll(o)
		if err != nil {
			log.Fatalf("gosnmp.BulkWalkAll failed: %v", err)
			fmt.Printf("gosnmp.BulkWalkAll failed: %v", err)
		} else {
			//fmt.Printf("Results for oid ", o)
			//spew.Dump(result)
			for _, r := range result {
				v := g.ToBigInt(r.Value)
				fmt.Println("oid ", r.Name, ", value ", v)
				ndxs := strings.Split(r.Name[len(o)+1:], ",")
				ndxCount := 0
				tags := map[string]string{
					"host": h.Name,
					"site": h.Site,
					"name": oSlice[o].Name,
				}
				for i := range ndxs {
					tagName = fmt.Sprintf("ndx%d", ndxCount)
					ndxCount++
					tags[tagname] = i
				}
				fields := map[string]interface{}{
					"value": v,
				}
			}
		}
		//runtime.Gosched()
		//c <- true
	}
}
Exemple #5
0
func main() {

	// get Target and Port from environment
	envTarget := "192.168.1.105"
	envPort := "161"
	if len(envTarget) <= 0 {
		log.Fatalf("environment variable not set: GOSNMP_TARGET")
	}
	if len(envPort) <= 0 {
		log.Fatalf("environment variable not set: GOSNMP_PORT")
	}
	port, _ := strconv.ParseUint(envPort, 10, 16)

	// Build our own GoSNMP struct, rather than using g.Default.
	// Do verbose logging of packets.
	params := &g.GoSNMP{
		Target:    envTarget,
		Port:      uint16(port),
		Community: "public",
		Version:   g.Version2c,
		Timeout:   time.Duration(2) * time.Second,
		Logger:    log.New(os.Stdout, "", 0),
	}
	err := params.Connect()
	if err != nil {
		log.Fatalf("Connect() err: %v", err)
	}
	defer params.Conn.Close()

	oids := []string{"1.3.6.1.2.1.1.5.0"}
	result, err2 := params.Get(oids) // Get() accepts up to g.MAX_OIDS
	if err2 != nil {
		log.Fatalf("Get() err: %v", err2)
	}

	for i, variable := range result.Variables {
		fmt.Printf("%d: oid: %s ", i, variable.Name)

		// the Value of each variable returned by Get() implements
		// interface{}. You could do a type switch...
		switch variable.Type {
		case g.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("default: %v\n", g.ToBigInt(variable.Value))
		}
	}
}
Exemple #6
0
func printValue(pdu gosnmp.SnmpPDU) error {
	fmt.Printf("%s = ", pdu.Name)
	dec := mahonia.NewDecoder("gbk")
	switch pdu.Type {
	case gosnmp.OctetString:
		b := pdu.Value.([]byte)
		_, date, _ := dec.Translate(b, true)
		fmt.Printf("STRING: %s\n", string(date))
	case gosnmp.IPAddress:
		b := pdu.Value.(string)
		fmt.Printf("IpAddress: %s\n", string(b))
	default:
		fmt.Printf("TYPE %d: %d\n", pdu.Type, gosnmp.ToBigInt(pdu.Value))
	}
	return nil
}
Exemple #7
0
func pduToSample(indexOids []int, pdu *gosnmp.SnmpPDU, metric *Metric, oidToPdu map[string]gosnmp.SnmpPDU) prometheus.Metric {
	// The part of the OID that is the indexes.
	labels := indexesToLabels(indexOids, metric, oidToPdu)

	labelnames := make([]string, 0, len(labels))
	labelvalues := make([]string, 0, len(labels))
	for k, v := range labels {
		labelnames = append(labelnames, k)
		labelvalues = append(labelvalues, v)
	}
	return prometheus.MustNewConstMetric(prometheus.NewDesc(metric.Name, "", labelnames, nil),
		prometheus.UntypedValue,
		float64(gosnmp.ToBigInt(pdu.Value).Int64()),
		labelvalues...,
	)
}
Exemple #8
0
func main() {

	// build our own GoSNMP struct, rather than using g.Default
	params := &g.GoSNMP{
		Target:        "192.168.91.20",
		Port:          161,
		Version:       g.Version3,
		Timeout:       time.Duration(30) * time.Second,
		SecurityModel: g.UserSecurityModel,
		MsgFlags:      g.AuthPriv,
		SecurityParameters: &g.UsmSecurityParameters{UserName: "******",
			AuthenticationProtocol:   g.SHA,
			AuthenticationPassphrase: "password",
			PrivacyProtocol:          g.DES,
			PrivacyPassphrase:        "password",
		},
	}
	err := params.Connect()
	if err != nil {
		log.Fatalf("Connect() err: %v", err)
	}
	defer params.Conn.Close()

	oids := []string{"1.3.6.1.2.1.1.4.0", "1.3.6.1.2.1.1.7.0"}
	result, err2 := params.Get(oids) // Get() accepts up to g.MAX_OIDS
	if err2 != nil {
		log.Fatalf("Get() err: %v", err2)
	}

	for i, variable := range result.Variables {
		fmt.Printf("%d: oid: %s ", i, variable.Name)

		// the Value of each variable returned by Get() implements
		// interface{}. You could do a type switch...
		switch variable.Type {
		case g.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", g.ToBigInt(variable.Value))
		}
	}
}
Exemple #9
0
func getTable(target *gosnmp.GoSNMP, oid string) {
	results, err := gosnmp.Default.BulkWalkAll(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))
		}
	}
}
Exemple #10
0
func main() {

	// build our own GoSNMP struct, rather than using g.Default
	params := &g.GoSNMP{
		Target:    "192.168.1.10",
		Port:      161,
		Community: "public",
		Version:   g.Version2c,
		Timeout:   time.Duration(2) * time.Second,
	}
	err := params.Connect()
	if err != nil {
		log.Fatalf("Connect() err: %v", err)
	}
	defer params.Conn.Close()

	oids := []string{"1.3.6.1.2.1.1.4.0", "1.3.6.1.2.1.1.7.0"}
	result, err2 := params.Get(oids) // Get() accepts up to g.MAX_OIDS
	if err2 != nil {
		log.Fatalf("Get() err: %v", err2)
	}

	for i, variable := range result.Variables {
		fmt.Printf("%d: oid: %s ", i, variable.Name)

		// the Value of each variable returned by Get() implements
		// interface{}. You could do a type switch...
		switch variable.Type {
		case g.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", g.ToBigInt(variable.Value))
		}
	}
}