-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
182 lines (143 loc) · 3.61 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/sensu/sensu-go/types"
"github.com/spf13/cobra"
pushbullet "github.com/xconstruct/go-pushbullet"
)
var (
token, device string
stdin *os.File
allDevices bool
)
func main() {
rootCmd := configureRootCommand()
if err := rootCmd.Execute(); err != nil {
log.Fatal(err.Error())
}
}
func configureRootCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "sensu-pushbullet-handler",
Short: "The Sensu Go handler plugin for pushbullet",
RunE: run,
}
cmd.Flags().StringVarP(&token,
"token",
"t",
os.Getenv("PUSHBULLET_APP_TOKEN"),
"Pushbullet API app token, use default from PUSHBULLET_APP_TOKEN env var")
cmd.Flags().StringVarP(&device,
"device",
"d",
os.Getenv("DEVICE"),
"A device registered with Pushbullet")
cmd.Flags().BoolVar(&allDevices,
"alldevices",
false,
"Bool for sending notifications to all devices")
return cmd
}
func run(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
_ = cmd.Help()
return fmt.Errorf("invalid argument(s) received")
}
if token == "" {
_ = cmd.Help()
return fmt.Errorf("api token is empty")
}
if stdin == nil {
stdin = os.Stdin
}
eventJSON, err := ioutil.ReadAll(stdin)
if err != nil {
return fmt.Errorf("failed to read stdin: %s", err.Error())
}
event := &types.Event{}
err = json.Unmarshal(eventJSON, event)
if err != nil {
return fmt.Errorf("failed to unmarshal stdin data: %s", err.Error())
}
if err = validateEvent(event); err != nil {
return errors.New(err.Error())
}
if allDevices && device == "" {
if err = notifyPushbulletAll(event); err != nil {
return errors.New(err.Error())
}
}
if !allDevices && device != "" {
if err = notifyPushbulletOne(event); err != nil {
return errors.New(err.Error())
}
}
if !allDevices && device == "" {
fmt.Printf("%s\n", "Must chose to send notification to all devices or one")
}
if allDevices && device != "" {
fmt.Printf("%s\n", "Can not send to all device and one device")
}
return nil
}
// Send event notification to all devices
func notifyPushbulletAll(event *types.Event) error {
pushall := pushbullet.New(token)
devs, err := pushall.Devices()
if err != nil {
panic(err)
}
title := fmt.Sprintf("%s/%s", event.Entity.Name, event.Check.Name)
message := event.Check.Output
for i := 0; i < len(devs); i++ {
err = pushall.PushNote(devs[i].Iden, title, message)
// Need to add something better here than a panic.
// PB seems to hold onto old device Identities.
// For now doing nothing seems ok
if err != nil {
// panic(err)
}
}
return nil
}
func notifyPushbulletOne(event *types.Event) error {
pushone := pushbullet.New(token)
dev, err := pushone.Device(device)
if err != nil {
panic(err)
}
title := fmt.Sprintf("%s/%s", event.Entity.Name, event.Check.Name)
message := event.Check.Output
err = dev.PushNote(title, message)
// Need to add something better here than a panic.
// PB seems to hold onto old device Identities.
// For now doing nothing seems ok
if err != nil {
// panic(err)
}
return nil
}
func validateEvent(event *types.Event) error {
// Doesn't work for known sample events.
// if event.Timestamp <= 0 {
// return errors.New("timestamp is missing or must be greater than zero")
// }
if event.Entity == nil {
return errors.New("entity is missing from event")
}
if !event.HasCheck() {
return errors.New("check is missing from event")
}
if err := event.Entity.Validate(); err != nil {
return err
}
if err := event.Check.Validate(); err != nil {
return errors.New(err.Error())
}
return nil
}