JAVA 调用LINUX命令类

这个是我写的系统采集类实现了CPU个数,CPU频率,内存信息,磁盘信息,CPU利用率,内存利用率,磁盘利用率,系统启动时间的获得
    /**Tochangethistemplate,chooseTools|Templates*andopenthetemplateintheeditor.*/packagecom.fenghuo.jsnmp.logic.task;importjava.io.*;importjava.util.StringTokenizer;/***用于执行linux命令*@authorhuangxf**/publicclassLinuxExec{/***获取cpu使用情况*@return*@throwsException*/publicdoublegetCpuUsage()throwsException{doublecpuUsed=0;Runtimert=Runtime.getRuntime();Processp=rt.exec(“top-b-n1”);//调用系统的“top”命令 BufferedReaderin=null;try{in=newBufferedReader(newInputStreamReader(p.getInputStream()));Stringstr=null;String[]strArray=null;while((str=in.readLine())!=null){intm=0;if(str.indexOf(“R”)!=-1){//只分析正在运行的进程,top进程本身除外&& strArray=str.split(“”);for(Stringtmp:strArray){if(tmp.trim().length()==0){continue;}if(++m==9){//第9列为CPU的使用百分比(RedHat cpuUsed+=Double.parseDouble(tmp);}}}}}catch(Exceptione){e.printStackTrace();}finally{in.close();}returncpuUsed;}/***内存监控*@return*@throwsException*/publicdoublegetMemUsage()throwsException{doublemenUsed=0;Runtimert=Runtime.getRuntime();Processp=rt.exec(“top-b-n1”);//调用系统的“top”命令 BufferedReaderin=null;try{in=newBufferedReader(newInputStreamReader(p.getInputStream()));Stringstr=null;String[]strArray=null;while((str=in.readLine())!=null){intm=0;if(str.indexOf(“R”)!=-1){//只分析正在运行的进程,top进程本身除外&& // //System.out.println(“——————3—————–“); strArray=str.split(“”);for(Stringtmp:strArray){if(tmp.trim().length()==0){continue;}if(++m==10){//9)–第10列为mem的使用百分比(RedHat9) menUsed+=Double.parseDouble(tmp);}}}}}catch(Exceptione){e.printStackTrace();}finally{in.close();}returnmenUsed;}/***获取磁盘空间大小**@return*@throwsException*/publicdoublegetDeskUsage()throwsException{doubletotalHD=0;doubleusedHD=0;Runtimert=Runtime.getRuntime();Processp=rt.exec(“df-hl”);//df-hl查看硬盘空间 BufferedReaderin=null;try{in=newBufferedReader(newInputStreamReader(p.getInputStream()));Stringstr=null;String[]strArray=null;while((str=in.readLine())!=null){intm=0;strArray=str.split(“”);for(Stringtmp:strArray){if(tmp.trim().length()==0){continue;}++m;//System.out.println(“—-tmp—-“+tmp); if(tmp.indexOf(“G”)!=-1){if(m==2){//System.out.println(“—G—-“+tmp); if(!tmp.equals(“”)&&!tmp.equals(“0”)){totalHD+=Double.parseDouble(tmp.substring(0,tmp.length()-1))*1024;}}if(m==3){//System.out.println(“—G—-“+tmp); if(!tmp.equals(“none”)&&!tmp.equals(“0”)){usedHD+=Double.parseDouble(tmp.substring(0,tmp.length()-1))*1024;}}}if(tmp.indexOf(“M”)!=-1){if(m==2){//System.out.println(“—M—“+tmp); if(!tmp.equals(“”)&&!tmp.equals(“0”)){totalHD+=Double.parseDouble(tmp.substring(0,tmp.length()-1));}}if(m==3){//System.out.println(“—M—“+tmp); if(!tmp.equals(“none”)&&!tmp.equals(“0”)){usedHD+=Double.parseDouble(tmp.substring(0,tmp.length()-1));//System.out.println(“—-3—-“+usedHD); }}}}}}catch(Exceptione){e.printStackTrace();}finally{in.close();}return(usedHD/totalHD)*100;}/***获得CPU的个数*@return*@throwsjava.lang.Exception*/publicintgetIntCpuNum()throwsException{intrs=0;Runtimert=Runtime.getRuntime();//Processp=rt.exec(“cat/proc/cpuinfo|grepprocessor|wc-l”);//获得CPU个数 Processp=rt.exec(“cat/proc/cpuinfo”);//获得CPU个数 BufferedReaderin=null;try{in=newBufferedReader(newInputStreamReader(p.getInputStream()));Stringstr=null;String[]strarr=null;while((str=in.readLine())!=null){strarr=str.split(“:”);if(strarr[0].trim().equals(“processor”)){rs=Integer.valueOf(strarr[1].trim())+1;}}//Stringstr=in.readLine(); //System.out.println(“============================================”); //System.out.println(str); //str=str.trim(); //System.out.println(str); //System.out.println(“============================================”); //rs=Integer.valueOf(str); }catch(Exceptione){e.printStackTrace();}finally{in.close();}returnrs;}/***获得内存的大小*@return*@throwsjava.lang.Exception*/publicintgetIntMemMax()throwsException{intrs=0;Runtimert=Runtime.getRuntime();//Processp=rt.exec(“cat/proc/meminfo|grep-i/”^memtotal/”|cut-d/”:/”-f2|cut-d/”k/”-f1″);//获得内存大小 Processp=rt.exec(“cat/proc/meminfo”);//获得内存大小 BufferedReaderin=null;try{in=newBufferedReader(newInputStreamReader(p.getInputStream()));int[]result=newint[4];Stringstr=null;StringTokenizertoken=null;while((str=in.readLine())!=null){token=newStringTokenizer(str);if(!token.hasMoreTokens()){continue;}str=token.nextToken();if(!token.hasMoreTokens()){continue;}if(str.equalsIgnoreCase(“MemTotal:”)){result[0]=Integer.parseInt(token.nextToken());rs=result[0];}elseif(str.equalsIgnoreCase(“MemFree:”)){result[1]=Integer.parseInt(token.nextToken());}elseif(str.equalsIgnoreCase(“SwapTotal:”)){result[2]=Integer.parseInt(token.nextToken());}elseif(str.equalsIgnoreCase(“SwapFree:”)){result[3]=Integer.parseInt(token.nextToken());}}}catch(Exceptione){e.printStackTrace();}finally{in.close();}returnrs/1000;}/***获得的CPUHMZ*@return*@throwsjava.lang.Exception*/publicintgetIntCpuHMZ()throwsException{intcpuhmz=0;Runtimert=Runtime.getRuntime();Processp=rt.exec(“cat/proc/cpuinfo”);//读CPU信息文件 BufferedReaderin=null;try{in=newBufferedReader(newInputStreamReader(p.getInputStream()));Stringstr=null;String[]strarr=null;while((str=in.readLine())!=null){strarr=str.split(“:”);if(strarr.length>0){if(strarr[0].trim().equals(“cpuMHz”)){cpuhmz=Double.valueOf(strarr[1].trim()).intValue();}}}}catch(Exceptione){e.printStackTrace();}finally{in.close();}returncpuhmz;}/***获取磁盘空间大小(总)**@return*@throwsException*/publicintgetDeskAll()throwsException{doubletotalHD=0;Runtimert=Runtime.getRuntime();Processp=rt.exec(“df-hl”);//df-hl查看硬盘空间 BufferedReaderin=null;try{in=newBufferedReader(newInputStreamReader(p.getInputStream()));Stringstr=null;String[]strArray=null;while((str=in.readLine())!=null){intm=0;strArray=str.split(“”);for(Stringtmp:strArray){if(tmp.trim().length()==0){continue;}++m;//System.out.println(“—-tmp—-“+tmp); if(tmp.indexOf(“G”)!=-1){if(m==2){//System.out.println(“—G—-“+tmp); if(!tmp.equals(“”)&&!tmp.equals(“0”)){totalHD+=Double.parseDouble(tmp.substring(0,tmp.length()-1))*1024;}}if(m==3){//System.out.println(“—G—-“+tmp); }}if(tmp.indexOf(“M”)!=-1){if(m==2){//System.out.println(“—M—“+tmp); if(!tmp.equals(“”)&&!tmp.equals(“0”)){totalHD+=Double.parseDouble(tmp.substring(0,tmp.length()-1));}}if(m==3){//System.out.println(“—M—“+tmp); }}}}}catch(Exceptione){e.printStackTrace();}finally{in.close();}Doubled=newDouble(totalHD);returnd.intValue();}/***查询系统时间*@return*@throwsjava.io.IOException*/publiclonggetSysStartTime()throwsIOException,InterruptedException{longstatime=0;Runtimert=Runtime.getRuntime();Processp=rt.exec(“uptime”);//查看系统启动时间 //System.out.println(p.exitValue()); //p.waitFor(); BufferedReaderin=null;try{in=newBufferedReader(newInputStreamReader(p.getInputStream()));Stringstr=in.readLine();String[]strs=str.split(“,”);String[]rss=strs[1].trim().split(“”);if(rss.length==1){String[]times=rss[0].split(“:”);inthour=Integer.valueOf(times[0]);intmin=Integer.valueOf(times[1]);statime=(hour*60+min)*60*1000;}elseif(rss.length==2){intmin=Integer.valueOf(rss[0]);statime=min*60*1000;}}catch(Exceptione){e.printStackTrace();}finally{in.close();}returnstatime;}publicvoidshutdownh()throwsIOException{Runtimert=Runtime.getRuntime();Processp=rt.exec(“shutdown-h”);}publicvoidshutdownr()throwsIOException{Runtimert=Runtime.getRuntime();Processp=rt.exec(“shutdown-r”);}publicstaticvoidmain(String[]args)throwsException{LinuxExeccpu=newLinuxExec();System.out.println(“—————cpuused:”+cpu.getCpuUsage()+”%”);System.out.println(“—————memused:”+cpu.getMemUsage()+”%”);System.out.println(“—————HDused:”+cpu.getDeskUsage()+”%”);System.out.println(“—————CPUNUM:”+cpu.getIntCpuNum());System.out.println(“—————CPUHMZ:”+cpu.getIntCpuHMZ());System.out.println(“—————MENMAX:”+cpu.getIntMemMax());System.out.println(“—————DISKMAX:”+cpu.getDeskAll());System.out.println(“—————STARTTIME:”+cpu.getSysStartTime());System.out.println(“————jvm监控———————-“);RuntimelRuntime=Runtime.getRuntime();System.out.println(“————–FreeMomery:”+lRuntime.freeMemory()+”K”);System.out.println(“————–MaxMomery:”+lRuntime.maxMemory()+”K”);System.out.println(“————–TotalMomery:”+lRuntime.totalMemory()+”K”);System.out.println(“—————AvailableProcessors:”+lRuntime.availableProcessors());}}
让你的心情地落到极点,一直学习生活等各个方面都做不好,最终害的还是自己。

JAVA 调用LINUX命令类

相关文章:

你感兴趣的文章:

标签云: