import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
public class GetDetail {
public static Map<String,Integer> mapPids=new HashMap<String,Integer>();
public static int runClount=0;
public static Log log = LogFactory.get();
public static void main(String[] args) {
try {
mapPids.clear();
cycle1();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void cycle1() throws Exception {
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
try {
runClount++;
if(runClount >10000) {
runClount=0;
mapPids.clear();
}
log.info("正在监控PoolManage:"+runClount+"......");
String pid="";
String user="";
String pr="";
String ni="";
String virt="";
String res="";
String shr="";
String s="";
String cpu="";
String mem="";
String time="";
String command="";
Runtime rt = Runtime.getRuntime();
//Process p = rt.exec("top -u infodba");
Process p = rt.exec("top -b -n 1 -u infodba");//5分钟
BufferedReader in = null;
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = "";
int linecount = 0;
while ((str = in.readLine()) != null) {
linecount++;
//log.info("第"+linecount+":"+str);
String[] strArray=str.split("\\s+");
if(strArray!=null && strArray.length==12) {
pid=strArray[0];
user=strArray[1];
cpu=strArray[9];
command=strArray[11];
//System.out.println("pid"+pid+";cpu"+cpu);
double dCpu=Double.parseDouble(cpu);
if(dCpu > 0.9 && user.equals("infodba") && command.equals("tcserver")) {
log.info("pid1"+pid+";cpu"+cpu);
if(mapPids.containsKey(pid)) {
int count=mapPids.get(pid);
if(count >1000) {
//Process p1 = rt.exec("kill -9 -"+pid);//kill进程
log.info("执行 kill pid1"+pid+";cpu"+cpu);
}else {
count =count+1;
mapPids.put(pid, count);
}
}else{
mapPids.put(pid, 1);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, 0, 1000*300);//5分钟
}
}
标签:java,String,poolmanger,pid,util,Teamcenter,监控,import,cpu From: https://www.cnblogs.com/PLM-Teamcenter/p/18305412