Compare commits
No commits in common. 'master' and 'develop' have entirely different histories.
@ -0,0 +1,26 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TODO: insert automatically via build step?
|
||||||
|
const (
|
||||||
|
VERSION = "1.0.7"
|
||||||
|
BUILDDATE = "2018-06-26T00:59:00+02"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(versionCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
var versionCmd = &cobra.Command{
|
||||||
|
Use: "version",
|
||||||
|
Short: "Get build and version information",
|
||||||
|
Long: "osem_notify version returns its build and version information",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
fmt.Printf("%s %s", VERSION, BUILDDATE)
|
||||||
|
},
|
||||||
|
}
|
@ -1,73 +0,0 @@
|
|||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/dghubble/sling"
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
)
|
|
||||||
|
|
||||||
var slackClient = sling.New().Client(&http.Client{})
|
|
||||||
|
|
||||||
var notificationColors = map[string]string{
|
|
||||||
CheckOk: "#00ff00",
|
|
||||||
CheckErr: "#ff0000",
|
|
||||||
}
|
|
||||||
|
|
||||||
// slack Notifier has no configuration
|
|
||||||
type SlackNotifier struct {
|
|
||||||
webhook string
|
|
||||||
}
|
|
||||||
|
|
||||||
type SlackMessage struct {
|
|
||||||
Text string `json:"text"`
|
|
||||||
Username string `json:"username,omitempty`
|
|
||||||
Attachments []SlackAttachment `json:"attachments,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type SlackAttachment struct {
|
|
||||||
Text string `json:"text"`
|
|
||||||
Color string `json:"color,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n SlackNotifier) New(config TransportConfig) (AbstractNotifier, error) {
|
|
||||||
// validate transport configuration
|
|
||||||
// :TransportConfSourceHack
|
|
||||||
baseUrl := viper.GetString("slack.webhook")
|
|
||||||
if baseUrl == "" {
|
|
||||||
return nil, fmt.Errorf("Missing configuration key slack.webhook")
|
|
||||||
}
|
|
||||||
|
|
||||||
return SlackNotifier{
|
|
||||||
webhook: baseUrl,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n SlackNotifier) Submit(notification Notification) error {
|
|
||||||
message := &SlackMessage{
|
|
||||||
Username: "osem_notify box healthcheck",
|
|
||||||
Text: notification.Subject,
|
|
||||||
Attachments: []SlackAttachment{{notification.Body, notificationColors[notification.Status]}},
|
|
||||||
}
|
|
||||||
|
|
||||||
req, err := slackClient.Post(n.webhook).BodyJSON(message).Request()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
c := http.Client{}
|
|
||||||
res, err2 := c.Do(req)
|
|
||||||
if err2 != nil {
|
|
||||||
return err2
|
|
||||||
}
|
|
||||||
|
|
||||||
if res.StatusCode > 200 {
|
|
||||||
defer res.Body.Close()
|
|
||||||
body, _ := ioutil.ReadAll(res.Body)
|
|
||||||
return fmt.Errorf("slack webhook failed: %s", body)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
@ -1,102 +0,0 @@
|
|||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
xmpp "github.com/mattn/go-xmpp"
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
)
|
|
||||||
|
|
||||||
var xmppClient = &xmpp.Client{} // @Hacky
|
|
||||||
|
|
||||||
// box config required for the XmppNotifier (TransportConfig.Options)
|
|
||||||
type XmppNotifier struct {
|
|
||||||
Recipients []string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n XmppNotifier) New(config TransportConfig) (AbstractNotifier, error) {
|
|
||||||
// validate transport configuration
|
|
||||||
// :TransportConfSourceHack
|
|
||||||
requiredConf := []string{"xmpp.user", "xmpp.pass", "xmpp.host", "xmpp.starttls"}
|
|
||||||
for _, key := range requiredConf {
|
|
||||||
if viper.GetString(key) == "" {
|
|
||||||
return nil, fmt.Errorf("Missing configuration key %s", key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// establish connection with server once, and share it accross instances
|
|
||||||
// @Hacky
|
|
||||||
if xmppClient.JID() == "" {
|
|
||||||
c, err := connectXmpp()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
xmppClient = c
|
|
||||||
}
|
|
||||||
|
|
||||||
// assign configuration to the notifier after ensuring the correct type.
|
|
||||||
// lesson of this project: golang requires us to fuck around with type
|
|
||||||
// assertions, instead of providing us with proper inheritance.
|
|
||||||
asserted, ok := config.Options.(XmppNotifier)
|
|
||||||
if !ok || asserted.Recipients == nil {
|
|
||||||
// config did not contain valid options.
|
|
||||||
// first try fallback: parse result of viper is a map[string]interface{},
|
|
||||||
// which requires a different assertion change
|
|
||||||
asserted2, ok := config.Options.(map[string]interface{})
|
|
||||||
if ok {
|
|
||||||
asserted3, ok := asserted2["recipients"].([]interface{})
|
|
||||||
if ok {
|
|
||||||
asserted = XmppNotifier{Recipients: []string{}}
|
|
||||||
for _, rec := range asserted3 {
|
|
||||||
asserted.Recipients = append(asserted.Recipients, rec.(string))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if asserted.Recipients == nil {
|
|
||||||
return nil, errors.New("Invalid XmppNotifier options")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return XmppNotifier{
|
|
||||||
Recipients: asserted.Recipients,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n XmppNotifier) Submit(notification Notification) error {
|
|
||||||
if xmppClient.JID() == "" {
|
|
||||||
return fmt.Errorf("xmpp client not correctly initialized!")
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, recipient := range n.Recipients {
|
|
||||||
_, err := xmppClient.Send(xmpp.Chat{
|
|
||||||
Remote: recipient,
|
|
||||||
Subject: notification.Subject,
|
|
||||||
Text: fmt.Sprintf("%s\n\n%s", notification.Subject, notification.Body),
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func connectXmpp() (*xmpp.Client, error) {
|
|
||||||
// :TransportConfSourceHack
|
|
||||||
xmppOpts := xmpp.Options{
|
|
||||||
Host: viper.GetString("xmpp.host"),
|
|
||||||
User: viper.GetString("xmpp.user"),
|
|
||||||
Password: viper.GetString("xmpp.pass"),
|
|
||||||
Resource: "osem_notify",
|
|
||||||
}
|
|
||||||
|
|
||||||
if viper.GetBool("xmpp.starttls") {
|
|
||||||
xmppOpts.NoTLS = true
|
|
||||||
xmppOpts.StartTLS = true
|
|
||||||
}
|
|
||||||
|
|
||||||
return xmppOpts.NewClient()
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
theme: jekyll-theme-hacker
|
|
||||||
plugins:
|
|
||||||
- jekyll-redirect-from
|
|
||||||
whitelist:
|
|
||||||
- jekyll-redirect-from
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
|||||||
---
|
|
||||||
redirect_to:
|
|
||||||
- "osem_notify"
|
|
||||||
---
|
|
@ -1,42 +0,0 @@
|
|||||||
## osem_notify check all
|
|
||||||
|
|
||||||
one-off check on all boxes registered on the opensensemap instance
|
|
||||||
|
|
||||||
### Synopsis
|
|
||||||
|
|
||||||
one-off check on all boxes registered on the opensensemap instance
|
|
||||||
|
|
||||||
```
|
|
||||||
osem_notify check all [flags]
|
|
||||||
```
|
|
||||||
|
|
||||||
### Options
|
|
||||||
|
|
||||||
```
|
|
||||||
-h, --help help for all
|
|
||||||
```
|
|
||||||
|
|
||||||
### Options inherited from parent commands
|
|
||||||
|
|
||||||
```
|
|
||||||
-a, --api string openSenseMap API to query against (default "https://api.opensensemap.org")
|
|
||||||
-c, --config string path to config file (default $HOME/.osem_notify.yml)
|
|
||||||
-d, --debug enable verbose logging
|
|
||||||
-l, --logformat string log format, can be plain or json (default "plain")
|
|
||||||
--no-cache send all notifications, ignoring results from previous runs. also don't update the cache.
|
|
||||||
-n, --notify string If set, will send out notifications for the specified type of check result,
|
|
||||||
otherwise results are printed to stdout only.
|
|
||||||
Allowed values are "all", "error", "ok".
|
|
||||||
You might want to run 'osem_notify debug notifications' first to verify everything works.
|
|
||||||
|
|
||||||
Notifications for failing checks are sent only once, and then cached until the issue got
|
|
||||||
resolved, unless --no-cache is set.
|
|
||||||
To clear the cache, run 'osem_notify debug cache --clear'.
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
### SEE ALSO
|
|
||||||
|
|
||||||
* [osem_notify check](osem_notify_check.md) - One-off check for events on boxes
|
|
||||||
|
|
||||||
###### Auto generated by spf13/cobra on 10-Feb-2019
|
|
@ -1,43 +0,0 @@
|
|||||||
## osem_notify debug cache
|
|
||||||
|
|
||||||
Print or clear the notifications cache
|
|
||||||
|
|
||||||
### Synopsis
|
|
||||||
|
|
||||||
osem_notify debug cache prints the contents of the notifications cache
|
|
||||||
|
|
||||||
```
|
|
||||||
osem_notify debug cache [flags]
|
|
||||||
```
|
|
||||||
|
|
||||||
### Options
|
|
||||||
|
|
||||||
```
|
|
||||||
--clear reset the notifications cache
|
|
||||||
-h, --help help for cache
|
|
||||||
```
|
|
||||||
|
|
||||||
### Options inherited from parent commands
|
|
||||||
|
|
||||||
```
|
|
||||||
-a, --api string openSenseMap API to query against (default "https://api.opensensemap.org")
|
|
||||||
-c, --config string path to config file (default $HOME/.osem_notify.yml)
|
|
||||||
-d, --debug enable verbose logging
|
|
||||||
-l, --logformat string log format, can be plain or json (default "plain")
|
|
||||||
--no-cache send all notifications, ignoring results from previous runs. also don't update the cache.
|
|
||||||
-n, --notify string If set, will send out notifications for the specified type of check result,
|
|
||||||
otherwise results are printed to stdout only.
|
|
||||||
Allowed values are "all", "error", "ok".
|
|
||||||
You might want to run 'osem_notify debug notifications' first to verify everything works.
|
|
||||||
|
|
||||||
Notifications for failing checks are sent only once, and then cached until the issue got
|
|
||||||
resolved, unless --no-cache is set.
|
|
||||||
To clear the cache, run 'osem_notify debug cache --clear'.
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
### SEE ALSO
|
|
||||||
|
|
||||||
* [osem_notify debug](osem_notify_debug.md) - Run some debugging checks on osem_notify itself
|
|
||||||
|
|
||||||
###### Auto generated by spf13/cobra on 10-Feb-2019
|
|
@ -1,43 +0,0 @@
|
|||||||
## osem_notify watch all
|
|
||||||
|
|
||||||
watch all boxes registered on the map
|
|
||||||
|
|
||||||
### Synopsis
|
|
||||||
|
|
||||||
watch all boxes registered on the map
|
|
||||||
|
|
||||||
```
|
|
||||||
osem_notify watch all [flags]
|
|
||||||
```
|
|
||||||
|
|
||||||
### Options
|
|
||||||
|
|
||||||
```
|
|
||||||
-h, --help help for all
|
|
||||||
```
|
|
||||||
|
|
||||||
### Options inherited from parent commands
|
|
||||||
|
|
||||||
```
|
|
||||||
-a, --api string openSenseMap API to query against (default "https://api.opensensemap.org")
|
|
||||||
-c, --config string path to config file (default $HOME/.osem_notify.yml)
|
|
||||||
-d, --debug enable verbose logging
|
|
||||||
-i, --interval int interval to run checks in minutes (default 30)
|
|
||||||
-l, --logformat string log format, can be plain or json (default "plain")
|
|
||||||
--no-cache send all notifications, ignoring results from previous runs. also don't update the cache.
|
|
||||||
-n, --notify string If set, will send out notifications for the specified type of check result,
|
|
||||||
otherwise results are printed to stdout only.
|
|
||||||
Allowed values are "all", "error", "ok".
|
|
||||||
You might want to run 'osem_notify debug notifications' first to verify everything works.
|
|
||||||
|
|
||||||
Notifications for failing checks are sent only once, and then cached until the issue got
|
|
||||||
resolved, unless --no-cache is set.
|
|
||||||
To clear the cache, run 'osem_notify debug cache --clear'.
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
### SEE ALSO
|
|
||||||
|
|
||||||
* [osem_notify watch](osem_notify_watch.md) - Watch boxes for events at an interval
|
|
||||||
|
|
||||||
###### Auto generated by spf13/cobra on 10-Feb-2019
|
|
Loading…
Reference in New Issue