1、配置类(类型public)
@Configuration
@ConfigurationProperties(prefix = "checkcpumemuseconf")
@Data
public class CheckCpuMemUseConf {
public String checkTidefingerCpuMemUse;
public String checkCpuMemUse;
public double lc2022CpuUse;
public int lc2022TimeOut;
public double tidefingerCpuUse;
public int tidefingerTimeOut;
public double curlCpuUse;
public int curlTimeOut;
public double nmapCpuUse;
public int nampTimeOut;
public double nodeCpuUse;
public int nodeTimeOut;
}
获取配置文件中的值:
checkcpumemuseconf: checkTidefingerCpuMemUse: ps -eo pid,command,pcpu,pmem,etime checkCpuMemUse: ps -eo pid,comm,pcpu,pmem,etime # 如果值<=0,则不进行进程检查 tidefingerCpuUse: 100 tidefingerTimeOut: 600 curlCpuUse: 50 curlTimeOut: 60 nmapCpuUse: 50 nampTimeOut: 60 nodeCpuUse: 50 nodeTimeOut: 60
2、执行命令
// cpu内存使用检测
private void checkCpuMemUse() throws IOException {
ArrayList<String> nameList = new ArrayList<>();
nameList.add("tidefinger");
nameList.add("curl");
nameList.add("namp");
nameList.add("node");
for (String name : nameList) {
InputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
String cmds = "";
if ("tidefinger".equals(name)) {
cmds = checkCpuMemUseConf.checkTidefingerCpuMemUse;
} else {
cmds = checkCpuMemUseConf.checkCpuMemUse;
}
Process process = Runtime.getRuntime().exec(cmds);
// 取得命令结果的输入流
fis = process.getInputStream();
// 用一个读输入流类去读
isr = new InputStreamReader(fis);
// 用缓冲器读行
br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
if (line.contains("tidefinger") || line.contains("curl") || line.contains("namp") || line.contains("node")) {
// cpu内存使用检测是否cpu、用时超时
if ("tidefinger".equals(name)) {
CheckCpuMemUseUtil.checkCpuMemUse(line, checkCpuMemUseConf.tidefingerCpuUse, checkCpuMemUseConf.tidefingerTimeOut);
} else if ("curl".equals(name)) {
CheckCpuMemUseUtil.checkCpuMemUse(line, checkCpuMemUseConf.curlCpuUse, checkCpuMemUseConf.curlTimeOut);
} else if ("namp".equals(name)) {
CheckCpuMemUseUtil.checkCpuMemUse(line, checkCpuMemUseConf.nmapCpuUse, checkCpuMemUseConf.nampTimeOut);
} else if ("node".equals(name)) {
CheckCpuMemUseUtil.checkCpuMemUse(line, checkCpuMemUseConf.nodeCpuUse, checkCpuMemUseConf.nodeTimeOut);
}
}
}
}
}
3、通过工具类处理输出信息
其中,获取数组倒数第三个字段可通过获取:
String cpuString = info.split(" ")[info.split(" ").length - 3];
/**
* 计算cpu使用率
*/
@Slf4j
public class CheckCpuMemUseUtil {
public static void main(String[] args) throws IOException {
checkCpuMemUse("45295 nmap 137 0.0 01:01:06", 100, 360000);
}
public static void checkCpuMemUse(String info, double cpuUse, int timeOut) throws IOException {
// 判断是 如果配置项的值 ≤0,则不进行判断
if (cpuUse <= 0 && timeOut <= 0) {
return;
}
// 除去前后空格
info = info.trim();
// 将两个空格转换为一个空格
info = info.replaceAll(" {2,}", " ");
// 如果第二个是 grep,则,这1行不处理
if ("grep".equals(info.split(" ")[1])) {
return;
}
// 进程id
String processId = info.split(" ")[0];
String name = info.split(" ")[1];
// cpu使用率
String cpuString = info.split(" ")[info.split(" ").length - 3];
double cpu = Double.parseDouble(cpuString);
// 执行时间
String time = info.split(" ")[info.split(" ").length - 1];
// 计算总秒数
int day = 0;
int hour = 0;
int minute = 0;
int second = 0;
if (time.contains("-")) {
day = Integer.parseInt(time.split("-")[0]);
time = time.split("-")[1];
}
String[] split = time.split(":");
if (split.length > 2) {
hour = Integer.parseInt(split[0]);
minute = Integer.parseInt(split[1]);
second = Integer.parseInt(split[2]);
} else if (split.length > 1) {
minute = Integer.parseInt(split[0]);
second = Integer.parseInt(split[1]);
} else {
second = Integer.parseInt(split[0]);
}
int secondSum = day * 24 * 60 * 60 + hour * 60 * 60 + minute * 60 + second;
// 如果cpu>100
if (info.contains("lc2022")) {
if (cpu > cpuUse) {
} else if (secondSum > timeOut) {
// 执行时间 时间>配置项,kill掉
log.info("checkCpuMemUse--cpu > " + ",secondSum > " + timeOut + ",执行命令 kill-9 进程id:" + processId + " 进程名称:" + name);
Runtime.getRuntime().exec("kill -9 " + processId);
}
} else if (info.contains("tidefinger")) {
if (cpu > cpuUse) {
// 如果时间>60 kill掉
if (secondSum > 60) {
// 执行命令 kill-9 进程id
log.info("checkCpuMemUse--cpu > " + cpuUse + ",secondSum > 60,执行命令 kill-9 进程id:" + processId + " 进程名称:" + name);
Runtime.getRuntime().exec("kill -9 " + processId);
}
} else if (secondSum > timeOut) {
// 执行时间 时间>配置项,kill掉
log.info("checkCpuMemUse--cpu > " + ",secondSum > " + timeOut + ",执行命令 kill-9 进程id:" + processId + " 进程名称:" + name);
Runtime.getRuntime().exec("kill -9 " + processId);
}
} else if (info.contains("curl")) {
if (cpu > cpuUse) {
} else if (secondSum > timeOut) {
// 执行时间 时间>配置项,kill掉
log.info("checkCpuMemUse--cpu > " + ",secondSum > " + timeOut + ",执行命令 kill-9 进程id:" + processId + " 进程名称:" + name);
Runtime.getRuntime().exec("kill -9 " + processId);
}
} else if (info.contains("namp")) {
if (cpu > cpuUse) {
} else if (secondSum > timeOut) {
// 执行时间 时间>配置项,kill掉
log.info("checkCpuMemUse--cpu > " + ",secondSum > " + timeOut + ",执行命令 kill-9 进程id:" + processId + " 进程名称:" + name);
Runtime.getRuntime().exec("kill -9 " + processId);
}
} else if (info.contains("node")) {
if (cpu > cpuUse) {
} else if (secondSum > timeOut) {
// 执行时间 时间>配置项,kill掉
log.info("checkCpuMemUse--cpu > " + ",secondSum > " + timeOut + ",执行命令 kill-9 进程id:" + processId + " 进程名称:" + name);
Runtime.getRuntime().exec("kill -9 " + processId);
}
}
}
}