-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (75 loc) · 1.69 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
package main
import (
"bufio"
"fmt"
"io"
"net/http"
relic "new_relic_example/new_relic"
"os"
"path/filepath"
"strings"
"github.com/newrelic/go-agent/v3/newrelic"
)
type Host struct {
Name string
}
func main() {
appName := "App Example"
license := "LICENSE KEY"
newRelicClient := relic.New(appName, license)
txn := newRelicClient.StartTransaction("transaction")
defer txn.End()
hosts := readFile(txn)
testHosts(hosts, txn)
}
func testHosts(hosts []Host, txn *newrelic.Transaction) {
for _, host := range hosts {
segment := txn.StartSegment("testHosts")
if host.Name != "" {
response, err := http.Get(host.Name)
if err != nil {
txn.NoticeError(newrelic.Error{
Message: "error on make request",
Class: "TestHosts",
Attributes: map[string]interface{}{
"HOSTNAME": host.Name,
},
})
fmt.Println("error on make request")
}
segment.AddAttribute("HOSTNAME", host.Name)
segment.AddAttribute("STATUS", response.StatusCode)
if response.StatusCode == 200 {
fmt.Printf("Host: %s está online\n", host.Name)
} else {
fmt.Printf("Host: %s está offline\n", host.Name)
}
}
segment.End()
}
}
func readFile(txn *newrelic.Transaction) []Host {
var hosts []Host
path, _ := filepath.Abs("hosts.txt")
file, err := os.Open(path)
if err != nil {
txn.NoticeError(newrelic.Error{
Message: "error on open file",
Class: "ReadFile",
})
fmt.Println("error on open file")
return nil
}
reader := bufio.NewReader(file)
for {
row, err := reader.ReadString('\n')
row = strings.TrimSpace(row)
host := Host{Name: row}
hosts = append(hosts, host)
if err == io.EOF {
break
}
}
file.Close()
return hosts
}