-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
149 lines (135 loc) · 2.97 KB
/
main.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"bufio"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"gopkg.in/yaml.v2"
)
// Config :
type Config struct {
Time time.Duration
Parallel int
Rules map[string]string
}
func setUpLogFile() *os.File {
f, err := os.OpenFile("requests.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
log.SetOutput(f)
return f
}
func main() {
defer setUpLogFile().Close()
config := getConfig()
urls := getUrls()
runPeriodicalChecks(urls, config)
blockForever()
}
func getConfig() Config {
config := Config{}
dat, err := ioutil.ReadFile("config.yaml")
if err != nil {
log.Fatal(err)
}
err = yaml.Unmarshal(dat, &config)
if err != nil {
panic(err)
}
return config
}
func getUrls() (urls []string) {
fileHandle, err := os.Open("url.txt")
if err != nil {
panic(err)
}
defer fileHandle.Close()
fileScanner := bufio.NewScanner(fileHandle)
for fileScanner.Scan() {
urls = append(urls, fileScanner.Text())
}
return urls
}
func blockForever() {
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
os.Exit(1)
}()
for {
time.Sleep(1 * time.Hour)
}
}
func runPeriodicalChecks(urls []string, config Config) {
// run first checks
runChecks(urls, config)
ticker := time.NewTicker(config.Time * time.Minute)
go func() {
for {
select {
case <-ticker.C:
// run every N minutes
runChecks(urls, config)
}
}
}()
}
func runChecks(urls []string, config Config) {
var url string
var wg sync.WaitGroup
// config.Parallel to throttle no.of parallel request, so depending on system, network capabilities we can increase and decrease no.of parallel requests
chanParallel := make(chan bool, config.Parallel)
for _, url = range urls {
wg.Add(1)
chanParallel <- true
go func(url string) {
checkURL(url, config.Rules[url], &wg)
time.Sleep(1 * time.Second)
<-chanParallel
}(url)
}
wg.Wait()
}
func checkURL(url string, match string, wg *sync.WaitGroup) {
defer wg.Done()
if !strings.HasPrefix(url, "http") {
url = "http://" + url
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Printf("%s %s\n", url, err.Error())
return
}
client := &http.Client{Timeout: time.Duration(10 * time.Second)}
start := time.Now()
resp, err := client.Do(req)
if err != nil {
log.Printf("%s %s %v\n", url, err.Error(), time.Since(start))
return
}
defer resp.Body.Close()
elapsed := time.Since(start)
if resp.StatusCode != 200 {
log.Printf("%s %s %v\n", url, resp.Status, elapsed)
return
}
html, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("%s %s %v %s\n", url, resp.Status, elapsed, err.Error())
return
}
log.Printf("%s %s %v\n", url, resp.Status, elapsed)
if match != "" && strings.Contains(string(html), match) {
log.Printf("content requirement for %s is satisfied\n", url)
} else {
log.Printf("content requirement for %s isn't satisfied\n", url)
}
}