/
reporter.go
169 lines (151 loc) · 4.08 KB
/
reporter.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"time"
"bytes"
"net"
"net/http"
"os/exec"
"errors"
"strings"
"fmt"
"log"
"encoding/json"
)
type Info interface {
Encode() ([]byte, error);
GetType() (string);
}
type Module interface {
Monitor() (Info, error);
}
type Reporter struct {
config *Config;
modules []Module;
running bool;
moduleType string;
url string;
}
type HostInfo struct {
Hostnames []string `json:"hostnames"`;
IpAddresses []string `json:"ipAddresses"`;
Uname string `json:"uname"`;
UnameA string `json:"unameA"`;
}
func (this *Reporter) Report() (error) {
reportsData := make([]string, 0);
for i := range(this.modules) {
info, err := this.modules[i].Monitor();
if err != nil {
return err;
}
reportData, err := info.Encode();
if err != nil {
return err
}
reportDataStr := string(reportData)
reportDataStr = fmt.Sprintf(`{"type":"%s","report":%s}`, info.GetType(), reportDataStr);
reportsData = append(reportsData, reportDataStr);
}
hostData, err := json.Marshal(this.GetHostInfo());
reportsDataJson := fmt.Sprintf("[%s]", strings.Join(reportsData, ","))
envelope := fmt.Sprintf(`{"host":%s,"reports":%s}`, hostData, reportsDataJson);
req, err := http.NewRequest("POST", this.url, bytes.NewBuffer([]byte(envelope)))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
log.Printf("Reported %s status to %s\n", this.moduleType, this.url);
defer resp.Body.Close()
if resp.StatusCode != 200 {
return errors.New("Invalid response from Atoll server: " + resp.Status)
}
return nil
}
func arrayContains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func (this *Reporter) GetHostInfo() (HostInfo) {
hostInfo := HostInfo{};
hostInfo.Hostnames = make([]string, 0);
if len(this.config.Hostname) > 0 {
hostInfo.Hostnames = append(hostInfo.Hostnames, this.config.Hostname);
}
data, err := exec.Command("hostname").Output();
if err == nil {
hostInfo.Hostnames = append(hostInfo.Hostnames, strings.TrimSpace(string(data)));
}
data, err = exec.Command("uname").Output();
if err == nil {
hostInfo.Uname = strings.TrimSpace(string(data));
}
data, err = exec.Command("uname", "-a").Output();
if err == nil {
hostInfo.UnameA = strings.TrimSpace(string(data));
}
hostInfo.IpAddresses = this.GetIpAddresses();
for _,ipAddress := range hostInfo.IpAddresses {
hostnames, err := net.LookupAddr(ipAddress)
if err == nil {
for _,hostname := range hostnames {
if arrayContains(hostInfo.Hostnames, hostname) == false {
hostInfo.Hostnames = append(hostInfo.Hostnames, hostname);
}
}
}
}
return hostInfo;
}
func (this *Reporter) GetIpAddresses() ([]string) {
ipAddresses := make([]string, 0);
interfaces, err := net.Interfaces()
if err != nil {
log.Printf("Warning could not get net.Interfaces information: %s\n", err);
return ipAddresses
}
for _, i := range interfaces {
addrs, err := i.Addrs()
if err == nil {
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
ipnet := addr.(*net.IPNet);
if (!ipnet.IP.IsLoopback()) {
ipAddresses = append(ipAddresses, ip.String());
}
}
}
}
return ipAddresses;
}
func (this *Reporter) Start() (error) {
numSeconds, err := IntervalToSeconds(this.config.Publish.Frequency);
if numSeconds == 0 {
return errors.New("Need an interval of at least 1 second")
}
if err != nil {
return errors.New("Invalid interval configured")
}
for this.running == true {
err := this.Report();
if err != nil {
log.Printf("Warning, could not report: %v\n", err);
}
time.Sleep(time.Duration(numSeconds) * time.Second);
}
return nil
}
func (this *Reporter) Stop() () {
this.running = false;
}