bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/snmpTester/main.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "flag" 6 "io/ioutil" 7 "log" 8 "net/http" 9 10 "bosun.org/cmd/scollector/collectors" 11 "bosun.org/cmd/scollector/conf" 12 "github.com/BurntSushi/toml" 13 ) 14 15 var devMode = flag.Bool("dev", false, "Dev mode. Use html from file-system instead of embedded copy.") 16 17 // to embed static again: go:generate esc -modtime 0 -o=static.go -prefix=static static 18 19 func main() { 20 flag.Parse() 21 fs := FS(*devMode) 22 http.Handle("/", http.FileServer(fs)) 23 http.HandleFunc("/test", TestMib) 24 http.HandleFunc("/toml", Toml) 25 http.ListenAndServe(":8888", nil) 26 } 27 28 func TestMib(w http.ResponseWriter, r *http.Request) { 29 buf, err := ioutil.ReadAll(r.Body) 30 if err != nil { 31 http.Error(w, err.Error(), 500) 32 return 33 } 34 mib := conf.MIB{} 35 err = json.Unmarshal(buf, &mib) 36 if err != nil { 37 http.Error(w, err.Error(), 500) 38 return 39 } 40 snmp := conf.SNMP{} 41 err = json.Unmarshal(buf, &snmp) 42 if err != nil { 43 http.Error(w, err.Error(), 500) 44 return 45 } 46 47 md, err := collectors.GenericSnmp(snmp, mib) 48 if err != nil { 49 log.Println(err) 50 http.Error(w, err.Error(), 500) 51 return 52 } 53 54 mdJson, err := json.MarshalIndent(md, "", " ") 55 if err != nil { 56 log.Println(err) 57 http.Error(w, err.Error(), 500) 58 return 59 } 60 w.Write(mdJson) 61 } 62 63 func Toml(w http.ResponseWriter, r *http.Request) { 64 buf, err := ioutil.ReadAll(r.Body) 65 if err != nil { 66 http.Error(w, err.Error(), 500) 67 return 68 } 69 mib := conf.MIB{} 70 err = json.Unmarshal(buf, &mib) 71 if err != nil { 72 http.Error(w, err.Error(), 500) 73 return 74 } 75 meta := &struct{ Name string }{} 76 err = json.Unmarshal(buf, meta) 77 if err != nil { 78 http.Error(w, err.Error(), 500) 79 return 80 } 81 82 toToml := struct { 83 MIBs map[string]conf.MIB 84 }{MIBs: map[string]conf.MIB{meta.Name: mib}} 85 86 toml.NewEncoder(w).Encode(toToml) 87 }