-
Notifications
You must be signed in to change notification settings - Fork 0
/
zennag.go
88 lines (69 loc) · 1.28 KB
/
zennag.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
package main
import (
"fmt"
"net/http"
"net/url"
"os"
"time"
"github.com/boltdb/bolt"
)
func Worker(jobs <-chan string, alerts chan<- string, db *bolt.DB) {
for j := range jobs {
ts := time.Now()
resp, err := http.Get(j)
if err != nil {
fmt.Println(err)
continue
}
//get HTTP request time
te := time.Now()
dur := te.Sub(ts)
defer resp.Body.Close()
u, err := url.Parse(j)
if err != nil {
fmt.Println(err)
continue
}
if err := SaveStatus(db, u, resp, dur); err != nil {
fmt.Println(err)
}
}
}
func main() {
workerPoolSize := 3
config := ParseConfig()
urls := config.Urls
timeout := config.Timeout
db := OpenDb()
err := PrepareDb(db, urls)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer db.Close()
jobs := make(chan string, 100)
alerts := make(chan string, 100)
// only show stored info in db
if len(os.Args) > 1 && os.Args[1] == "-v" {
for _, u := range urls {
if err := ShowStatus(db, u); err != nil {
fmt.Println(err)
}
}
os.Exit(0)
}
for w := 1; w <= workerPoolSize; w++ {
go Worker(jobs, alerts, db)
}
go AlertWorker(urls, db)
for {
for i := 0; i < len(urls); i++ {
jobs <- urls[i]
}
time.Sleep(timeout * time.Second)
}
close(jobs)
for _, u := range urls {
ShowStatus(db, u)
}
}