Skip to content

Commit de9e674

Browse files
authored
Cpu usage metric (#83)
* cpu plugin * cpu plugin
1 parent bf20c05 commit de9e674

File tree

9 files changed

+318
-0
lines changed

9 files changed

+318
-0
lines changed

agent-core/src/main/java/cloud/erda/agent/core/utils/PluginConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,7 @@ public class PluginConstants {
2828

2929
public static final String JVM_PLUGIN = "trace";
3030

31+
public static final String CPU_PLUGIN = "cpu";
32+
3133
public static final String REPORTER_PLUGIN = "trace";
3234
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>agent-plugins</artifactId>
7+
<groupId>cloud.erda</groupId>
8+
<version>1.0.0</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>agent-cpu-plugin</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>com.github.oshi</groupId>
17+
<artifactId>oshi-core</artifactId>
18+
<version>3.9.1</version>
19+
</dependency>
20+
</dependencies>
21+
</project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cloud.erda.agent.plugin.cpu;
2+
3+
import cloud.erda.agent.core.config.AgentConfig;
4+
import cloud.erda.agent.core.config.ServiceConfig;
5+
import cloud.erda.agent.core.config.loader.ConfigAccessor;
6+
import cloud.erda.agent.core.metrics.Metric;
7+
8+
/**
9+
* @author zhaihongwei
10+
* @since 2022/2/21
11+
*/
12+
public interface CPUDefaultProvider {
13+
14+
AgentConfig agentConfig = ConfigAccessor.Default.getConfig(AgentConfig.class);
15+
ServiceConfig serviceConfig = ConfigAccessor.Default.getConfig(ServiceConfig.class);
16+
17+
default void addDefaultTags(Metric metric) {
18+
metric.addTag("terminus_key", agentConfig.terminusKey()).
19+
addTag("instance_id", serviceConfig.getServiceInstanceId()).
20+
addTag("service_instance_id", serviceConfig.getServiceInstanceId()).
21+
addTag("service_id", serviceConfig.getServiceId()).
22+
addTag("service_ip", serviceConfig.getServiceIp()).
23+
addTag("service_name", serviceConfig.getServiceName()).
24+
addTag("project_id", serviceConfig.getProjectId()).
25+
addTag("runtime_id", serviceConfig.getRuntimeId()).
26+
addTag("application_id", serviceConfig.getApplicationId()).
27+
addTag("runtime_name", serviceConfig.getRuntimeName()).
28+
addTag("application_name", serviceConfig.getApplicationName()).
29+
addTag("project_name", serviceConfig.getProjectName()).
30+
addTag("workspace", serviceConfig.getWorkspace()).
31+
addTag("org_name", serviceConfig.getOrgName()).
32+
addTag("org_id", serviceConfig.getOrgId());
33+
}
34+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package cloud.erda.agent.plugin.cpu;
2+
3+
import java.text.DecimalFormat;
4+
5+
/**
6+
* @author zhaihongwei
7+
* @since 2022/2/21
8+
*/
9+
public class CPUInfo {
10+
private static final DecimalFormat DF = new DecimalFormat("0.00");
11+
public float user;
12+
public float nice;
13+
public float system;
14+
public float idle;
15+
public float iowait;
16+
public float irq;
17+
public float softirq;
18+
public float steal;
19+
public float usage;
20+
public float total;
21+
public int physicalCount;
22+
23+
public CPUInfo() {
24+
}
25+
26+
public double getUserUsage() {
27+
return Double.parseDouble(DF.format((this.user * 1.0 / this.total) * 100));
28+
}
29+
30+
public double getSystemUsage() {
31+
return Double.parseDouble(DF.format((this.system * 1.0 / this.total) * 100));
32+
}
33+
34+
public double getIdleUsage() {
35+
return Double.parseDouble(DF.format((this.idle * 1.0 / this.total) * 100));
36+
}
37+
38+
public float getTotal() {
39+
return total;
40+
}
41+
42+
public void setTotal(float total) {
43+
this.total = total;
44+
}
45+
46+
public int getPhysicalCount() {
47+
return physicalCount;
48+
}
49+
50+
public void setPhysicalCount(int physicalCount) {
51+
this.physicalCount = physicalCount;
52+
}
53+
54+
public float getUser() {
55+
return user;
56+
}
57+
58+
public void setUser(float user) {
59+
this.user = user;
60+
}
61+
62+
public float getNice() {
63+
return nice;
64+
}
65+
66+
public void setNice(float nice) {
67+
this.nice = nice;
68+
}
69+
70+
public float getSystem() {
71+
return system;
72+
}
73+
74+
public void setSystem(float system) {
75+
this.system = system;
76+
}
77+
78+
public float getIdle() {
79+
return idle;
80+
}
81+
82+
public void setIdle(float idle) {
83+
this.idle = idle;
84+
}
85+
86+
public float getIowait() {
87+
return iowait;
88+
}
89+
90+
public void setIowait(float iowait) {
91+
this.iowait = iowait;
92+
}
93+
94+
public float getIrq() {
95+
return irq;
96+
}
97+
98+
public void setIrq(float irq) {
99+
this.irq = irq;
100+
}
101+
102+
public float getSoftirq() {
103+
return softirq;
104+
}
105+
106+
public void setSoftirq(float softirq) {
107+
this.softirq = softirq;
108+
}
109+
110+
public float getSteal() {
111+
return steal;
112+
}
113+
114+
public void setSteal(float steal) {
115+
this.steal = steal;
116+
}
117+
118+
public float getUsage() {
119+
return usage;
120+
}
121+
122+
public void setUsage(float usage) {
123+
this.usage = usage;
124+
}
125+
126+
@Override
127+
public String toString() {
128+
return "CPU usage: " + getUser() + " user, " + getSystem() + " sys, " + getIdle() + " idle";
129+
}
130+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package cloud.erda.agent.plugin.cpu;
2+
3+
import cloud.erda.agent.core.metrics.Metric;
4+
import cloud.erda.agent.core.metrics.MetricDispatcher;
5+
import org.apache.skywalking.apm.agent.core.boot.ServiceManager;
6+
7+
8+
/**
9+
* @author zhaihongwei
10+
* @since 2022/2/21
11+
*/
12+
public class CPUStatCollector {
13+
private final CPUUsageProvider cpuUsageProvider;
14+
15+
public CPUStatCollector() {
16+
this.cpuUsageProvider = new CPUUsageProvider();
17+
}
18+
19+
public void collect() {
20+
Metric metric = cpuUsageProvider.get();
21+
ServiceManager.INSTANCE.findService(MetricDispatcher.class).dispatch(metric);
22+
}
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cloud.erda.agent.plugin.cpu;
2+
3+
import cloud.erda.agent.core.utils.PluginConstants;
4+
import org.apache.skywalking.apm.agent.core.boot.ScheduledService;
5+
6+
/**
7+
* @author zhaihongwei
8+
* @since 2022/2/21
9+
*/
10+
public class CPUStatService extends ScheduledService {
11+
12+
private final CPUStatCollector collector = new CPUStatCollector();
13+
14+
@Override
15+
public String pluginName() {
16+
return PluginConstants.CPU_PLUGIN;
17+
}
18+
19+
@Override
20+
protected void executing() {
21+
collector.collect();
22+
}
23+
24+
@Override
25+
protected long initialDelay() {
26+
return 20;
27+
}
28+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package cloud.erda.agent.plugin.cpu;
2+
3+
import cloud.erda.agent.core.metrics.Metric;
4+
import cloud.erda.agent.core.utils.DateTime;
5+
import oshi.SystemInfo;
6+
import oshi.hardware.CentralProcessor;
7+
import oshi.hardware.HardwareAbstractionLayer;
8+
import oshi.util.Util;
9+
10+
import java.text.DecimalFormat;
11+
12+
/**
13+
* @author zhaihongwei
14+
* @since 2022/2/21
15+
*/
16+
public class CPUUsageProvider implements CPUDefaultProvider {
17+
18+
19+
private static final String METRIC_NAME = "cpu_usage";
20+
private static final SystemInfo SI = new SystemInfo();
21+
private static final HardwareAbstractionLayer HAL = SI.getHardware();
22+
23+
public Metric get() {
24+
CPUInfo cpuInfo = getCpuUsage(HAL.getProcessor());
25+
Metric metric = Metric.New(METRIC_NAME, DateTime.currentTimeNano()).
26+
addField("user", cpuInfo.user).
27+
addField("nice", cpuInfo.nice).
28+
addField("system", cpuInfo.system).
29+
addField("idle", cpuInfo.idle).
30+
addField("iowait", cpuInfo.iowait).
31+
addField("irq", cpuInfo.irq).
32+
addField("softirq", cpuInfo.softirq).
33+
addField("steal", cpuInfo.steal).
34+
addField("usage", cpuInfo.usage).
35+
addField("total", cpuInfo.total).
36+
addField("user_usage", cpuInfo.getUserUsage()).
37+
addField("system_usage", cpuInfo.getSystemUsage()).
38+
addField("idle_usage", cpuInfo.getIdleUsage()).
39+
addField("physicalCount", cpuInfo.physicalCount);
40+
this.addDefaultTags(metric);
41+
return metric;
42+
}
43+
44+
/**
45+
* Get Cpu Usage information
46+
*
47+
* @param processor CentralProcessor
48+
* @return CPUUsage
49+
*/
50+
private static CPUInfo getCpuUsage(CentralProcessor processor) {
51+
long[] prevTicks = processor.getSystemCpuLoadTicks();
52+
Util.sleep(1000);
53+
int physicalProcessorCount = processor.getPhysicalProcessorCount();
54+
long[] ticks = processor.getSystemCpuLoadTicks();
55+
long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
56+
long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
57+
long system = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
58+
long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
59+
long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
60+
long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
61+
long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
62+
long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
63+
long totalCpu = user + nice + system + idle + iowait + irq + softirq + steal;
64+
65+
CPUInfo cpuInfo = new CPUInfo();
66+
cpuInfo.setUser(user);
67+
cpuInfo.setNice(nice);
68+
cpuInfo.setSystem(system);
69+
cpuInfo.setIdle(idle);
70+
cpuInfo.setIowait(iowait);
71+
cpuInfo.setIrq(irq);
72+
cpuInfo.setSoftirq(softirq);
73+
cpuInfo.setSteal(steal);
74+
cpuInfo.setPhysicalCount(physicalProcessorCount);
75+
cpuInfo.setTotal(totalCpu);
76+
return cpuInfo;
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cloud.erda.agent.plugin.cpu.CPUStatService

agent-plugins/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<module>agent-jedis-2.x-plugin</module>
2121
<module>agent-log-plugins</module>
2222
<module>agent-jvm-plugin</module>
23+
<module>agent-cpu-plugin</module>
2324
<module>agent-httpasyncclient-4.x-plugin</module>
2425
<module>agent-servlet-plugins</module>
2526
<module>agent-rocketmq-4.x-plugin</module>

0 commit comments

Comments
 (0)