-
Notifications
You must be signed in to change notification settings - Fork 0
/
Internet.go
61 lines (48 loc) · 1.09 KB
/
Internet.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
//Simple Internet monitor with GO
package main
import (
"time"
"net"
"fmt"
"os"
"log")
func create_log(message string) error {
f, err := os.OpenFile("connection.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatal(err)
}
//defer to close when you're done with it, not because you think it's idiomatic!
defer f.Close()
//set output of logs to f
log.SetOutput(f)
//test case
log.Println(message)
return err
}
func main(){
err := create_log("Starting the script ...")
if err != nil {
fmt.Println("Error")
}
status := true
tFailed := time.Now()
tRestored := time.Now()
for {
time.Sleep(1 * time.Second)
if status == true {
_, err = net.LookupIP("google.com")
if err != nil {
create_log("Internet went down ")
status = false
tFailed = time.Now()
}
} else {
_, err := net.LookupIP("google.com")
if err == nil {
tRestored = time.Now()
create_log(fmt.Sprintf(" Internet restored after %v" , tRestored.Sub(tFailed).Seconds()))
status = true
}
}
}
}