-
Notifications
You must be signed in to change notification settings - Fork 1
/
gnm_collect.go
215 lines (201 loc) · 8.15 KB
/
gnm_collect.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"github.com/geonetwork/gnm_collect/gnmsys"
"os"
"log"
"github.com/gonum/plot/vg"
"time"
"flag"
"io"
"io/ioutil"
"fmt"
"net/http"
"github.com/geonetwork/gnm_collect/gnmserver"
)
type Listener interface {
start()
}
var targetServer, username, password, out string
var port = flag.Int("port", 10100, "The port to start the http server on")
var logging = flag.Bool("logging", false, "Set to true to enable logging")
func main() {
flag.StringVar(&targetServer, "target", "http://localhost:8080", "The url of the server to test. Default [http://localhost:8080/geonetwork")
flag.StringVar(&username, "user", "", "The user name of a user that has Monitor privileges on the target server")
flag.StringVar(&password, "pass", "", "The user's password")
flag.StringVar(&out, "out", "./gnm_reports", "The directory to write the reports to.")
flag.Parse()
_, err := http.Get(fmt.Sprintf("http://localhost:%d/status", *port))
if err == nil {
fmt.Printf("Monitoring server is already running\n")
os.Exit(0)
}
var logOut io.Writer = ioutil.Discard
if *logging {
f, err := os.OpenFile("gnm_collect.log", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer f.Close()
logOut = f
}
log.SetOutput(logOut)
if targetServer[len(targetServer) - 1] == '/' {
targetServer = targetServer[:len(targetServer) - 1]
}
config := loadSystemConfig()
reports := loadReports()
sys := gnmsys.CreateSystem(config, reports...)
listener := gnmsys.CliListener{sys}
go listener.Start()
server := gnmserver.Server{
Port: *port,
Config: config,
Sys: &sys}
go server.Start()
sys.Run()
}
func loadSystemConfig() gnmsys.SysConfig {
if username == "" || password == "" {
fmt.Fprintf(os.Stderr, "-user and -pass are required\n")
flag.Usage()
os.Exit(1)
}
return gnmsys.SysConfig{
UrlStem: targetServer,
Username: username,
Password: password,
OutputDir: out,
SampleConfigs: []gnmsys.SampleConfig{
gnmsys.SampleConfig{Name: "Last Five Minutes", DirName: "five_minutes", MaxSamples: 5 * 60, UpdateInterval: time.Second},
gnmsys.SampleConfig{Name: "Last Five Hours", DirName: "five_hours", MaxSamples: 5 * 60, UpdateInterval: time.Minute},
gnmsys.SampleConfig{Name: "Last Five Days", DirName: "five_days", MaxSamples: 5 * 24, UpdateInterval: time.Hour},
gnmsys.SampleConfig{Name: "Last Five Months", DirName: "five_months", MaxSamples: 5 * 30, UpdateInterval: 24 * time.Hour},
}}
}
func loadReports() []gnmsys.ReportFactory {
return [] gnmsys.ReportFactory{
gnmsys.LineReportFactoryBuilder{
Title: "Memory",
Filename: "memory",
YAxis: "Bytes",
X: 12 * vg.Inch,
Y: 5 * vg.Inch,
CollectorFactories: []gnmsys.CollectorFactory{
gnmsys.NewFloatCollector("Max", "jvm", "memory", "totalMax"),
gnmsys.NewFloatCollector("Total Used", "jvm", "memory", "totalUsed"),
gnmsys.NewFloatCollector("Heap Used", "jvm", "memory", "heapUsed")}}.ToRequestFactory(),
gnmsys.LineReportFactoryBuilder{
Title: "Resource Usage (%)",
Filename: "resource_usage",
YAxis: "%",
X: 12 * vg.Inch,
Y: 5 * vg.Inch,
CollectorFactories: []gnmsys.CollectorFactory{
gnmsys.NewFloatCollector("Non Heap Mem Used", "jvm", "memory", "non_heap_usage"),
gnmsys.NewFloatCollector("Heap Mem Used", "jvm", "memory", "heap_usage"),
gnmsys.NewFloatCollector("File Descriptor Usage", "jvm", "fd_usage")}}.ToRequestFactory(),
gnmsys.LineReportFactoryBuilder{
Title: "Time Used by Garbage Collectors",
Filename: "garbage_collectors",
YAxis: "Milliseconds",
X: 12 * vg.Inch,
Y: 5 * vg.Inch,
CollectorFactories: []gnmsys.CollectorFactory{
gnmsys.NewFloatCollector("MarkSweep", "jvm", "garbage-collectors", "PS MarkSweep", "time"),
gnmsys.NewFloatCollector("Scavenge", "jvm", "garbage-collectors", "PS Scavenge", "time")}}.ToRequestFactory(),
gnmsys.LineReportFactoryBuilder{
Title: "CPU Load",
Filename: "cpu_load",
YAxis: "Load (%)",
X: 12 * vg.Inch,
Y: 5 * vg.Inch,
CollectorFactories: []gnmsys.CollectorFactory{
gnmsys.NewFloatCollector("System", "java.lang.management.OperatingSystemMXBean",
"System_CPU_Load", "value"),
gnmsys.NewFloatCollector("Process", "java.lang.management.OperatingSystemMXBean",
"Process_CPU_Load", "value"),
gnmsys.NewFloatCollector("Average Load", "java.lang.management.OperatingSystemMXBean",
"systemLoadAverage", "value")}}.ToRequestFactory(),
gnmsys.LineReportFactoryBuilder{
Title: "Mean Logging Events",
Filename: "mean_logging_events",
YAxis: "Events / second",
X: 12 * vg.Inch,
Y: 5 * vg.Inch,
CollectorFactories: []gnmsys.CollectorFactory{
gnmsys.NewFloatCollector("All", "org.apache.log4j.Appender",
"all", "mean"),
gnmsys.NewFloatCollector("Fatal", "org.apache.log4j.Appender",
"fatal", "mean"),
gnmsys.NewFloatCollector("Error", "org.apache.log4j.Appender",
"error", "mean"),
gnmsys.NewFloatCollector("Warn", "org.apache.log4j.Appender",
"warn", "mean"),
gnmsys.NewFloatCollector("Info", "org.apache.log4j.Appender",
"info", "mean"),
gnmsys.NewFloatCollector("Debug", "org.apache.log4j.Appender",
"debug", "mean"),
gnmsys.NewFloatCollector("Trace", "org.apache.log4j.Appender",
"trace", "mean")}}.ToRequestFactory(),
gnmsys.LineReportFactoryBuilder{
Title: "Mean Web Requests",
Filename: "mean_web_requests",
YAxis: "requests / second",
X: 12 * vg.Inch,
Y: 5 * vg.Inch,
CollectorFactories: []gnmsys.CollectorFactory{
gnmsys.NewFloatCollector("Requests", "org.fao.geonet.monitor.webapp.WebappMetricsFilter",
"requests", "rate", "mean")}}.ToRequestFactory(),
gnmsys.LineReportFactoryBuilder{
Title: "Active Web Requests",
Filename: "active_web_requests",
YAxis: "requests / second",
X: 12 * vg.Inch,
Y: 5 * vg.Inch,
CollectorFactories: []gnmsys.CollectorFactory{
gnmsys.NewFloatCollector("Requests", "org.fao.geonet.monitor.webapp.WebappMetricsFilter",
"activeRequests", "count")}}.ToRequestFactory(),
gnmsys.LineReportFactoryBuilder{
Title: "Server Response Codes",
Filename: "server_response_codes",
YAxis: "responses / second",
X: 12 * vg.Inch,
Y: 5 * vg.Inch,
CollectorFactories: []gnmsys.CollectorFactory{
gnmsys.NewFloatCollector("Bad", "org.fao.geonet.monitor.webapp.WebappMetricsFilter",
"responseCodes.badRequest", "mean"),
gnmsys.NewFloatCollector("NoContent", "org.fao.geonet.monitor.webapp.WebappMetricsFilter",
"responseCodes.noContent", "mean"),
gnmsys.NewFloatCollector("NotFound", "org.fao.geonet.monitor.webapp.WebappMetricsFilter",
"responseCodes.notFound", "mean"),
gnmsys.NewFloatCollector("Ok", "org.fao.geonet.monitor.webapp.WebappMetricsFilter",
"responseCodes.ok", "mean"),
gnmsys.NewFloatCollector("Other", "org.fao.geonet.monitor.webapp.WebappMetricsFilter",
"responseCodes.other", "mean"),
gnmsys.NewFloatCollector("ServerError", "org.fao.geonet.monitor.webapp.WebappMetricsFilter",
"responseCodes.serverError", "mean"),
gnmsys.NewFloatCollector("Created", "org.fao.geonet.monitor.webapp.WebappMetricsFilter",
"responseCodes.created", "mean")}}.ToRequestFactory(),
gnmsys.LineReportFactoryBuilder{
Title: "Duration Web Requests",
Filename: "duration_web_requests",
YAxis: "milliseconds",
X: 12 * vg.Inch,
Y: 5 * vg.Inch,
CollectorFactories: []gnmsys.CollectorFactory{
gnmsys.NewFloatCollector("Requests", "org.fao.geonet.monitor.webapp.WebappMetricsFilter",
"requests", "duration", "mean")}}.ToRequestFactory(),
gnmsys.LineReportFactoryBuilder{
Title: "Thread states (%)",
Filename: "thread_states",
YAxis: "%",
X: 12 * vg.Inch,
Y: 5 * vg.Inch,
CollectorFactories: []gnmsys.CollectorFactory{
gnmsys.NewFloatCollector("Blocked", "jvm", "thread-states", "blocked"),
gnmsys.NewFloatCollector("New", "jvm", "thread-states", "new"),
gnmsys.NewFloatCollector("Timed Waiting", "jvm", "thread-states", "timed_waiting"),
gnmsys.NewFloatCollector("Waiting", "jvm", "thread-states", "waiting"),
gnmsys.NewFloatCollector("Terminated", "jvm", "thread-states", "terminated"),
gnmsys.NewFloatCollector("Runnable", "jvm", "thread-states", "runnable")}}.ToRequestFactory()}
}