From 3e0c70bb03dd0562da5129ec8a75253a2a699b34 Mon Sep 17 00:00:00 2001 From: Stefan Date: Mon, 20 Mar 2023 20:49:01 +0100 Subject: [PATCH] tidy up unter hempels sofa --- unms.go | 166 ++++++++++++++++++-------------------------------------- 1 file changed, 53 insertions(+), 113 deletions(-) diff --git a/unms.go b/unms.go index 14bf324..b5f6f73 100644 --- a/unms.go +++ b/unms.go @@ -7,7 +7,6 @@ import ( "io/ioutil" "log" "net/http" - "strconv" "strings" "time" @@ -111,14 +110,16 @@ func processUNMSAPIRouter() []node { d := getDevices(conf.Unms.RouterURL) - // API CALL 1 - log.Println("calling API 1") + // API CALL 1, get all devices list from UNMS + log.Println("Get all devices from UNMS") var u []unifiAPIResponse err := UnmsCallAPI("/devices", &u) if err != nil { log.Fatalln(err) } + // END OF API CALL 1 + // Get Information for devices device for i := range d.Devices { var dev unifiAPIResponse var currentDevice device @@ -133,12 +134,23 @@ func processUNMSAPIRouter() []node { if dev.Overview.Status == "active" { isOnline = true } - // END OF API CALL 1 - // API CALL FOR ROUTER DETAILS - log.Println("calling API 2 for device", d.Devices[i].Name) + // API CALL FOR ROUTER DETAILS (Interface RX/TX) + log.Println("Getting details of ", d.Devices[i].Name, "from UNMS API") var details unifiAPIDetails UnmsCallAPI("/devices/erouters/"+dev.Identification.ID, &details) + + // API CALL FOR DEVICE STATISTICS (CPU, RAM) + log.Println("Getting statistics of ", d.Devices[i].Name, "from UNMS API") + var statistics UNMSstatistics + UnmsCallAPI("/devices/"+dev.Identification.ID+"/statistics?interval=hour", &statistics) + + // API CALL FOR DHCP LEASES + log.Println("Getting DHCP Leases of ", d.Devices[i].Name, "from UNMS API") + var dhcpleases UNMSdhcp + UnmsCallAPI("/devices/erouters/"+dev.Identification.ID+"/dhcp/leases", &dhcpleases) + + // Open connection to InfluxDB bp, err := client.NewBatchPoints(client.BatchPointsConfig{ Database: "freifunk", Precision: "s", @@ -146,119 +158,47 @@ func processUNMSAPIRouter() []node { if err != nil { log.Fatalln("Error: ", err) } - for i := range d.Devices { - log.Println("calling API 3 for device", d.Devices[i].Name) - var statistics UNMSstatistics - UnmsCallAPI("/devices/"+dev.Identification.ID+"/statistics?interval=hour", &statistics) - for eth := range details.Interfaces { - tags := map[string]string{ - "hostname": d.Devices[i].Name, - "nodeid": strings.ReplaceAll(dev.Identification.MAC, ":", ""), - } - interface_name_rx := ("rate.rx" + "_" + details.Interfaces[eth].Identification.Name) - interface_name_tx := ("rate.tx" + "_" + details.Interfaces[eth].Identification.Name) - fields := map[string]interface{}{ - interface_name_rx: details.Interfaces[eth].Statistics.Rxrate, - interface_name_tx: details.Interfaces[eth].Statistics.Txrate, - } - tm := time.Now() - point, err := client.NewPoint( - "node", - tags, - fields, - tm, - ) - if err != nil { - log.Fatalln("Error: ", err) - } + // - bp.AddPoint(point) - c := influxDBClient() - err = c.Write(bp) - if err != nil { - log.Fatal(err) - } - } + fields := map[string]interface{}{} + tags := map[string]string{ + "hostname": strings.ReplaceAll(d.Devices[i].Name, " ", "-"), + "nodeid": strings.ReplaceAll(dev.Identification.MAC, ":", ""), } - // END OF API CALL - - // API CALL 3 STATISTICS - for i := range d.Devices { - log.Println("calling API 3 for device", d.Devices[i].Name) - var statistics UNMSstatistics - UnmsCallAPI("/devices/"+dev.Identification.ID+"/statistics?interval=hour", &statistics) - for t := range statistics.CPU { - if t > 9 { - break - } - tags := map[string]string{ - "hostname": strings.ReplaceAll(d.Devices[i].Name, " ", "-"), - "nodeid": strings.ReplaceAll(dev.Identification.MAC, ":", ""), - } - load := (float64(statistics.CPU[t].Y) / float64(100)) - fields := map[string]interface{}{ - "cpu": statistics.CPU[t].Y, - "load": load, - "ram": statistics.RAM[t].Y, - } - slice := strconv.Itoa(statistics.CPU[t].X) - timeunix, err := strconv.Atoi(slice[:10]) - if err != nil { - log.Fatalln("Error: ", err) - } - tm := time.Unix(int64(timeunix), 0) - time_local := tm.Add(1 * time.Hour) - point, err := client.NewPoint( - "node", - tags, - fields, - time_local, - ) - if err != nil { - log.Fatalln("Error: ", err) - } - - bp.AddPoint(point) - c := influxDBClient() - err = c.Write(bp) - if err != nil { - log.Fatal(err) - } - } + // Generate fields for all network interfaces + for eth := range details.Interfaces { + interface_name_rx := ("rate.rx" + "_" + details.Interfaces[eth].Identification.Name) + interface_name_tx := ("rate.tx" + "_" + details.Interfaces[eth].Identification.Name) + fields[interface_name_rx] = details.Interfaces[eth].Statistics.Rxrate + fields[interface_name_tx] = details.Interfaces[eth].Statistics.Txrate } - // END OF API CALL 3 - // Start API Call 4 (DHCP Leases) - for i := range d.Devices { - log.Println("calling API 4 for device", d.Devices[i].Name) - var dhcpleases UNMSdhcp - UnmsCallAPI("/devices/erouters/"+dev.Identification.ID+"/dhcp/leases", &dhcpleases) + // Generate fields for all Statistics + load := (float64(statistics.CPU[0].Y) / float64(100)) + fields["cpu"] = statistics.CPU[0].Y + fields["load"] = load + fields["ram"] = statistics.RAM[0].Y - leases := len(dhcpleases) - tags := map[string]string{ - "hostname": strings.ReplaceAll(d.Devices[i].Name, " ", "-"), - "nodeid": strings.ReplaceAll(dev.Identification.MAC, ":", ""), - } - fields := map[string]interface{}{ - "clients.total": leases, - } - tm := time.Now() - point, err := client.NewPoint( - "node", - tags, - fields, - tm, - ) - if err != nil { - log.Fatalln("Error: ", err) - } + // Generate field for DHCP Leases + leases := len(dhcpleases) + fields["clients.total"] = leases - bp.AddPoint(point) - c := influxDBClient() - err = c.Write(bp) - if err != nil { - log.Fatal(err) - } + // Generate Dataponts + point, err := client.NewPoint( + "node", + tags, + fields, + time.Now(), + ) + if err != nil { + log.Fatalln("Error: ", err) + } + // Add Datapoints in InfluxDB + bp.AddPoint(point) + c := influxDBClient() + err = c.Write(bp) + if err != nil { + log.Fatal(err) } // Get info from json file (static)