Last active
April 3, 2017 03:31
-
-
Save toni-moreno/14c6fa16ef3b6ba6acb0035f58139298 to your computer and use it in GitHub Desktop.
concurrent-gonsmp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
l "github.com/Sirupsen/logrus" | |
g "github.com/soniah/gosnmp" | |
"log" | |
"sync" | |
"time" | |
) | |
func printValue(pdu g.SnmpPDU) error { | |
return nil | |
} | |
func gosnmpGather() { | |
oids := []string{".1.3", ".1.3", ".1.3", ".1.3"} | |
startSnmpStats := time.Now() | |
var wg sync.WaitGroup | |
for _, o := range oids { | |
snmp := initSnmp() | |
wg.Add(1) | |
go func(oid string, snmp *g.GoSNMP) { | |
defer wg.Done() | |
l.Infof("-------Processing oid: %s", oid) | |
err := snmp.BulkWalk(oid, printValue) | |
if err != nil { | |
l.Printf("Walk Error: %v\n", err) | |
return | |
} | |
}(o, snmp) | |
} | |
wg.Wait() | |
elapsedSnmpStats := time.Since(startSnmpStats) | |
l.Infof("snmp pooling took [%s] ", elapsedSnmpStats) | |
} | |
func initSnmp() *g.GoSNMP { | |
params := &g.GoSNMP{ | |
Target: "127.0.0.1", | |
Port: 161, | |
Version: g.Version3, | |
MaxRepetitions: 10, | |
Timeout: time.Duration(60) * time.Second, | |
SecurityModel: g.UserSecurityModel, | |
MsgFlags: g.AuthPriv, | |
SecurityParameters: &g.UsmSecurityParameters{ | |
UserName: "v3userpriv", | |
AuthenticationProtocol: g.MD5, | |
AuthenticationPassphrase: "v3passpriv", | |
PrivacyProtocol: g.DES, | |
PrivacyPassphrase: "v3passenc", | |
}, | |
} | |
err := params.Connect() | |
if err != nil { | |
log.Fatalf("Connect() err: %v", err) | |
} | |
// defer params.Conn.Close() | |
return params | |
} | |
func main() { | |
gosnmpGather() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment