From f5008f6da2956bdfdf7f43ffa0bc2f0df0748fd7 Mon Sep 17 00:00:00 2001 From: Patrick Mischler Date: Thu, 22 Apr 2021 13:48:21 +0200 Subject: [PATCH] Reverted optimizations because they caused the program not to work properly. --- main.go | 39 ++++++++------------------------------- 1 file changed, 8 insertions(+), 31 deletions(-) diff --git a/main.go b/main.go index b0aa716..bd22176 100644 --- a/main.go +++ b/main.go @@ -22,7 +22,6 @@ import ( "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promhttp" "gopkg.in/yaml.v2" - "io" "io/ioutil" "log" "math/rand" @@ -56,12 +55,7 @@ func NewConfig(configPath string) (*Config, error) { if err != nil { return nil, err } - defer func(file *os.File) { - err := file.Close() - if err != nil { - - } - }(file) + defer file.Close() d := yaml.NewDecoder(file) @@ -107,8 +101,8 @@ type TSEventAttributes struct { McObjectURI string `json:"mc_object_uri"` } -func NewTSEventAttributes(CLASS string, severity string, msg string, mcObjectClass string, mcObject string, mcParameter string, mcObjectUri string) *TSEventAttributes { - return &TSEventAttributes{CLASS: CLASS, Severity: severity, Msg: msg, McObjectClass: mcObjectClass, McObject: mcObject, McParameter: mcParameter, McObjectURI: mcObjectUri} +func NewTSEventAttributes(CLASS string, severity string, msg string, mc_object_class string, mc_object string, mc_parameter string, mc_object_uri string) *TSEventAttributes { + return &TSEventAttributes{CLASS: CLASS, Severity: severity, Msg: msg, McObjectClass: mc_object_class, McObject: mc_object, McParameter: mc_parameter, McObjectURI: mc_object_uri} } type TSEvent struct { @@ -231,16 +225,10 @@ func (config Config) Run() { if events != nil { if SendEventToTS(tsToken.getToken(), config.Server.TrueSight.TSIMServer, config.Server.TrueSight.TSIMPort, config.Server.TrueSight.TSCell, events) { InfoLogger.Println("Event sent to TrueSight.") - _, err := fmt.Fprintf(w, "Event(s) created") - if err != nil { - return - } + fmt.Fprintf(w, "Event(s) created") } else { WarningLogger.Println("Failed to send event(s) to TrueSight.") - err := promAlertsCache.Add(RandomString(10), events, cache.DefaultExpiration) - if err != nil { - return - } + promAlertsCache.Add(RandomString(10), events, cache.DefaultExpiration) http.Error(w, "Something went wrong on the server!", http.StatusInternalServerError) } } @@ -273,10 +261,7 @@ func GetTSToken(tspsServer string, tspsPort string, tsUser string, tsUserPw stri rawData, _ := ioutil.ReadAll(resp.Body) InfoLogger.Println("Get TS auth token response: ", rawData) var data map[string]map[string]interface{} - err := json.Unmarshal(rawData, &data) - if err != nil { - return "" - } + json.Unmarshal(rawData, &data) authToken = data["response"]["authToken"].(string) break } @@ -302,12 +287,7 @@ func VerifyTSToken(token string, tspsServer string, tspsPort string) bool { if err != nil { log.Fatal("Unable to perform request: ", err) } else { - defer func(Body io.ReadCloser) { - err := Body.Close() - if err != nil { - - } - }(resp.Body) + defer resp.Body.Close() rawData, _ := ioutil.ReadAll(resp.Body) var data map[string]interface{} if err := json.Unmarshal(rawData, &data); err != nil { @@ -418,10 +398,7 @@ func main() { fmt.Println("starting heartbeat") go Heartbeat() http.Handle("/metrics", promhttp.Handler()) - err = http.ListenAndServe(":"+cfg.Server.PromMetricPort, nil) - if err != nil { - return - } + http.ListenAndServe(":"+cfg.Server.PromMetricPort, nil) time.Sleep(1 * time.Second) }