-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
SystemResource.java
119 lines (106 loc) · 4.55 KB
/
SystemResource.java
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
/**
* This file is part of Graylog.
*
* Graylog is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog. If not, see <http://www.gnu.org/licenses/>.
*/
package org.graylog2.shared.rest.resources.system;
import com.codahale.metrics.annotation.Timed;
import com.codahale.metrics.jvm.ThreadDump;
import com.github.joschi.jadconfig.util.Size;
import com.google.common.collect.ImmutableMap;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.graylog2.plugin.ServerStatus;
import org.graylog2.plugin.Tools;
import org.graylog2.rest.models.system.responses.SystemJVMResponse;
import org.graylog2.rest.models.system.responses.SystemOverviewResponse;
import org.graylog2.shared.ServerVersion;
import org.graylog2.shared.rest.resources.RestResource;
import org.graylog2.shared.security.RestPermissions;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.io.ByteArrayOutputStream;
import java.lang.management.ManagementFactory;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.Map;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
@RequiresAuthentication
@Api(value = "System", description = "System information of this node.")
@Path("/system")
public class SystemResource extends RestResource {
private final ServerStatus serverStatus;
@Inject
public SystemResource(ServerStatus serverStatus) {
this.serverStatus = serverStatus;
}
@GET
@Timed
@ApiOperation(value = "Get system overview")
@Produces(APPLICATION_JSON)
public SystemOverviewResponse system() {
checkPermission(RestPermissions.SYSTEM_READ, serverStatus.getNodeId().toString());
return SystemOverviewResponse.create("graylog2-server",
ServerVersion.CODENAME,
serverStatus.getNodeId().toString(),
ServerVersion.VERSION.toString(),
Tools.getISO8601String(serverStatus.getStartedAt()),
serverStatus.isProcessing(),
Tools.getLocalCanonicalHostname(),
serverStatus.getLifecycle().getDescription().toLowerCase(Locale.ENGLISH),
serverStatus.getLifecycle().getLoadbalancerStatus().toString().toLowerCase(Locale.ENGLISH),
serverStatus.getTimezone().getID());
}
@GET
@ApiOperation(value = "Get JVM information")
@Path("/jvm")
@Timed
@Produces(APPLICATION_JSON)
public SystemJVMResponse jvm() {
checkPermission(RestPermissions.JVMSTATS_READ, serverStatus.getNodeId().toString());
Runtime runtime = Runtime.getRuntime();
return SystemJVMResponse.create(
bytesToValueMap(runtime.freeMemory()),
bytesToValueMap(runtime.maxMemory()),
bytesToValueMap(runtime.totalMemory()),
bytesToValueMap(runtime.totalMemory() - runtime.freeMemory()),
serverStatus.getNodeId().toString(),
Tools.getPID(),
Tools.getSystemInformation());
}
@GET
@Timed
@ApiOperation(value = "Get a thread dump")
@Path("/threaddump")
@Produces(TEXT_PLAIN)
public String threaddump() {
checkPermission(RestPermissions.THREADS_DUMP, serverStatus.getNodeId().toString());
// The ThreadDump is built by internal codahale.metrics servlet library we are abusing.
final ThreadDump threadDump = new ThreadDump(ManagementFactory.getThreadMXBean());
final ByteArrayOutputStream output = new ByteArrayOutputStream();
threadDump.dump(output);
return new String(output.toByteArray(), StandardCharsets.UTF_8);
}
private Map<String, Long> bytesToValueMap(long bytes) {
final Size size = Size.bytes(bytes);
return ImmutableMap.of(
"bytes", size.toBytes(),
"kilobytes", size.toKilobytes(),
"megabytes", size.toMegabytes());
}
}