获取Linux(redhat)系统的一些基本信息
来源:互联网 发布:mac照片浏览软件 编辑:程序博客网 时间:2024/04/29 12:14
package com.sundy.util;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.text.DecimalFormat;import java.util.Properties;public class LinuxUtil {/** * 获取CPU的数量 * @return */public static int getCPUNumber(){return Runtime.getRuntime().availableProcessors();} /** * 获取CPU使用率 * @return 如: 32.54% */ public static String getCPUUsage() throws IOException{ float cpuUsage = 0; Process pro1,pro2; Runtime r = Runtime.getRuntime(); try { String command = "cat /proc/stat"; //第一次采集CPU时间 pro1 = r.exec(command); BufferedReader in1 = new BufferedReader(new InputStreamReader(pro1.getInputStream())); String line = null; long idleCpuTime1 = 0, totalCpuTime1 = 0; //分别为系统启动后空闲的CPU时间和总的CPU时间 while((line=in1.readLine()) != null){ if(line.startsWith("cpu")){ line = line.trim(); String[] temp = line.split("\\s+"); idleCpuTime1 = Long.parseLong(temp[4]); for(String s : temp){ if(!s.equals("cpu")){ totalCpuTime1 += Long.parseLong(s); } } break; } } in1.close(); pro1.destroy(); try { Thread.sleep(1500); } catch (InterruptedException e) { e.printStackTrace(); } //第二次采集CPU时间 pro2 = r.exec(command); BufferedReader in2 = new BufferedReader(new InputStreamReader(pro2.getInputStream())); long idleCpuTime2 = 0, totalCpuTime2 = 0; //分别为系统启动后空闲的CPU时间和总的CPU时间 while((line=in2.readLine()) != null){ if(line.startsWith("cpu")){ line = line.trim(); String[] temp = line.split("\\s+"); idleCpuTime2 = Long.parseLong(temp[4]); for(String s : temp){ if(!s.equals("cpu")){ totalCpuTime2 += Long.parseLong(s); } } break; } } if(idleCpuTime1 != 0 && totalCpuTime1 !=0 && idleCpuTime2 != 0 && totalCpuTime2 !=0){ cpuUsage = 1 - (float)(idleCpuTime2 - idleCpuTime1)/(float)(totalCpuTime2 - totalCpuTime1); } in2.close(); pro2.destroy(); } catch (IOException e) { e.printStackTrace(); } return new DecimalFormat("##0.00").format(cpuUsage*100)+"%"; } /** * 获取可用的内存 * @return 如: 42.54% * @throws IOException */ public static String availableMemory() throws IOException{ Runtime runtime = Runtime.getRuntime(); String command = " free"; Process process = runtime.exec(command); BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream())); String line = null; long tatal = 0; long available = 0; long free = 0; String str = null; while((line=br.readLine())!=null){ if(line.startsWith("Mem:")){ String[] split = line.split("\\s+"); tatal = Long.valueOf(split[1]); available = Long.valueOf(split[3])+Long.valueOf(split[5])+Long.valueOf(split[6]); } } if(tatal!=0){ str = new DecimalFormat("##0.00").format((free+available)/(tatal*1.0f)*100); } if(br!=null) br.close(); if(process!=null) process.destroy(); return str==null?"0.00%":str+"%"; } /** * 已经用的内存 * @return 如: 42.54% * @throws IOException */ public static String usedMemory() throws IOException{ Runtime runtime = Runtime.getRuntime(); String command = " free"; Process process = runtime.exec(command); BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream())); String line = null; long tatal = 0; long used = 0; String str = null; while((line=br.readLine())!=null){ if(line.startsWith("Mem:")){ String[] split = line.split("\\s+"); tatal = Long.valueOf(split[1]); used = Long.valueOf(split[2])-Long.valueOf(split[5])-Long.valueOf(split[6]); } } if(tatal!=0){ str = new DecimalFormat("##0.00").format(used/(tatal*1.0f)*100); } if(br!=null) br.close(); if(process!=null) process.destroy(); return str==null?"0.00%":str+"%"; } /** * 磁盘使用率 * @return * @throws Exception */public static String getDiskUsage() throws Exception{Long[] info = getDiskInfo();if(info==null || info.length<=0) return "0.00%";long total = info[0]+info[1];return new DecimalFormat("#0.00").format(info[0]/(total*1.0f));}/** * 已用磁盘空间情况 * @return 下标0为已用,1为可用。 */public static Long[] getDiskInfo() throws Exception{BufferedReader br = null ;Process process = null;Long[] str = null;try {Runtime runtime = Runtime.getRuntime(); String command = " df -a"; process = runtime.exec(command); br = new BufferedReader(new InputStreamReader(process.getInputStream())); String line = null; while((line = br.readLine())!=null){ String[] split = line.split("\\s+"); if(line.startsWith("/dev")&&split.length>3){ str = new Long[2]; str[0] = Long.parseLong(line.split("\\s+")[2]); str[1] = Long.parseLong(line.split("\\s+")[3]); break; }else if(line.startsWith("")||line.startsWith(" ")){ str = new Long[2]; str[0] = Long.parseLong(line.split("\\s+")[1]); str[1] = Long.parseLong(line.split("\\s+")[2]); break; } }} catch (Exception e) {e.printStackTrace();}finally{if(process!=null) process.destroy();if(br!=null) br.close();}return str;}/** * 获取系统的信息 * @return下标0表示 系统名称,下标1表示系统版本 */public static String[] getSystemInfo(){Properties props = System.getProperties();String[] str = {props.getProperty("os.name"),props.getProperty("os.version")};return str;}}
阅读全文
0 0
- 获取Linux(redhat)系统的一些基本信息
- 获取系统的一些基本信息
- java获取系统的基本信息
- 获取数据库表的一些基本信息
- [MSSQL]获取数据库的一些基本信息
- 获取未安装APK的一些基本信息
- Android 获取手机的一些基本信息
- Android 获取手机的一些基本信息类
- 得到系统的一些基本信息的类
- 获取系统内Applications的基本信息
- 获取系统内Applications的基本信息
- Linux中一些基本信息的查看
- 获取Solaris系统基本信息
- Android获取系统基本信息
- 系统基本信息 linux
- Linux 查看系统基本信息
- C++ 获取windows一些基本信息
- 在网站了解到的一些,获取数据库基本信息的语句
- tcp/ip协议栈--socket API 之发送函数(send/write)
- 转发和重定向的区别
- 【codevs 1079】回家
- 分享简单数据
- Shell练习 一:变量基础
- 获取Linux(redhat)系统的一些基本信息
- Js 数据类型强制转换
- 通知NotificationManager的简单使用
- Java获取文件大小方式
- VGG16结构图
- java实现文件上传下载功能
- Spring2.5.5常用配置
- 常用UNIX网络命令
- logstash安装和logstash-input-jdbc插件