Skip to content

Commit 74ae559

Browse files
committed
All testing function
1 parent 5ec43bf commit 74ae559

File tree

1 file changed

+89
-7
lines changed

1 file changed

+89
-7
lines changed

main.go

Lines changed: 89 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"fmt"
66
"log"
77
"net/http"
8+
"os"
89
"runtime"
10+
"strings"
911
"time"
1012

1113
"github.com/ReinforceZwei/network-mon/config"
@@ -20,6 +22,8 @@ func main() {
2022
}
2123
log.Println("Loaded config file from default location")
2224

25+
handleTestArgs(os.Args[1:], c)
26+
2327
var p pingwrap.PingWrap
2428
if runtime.GOOS == "linux" || runtime.GOOS == "darwin" {
2529
p = pingwrap.PingLinux{}
@@ -33,7 +37,6 @@ func main() {
3337
for {
3438
if p.PingOnce(c.TestTarget[0]) {
3539
// Ping success. Wait for next test cycle
36-
log.Printf("ping %s success\n", c.TestTarget[0])
3740
} else {
3841
// Ping fail. Retry other test target
3942
log.Printf("ping %s failed. Enter rapid ping mode...\n", c.TestTarget[0])
@@ -104,7 +107,7 @@ func main() {
104107
}
105108
}
106109
}
107-
log.Printf("Sleeping for %d seconds\n", c.TestIntervalSecond)
110+
//log.Printf("Sleeping for %d seconds\n", c.TestIntervalSecond)
108111
time.Sleep(time.Duration(c.TestIntervalSecond) * time.Second)
109112
}
110113
}
@@ -124,16 +127,95 @@ func rapidPingTest(round, interval int, targets []string, p pingwrap.PingWrap) b
124127

125128
func notifyResumeNormal(webhookUrl, payload, message string) {
126129
if webhookUrl != "" && payload != "" && message != "" {
127-
client := http.Client{
128-
Timeout: 15 * time.Second,
129-
}
130130
payload = fmt.Sprintf(payload, message)
131-
resp, err := client.Post(webhookUrl, "application/json", bytes.NewBuffer([]byte(payload)))
131+
err := httpPost(webhookUrl, "application/json", payload)
132132
if err != nil {
133133
log.Println("notifyResumeNormal: http request error: " + err.Error())
134134
} else {
135135
log.Println("notifyResumeNormal: message sent")
136-
defer resp.Body.Close()
136+
}
137+
}
138+
}
139+
140+
func httpPost(url, contentType, body string) error {
141+
client := http.Client{
142+
Timeout: 15 * time.Second,
143+
}
144+
resp, err := client.Post(url, contentType, bytes.NewBuffer([]byte(body)))
145+
if err != nil {
146+
return err
147+
} else {
148+
defer resp.Body.Close()
149+
return nil
150+
}
151+
}
152+
153+
func handleTestArgs(args []string, c *config.AppConfig) {
154+
if len(args) > 0 {
155+
if strings.ToLower(args[0]) == "test" {
156+
if len(args) > 1 {
157+
switch strings.ToLower(args[1]) {
158+
case "webhook":
159+
testWebhook(c)
160+
161+
case "ping":
162+
testPing(c)
163+
164+
case "ssh":
165+
testSsh(c)
166+
167+
case "all":
168+
testWebhook(c)
169+
testPing(c)
170+
testSsh(c)
171+
172+
default:
173+
log.Println("Unknown test function")
174+
}
175+
} else {
176+
log.Println("Test function. Available function: webhook, ping, ssh, all")
177+
}
178+
os.Exit(0)
179+
}
180+
}
181+
}
182+
183+
func testWebhook(c *config.AppConfig) {
184+
payload := fmt.Sprintf(c.Notify.MessagePayload, "[netmon] Test message")
185+
err := httpPost(c.Notify.Url, "application/json", payload)
186+
if err != nil {
187+
log.Println("webhook test: http request error: " + err.Error())
188+
} else {
189+
log.Println("webhook test: ok")
190+
}
191+
}
192+
193+
func testSsh(c *config.AppConfig) {
194+
conn, err := ssh.Connect(c.Router+":"+"22", c.SshUser, c.SshPassword)
195+
if err == nil {
196+
err = conn.Execute("pwd")
197+
if err == nil {
198+
log.Println("ssh test: ok")
199+
return
200+
}
201+
}
202+
log.Println("ssh test: error: " + err.Error())
203+
}
204+
205+
func testPing(c *config.AppConfig) {
206+
var p pingwrap.PingWrap
207+
if runtime.GOOS == "linux" || runtime.GOOS == "darwin" {
208+
p = pingwrap.PingLinux{}
209+
} else if runtime.GOOS == "windows" {
210+
p = pingwrap.PingWindows{}
211+
} else {
212+
log.Fatalf("Platform not supported: %s\n", runtime.GOOS)
213+
}
214+
for _, ip := range c.TestTarget {
215+
if p.PingOnce(ip) {
216+
log.Printf("ping test: %s ok\n", ip)
217+
} else {
218+
log.Printf("ping test: %s failed\n", ip)
137219
}
138220
}
139221
}

0 commit comments

Comments
 (0)