func swMainData(w http.ResponseWriter, r *http.Request) { ip := r.URL.Path[12:] for i := range snmp_coms_r { s, err := gosnmp.NewGoSNMP(ip, snmp_coms_r[i], gosnmp.Version2c, 1) checkErr(err) resp, err := s.Get(".1.3.6.1.2.1.17.1.1.0") if err == nil { umac := fmt.Sprintf("%X", resp.Variables[0].Value) //fmt.Fprintf(w,"<html><head></head><body>%s<br>\n",snmp_coms_r[i]) fmt.Fprintf(w, "<table border=1><tr><td>MAC Address</td><td>%s</td></tr>\n", umac) resp, _ = s.Get(".1.3.6.1.2.1.1.1.0") fmt.Fprintf(w, "<tr><td>Device Type</td><td>%s</td></tr>\n", resp.Variables[0].Value.(string)) resp, _ = s.Get(".1.3.6.1.2.1.16.19.2.0") fmt.Fprintf(w, "<tr><td>Firmware Version</td><td>%s</td></tr>\n", resp.Variables[0].Value.(string)) resp, _ = s.Get(".1.3.6.1.2.1.16.19.3.0") fmt.Fprintf(w, "<tr><td>Hardware Version</td><td>%s</td></tr>\n", resp.Variables[0].Value.(string)) resp, _ = s.Get(".1.3.6.1.2.1.1.3.0") fmt.Fprintf(w, "<tr><td>System Uptime</td><td>%d</td></tr>\n", resp.Variables[0].Value) resp, _ = s.Get(".1.3.6.1.2.1.1.4.0") fmt.Fprintf(w, "<tr><td>System Contact</td><td>%s</td></tr>\n", resp.Variables[0].Value.(string)) resp, _ = s.Get(".1.3.6.1.2.1.1.5.0") fmt.Fprintf(w, "<tr><td>System Name</td><td>%s</td></tr>\n", resp.Variables[0].Value.(string)) resp, _ = s.Get(".1.3.6.1.2.1.1.6.0") fmt.Fprintf(w, "<tr><td>System Location</td><td>%s</td></tr>\n", resp.Variables[0].Value.(string)) resp, _ = s.Get(".1.3.6.1.4.1.171.12.15.2.3.1.21.0") stpt := time.Unix(int64(resp.Variables[0].Value.(int)/100), 0) fmt.Fprintf(w, "<tr><td>STP Last Change</td><td>%d %d:%d:%d</td></tr>\n", stpt.Day()-1, stpt.Hour(), stpt.Minute(), stpt.Second()) fmt.Fprintf(w, "</table>") return } } }
func swChec1Cable(w http.ResponseWriter, ip string, port string) { setSnmpInt(ip, ".1.3.6.1.4.1.171.12.58.1.1.1.12."+port, 1) for i := range snmp_coms_r { s, err := gosnmp.NewGoSNMP(ip, snmp_coms_r[i], gosnmp.Version2c, 1) checkErr(err) resp, err := s.Get(".1.3.6.1.4.1.171.12.58.1.1.1.12." + port) if err == nil { // fmt.Fprintf(w,"%d",resp.Variables[0].Value) fmt.Fprintf(w, "<tr><td>"+port+"</td>") resp, _ = s.Get(".1.3.6.1.4.1.171.12.58.1.1.1.3." + port) fmt.Fprintf(w, "<td>%d</td>", resp.Variables[0].Value) resp, _ = s.Get(".1.3.6.1.4.1.171.12.58.1.1.1.4." + port) fmt.Fprintf(w, "<td>%s</td>", cdStatus[resp.Variables[0].Value.(int)]) resp, _ = s.Get(".1.3.6.1.4.1.171.12.58.1.1.1.5." + port) fmt.Fprintf(w, "<td>%s</td>", cdStatus[resp.Variables[0].Value.(int)]) resp, _ = s.Get(".1.3.6.1.4.1.171.12.58.1.1.1.6." + port) fmt.Fprintf(w, "<td>%s</td>", cdStatus[resp.Variables[0].Value.(int)]) resp, _ = s.Get(".1.3.6.1.4.1.171.12.58.1.1.1.7." + port) fmt.Fprintf(w, "<td>%s</td>", cdStatus[resp.Variables[0].Value.(int)]) resp, _ = s.Get(".1.3.6.1.4.1.171.12.58.1.1.1.8." + port) fmt.Fprintf(w, "<td>%d</td>", resp.Variables[0].Value) resp, _ = s.Get(".1.3.6.1.4.1.171.12.58.1.1.1.9." + port) fmt.Fprintf(w, "<td>%d</td>", resp.Variables[0].Value) resp, _ = s.Get(".1.3.6.1.4.1.171.12.58.1.1.1.10." + port) fmt.Fprintf(w, "<td>%d</td>", resp.Variables[0].Value) resp, _ = s.Get(".1.3.6.1.4.1.171.12.58.1.1.1.11." + port) fmt.Fprintf(w, "<td>%d</td></tr>", resp.Variables[0].Value) return } } }
func getWanBytesPerSecond() (inBytes float64, outBytes float64, Error error) { snmp, err := gosnmp.NewGoSNMP("192.168.37.1", "home.hakshak.com", gosnmp.Version1, 10) if err != nil { return 0, 0, err } oids := []string{".1.3.6.1.2.1.2.2.1.10.8", ".1.3.6.1.2.1.2.2.1.16.8"} timeStart := time.Now() firstSample, err := snmp.GetMulti(oids) if err != nil { return 0, 0, err } secondSample, err := snmp.GetMulti(oids) if err != nil { return 0, 0, err } duration := time.Now().Sub(timeStart) firstIn := firstSample.Variables[0].Value.(int) secondIn := secondSample.Variables[0].Value.(int) inOctets := secondIn - firstIn inBytes = float64(inOctets) / duration.Seconds() //extrapolate per second firstOut := firstSample.Variables[1].Value.(int) secondOut := secondSample.Variables[1].Value.(int) outOctets := secondOut - firstOut outBytes = float64(outOctets) / duration.Seconds() //extrapolate per second return }
func (m SNMPPlugin) FetchMetrics() (map[string]float64, error) { stat := make(map[string]float64) s, err := gosnmp.NewGoSNMP(m.Host, m.Community, gosnmp.Version2c, 30) if err != nil { return nil, err } for _, sm := range m.SNMPMetricsSlice { resp, err := s.Get(sm.OID) if err != nil { log.Println("SNMP get failed: ", err) continue } ret, err := strconv.ParseFloat(fmt.Sprint(resp.Variables[0].Value), 64) if err != nil { log.Println(err) continue } stat[sm.Metrics.Name] = ret } return stat, err }
func main() { var err error // getopt flag.Usage = usage flag.Parse() if flag.NArg() != 2 { usage() os.Exit(2) } mountpoint := flag.Arg(0) snmpServer := flag.Arg(1) // connect snmp snmp.client, err = gosnmp.NewGoSNMP(snmpServer, "public", gosnmp.Version2c, 5) if err != nil { logrus.Fatalf("gosnmp.NewGoSNMP: %v", err) } //snmp.client.SetDebug(true) //snmp.client.SetVerbose(true) snmp.currentId = 1 snmp.cache = make(map[string]SnmpCacheEntry) snmp.cacheMap = make(map[uint64]string) // preload initial cache err = snmp.LoadWalk(".1.3.6.1") if err != nil { logrus.Fatalf("snmp.LoadWalk: %v", err) } // mount fuse c, err := fuse.Mount( mountpoint, fuse.FSName("fuse-snmp"), fuse.Subtype("snmpfs"), fuse.LocalVolume(), fuse.VolumeName("Fuse SNMP"), ) if err != nil { logrus.Fatalf("fuse.Mount: %v", err) } defer c.Close() // map fuse err = fs.Serve(c, FS{}) if err != nil { logrus.Fatalf("fs.Serve: %v", err) } // wait for fuse close <-c.Ready if err := c.MountError; err != nil { logrus.Fatalf("c.MountError: %v", err) } logrus.Fatalf("BYEBYE") }
func (rb *MikrotikSnmp) SnmpGetPDUList(oid string) ([]gosnmp.SnmpPDU, error) { s, err := gosnmp.NewGoSNMP(rb.Host, rb.Community, gosnmp.Version2c, 5) if err != nil { return nil, err } resp, err := s.Walk(oid) if err != nil { return nil, err } return resp, nil }
func main() { defer profile.Start(profile.CPUProfile).Stop() if cmdTarget == "" { flag.PrintDefaults() return } s, err := gosnmp.NewGoSNMP(cmdTarget, cmdCommunity, gosnmp.Version2c, cmdTimeout) s.SetVerbose(true) if err != nil { fmt.Printf("UNKNOWN: Error creating SNMP instance: %s\n", err.Error()) os.Exit(utils.UNKNOWN) } const totalSwapOid string = ".1.3.6.1.4.1.2021.4.3.0" const availSwapOid string = ".1.3.6.1.4.1.2021.4.4.0" resp, err := s.Get(totalSwapOid) if err != nil { fmt.Printf("UNKNOWN: Error getting response: %s\n", err.Error()) os.Exit(utils.UNKNOWN) } else { totalSwap = resp.Variables[0].Value.(int) fmt.Printf("total swap [%d]\n", totalSwap) } availResp, err := s.Get(availSwapOid) if err != nil { fmt.Printf("UNKNOWN: Error getting response: %s\n", err.Error()) os.Exit(utils.UNKNOWN) } else { availSwap = availResp.Variables[0].Value.(int) fmt.Printf("availSwap [%d]\n", availSwap) } if availSwap != totalSwap { usedSwap = int(100 - int(100*float64(float64(availSwap)/float64(totalSwap)))) fmt.Printf("Used swap = %d\n", usedSwap) if usedSwap >= swapLimit { fmt.Printf("CRITICAL - SWAP Available [%d] Total [%d] In Use [%d%%]", availSwap, totalSwap, usedSwap) os.Exit(utils.CRITICAL) } } fmt.Printf("OK - SWAP Available [%d] Total [%d] In Use [%d%%]", availSwap, totalSwap, usedSwap) os.Exit(utils.OK) }
func GetOID(res http.ResponseWriter, req *http.Request) { r := render.New() vars := mux.Vars(req) valueOnly := false if req.URL.Query().Get("value_only") != "" { valueOnly = true } rq := struct { community string node string oid string }{ req.URL.Query().Get("community"), vars["node"], vars["oid"], } if rq.community == "" { rq.community = "public" } snmp, err := gosnmp.NewGoSNMP(rq.node, rq.community, gosnmp.Version2c, 5) if err != nil { r.JSON(res, http.StatusInternalServerError, err.Error()) return } resp, err := snmp.Get(rq.oid) if err != nil { r.JSON(res, http.StatusInternalServerError, err.Error()) return } for _, v := range resp.Variables { switch v.Type { case gosnmp.OctetString: if valueOnly { r.JSON(res, http.StatusOK, v.Value) } else { r.JSON(res, http.StatusOK, v) } return } } r.JSON(res, http.StatusNotFound, "No matching OID found") }
func (rb *MikrotikSnmp) SnmpGetPDU(oid string) (gosnmp.SnmpPDU, error) { s, err := gosnmp.NewGoSNMP(rb.Host, rb.Community, gosnmp.Version2c, 5) if err != nil { return zeroValPdu, err } resp, err := s.Get(oid) if err != nil { return zeroValPdu, err } if len(resp.Variables) > 0 { return resp.Variables[0], nil } return zeroValPdu, nil }
func main() { // for i := 0; i < 100000 ; i++ { // fmt.Print("weee\a") // } ipPtr := flag.String("ip", " ", "ip or hostname") commPtr := flag.String("community", "nagios", "SNMP community string") flag.Parse() if *ipPtr == " " { fmt.Printf("-ip=<ip or hostname>") os.Exit(utils.UNKNOWN) } //else { //fmt.Printf("wordPtr is nil") // } ip := *ipPtr comm := *commPtr timeout := 5 s, err := gosnmp.NewGoSNMP(ip, comm, gosnmp.Version2c, timeout) if err != nil { fmt.Printf("UNKNOWN: Could not create snmp object for %s", ip) os.Exit(utils.UNKNOWN) } resp, err := s.Get(".1.3.6.1.2.1.1.1.0") if err != nil { fmt.Printf("UNKNOWN: Could not connect to %s", ip) os.Exit(utils.UNKNOWN) } if err == nil { for _, v := range resp.Variables { switch v.Type { case gosnmp.OctetString: fmt.Printf("Response: %s : %s : %s \n", v.Name, v.Value.(string), v.Type.String()) } } } }
func main() { s, err := gosnmp.NewGoSNMP("192.168.1.234", "public", gosnmp.Version2c, 5) if err != nil { log.Fatal(err) } resp, err := s.Get(".1.3.6.1.2.1.1.1.0") //resp, err := s.Get("system.sysDescr") if err != nil { log.Fatal(err) } if err == nil { for _, v := range resp.Variables { switch v.Type { case gosnmp.OctetString: //log.Printf("response: %s : %s : %s \n", v.Name, v.Value.(string), v.Type.String()) log.Printf("response: %s : %s : %s \n", v.Name, v.Value.(string), v.Type.String()) } } } // walk ------------- //pdus, err := s.Walk(".1.3.6.1.2.1.1") //pdus, err := s.Walk(".1.3.6.1.2.1.1") pdus, err := s.Walk(".1.3.6.1.2.1.43") if err != nil { log.Fatal(err) } for _, val := range pdus { switch val.Type { case gosnmp.OctetString: fmt.Printf("check : %s : %s\n", val.Name, val.Value) case gosnmp.ObjectIdentifier: fmt.Printf("check : %s : %d\n", val.Name, val.Value) default: fmt.Printf("check : %s : %s\n", val.Name, val.Type) } } }
func pollDevice(name string, device *Device, platform string) { fmt.Printf("community: %s\n", device.SnmpCommunity) s, err := gosnmp.NewGoSNMP(device.Host, device.SnmpCommunity, gosnmp.Version1, 5) if err != nil { fmt.Printf("err : %s\n", err) return } for { var values = make(map[string]interface{}) fmt.Printf("Polling device with model %s\n", device.Model) // get the model model := Models[device.Model] for k, v := range model { fmt.Printf("polling '%s'\n", k) resp, err := s.Get(k) if err != nil { fmt.Printf("SNMP err : %s\n", err) } else { pdu := resp.Variables[0] var t = pdu.Type switch t { case gosnmp.Integer: fmt.Println("Integer!") values[v] = pdu.Value.(int) case gosnmp.OctetString: fmt.Println("OctetString!") values[v] = string(pdu.Value.([]uint8)) default: fmt.Printf("unknown type: %d (%s)\n", t, v) } } } fmt.Println("push...") dataPush(values, platform, device.Identifier, device.Password) time.Sleep(time.Duration(device.Polling) * time.Second) } w.Done() }
func ospfIpNeib(w http.ResponseWriter, r *http.Request) { ip := r.URL.Path[12:] for i := range snmp_coms_r { s, err := gosnmp.NewGoSNMP(ip, snmp_coms_r[i], gosnmp.Version2c, 1) checkErr(err) _, err = s.Get(".1.3.6.1.2.1.17.1.1.0") if err == nil { fmt.Fprintf(w, "<table border=1>") ospfIpNeib, err := s.Walk(".1.3.6.1.2.1.14.10.1.1") checkErr(err) for i := range ospfIpNeib { fmt.Fprintf(w, "<tr><td><a href=/checkIfaces/%s>%s</a></td></tr>", ospfIpNeib[i].Value, ospfIpNeib[i].Value) } fmt.Fprintf(w, "</table>") return } } }
func main() { debugOut("Running in DEBUG mode") s, err := gosnmp.NewGoSNMP(cmdTarget, cmdCommunity, gosnmp.Version2c, 5) if cmdDebug != "" { debugOut(fmt.Sprintf("Sending SNMP Bulk Query against %s on OID %s via community %s.", cmdTarget, cmdOid, cmdCommunity)) s.SetDebug(false) s.SetVerbose(true) } else { s.SetDebug(false) s.SetVerbose(false) } if cmdTarget == "" { flag.PrintDefaults() return } if err != nil { fmt.Printf("Error creating SNMP instance: %s\n", err.Error()) return } s.SetTimeout(cmdTimeout) fmt.Printf("Getting %s\n", cmdOid) resp, err := s.Get(cmdOid) if err != nil { fmt.Printf("Error getting response: %s\n", err.Error()) } else { for _, v := range resp.Variables { fmt.Printf("%s -> ", v.Name) switch v.Type { case gosnmp.OctetString: if s, ok := v.Value.(string); ok { fmt.Printf("%s\n", s) } else { fmt.Printf("Response is not a string\n") } default: fmt.Printf("Type: %d - Value: %v\n", v.Type, v.Value) } } } }
func (rb *MikrotikSnmp) DumpOID(walk bool, oid string) { s, err := gosnmp.NewGoSNMP(rb.Host, rb.Community, gosnmp.Version2c, 5) if err != nil { log.Fatal(err) } if walk { resp, err := s.Walk(oid) if err != nil { log.Fatal(err) } dumpSnmpPDU(resp) } else { resp, err := s.Get(oid) if err != nil { log.Fatal(err) } dumpSnmpPacket(resp) } }
func swArpTable(w http.ResponseWriter, r *http.Request) { ip := r.URL.Path[12:] for i := range snmp_coms_r { s, err := gosnmp.NewGoSNMP(ip, snmp_coms_r[i], gosnmp.Version2c, 1) checkErr(err) _, err = s.Get(".1.3.6.1.2.1.17.1.1.0") if err == nil { arptable, _ := s.Walk(".1.3.6.1.2.1.4.22.1.2") checkErr(err) fmt.Fprintf(w, "<table>") for k := range arptable { ipr := strings.Replace(arptable[k].Name, ".1.3.6.1.2.1.4.22.1.2.", "", 1) ipr = strings.Replace(ipr, strings.Split(ipr, ".")[0]+".", "", 1) fmt.Fprintf(w, "<tr><td>%s</td><td><a href=/checkIfaces/%s>%s</a></td></tr>", fmt.Sprintf("%X", arptable[k].Value), ipr, ipr) } fmt.Fprintf(w, "</table>") return } } }
func main() { if cmdTarget == "" || cmdOid == "" { flag.PrintDefaults() return } s, err := gosnmp.NewGoSNMP(cmdTarget, cmdCommunity, gosnmp.Version2c, cmdTimeout) if cmdDebug == "yes" { s.SetDebug(true) s.SetVerbose(true) } if err != nil { fmt.Printf("Error creating SNMP instance: %s\n", err.Error()) return } s.SetTimeout(cmdTimeout) fmt.Printf("Getting %s\n", cmdOid) oid := strings.Split(cmdOid, ",") resp, err := s.GetMulti(oid) if err != nil { fmt.Printf("Error getting response: %s\n", err.Error()) } else { for _, v := range resp.Variables { fmt.Printf("%s -> ", v.Name) switch v.Type { case gosnmp.OctetString: if s, ok := v.Value.(string); ok { fmt.Printf("%s\n", s) } else { fmt.Printf("Response is not a string\n") } default: fmt.Printf("Type: %d - Value: %v\n", v.Type, v.Value) } } } }
func uplinkRunner() { // Initiate the SNMP client snmp, err := gosnmp.NewGoSNMP(config.Uplink.IP, config.Uplink.Community, gosnmp.Version2c, 5) if err != nil { log.Println("Error: ", err) } ifps := NetworkInterface{BytesSent: 0, BytesReceived: 0} ifcs := NetworkInterface{BytesSent: 0, BytesReceived: 0, MaxSpeed: config.Uplink.MaxSpeed} var pastUpT time.Time var pastDownT time.Time for { //resp, err := s.Get(".1.3.6.1.2.1.31.1.1.1.6.4") // Interface 4 linux //resp, err := s.Get(".1.3.6.1.2.1.31.1.1.1.6.9") // Interface 9 cisco 2940 ifcs.BytesReceived = getFirstSNMPValue(config.Uplink.InByte, snmp) ifcs.BytesSent = getFirstSNMPValue(config.Uplink.OutByte, snmp) ifcs.SpeedDown = calculateSpeed(&ifcs.BytesReceived, &ifps.BytesReceived, &pastDownT) ifcs.SpeedUp = calculateSpeed(&ifcs.BytesSent, &ifps.BytesSent, &pastUpT) if ifcs.SpeedDown > ifcs.PeakSpeedDown { ifcs.PeakSpeedDown = ifcs.SpeedDown } if ifcs.SpeedUp > ifcs.PeakSpeedUp { ifcs.PeakSpeedUp = ifcs.SpeedUp } log.Println(ifcs) prepareAndDistributeWSData("uplink", ifcs) ifps = ifcs time.Sleep(config.Misc.Interval * time.Second) } }
func main() { if cmdTarget == "" { flag.PrintDefaults() return } s, err := gosnmp.NewGoSNMP(cmdTarget, cmdCommunity, gosnmp.Version2c, cmdTimeout) if err != nil { fmt.Printf("UNKNOWN: Error creating SNMP instance: %s\n", err.Error()) os.Exit(utils.UNKNOWN) } if cmdVerbose == true { s.SetVerbose(true) } if cmdDebug == true { s.SetDebug(true) } s.SetTimeout(cmdTimeout) resp, err := s.Get(totalSwapOid) if err != nil { fmt.Printf("UNKNOWN: Error getting response: %s\n", err.Error()) os.Exit(utils.UNKNOWN) } else if cmdVerbose == true { fmt.Printf("total swap space [%v]\n", resp.Variables[0].Value) totalSwap = resp.Variables[0].Value.(int) } availResp, err := s.Get(availSwapOid) if err != nil { fmt.Printf("UNKNOWN: Error getting response: %s\n", err.Error()) os.Exit(utils.UNKNOWN) } else if cmdVerbose == true { fmt.Printf("available swap space [%v]\n", availResp.Variables[0].Value) availSwap = availResp.Variables[0].Value.(int) } var usedSwap = totalSwap - availSwap if cmdVerbose == true { fmt.Printf("swapLimit = %d\n", swapLimit) fmt.Printf("usedSwap = %d\n", usedSwap) } if availSwap != totalSwap { //usedSwap := int(100 - (100 * (totalSwap / availSwap))) usedSwap := int(100 - int(100*float64(float64(availSwap)/float64(totalSwap)))) if availSwap >= swapLimit { fmt.Printf("CRITICAL - Swap at [%d%]", usedSwap) os.Exit(utils.CRITICAL) } } else { fmt.Printf("OK - Swap at [%d%] Available Swap [%d] Used [%d]", usedSwap, availSwap, totalSwap) os.Exit(utils.OK) } }
func checkIfaces(w http.ResponseWriter, r *http.Request) { ip := r.URL.Path[13:] fmt.Fprintf(w, "<html><head></head><body>") for i := range snmp_coms_r { s, err := gosnmp.NewGoSNMP(ip, snmp_coms_r[i], gosnmp.Version2c, 1) checkErr(err) resp, err := s.Get(".1.3.6.1.2.1.17.1.1.0") if err == nil { _ = resp // umac := fmt.Sprintf("%X",resp.Variables[0].Value) fmt.Fprintf(w, "<script>function changeAlias(e,num,txt){var code = (e.keyCode ? e.keyCode : e.which); if (code == 13) {window.open('/swSetAlias/%s/'+num+'/'+txt.value);}};</script>", ip) fmt.Fprintf(w, "<table border=1 width='100%'><tr><td width='50%' height=300>") fmt.Fprintf(w, "<div style='width:100%%; height:100%%;'><object style='width:100%%; height:100%%;' type='text/html' data='/swMainData/%s'></object></div>", ip) fmt.Fprintf(w, "</td><td width='50%' height=300>") fmt.Fprintf(w, "<div style='width:100%%; height:100%%;'><object style='width:100%%; height:100%%;' type='text/html' data='/ospfIpNeib/%s'></object></div>", ip) fmt.Fprintf(w, "</td></tr><tr><td>") fmt.Fprintf(w, "<div style='width:100%%; height:100%%;'><object style='width:100%%; height:100%%;' type='text/html' data='/swArpTable/%s'></object></div>", ip) fmt.Fprintf(w, "</td></tr></table>") fmt.Fprintf(w, "<a href='#' onclick='window.open(\"/swCheckAllCable/%s/\");'>ShowAllCableStatus</a>", ip) fmt.Fprintf(w, "<table border=1 width='100%'>") fmt.Fprintf(w, "<tr><th>ifId</th><th>ifAdminStatus</th><th>ifOperStatus</th><th>ifName</th><th>ifAlias</th><th>ifSpeed</th><th>ifType</th><th>ifMAC</th><th>ifInErr</th><th>Mcast</th><th>LLDP&FDB Table</th></tr>") ifadmstat, err := s.Walk(".1.3.6.1.2.1.2.2.1.7") checkErr(err) ifoperstat, err := s.Walk(".1.3.6.1.2.1.2.2.1.8") checkErr(err) ifname, err := s.Walk(".1.3.6.1.2.1.2.2.1.2") checkErr(err) ifalias, err := s.Walk("1.3.6.1.2.1.31.1.1.1.18") checkErr(err) ifmac, err := s.Walk(".1.3.6.1.2.1.2.2.1.6") checkErr(err) ifspeed, err := s.Walk("1.3.6.1.2.1.2.2.1.5") checkErr(err) iftype, err := s.Walk("1.3.6.1.2.1.2.2.1.3") checkErr(err) ifinerr, err := s.Walk("1.3.6.1.2.1.2.2.1.14") checkErr(err) // mcast14, _ := s.Walk(".1.3.6.1.4.1.171.12.73.2.1.5.1.2") // checkErr(err) mcast15, _ := s.Walk(".1.3.6.1.4.1.171.12.73.2.1.2.1.4") checkErr(err) mcast16, _ := s.Walk(".1.3.6.1.4.1.171.11.113.1.5.2.7.13.1.4") checkErr(err) lldpchasid, _ := s.Walk(".1.0.8802.1.1.2.1.4.1.1.5") checkErr(err) lldpporttype, _ := s.Walk(".1.0.8802.1.1.2.1.4.1.1.6") checkErr(err) lldpport, _ := s.Walk(".1.0.8802.1.1.2.1.4.1.1.7") checkErr(err) lldpportdescr, _ := s.Walk(".1.0.8802.1.1.2.1.4.1.1.8") checkErr(err) lldpsysname, _ := s.Walk(".1.0.8802.1.1.2.1.4.1.1.9") checkErr(err) lldppeerip, _ := s.Walk(".1.0.8802.1.1.2.1.4.2.1.3") checkErr(err) arptable, _ := s.Walk(".1.3.6.1.2.1.4.22.1.2") checkErr(err) fdbtable, _ := s.Walk(".1.3.6.1.2.1.17.7.1.2.2.1.2") checkErr(err) //// db, err := sql.Open("mysql", mysql_user+":"+mysql_password+"@tcp("+mysql_host+":"+mysql_port+")/"+mysql_db+"?charset=utf8") //// checkErr(err) //// stmt, err := db.Prepare("update sw_if set if_admin_status=?, if_oper_status=? where umac=CONV(?,16,10) and if_id=?") //// checkErr(err) color := "green" for i := range ifadmstat { ifadmstat[i].Name = strings.Replace(ifadmstat[i].Name, ".1.3.6.1.2.1.2.2.1.7.", "", 1) ifoperstat[i].Name = strings.Replace(ifoperstat[i].Name, ".1.3.6.1.2.1.2.2.1.8.", "", 1) if ifoperstat[i].Value != 1 { color = "#aa5555" } else { color = "#55aa55" } if ifType[iftype[i].Value.(int)-1] == "ethernetCsmacd" && ifspeed[i].Value.(int)/1000000 == 10 { color = "#55aaaa" } if ifStatus[ifadmstat[i].Value.(int)-1] != "up" { color = "#ff3333" } fmt.Fprintf(w, "<tr bgcolor=%s>", color) // fmt.Fprintf(w,"<td>%s</td>",umac) fmt.Fprintf(w, "<td>%s</td>", ifadmstat[i].Name) fmt.Fprintf(w, "<td>%s<br>", ifStatus[ifadmstat[i].Value.(int)-1]) fmt.Fprintf(w, "<a href='#' style='color: yellow;' onclick='window.open(\"/swPortUp/%s/%s\");'>Вкл </a>", ip, ifadmstat[i].Name) fmt.Fprintf(w, "<a href='#' style='color: yellow;' onclick='window.open(\"/swPortDown/%s/%s\");'>Выкл</a>", ip, ifadmstat[i].Name) fmt.Fprintf(w, "</td>") // fmt.Fprintf(w,"<td>%s</td>",ifStatus[ifoperstat[i].Value.(int)-1]) fmt.Fprintf(w, "<td><a href='#' onclick='window.open(\"/swCheckCable/%s/%s\");'>%s</a></td>", ip, ifadmstat[i].Name, ifStatus[ifoperstat[i].Value.(int)-1]) fmt.Fprintf(w, "<td>%s</td>", ifname[i].Value) fmt.Fprintf(w, "<td><textarea id='txtArea%s' onkeypress='changeAlias(event,%s,this);'>", ifadmstat[i].Name, ifadmstat[i].Name) fmt.Fprintf(w, "%s</textarea></td>", ifalias[i].Value) fmt.Fprintf(w, "<td>%d</td>", ifspeed[i].Value.(int)/1000000) fmt.Fprintf(w, "<td>%s</td>", ifType[iftype[i].Value.(int)-1]) fmt.Fprintf(w, "<td>%s</td>", fmt.Sprintf("%X", ifmac[i].Value)) fmt.Fprintf(w, "<td>%d</td>", ifinerr[i].Value) // fmt.Fprintf(w,"<td>") // for j := range mcast14 { // boid := strings.Split(mcast14[j].Name,".") // if boid[20] == ifadmstat[i].Name { // fmt.Fprintf(w,"%s<br>",mcast14[j].Value) // } // } // fmt.Fprintf(w,"</td>") fmt.Fprintf(w, "<td>") for j := range mcast16 { cmip := strings.Split(mcast16[j].Name, ".")[18] cmip = cmip + "." + strings.Split(mcast16[j].Name, ".")[19] cmip = cmip + "." + strings.Split(mcast16[j].Name, ".")[20] cmip = cmip + "." + strings.Split(mcast16[j].Name, ".")[21] tport := strings.Split(mcast16[j].Name, ".")[22] if fmt.Sprintf("%d", i+1) == tport { fmt.Fprintf(w, "%s(%s)<br>", cmip, mcast16[j].Value) } } for j := range mcast15 { b0 := fmt.Sprintf("%X", mcast15[j].Value)[0:8] b1 := fmt.Sprintf("%X", mcast15[j].Value)[8:16] bi0, _ := strconv.ParseInt(b0, 16, 64) bi1, _ := strconv.ParseInt(b1, 16, 64) bs := fmt.Sprintf("%32.32b%32.32b<br>", bi0, bi1) cmip := strings.Split(mcast15[j].Name, ".")[20] cmip = cmip + "." + strings.Split(mcast15[j].Name, ".")[21] cmip = cmip + "." + strings.Split(mcast15[j].Name, ".")[22] cmip = cmip + "." + strings.Split(mcast15[j].Name, ".")[23] if bs[i:i+1] == "1" { fmt.Fprintf(w, "%s<br>", cmip) } //fmt.Fprintf(w,"%s",bs) //boid := strings.Split(mcast15[j].Name,".") //if boid[20] == ifadmstat[i].Name { //} } fmt.Fprintf(w, "</td>") fmt.Fprintf(w, "<td>\n") for j := range lldpchasid { if fmt.Sprintf("%d", i+1) == strings.Split(lldpchasid[j].Name, ".")[13] { fmt.Fprintf(w, "<table border=1><tr><td>ChassisID</td><td>%s</td><tr>", fmt.Sprintf("%X", lldpchasid[j].Value)) if lldpporttype[j].Value == 3 { fmt.Fprintf(w, "<tr><td>PortID</td><td>%s</td></tr>", fmt.Sprintf("%X", lldpport[j].Value)) } else { fmt.Fprintf(w, "<tr><td>PortID</td><td>%s</td></tr>", lldpport[j].Value) } fmt.Fprintf(w, "<tr><td>PortDescr</td><td>%s</td></tr>", lldpportdescr[j].Value) fmt.Fprintf(w, "<tr><td>SystemName</td><td>%s</td></tr>", lldpsysname[j].Value) // fmt.Fprintf(w,"%i",lldppeerip[0].Name) for k := range lldppeerip { if strings.Split(lldppeerip[k].Name, ".")[13] == fmt.Sprintf("%d", i+1) { lldppeeripc := strings.Split(lldppeerip[k].Name, ".")[17] + "." + strings.Split(lldppeerip[k].Name, ".")[18] + "." + strings.Split(lldppeerip[k].Name, ".")[19] + "." + strings.Split(lldppeerip[k].Name, ".")[20] fmt.Fprintf(w, "<tr><td>PeerIP</td><td><a href=%s>%s</a></td></tr>", lldppeeripc, lldppeeripc) } } fmt.Fprintf(w, "</table>") for k := range arptable { ipr := strings.Replace(arptable[k].Name, ".1.3.6.1.2.1.4.22.1.2.", "", 1) ipr = strings.Replace(ipr, strings.Split(ipr, ".")[0]+".", "", 1) if lldpchasid[j].Value == arptable[k].Value { fmt.Fprintf(w, "%s <a href=/checkIfaces/%s>%s</a><br>", fmt.Sprintf("%X", arptable[k].Value), ipr, ipr) } } } } for k := range fdbtable { macr := strings.Split(strings.Replace(fdbtable[k].Name, ".1.3.6.1.2.1.17.7.1.2.2.1.2.", "", 1), ".") if ifadmstat[i].Name == fmt.Sprintf("%d", fdbtable[k].Value) { m1, _ := strconv.Atoi(macr[1]) m2, _ := strconv.Atoi(macr[2]) m3, _ := strconv.Atoi(macr[3]) m4, _ := strconv.Atoi(macr[4]) m5, _ := strconv.Atoi(macr[5]) m6, _ := strconv.Atoi(macr[6]) fmt.Fprintf(w, "Vlan:%s_Mac:", macr[0]) macrs := fmt.Sprintf("%2X%2X%2X%2X%2X%2X", m1, m2, m3, m4, m5, m6) macrs = strings.Replace(macrs, " ", "0", 16) fmt.Fprintf(w, "%s<br>", macrs) } } fmt.Fprintf(w, "</td>") fmt.Fprintf(w, "</tr>\n") //// _, err = stmt.Exec(ifadmstat[i].Value,ifoperstat[i].Value,umac, ifadmstat[i].Name) //// checkErr(err) } fmt.Fprintf(w, "</table><br>") fmt.Fprintf(w, "</body></html>") return } } fmt.Fprintf(w, "<h1>Хост не отвечает!</h1></body></html>") }
func main() { if cmdDebug != "" { fmt.Printf("Running in debug mode\n") s, err := gosnmp.NewGoSNMP("", "", gosnmp.Version2c, 5) s.SetDebug(true) s.SetVerbose(true) packet, err := hex.DecodeString(cmdDebug) if err != nil { fmt.Printf("Unable to decode raw packet: %s\n", err.Error()) return } pckt, err := s.Debug(packet) if err != nil { fmt.Printf("Error while debugging: %s\n", err.Error()) } else { for _, resp := range pckt.Variables { fmt.Printf("%s -> %v\n", resp.Name, resp.Value) } } return } if cmdTarget == "" || cmdOid == "" { flag.PrintDefaults() return } s, err := gosnmp.NewGoSNMP(cmdTarget, cmdCommunity, gosnmp.Version2c, cmdTimeout) s.SetDebug(false) s.SetVerbose(false) if err != nil { fmt.Printf("Error creating SNMP instance: %s\n", err.Error()) return } s.SetTimeout(cmdTimeout) fmt.Printf("Getting %s\n", cmdOid) resp, err := s.Get(cmdOid) if err != nil { fmt.Printf("Error getting response: %s\n", err.Error()) } else { for _, v := range resp.Variables { fmt.Printf("%s -> ", v.Name) switch v.Type { case gosnmp.OctetString: if s, ok := v.Value.(string); ok { fmt.Printf("%s\n", s) } else { fmt.Printf("Response is not a string\n") } default: fmt.Printf("Type: %d - Value: %v\n", v.Type, v.Value) } } } }
func main() { const cmdTimeout = 1 const cmdOid = ".1.3.6.1.4.1.12356.101.4.1.1" // Device Version //const cmdOid = ".1.3.6.1.4.1.12356.101.4.1.4.0" // MemUsage s, err := gosnmp.NewGoSNMP("10.10.120.1", "public", gosnmp.Version2c, cmdTimeout) if err != nil { fmt.Printf("Error creating SNMP instance: %s\n", err.Error()) return } //s.SetDebug(true) //s.SetVerbose(true) oids := []string{ ".1.3.6.1.4.1.12356.101.4.1.1", //".1.3.6.1.4.1.12356.101.4.1.2.0", // ".1.3.6.1.4.1.12356.101.4.1.3.0", // ".1.3.6.1.4.1.12356.101.4.1.4.0", // ".1.3.6.1.4.1.12356.101.4.1.5.0", // ".1.3.6.1.4.1.12356.101.4.1.6.0", // ".1.3.6.1.4.1.12356.101.4.1.7.0", // ".1.3.6.1.4.1.12356.101.4.1.8.0", // ".1.3.6.1.4.1.12356.101.4.1.9.0", } s.SetTimeout(cmdTimeout) oid := cmdOid oids := []string{ ".1.3.6.1.4.1.12356.101.4.1.1.0", ".1.3.6.1.4.1.12356.101.4.1.2.0", ".1.3.6.1.4.1.12356.101.4.1.3.0", ".1.3.6.1.4.1.12356.101.4.1.4.0", ".1.3.6.1.4.1.12356.101.4.1.5.0", } _ = oids //for i := 0; i < 10; i++ { fmt.Printf("Getting %s\n", oid) //resp, err := s.GetMulti(oids) // Max 5 resp, err := s.GetBulk(0, 50, ".1.3.6.1.4.1.12356.101.4.1.1") //fmt.Printf("Getting %s\n", oids) //resp, err := s.GetBulk(0, 50, oids...) if err != nil { fmt.Printf("Error getting response: %s\n", err.Error()) } else { for _, v := range resp.Variables { fmt.Printf("%s -> ", v.Name) switch v.Type { case gosnmp.OctetString: if s, ok := v.Value.(string); ok { fmt.Printf("%s\n", s) } else { fmt.Printf("Response is not a string\n") } default: fmt.Printf("Type: %s(%#x) - Value: %v\n", v.Type, int(v.Type), v.Value) } oid = v.Name } } //} }