From 3f0d6fc3d4a002efefae28785b4c7b3006357c8b Mon Sep 17 00:00:00 2001 From: Stefan Hoffmann Date: Mon, 5 Apr 2021 12:52:35 +0200 Subject: [PATCH] Added output of Outages from UNMS --- main.go | 48 ++++++++++++++++++++++++++++++++++++++++++------ types.go | 18 ++++++++++++++++++ 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 4b9e0ce..0e0bb20 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "encoding/csv" "encoding/json" "errors" "flag" @@ -245,19 +246,19 @@ func processAPIs() { var links []link if conf.Unms.Enabled == true { - fmt.Println("Processing UNMS") + log.Println("Processing UNMS") unmsNodes, unmsLinks := processUNMSAPI() nodes = append(nodes, unmsNodes...) links = append(links, unmsLinks...) } if conf.Unifi.Enabled == true { - fmt.Println("Processing Unifi") + log.Println("Processing Unifi") ucNodes, ucLinks := processUcAPIs() nodes = append(nodes, ucNodes...) links = append(links, ucLinks...) } if conf.Meshviewer.Enabled == true { - fmt.Println("Processing Meshviewer") + log.Println("Processing Meshviewer") mvNodes, mvLinks := getMeshviewer() nodes = append(nodes, mvNodes...) links = append(links, mvLinks...) @@ -275,7 +276,12 @@ func processAPIs() { if err := o.writeToFile(); err != nil { log.Fatalln(err) } - + // get outages to serve as .csv + l := getUNMSLogs() + err := writeOutagesToCSV(l) + if err != nil { + log.Println("Error writing outages.csv") + } // we're done here log.Println("...done") } @@ -283,7 +289,7 @@ func processAPIs() { func getFile(url string) []byte { resp, err := http.Get(url) if err != nil { - fmt.Printf("error") + log.Println("Error getting file from:", url) } data := resp.Body byteValue, _ := ioutil.ReadAll(data) @@ -608,7 +614,7 @@ func getMeshviewer() ([]node, []link) { var links []link for i := range conf.Meshviewer.Files { - fmt.Println("Hole Meshviewer JSON von: ", conf.Meshviewer.Files[i].URL) + log.Println("Hole Meshviewer JSON von: ", conf.Meshviewer.Files[i].URL) m, err := getMeshviewerJSON(conf.Meshviewer.Files[i].URL) if err != nil { return nodes, links @@ -619,3 +625,33 @@ func getMeshviewer() ([]node, []link) { } return nodes, links } + +func getUNMSLogs() UNMSLogResponse { + + var l UNMSLogResponse + log.Println("Get Outages from UNMS") + err := callUnifiAPI("/outages?count=100&page=1&type=outage", &l) + if err != nil { + log.Fatalln("Error calling Outages API") + } + return l +} + +func writeOutagesToCSV(l UNMSLogResponse) error { + csvFile, err := os.Create("output/outages.csv") + if err != nil { + return err + } + writer := csv.NewWriter(csvFile) + + for _, o := range l.Items { + var row []string + row = append(row, o.StartTime.Format("02.01.2006 15:04:05")) + row = append(row, o.EndTime.Format("02.01.2006 15:04:05")) + row = append(row, o.Site.Name) + row = append(row, o.Device.DisplayName) + writer.Write(row) + } + writer.Flush() + return nil +} diff --git a/types.go b/types.go index 6dbc04d..ac87886 100644 --- a/types.go +++ b/types.go @@ -283,3 +283,21 @@ func lookupModels(model string) string { return "Unifi Gerät" } } + +type UNMSLogResponse struct { + Items []struct { + ID string `json:"id"` + StartTime time.Time `json:"startTimestamp"` + EndTime time.Time `json:"endTimestamp"` + Type string `json:"type"` + + Site struct { + Name string `json:"name"` + } `json:"site"` + Device struct { + ModelName string `json:"modelName"` + Name string `json:"name"` + DisplayName string `json:"displayName"` + } `json:"device"` + } `json:"items"` +}