Browse Source

fixes

- improved log levels
- less error prone float string parsing
- TODOs for more & better measurement_faulty checks
- go fmt
master
Norwin Roosen 5 months ago
parent
commit
8e1cf7ed38

+ 3
- 3
cmd/cmd_root.go View File

@@ -101,9 +101,9 @@ var configHelpCmd = &cobra.Command{
101 101
 }
102 102
 
103 103
 var rootCmd = &cobra.Command{
104
-	Use:   "osem_notify",
105
-	Short: "Root command displaying help",
106
-	Long:  "Run healthchecks and send notifications for boxes on opensensemap.org",
104
+	Use:     "osem_notify",
105
+	Short:   "Root command displaying help",
106
+	Long:    "Run healthchecks and send notifications for boxes on opensensemap.org",
107 107
 	Version: "1.3.0",
108 108
 	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
109 109
 		// set up logger

+ 5
- 5
cmd/shared.go View File

@@ -93,14 +93,14 @@ func checkAndNotify(boxIds []string) error {
93 93
 }
94 94
 
95 95
 var ( // values are set during cli flag parsing of checkAllCmd & watchAllCmd
96
-	date string
97
-	exposure string
98
-	grouptag string
99
-	model string
96
+	date       string
97
+	exposure   string
98
+	grouptag   string
99
+	model      string
100 100
 	phenomenon string
101 101
 )
102 102
 
103
-func parseBoxFilters () core.BoxFilters {
103
+func parseBoxFilters() core.BoxFilters {
104 104
 	filters := core.BoxFilters{}
105 105
 	if date != "" {
106 106
 		filters.Date = date // TODO: parse date & format as ISO date?

+ 2
- 2
core/checkrunner.go View File

@@ -86,7 +86,7 @@ func (results BoxCheckResults) Log() {
86 86
 }
87 87
 
88 88
 func CheckBoxes(boxLocalConfs map[string]*NotifyConfig, osem *OsemClient) (BoxCheckResults, error) {
89
-	log.Debug("Checking notifications for ", len(boxLocalConfs), " box(es)")
89
+	log.Info("Checking notifications for ", len(boxLocalConfs), " box(es)")
90 90
 
91 91
 	results := BoxCheckResults{}
92 92
 	errs := []string{}
@@ -94,7 +94,7 @@ func CheckBoxes(boxLocalConfs map[string]*NotifyConfig, osem *OsemClient) (BoxCh
94 94
 	// @TODO: check boxes in parallel, capped at 5 at once. and/or rate limit?
95 95
 	for boxId, localConf := range boxLocalConfs {
96 96
 		boxLogger := log.WithField("boxId", boxId)
97
-		boxLogger.Info("checking box for events")
97
+		boxLogger.Debug("checking box for events")
98 98
 
99 99
 		box, res, err := checkBox(boxId, localConf, osem)
100 100
 		if err != nil {

+ 7
- 4
core/healthcheck_measurement_faulty.go View File

@@ -2,7 +2,8 @@ package core
2 2
 
3 3
 import (
4 4
 	"fmt"
5
-	"strconv"
5
+
6
+	"github.com/noerw/osem_notify/utils"
6 7
 )
7 8
 
8 9
 var checkMeasurementFaulty = checkType{
@@ -20,7 +21,7 @@ var checkMeasurementFaulty = checkType{
20 21
 			Status:     CheckOk,
21 22
 		}
22 23
 
23
-		val, err := strconv.ParseFloat(s.LastMeasurement.Value, 64)
24
+		val, err := utils.ParseFloat(s.LastMeasurement.Value)
24 25
 		if err != nil {
25 26
 			return result, err
26 27
 		}
@@ -42,8 +43,10 @@ type faultyValue struct {
42 43
 }
43 44
 
44 45
 var faultyVals = map[faultyValue]bool{
46
+	// @TODO: add UV & light sensor: check for 0 if not sunset based on boxlocation
47
+	// @TODO: add BME280 and other sensors..
45 48
 	faultyValue{sensor: "BMP280", val: 0.0}:  true,
46
-	faultyValue{sensor: "HDC1008", val: 0.0}: true,
49
+	faultyValue{sensor: "HDC1008", val: 0.0}: true, // @FIXME: check should be on luftfeuchte only!
47 50
 	faultyValue{sensor: "HDC1008", val: -40}: true,
48
-	faultyValue{sensor: "SDS 011", val: 0.0}: true,
51
+	faultyValue{sensor: "SDS 011", val: 0.0}: true, // @FIXME: 0.0 seems to be a correct value, need to check over longer periods
49 52
 }

+ 4
- 3
core/healthcheck_measurement_minmax.go View File

@@ -2,7 +2,8 @@ package core
2 2
 
3 3
 import (
4 4
 	"fmt"
5
-	"strconv"
5
+
6
+	"github.com/noerw/osem_notify/utils"
6 7
 )
7 8
 
8 9
 const (
@@ -36,12 +37,12 @@ func validateMeasurementMinMax(e NotifyEvent, s Sensor, b Box) (CheckResult, err
36 37
 		Status:     CheckOk,
37 38
 	}
38 39
 
39
-	thresh, err := strconv.ParseFloat(e.Threshold, 64)
40
+	thresh, err := utils.ParseFloat(e.Threshold)
40 41
 	if err != nil {
41 42
 		return result, err
42 43
 	}
43 44
 
44
-	val, err := strconv.ParseFloat(s.LastMeasurement.Value, 64)
45
+	val, err := utils.ParseFloat(s.LastMeasurement.Value)
45 46
 	if err != nil {
46 47
 		return result, err
47 48
 	}

+ 1
- 1
core/healthchecks.go View File

@@ -84,7 +84,7 @@ func (box Box) RunChecks() ([]CheckResult, error) {
84 84
 
85 85
 			result, err := checker.checkFunc(event, s, box)
86 86
 			if err != nil {
87
-				boxLogger.Errorf("error checking event %s", event.Type)
87
+				boxLogger.Errorf("error checking event %s: %v", event.Type, err)
88 88
 			}
89 89
 
90 90
 			results = append(results, result)

+ 3
- 2
core/notifier_email.go View File

@@ -65,11 +65,12 @@ func (n EmailNotifier) Submit(notification Notification) error {
65 65
 
66 66
 	from := viper.GetString("email.from")
67 67
 	body := fmt.Sprintf(
68
-		"From: openSenseMap Notifier <%s>\nDate: %s\nSubject: %s\nContent-Type: text/plain; charset=\"utf-8\"\n\n%s",
68
+		"From: openSenseMap Notifier <%s>\nDate: %s\nSubject: %s\nContent-Type: text/plain; charset=\"utf-8\"\n\n%s%s",
69 69
 		from,
70 70
 		time.Now().Format(time.RFC1123Z),
71 71
 		notification.Subject,
72
-		notification.Body)
72
+		notification.Body,
73
+		"\n\n--\nSent automatically by osem_notify (https://github.com/noerw/osem_notify)")
73 74
 
74 75
 	// Connect to the server, authenticate, set the sender and recipient,
75 76
 	// and send the email all in one step.

+ 4
- 4
core/notifier_slack.go View File

@@ -11,8 +11,8 @@ import (
11 11
 
12 12
 var slackClient = sling.New().Client(&http.Client{})
13 13
 
14
-var notificationColors = map[string]string {
15
-	CheckOk: "#00ff00",
14
+var notificationColors = map[string]string{
15
+	CheckOk:  "#00ff00",
16 16
 	CheckErr: "#ff0000",
17 17
 }
18 18
 
@@ -47,9 +47,9 @@ func (n SlackNotifier) New(config TransportConfig) (AbstractNotifier, error) {
47 47
 
48 48
 func (n SlackNotifier) Submit(notification Notification) error {
49 49
 	message := &SlackMessage{
50
-		Username: "osem_notify box healthcheck",
50
+		Username:    "osem_notify box healthcheck",
51 51
 		Text:        notification.Subject,
52
-		Attachments: []SlackAttachment{ { notification.Body, notificationColors[notification.Status] } },
52
+		Attachments: []SlackAttachment{{notification.Body, notificationColors[notification.Status]}},
53 53
 	}
54 54
 
55 55
 	req, err := slackClient.Post(n.webhook).BodyJSON(message).Request()

+ 3
- 3
core/notifiers.go View File

@@ -139,7 +139,7 @@ func ComposeNotification(box *Box, checks []CheckResult) Notification {
139 139
 		resolved     string
140 140
 		resolvedList string
141 141
 		errList      string
142
-		status string
142
+		status       string
143 143
 	)
144 144
 	if len(resolvedTexts) != 0 {
145 145
 		resolvedList = fmt.Sprintf("Resolved issue(s):\n\n%s\n\n", strings.Join(resolvedTexts, "\n"))
@@ -153,9 +153,9 @@ func ComposeNotification(box *Box, checks []CheckResult) Notification {
153 153
 	}
154 154
 
155 155
 	return Notification{
156
-		Status: status,
156
+		Status:  status,
157 157
 		Subject: fmt.Sprintf("Issues %swith your box \"%s\" on opensensemap.org!", resolved, box.Name),
158
-		Body: fmt.Sprintf("A check at %s identified the following updates for your box \"%s\":\n\n%s%sYou may visit https://opensensemap.org/explore/%s for more details.\n\n--\nSent automatically by osem_notify (https://github.com/noerw/osem_notify)",
158
+		Body: fmt.Sprintf("A check at %s identified the following updates for your box \"%s\":\n\n%s%sYou may visit https://opensensemap.org/explore/%s for more details.",
159 159
 			time.Now().Round(time.Minute), box.Name, errList, resolvedList, box.Id),
160 160
 	}
161 161
 }

+ 1
- 1
core/osem_api.go View File

@@ -9,7 +9,7 @@ import (
9 9
 )
10 10
 
11 11
 type OsemError struct {
12
-	Code    int    `json:"code"`
12
+	Code    string `json:"code"`
13 13
 	Message string `json:"message"`
14 14
 }
15 15
 

+ 6
- 0
utils/config.go View File

@@ -3,6 +3,8 @@ package utils
3 3
 import (
4 4
 	"os"
5 5
 	"path"
6
+	"strconv"
7
+	"strings"
6 8
 
7 9
 	log "github.com/sirupsen/logrus"
8 10
 	"github.com/spf13/viper"
@@ -70,3 +72,7 @@ func PrintConfig() {
70 72
 func printKV(key, val interface{}) {
71 73
 	log.Debugf("%20s: %v", key, val)
72 74
 }
75
+
76
+func ParseFloat(val string) (float64, error) {
77
+	return strconv.ParseFloat(strings.TrimSpace(val), 64)
78
+}

Loading…
Cancel
Save