public classWmiServiceUtils {public static final Logger logger = LoggerFactory.getLogger(WmiServiceUtils.class);private static List> getAllResult(String[] cmdStr, int flag) throwsIOException {
List> list = new ArrayList<>();
Integer index= 1;
Process p= null;
String str= null;
String[] arrStr= new String[2];
Map map = new HashMap();
InputStreamReader isr= null;
BufferedReader br= null;try{
p=Runtime.getRuntime().exec(cmdStr);
isr= newInputStreamReader(p.getInputStream());
br= newBufferedReader(isr);while ((str = br.readLine()) != null) {if(StringUtil.isNotEmpty(str)) {if (index % flag == 0) {
list.add(map);
map= new HashMap();
}
arrStr= str.split("=");
str= str.endsWith("=") ? "" : arrStr[1];
map.put(arrStr[0], str);
index++;
}
}
}catch(IOException e) {
logger.error("获取进程的所有信息失败!", e);throwe;
}catch(Exception e) {
logger.error("获取执行结果失败!", e);throwe;
}finally{try{if (br != null) {
}
br.close();if (isr != null) {
isr.close();
}
}catch(IOException e) {
logger.error("", e);
}if (p != null) {
p.destroy();
}
}returnlist;
}
@SuppressWarnings("unused")private static String parse2Time(longmilliseconds) {if (milliseconds == 0) {return "0秒";
}if (milliseconds / 1000 == 0) {return "0." + milliseconds + "秒";
}
milliseconds= milliseconds / 1000;long day = milliseconds / (24 * 3600);
milliseconds= milliseconds % (24 * 3600);if (milliseconds == 0) {return day + "天";
}long hour = milliseconds / (3600);
milliseconds= milliseconds % (3600);if (milliseconds == 0) {return day + "天" + hour + "小时";
}long minute = milliseconds / (60);
milliseconds= milliseconds % (60);if (milliseconds == 0) {return day + "天 " + hour + "小时 " + minute + "分钟";
}else{return day + "天 " + hour + "小时 " + minute + "分钟 " + milliseconds + "秒";
}
}private static Map printStream(InputStream input) throws IOException { //InputStream input final Process proc
InputStreamReader isr = new InputStreamReader(input); //proc.getInputStream()
BufferedReader br = newBufferedReader(isr);
Map map = new HashMap();
String str= null;
String[] arrStr= null;try{while ((str = br.readLine()) != null) {if(StringUtil.isNotEmpty(str)) {if (str.contains("=")) {
arrStr= str.split("=");
str= str.endsWith("=") ? "" : arrStr[1];
map.put(arrStr[0], str);
}else{
map.put(str,null);
}
}
}
}catch(IOException e) {
logger.error("关闭文件流失败!", e);throwe;
}finally{try{if (br != null) {
br.close();
}if (isr != null) {
isr.close();
}if (input != null) {
input.close();
}
}catch(IOException e) {
logger.error("关闭文件流失败!", e);throwe;
}
}returnmap;
}private static String printErrorStream(InputStream input) throwsIOException {
InputStreamReader reader= newInputStreamReader(input);
BufferedReader br= newBufferedReader(reader);
String msg= "";
String str= "";try{while ((str = br.readLine()) != null) {if(StringUtil.isNotEmpty(str)) {
msg+= str + ",";
}
}if(msg.endsWith(",")){
msg.substring(0, msg.lastIndexOf(","));
}returnmsg;
}catch(IOException e) {
logger.error("读取错误信息失败!", e);throwe;
}finally{try{if (br != null) {
br.close();
}if (reader != null) {
reader.close();
}if (input != null) {
input.close();
}
}catch(IOException e) {
logger.error("关闭文件流失败!", e);throwe;
}
}
}private static Map execCommand(String[] cmdStr) throwsIOException {
Process p= null;
Map map = new HashMap<>();try{
p=Runtime.getRuntime().exec(cmdStr);
logger.info("执行错误信息: " +printErrorStream(p.getErrorStream()));
map=printStream(p.getInputStream());
}catch(IOException e) {
logger.error("启动服务失败!", e);throwe;
}catch(Exception e) {
logger.error("获取执行结果失败!", e);throwe;
}finally{if (p != null) {
p.destroy();
}
}returnmap;
}/*** 启动服务
*@paramserviceName 右键 指定服务项-》属性 -》服务名称
*@return*@throwsIOException*/
public static Map startService(String serviceName) throwsIOException {
String[] cmdStr= { "cmd", "/C", " net start " + serviceName };//runAs /user:Administrator
logger.info(Arrays.toString(cmdStr));try{returnexecCommand(cmdStr);
}catch(IOException e) {
logger.error("开启服务失败!", e);throwe;
}
}/*** 关闭服务
*@paramserviceName 右键 指定服务项-》属性 -》服务名称
*@return*@throwsIOException*/
public static Map stopService(String serviceName) throwsIOException {
String[] cmdStr= { "cmd", "/C", "net stop " + serviceName };//runAs /user:Administrator
logger.info(Arrays.toString(cmdStr));try{returnexecCommand(cmdStr);
}catch(IOException e) {
logger.error("关闭服务失败!", e);throwe;
}
}/*** 禁用服务
*@paramserviceName
*@return*@throwsIOException*/
public static Map disableService(String serviceName) throwsIOException {
String[] cmdStr= { "cmd", "/C", "sc config " + serviceName + " start= disabled" };//runAs /user:Administrator
logger.info(Arrays.toString(cmdStr));try{returnexecCommand(cmdStr);
}catch(IOException e) {
logger.error("关闭服务失败!", e);throwe;
}
}/*** 启用服务 --自动
*@paramserviceName
*@return*@throwsIOException*/
public static Map enableService(String serviceName) throwsIOException {
String[] cmdStr= { "cmd", "/C", "sc config " + serviceName + " start= auto" };//runAs /user:Administrator
logger.info(Arrays.toString(cmdStr));try{returnexecCommand(cmdStr);
}catch(IOException e) {
logger.error("关闭服务失败!", e);throwe;
}
}/*** 启用服务 --手动
*@paramserviceName
*@return*@throwsIOException*/
public static Map demandService(String serviceName) throwsIOException {
String[] cmdStr= { "cmd", "/C", "sc config " + serviceName + " start= demand" };//runAs /user:Administrator
logger.info(Arrays.toString(cmdStr));try{returnexecCommand(cmdStr);
}catch(IOException e) {
logger.error("关闭服务失败!", e);throwe;
}
}/***
*@paramserviceName 映像名称 XXXX.exe
*@return*@throwsIOException*/
public static Map getTaskDetail(String taskName) throwsIOException {
String[] cmdStr= { "cmd", "/C", "wmic process where name='" + taskName + "' list full"};
logger.info(Arrays.toString(cmdStr));try{returnexecCommand(cmdStr);
}catch(IOException e) {
logger.error("关闭服务失败!", e);throwe;
}
}/***
*@paramprocessId PID
*@return*@throwsIOException*/
public static Map getTaskDetail(Integer processId) throwsIOException {
String[] cmdStr= { "cmd", "/C", "wmic process where processid='" + processId + "' list full" };//get /format:value
logger.info(Arrays.toString(cmdStr));try{returnexecCommand(cmdStr);
}catch(IOException e) {
logger.error("关闭服务失败!", e);throwe;
}
}public static List> getAllTaskDetail() throwsIOException {
String[] cmdStr= { "cmd", "/C", "wmic process get /value"};
logger.info(Arrays.toString(cmdStr));
List> list = null;try{
list= getAllResult(cmdStr, 45);
}catch(IOException e) {
logger.error("获取所有进程信息失败!", e);throwe;
}returnlist;
}public static List> getAllService() throwsIOException {
String[] cmdStr= { "cmd", "/C", "wmic service get /value"};
logger.info(Arrays.toString(cmdStr));
List> list = null;try{
list= getAllResult(cmdStr, 25);
}catch(IOException e) {
logger.error("获取所有服务信息失败!", e);throwe;
}returnlist;
}/***
*@paramserviceName 右键 指定服务项-》属性 -》服务名称
*@return*@throwsIOException*/
public static Map getServiceDetail(String serviceName) throwsIOException {
String[] cmdStr= { "cmd", "/C", "wmic service where name='" + serviceName + "' list full"};
logger.info(Arrays.toString(cmdStr));try{returnexecCommand(cmdStr);
}catch(IOException e) {
logger.error("关闭服务失败!", e);throwe;
}
}/***
*@paramprocessId PID
*@return*@throwsIOException*/
public static Map getServiceDetail(Integer processId) throwsIOException {
String[] cmdStr= { "cmd", "/C", "wmic service where processid='" + processId + "' list full"};
logger.info(Arrays.toString(cmdStr));try{returnexecCommand(cmdStr);
}catch(IOException e) {
logger.error("关闭服务失败!", e);throwe;
}
}public static Map createProcess(String taskpath) throwsIOException {
String[] cmdStr= { "cmd", "/C", "wmic process call create'" + taskpath + "'"};try{returnexecCommand(cmdStr);
}catch(IOException e) {
logger.error("关闭服务失败!", e);throwe;
}
}public static Map deleteProcess(String taskname) throwsIOException {
String[] cmdStr= { "cmd", "/C", " wmic process where name='" + taskname + "' delete" };//runAs /user:Administrator
try{returnexecCommand(cmdStr);
}catch(IOException e) {
logger.error("关闭服务失败!", e);throwe;
}
}/*** 计算某进程cpu使用率
*@paramprocessName
*@return*@throwsException
*@seesysTime:表示该时间段内总的CPU时间=CPU处于用户态和内核态CPU时间的总和,即sysTime =kerneTimel + userTime(注:这里并不包括idleTime,因为当CPU处于空闲状态时,实在内核模式下运行System Idle Process这个进程,所以kernelTime实际上已经包含了idleTime);
idleTime:表示在该时间段内CPU处于空闲状态的时间;CPU% = 1 – idleTime / sysTime * 100*/
public static String getCpuRatioForWindows(String processName) throwsException {
String[] cmdStr= { "cmd", "/C","wmic process get Caption,CommandLine,KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount /value"};try{
List> list1 = getAllResult(cmdStr, 7);long[] data1 =getCpuTime(list1, processName);
Thread.sleep(1000);
List> list2 = getAllResult(cmdStr, 7);//get(p.getInputStream());
long[] data2 =getCpuTime(list2, processName);long proctime = data2[2] - data1[2];long totaltime = data2[1] - data1[1]; //+ data2[0] - data1[0]
if(totaltime==0){return "0%";
}return Double.valueOf(10000 * proctime * 1.0 / totaltime).intValue()/100.00 + "%";
}catch(Exception e) {
logger.error("获取CPU占用率失败!", e);throwe;
}
}private static long[] getCpuTime(List>list, String processName) {long[] data = new long[3];long idletime = 0;long kneltime = 0;long usertime = 0;long processTime = 0;
String caption= "";
String kmtm= "";
String umtm= "";for (Mapm : list) {
caption= m.get("Caption").toString();
kmtm= m.get("KernelModeTime").toString();
umtm= m.get("UserModeTime").toString();if (caption.equals("System Idle Process") || caption.equals("System")) {if (kmtm != null && !kmtm.equals("")) {
idletime+=Long.parseLong(kmtm);
}if (umtm != null && !umtm.equals("")) {
idletime+=Long.parseLong(umtm);
}
}if(caption.equals(processName)) {if (kmtm != null && !kmtm.equals("")) {
processTime+=Long.parseLong(kmtm);
}if (umtm != null && !umtm.equals("")) {
processTime+=Long.parseLong(umtm);
}
}if (kmtm != null && !kmtm.equals("")) {
kneltime+=Long.parseLong(kmtm);
}if (umtm != null && !umtm.equals("")) {
usertime+=Long.parseLong(umtm);
}
}
data[0] =idletime;
data[1] = kneltime +usertime;
data[2] =processTime;returndata;
}