add debug command

develop
noerw 6 years ago
parent 992bc21b63
commit 13d7aea17b

@ -0,0 +1,68 @@
package cmd
import (
"fmt"
"os"
"github.com/noerw/osem_notify/core"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func init() {
debugCmd.AddCommand(debugNotificationsCmd)
rootCmd.AddCommand(debugCmd)
}
var debugCmd = &cobra.Command{
Use: "debug",
Short: "Run some debugging checks on osem_notify itself",
Long: "osem_notify debug <feature> tests the functionality of the given feature",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
log.SetLevel(log.DebugLevel)
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
printConfig()
},
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
var debugNotificationsCmd = &cobra.Command{
Use: "notifications",
Short: "Verify that notifications are working",
Long: "osem_notify debug <feature> tests the functionality of the given feature",
RunE: func(cmd *cobra.Command, args []string) error {
defaultNotifyConf := &core.NotifyConfig{}
err := viper.UnmarshalKey("defaultHealthchecks", defaultNotifyConf)
if err != nil {
return err
}
for transport, notifier := range core.Notifiers {
notLog := log.WithField("transport", transport)
opts := defaultNotifyConf.Notifications.Options
notLog.Infof("testing notifer %s with options %v", transport, opts)
n, err := notifier.New(opts)
if err != nil {
notLog.Warnf("could not initialize %s notifier. configuration might be missing?", transport)
continue
}
host, _ := os.Hostname()
err = n.Submit(core.Notification{
Subject: "Test notification from opeSenseMap notifier",
Body: fmt.Sprintf("Your notification set up on %s is working fine!", host),
})
if err != nil {
notLog.Warnf("could not submit test notification for %s notifier!", transport)
continue
}
notLog.Info("Test notification (successfully?) submitted, check the specified inbox")
}
return nil
},
}

@ -53,8 +53,8 @@ func (n EmailNotifier) ComposeNotification(box *Box, checks []CheckResult) Notif
}
return Notification{
subject: fmt.Sprintf("Issues with your box \"%s\" on opensensemap.org!", box.Name),
body: fmt.Sprintf("A check at %s identified the following issue(s) with your box %s:\n\n%s\n\nYou may visit https://opensensemap.org/explore/%s for more details.\n\n--\nSent automatically by osem_notify (https://github.com/noerw/osem_notify)",
Subject: fmt.Sprintf("Issues with your box \"%s\" on opensensemap.org!", box.Name),
Body: fmt.Sprintf("A check at %s identified the following issue(s) with your box %s:\n\n%s\n\nYou may visit https://opensensemap.org/explore/%s for more details.\n\n--\nSent automatically by osem_notify (https://github.com/noerw/osem_notify)",
time.Now().Round(time.Minute), box.Name, strings.Join(resultTexts, "\n"), box.Id),
}
}
@ -68,7 +68,7 @@ func (n EmailNotifier) Submit(notification Notification) error {
)
from := viper.GetString("email.from")
body := fmt.Sprintf("From: openSenseMap Notifier <%s>\nSubject: %s\nContent-Type: text/plain; charset=\"utf-8\"\n\n%s", from, notification.subject, notification.body)
body := fmt.Sprintf("From: openSenseMap Notifier <%s>\nSubject: %s\nContent-Type: text/plain; charset=\"utf-8\"\n\n%s", from, notification.Subject, notification.Body)
// Connect to the server, authenticate, set the sender and recipient,
// and send the email all in one step.

@ -7,7 +7,7 @@ import (
"github.com/spf13/viper"
)
var notifiers = map[string]AbstractNotifier{
var Notifiers = map[string]AbstractNotifier{
"email": EmailNotifier{},
}
@ -18,8 +18,8 @@ type AbstractNotifier interface {
}
type Notification struct {
body string
subject string
Body string
Subject string
}
//////
@ -30,7 +30,7 @@ func (box Box) GetNotifier() (AbstractNotifier, error) {
return nil, fmt.Errorf("No notification transport provided")
}
notifier := notifiers[transport]
notifier := Notifiers[transport]
if notifier == nil {
return nil, fmt.Errorf("%s is not a supported notification transport", transport)
}

Loading…
Cancel
Save