Skip to content

Commit d3ffadd

Browse files
committed
[#4] Add API : /app/env
1 parent 2bc4486 commit d3ffadd

File tree

6 files changed

+229
-171
lines changed

6 files changed

+229
-171
lines changed

README.md

+25-18
Original file line numberDiff line numberDiff line change
@@ -132,30 +132,37 @@ http://example.com:57911/app/ping
132132
```
133133

134134
### API
135+
#### GET /app/env
136+
* Get application environments
137+
##### Response
138+
```json
139+
{
140+
"data":{
141+
"applicationVersion": "1.4.0",
142+
"hostname": "hostname",
143+
"osVersion": "10.11.6",
144+
"jarFile": "code13k-zeroproxy-1.0.0-alpha.1.jar",
145+
"javaVersion": "1.8.0_25",
146+
"ip": "192.168.0.121",
147+
"javaVendor": "Oracle Corporation",
148+
"osName": "Mac OS X",
149+
"cpuProcessorCount": 4
150+
}
151+
}
152+
```
135153
#### GET /app/status
136-
* Get application status and environment.
154+
* Get application status
137155
##### Response
138156
```json
139157
{
140158
"data":{
141-
"applicationVersion":"0.1.0-alpha.1",
142-
"cpuUsage":2.56,
143159
"threadInfo":{...},
144-
"vmMemoryFree":"190M",
145-
"javaVersion":"1.8.0_25",
146-
"vmMemoryMax":"3,641M",
147-
"currentDate":"2018-09-18T18:48:58.795+09:00",
148-
"threadCount":15,
149-
"startedDate":"2018-09-18T18:48:40.901+09:00",
150-
"javaVendor":"",
151-
"runningTimeHour":0,
152-
"osName":"Mac OS X",
153-
"cpuProcessorCount":4,
154-
"vmMemoryTotalFree":"3,585M",
155-
"hostname":"",
156-
"osVersion":"10.11.6",
157-
"jarFile":"code13k-zeroproxy-0.1.0-alpha.1.jar",
158-
"vmMemoryAllocated":"245M",
160+
"cpuUsage": 2.88,
161+
"threadCount": 25,
162+
"currentDate": "2018-10-02T01:15:21.290+09:00",
163+
"startedDate": "2018-10-02T01:14:40.995+09:00",
164+
"runningTimeHour": 0,
165+
"vmMemoryUsage":{...}
159166
}
160167
}
161168
```

src/main/java/org/code13k/zeroproxy/app/Env.java

+109-42
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
package org.code13k.zeroproxy.app;
22

33
import org.apache.commons.lang3.StringUtils;
4-
import org.code13k.zeroproxy.lib.Util;
54
import org.slf4j.Logger;
65
import org.slf4j.LoggerFactory;
76

7+
import java.io.IOException;
8+
import java.io.InputStream;
89
import java.net.InetAddress;
10+
import java.net.URL;
11+
import java.util.Enumeration;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
import java.util.jar.Attributes;
15+
import java.util.jar.JarFile;
16+
import java.util.jar.Manifest;
917

1018
public class Env {
1119
// Logger
@@ -14,7 +22,6 @@ public class Env {
1422
// Data
1523
private String mHostname = "";
1624
private String mIP = "";
17-
private int mProcessorCount = 0;
1825
private String mVersionString = "";
1926
private String mJarFilename = "";
2027

@@ -56,30 +63,15 @@ public void init() {
5663
mLogger.error("Failed to get IP", e);
5764
}
5865

59-
// Processor Count
60-
try {
61-
mProcessorCount = Runtime.getRuntime().availableProcessors();
62-
} catch (Exception e) {
63-
mLogger.error("Filed to get processor count", e);
64-
}
65-
6666
// Version
6767
try {
68-
mVersionString = Util.getApplicationVersion();
68+
mVersionString = parseApplicationVersion();
6969
} catch (Exception e) {
7070
mLogger.error("Failed to get version", e);
7171
}
7272

7373
// Jar File Name
74-
String javaClassPath = System.getProperty("java.class.path");
75-
String jarFilename = "";
76-
if (StringUtils.isBlank(javaClassPath) == false) {
77-
String[] temp = StringUtils.split(javaClassPath, "/");
78-
if (temp.length > 0) {
79-
jarFilename = temp[temp.length - 1];
80-
}
81-
}
82-
mJarFilename = jarFilename;
74+
mJarFilename = parseJarFilename();
8375

8476
// End
8577
logging();
@@ -89,35 +81,31 @@ public void init() {
8981
* Logging
9082
*/
9183
public void logging() {
92-
// Begin
84+
Map<String, Object> values = values();
9385
mLogger.info("------------------------------------------------------------------------");
9486
mLogger.info("Application Environments");
9587
mLogger.info("------------------------------------------------------------------------");
96-
97-
// Hostname
98-
mLogger.info("Hostname = " + mHostname);
99-
100-
// IP
101-
mLogger.info("IP = " + mIP);
102-
103-
// Processor Count
104-
mLogger.info("Processor count = " + mProcessorCount);
105-
106-
// Version
107-
mLogger.info("Version = " + mVersionString);
108-
109-
// Jar File Name
110-
mLogger.info("Jar filename = " + mJarFilename);
111-
112-
// End
88+
values.forEach((k, v) -> mLogger.info(k + " = " + v));
11389
mLogger.info("------------------------------------------------------------------------");
11490
}
11591

11692
/**
117-
* String of app version
93+
* Get all values
11894
*/
119-
public String getVersionString() {
120-
return mVersionString;
95+
public Map<String, Object> values() {
96+
HashMap<String, Object> result = new HashMap<>();
97+
98+
result.put("hostname", getHostname());
99+
result.put("ip", getIP());
100+
result.put("cpuProcessorCount", getProcessorCount());
101+
result.put("applicationVersion", getVersionString());
102+
result.put("jarFile", getJarFilename());
103+
result.put("javaVersion", getJavaVersion());
104+
result.put("javaVendor", getJavaVendor());
105+
result.put("osVersion", getOsVersion());
106+
result.put("osName", getOsName());
107+
108+
return result;
121109
}
122110

123111
/**
@@ -138,13 +126,92 @@ public String getIP() {
138126
* Processor count of server
139127
*/
140128
public int getProcessorCount() {
141-
return mProcessorCount;
129+
return Runtime.getRuntime().availableProcessors();
130+
}
131+
132+
/**
133+
* String of app version
134+
*/
135+
public String getVersionString() {
136+
return mVersionString;
142137
}
143138

144139
/**
145140
* Filename of Jar
146141
*/
147-
public String getJarFilename(){
142+
public String getJarFilename() {
148143
return mJarFilename;
149144
}
145+
146+
/**
147+
* Get java version
148+
*/
149+
public String getJavaVersion() {
150+
return System.getProperty("java.version");
151+
}
152+
153+
/**
154+
* Get java vendor
155+
*/
156+
public String getJavaVendor() {
157+
return System.getProperty("java.vendor");
158+
}
159+
160+
/**
161+
* Get OS name
162+
*/
163+
public String getOsName() {
164+
return System.getProperty("os.name");
165+
}
166+
167+
/**
168+
* Get OS version
169+
*/
170+
public String getOsVersion() {
171+
return System.getProperty("os.version");
172+
}
173+
174+
/**
175+
* Get app version from manifest info
176+
*/
177+
private String parseApplicationVersion() {
178+
Enumeration resourceEnum;
179+
try {
180+
resourceEnum = Thread.currentThread().getContextClassLoader().getResources(JarFile.MANIFEST_NAME);
181+
while (resourceEnum.hasMoreElements()) {
182+
try {
183+
URL url = (URL) resourceEnum.nextElement();
184+
InputStream is = url.openStream();
185+
if (is != null) {
186+
Manifest manifest = new Manifest(is);
187+
Attributes attr = manifest.getMainAttributes();
188+
String version = attr.getValue("Implementation-Version");
189+
if (version != null) {
190+
return version;
191+
}
192+
}
193+
} catch (Exception e) {
194+
// Nothing
195+
}
196+
}
197+
} catch (IOException e1) {
198+
// Nothing
199+
}
200+
return null;
201+
}
202+
203+
/**
204+
* Get jar file name from system property
205+
*/
206+
private String parseJarFilename() {
207+
String javaClassPath = System.getProperty("java.class.path");
208+
String jarFilename = "";
209+
if (StringUtils.isBlank(javaClassPath) == false) {
210+
String[] temp = StringUtils.split(javaClassPath, "/");
211+
if (temp.length > 0) {
212+
jarFilename = temp[temp.length - 1];
213+
}
214+
}
215+
return jarFilename;
216+
}
150217
}

src/main/java/org/code13k/zeroproxy/app/Status.java

+76-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
77

8+
import java.lang.management.ManagementFactory;
9+
import java.lang.management.OperatingSystemMXBean;
10+
import java.lang.management.ThreadInfo;
11+
import java.lang.management.ThreadMXBean;
12+
import java.text.DecimalFormat;
13+
import java.text.NumberFormat;
814
import java.text.SimpleDateFormat;
9-
import java.util.Date;
10-
import java.util.Timer;
11-
import java.util.TimerTask;
15+
import java.util.*;
1216

1317
public class Status {
1418
// Logger
@@ -78,6 +82,25 @@ public void logging() {
7882
mLogger.info(sb.toString());
7983
}
8084

85+
/**
86+
* Get all values
87+
*/
88+
public Map<String, Object> values() {
89+
HashMap<String, Object> result = new HashMap<>();
90+
91+
// Common
92+
HashMap<Long, String> threadInfo = getThreadInfo();
93+
result.put("threadInfo", threadInfo);
94+
result.put("threadCount", threadInfo.size());
95+
result.put("startedDate", getAppStartedDateString());
96+
result.put("currentDate", getCurrentDateString());
97+
result.put("runningTimeHour", getAppRunningTimeHour());
98+
result.put("cpuUsage", getCpuUsage());
99+
result.put("vmMemoryUsage", getVmMemoryUsage());
100+
101+
return result;
102+
}
103+
81104
/**
82105
* Get application started time
83106
*/
@@ -114,4 +137,54 @@ public int getAppRunningTimeHour() {
114137
int runningTimeHour = runningTimeMin / 60;
115138
return runningTimeHour;
116139
}
140+
141+
/**
142+
* Get CPU usage
143+
*/
144+
public double getCpuUsage() {
145+
// CPU Usage
146+
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
147+
double cpuUsage = operatingSystemMXBean.getSystemLoadAverage();
148+
return new Double(new DecimalFormat("#.##").format(cpuUsage));
149+
}
150+
151+
/**
152+
* Get memory usage of VM
153+
*/
154+
public HashMap<String, String> getVmMemoryUsage() {
155+
// VM Memory Usage
156+
Runtime runtime = Runtime.getRuntime();
157+
NumberFormat format = NumberFormat.getInstance();
158+
long vmMemoryMax = runtime.maxMemory();
159+
long vmMemoryAllocated = runtime.totalMemory();
160+
long vmMemoryFree = runtime.freeMemory();
161+
String vmMemoryMaxString = format.format(vmMemoryMax / 1024 / 1024) + "M";
162+
String vmMemoryAllocatedString = format.format(vmMemoryAllocated / 1024 / 1024) + "M";
163+
String vmMemoryFreeString = format.format(vmMemoryFree / 1024 / 1024) + "M";
164+
String vmMemoryTotalFreeString = format.format((vmMemoryFree + (vmMemoryMax - vmMemoryAllocated)) / 1024 / 1024) + "M";
165+
166+
// Result
167+
HashMap<String, String> result = new HashMap<>();
168+
result.put("max", vmMemoryMaxString);
169+
result.put("allocated", vmMemoryAllocatedString);
170+
result.put("free", vmMemoryFreeString);
171+
result.put("totalFree", vmMemoryTotalFreeString);
172+
return result;
173+
}
174+
175+
/**
176+
* Get thread info
177+
*/
178+
public HashMap<Long, String> getThreadInfo() {
179+
NumberFormat format = NumberFormat.getInstance();
180+
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
181+
long[] threadIds = threadMXBean.getAllThreadIds();
182+
HashMap<Long, String> threadResult = new HashMap<>();
183+
for (long threadId : threadIds) {
184+
ThreadInfo threadInfo = threadMXBean.getThreadInfo(threadId);
185+
String threadInfoValue = threadInfo.getThreadName() + ", " + format.format(threadMXBean.getThreadCpuTime(threadId) / 1000000000) + "sec";
186+
threadResult.put(threadId, threadInfoValue);
187+
}
188+
return threadResult;
189+
}
117190
}

src/main/java/org/code13k/zeroproxy/lib/Util.java

-29
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,4 @@ public static boolean isValidPortNumber(Integer portNumber) {
1919
}
2020
return true;
2121
}
22-
23-
/**
24-
* Get app version from manifest info
25-
*/
26-
public static String getApplicationVersion() {
27-
Enumeration resourceEnum;
28-
try {
29-
resourceEnum = Thread.currentThread().getContextClassLoader().getResources(JarFile.MANIFEST_NAME);
30-
while (resourceEnum.hasMoreElements()) {
31-
try {
32-
URL url = (URL) resourceEnum.nextElement();
33-
InputStream is = url.openStream();
34-
if (is != null) {
35-
Manifest manifest = new Manifest(is);
36-
Attributes attr = manifest.getMainAttributes();
37-
String version = attr.getValue("Implementation-Version");
38-
if (version != null) {
39-
return version;
40-
}
41-
}
42-
} catch (Exception e) {
43-
// Nothing
44-
}
45-
}
46-
} catch (IOException e1) {
47-
// Nothing
48-
}
49-
return null;
50-
}
5122
}

0 commit comments

Comments
 (0)