Skip to content

Commit 8ef3b32

Browse files
committed
fixed panic happening when trying to call res.Body.Close() on URLs that failed DNS resolution as reported by mcqueenorama. cleaned up error messages. fixed email credential check
1 parent cb1a437 commit 8ef3b32

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

lilpinger.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"fmt"
54
"io/ioutil"
65
"log"
76
"net/http"
@@ -17,6 +16,8 @@ var (
1716
)
1817

1918
func main() {
19+
var err error
20+
2021
// read our text file of urls
2122
fileData, err := ioutil.ReadFile(config.Params.URLsFile)
2223
if err != nil {
@@ -36,7 +37,7 @@ func main() {
3637

3738
// output logs to the terminal
3839
for i := range c {
39-
fmt.Println(i)
40+
log.Println(i)
4041
}
4142
}
4243

@@ -48,12 +49,9 @@ func ping(url string) {
4849

4950
// make our request
5051
res, err := http.Get(url)
51-
5252
if err != nil {
5353
msg := "Error:" + err.Error()
5454

55-
fmt.Println(msg)
56-
5755
c <- msg
5856
reportError(msg)
5957
} else {
@@ -68,9 +66,10 @@ func ping(url string) {
6866

6967
msg = url + ", lag: " + lag.String()
7068
c <- msg
69+
70+
res.Body.Close()
7171
}
7272

73-
res.Body.Close()
7473
time.Sleep(time.Duration(config.Params.PingInterval) * time.Second)
7574
}
7675
}

tools/SMS.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
func SendSMS(msg string) {
1212
if config.Params.Twilio.AccountSid == "" || config.Params.Twilio.AuthToken == "" {
13-
log.Println("twilio creds not set")
13+
log.Println("twilio creds not set. skipping SMS notification")
1414
return
1515
}
1616

tools/SMTP.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ import (
99
)
1010

1111
func SendMail(msg string) {
12+
var err error
13+
1214
if config.Params.SMTP.Email == "" || config.Params.SMTP.Password == "" || config.Params.SMTP.Server == "" || config.Params.SMTP.Port == "" {
13-
log.Println("SMTP creds not set")
15+
log.Println("SMTP creds not set. skipping email notification")
16+
return
1417
}
1518

1619
auth := smtp.PlainAuth(
@@ -21,7 +24,7 @@ func SendMail(msg string) {
2124
)
2225

2326
server := fmt.Sprintf("%v:%v", config.Params.SMTP.Server, config.Params.SMTP.Port)
24-
err := smtp.SendMail(
27+
err = smtp.SendMail(
2528
server,
2629
auth,
2730
config.Params.SMTP.Email,
@@ -30,6 +33,6 @@ func SendMail(msg string) {
3033
)
3134

3235
if err != nil {
33-
fmt.Println("Error sending mail - %v - message: ", err, msg)
36+
log.Printf("error sending mail: %v. message: %v\n", err, msg)
3437
}
3538
}

0 commit comments

Comments
 (0)