-
Notifications
You must be signed in to change notification settings - Fork 8
/
lilpinger.go
80 lines (64 loc) · 1.19 KB
/
lilpinger.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
package main
import (
"io/ioutil"
"log"
"net/http"
"strings"
"time"
"lilpinger/config"
"lilpinger/tools"
)
var (
c = make(chan string)
)
func main() {
var err error
// read our text file of urls
fileData, err := ioutil.ReadFile(config.Params.URLsFile)
if err != nil {
log.Fatal(err)
}
urls := []string{}
// split at new lines
urls = strings.Split(string(fileData), "\n")
for _, v := range urls {
log.Println(v)
if v != "" {
go ping(v)
}
}
// output logs to the terminal
for i := range c {
log.Println(i)
}
}
func ping(url string) {
// loop forever
for {
// lag timer start
start := time.Now()
// make our request
res, err := http.Get(url)
if err != nil {
msg := "Error:" + err.Error()
c <- msg
reportError(msg)
} else {
lag := time.Since(start)
var msg string
// running slow
if lag > time.Duration(config.Params.LagThreshold)*time.Second {
msg = url + " lag: " + lag.String()
reportError(msg)
}
msg = url + ", lag: " + lag.String()
c <- msg
res.Body.Close()
}
time.Sleep(time.Duration(config.Params.PingInterval) * time.Second)
}
}
func reportError(msg string) {
tools.SendSMS(msg)
tools.SendMail(msg)
}