示例#1
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
}
示例#2
0
	// 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())
				send(metric, carbonAddr)
			}
		}

	}

}

func send(metric string, carbonAddr string) {
	conn, err := net.Dial("tcp", carbonAddr)