-
Notifications
You must be signed in to change notification settings - Fork 0
/
uptimeKuma.go
220 lines (181 loc) · 6.53 KB
/
uptimeKuma.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
Copyright © 2024 rwxd
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
uptimekuma "github.com/rwxd/notify-me/services/uptimeKuma"
"github.com/sagikazarmark/slog-shim"
"github.com/spf13/cobra"
)
var uptimeKumaCmd = &cobra.Command{
Use: "uptime-kuma",
Short: "Send a push notification to an uptime-kuma instance",
PreRun: func(cmd *cobra.Command, args []string) {
setupLogging()
},
Run: func(cmd *cobra.Command, args []string) {
if err := ensureUptimeKumaConfigCorrect(cmd); err != nil {
fmt.Println(err)
cmd.Help()
os.Exit(1)
} else if err := ensureUptimeKumaDefaultCmdConfigCorrect(cmd); err != nil {
fmt.Println(err)
cmd.Help()
os.Exit(1)
}
instance, _ := cmd.Flags().GetString("instance")
token, _ := cmd.Flags().GetString("token")
message, _ := cmd.Flags().GetString("message")
ping, _ := cmd.Flags().GetString("ping")
down, _ := cmd.Flags().GetBool("down")
up, _ := cmd.Flags().GetBool("up")
if !strings.Contains(instance, "http://") || !strings.Contains(instance, "https://") {
instance = "https://" + instance
}
if err := uptimekuma.SendMonitorStatus(instance, token, up || !down, message, ping); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Sent status to uptime-kuma")
},
}
var uptimeKumaWrapCmd = &cobra.Command{
Use: "wrap",
Short: "Wrap a command and send a push notification to an uptime-kuma instance",
PreRun: func(cmd *cobra.Command, args []string) {
setupLogging()
},
Run: func(cmd *cobra.Command, args []string) {
if err := ensureUptimeKumaConfigCorrect(cmd); err != nil {
fmt.Println(err)
cmd.Help()
os.Exit(1)
} else if err := ensureUptimeKumaWrapCmdConfigCorrect(cmd, args); err != nil {
fmt.Println(err)
cmd.Help()
os.Exit(1)
}
instance, _ := cmd.Flags().GetString("instance")
token, _ := cmd.Flags().GetString("token")
message, _ := cmd.Flags().GetString("message")
onlyMessage, _ := cmd.Flags().GetBool("only-message")
onlyFailure, _ := cmd.Flags().GetBool("error")
onlySuccess, _ := cmd.Flags().GetBool("success")
reverse, _ := cmd.Flags().GetBool("reverse")
if !strings.Contains(instance, "http://") || !strings.Contains(instance, "https://") {
instance = "https://" + instance
}
program := args[0]
programArgs := args[1:]
command := exec.Command(program, programArgs...)
var output bytes.Buffer
command.Stdout = &output
command.Stderr = &output
slog.Debug("Running command", "program", program, "args", programArgs)
err := command.Run()
if onlyMessage {
} else if message != "" {
message += "\n" + output.String()
} else {
message = output.String()
}
statusUp := true
if err != nil {
slog.Debug("Command failed", "error", err)
if message == "" {
message = err.Error()
} else {
message = message + "\n" + err.Error()
}
if !reverse {
statusUp = false
} else {
slog.Debug("Reverse is set, setting status to up")
}
if onlySuccess {
slog.Debug("Only success is set, not sending status")
return
}
if err := uptimekuma.SendMonitorStatus(instance, token, statusUp, message, ""); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Sent status to uptime-kuma")
} else {
if reverse {
slog.Debug("Reverse is set, setting status to down")
statusUp = false
}
if onlyFailure {
slog.Debug("Only error is set, not sending status")
return
}
if err := uptimekuma.SendMonitorStatus(instance, token, statusUp, message, ""); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Sent status to uptime-kuma")
}
},
}
func ensureUptimeKumaConfigCorrect(cmd *cobra.Command) error {
if !cmd.Flag("token").Changed {
return fmt.Errorf("You must set the token")
}
if !cmd.Flag("instance").Changed {
return fmt.Errorf("You must set the instance")
}
return nil
}
func ensureUptimeKumaDefaultCmdConfigCorrect(cmd *cobra.Command) error {
if cmd.Flag("down").Changed && cmd.Flag("up").Changed {
return fmt.Errorf("You can't set both down and up")
} else if !cmd.Flag("down").Changed && !cmd.Flag("up").Changed {
return fmt.Errorf("You must set either down or up")
}
return nil
}
func ensureUptimeKumaWrapCmdConfigCorrect(cmd *cobra.Command, args []string) error {
if cmd.Flag("fail").Changed && cmd.Flag("success").Changed {
return fmt.Errorf("You can't set both error and success")
}
if len(args) == 0 {
return fmt.Errorf("You must provide a command to wrap")
}
return nil
}
func init() {
rootCmd.AddCommand(uptimeKumaCmd)
uptimeKumaCmd.AddCommand(uptimeKumaWrapCmd)
uptimeKumaCmd.PersistentFlags().StringP("instance", "i", "", "The instance to send the notification to")
uptimeKumaCmd.PersistentFlags().StringP("token", "t", "", "Token for the push monitor")
uptimeKumaCmd.Flags().StringP("message", "m", "", "Message")
uptimeKumaCmd.Flags().StringP("ping", "p", "", "Measurement number to send to the monitor")
uptimeKumaCmd.Flags().Bool("down", false, "Set the monitor to down")
uptimeKumaCmd.Flags().Bool("up", false, "Set the monitor to up")
uptimeKumaWrapCmd.Flags().Bool("fail", false, "Send a notification only if the command fails")
uptimeKumaWrapCmd.Flags().Bool("success", false, "Send a notification only if the command succeeds")
uptimeKumaWrapCmd.Flags().Bool("reverse", false, "Send a up notification if the command fails and a down notification if the command succeeds")
uptimeKumaWrapCmd.Flags().StringP("message", "m", "", "Message before stdout/stderr")
uptimeKumaWrapCmd.Flags().Bool("only-message", false, "Only send the custom message, no stdout/stderr")
}