java代码读取linux主机的磁盘使用信息同时截取出文件系统和已使用情况.docx
《java代码读取linux主机的磁盘使用信息同时截取出文件系统和已使用情况.docx》由会员分享,可在线阅读,更多相关《java代码读取linux主机的磁盘使用信息同时截取出文件系统和已使用情况.docx(8页珍藏版)》请在冰点文库上搜索。
java代码读取linux主机的磁盘使用信息同时截取出文件系统和已使用情况
1.packagecom.cmmb.util;
2.
3.importjava.io.*;
4.
5.
6./**
7.*linux下cpu内存磁盘jvm的使用监控
8.*@authoravery_leo
9.*
10.*/
11.publicclassDiskSpace{
12./**
13.*获取cpu使用情况
14.*@return
15.*@throwsException
16.*/
17.publicdoublegetCpuUsage()throwsException{
18.doublecpuUsed=0;
19.
20.Runtimert=Runtime.getRuntime();
21.Processp=rt.exec("top-b-n1");//调用系统的“top"命令
22.
23.
24.BufferedReaderin=null;
25.try{
26.in=newBufferedReader(newInputStreamReader(p.getInputStream()));
27.Stringstr=null;
28.String[]strArray=null;
29.
30.while((str=in.readLine())!
=null){
31.intm=0;
32.
33.if(str.indexOf("R")!
=-1){//只分析正在运行的进程,top进程本身除外&&
34.
35.strArray=str.split("");
36.for(Stringtmp:
strArray){
37.if(tmp.trim().length()==0)
38.continue;
39.if(++m==9){//第9列为CPU的使用百分比(RedHat
40.
41.cpuUsed+=Double.parseDouble(tmp);
42.
43.}
44.
45.}
46.
47.}
48.}
49.}catch(Exceptione){
50.e.printStackTrace();
51.}finally{
52.in.close();
53.}
54.returncpuUsed;
55.}
56./**
57.*内存监控
58.*@return
59.*@throwsException
60.*/
61.publicdoublegetMemUsage()throwsException{
62.
63.doublemenUsed=0;
64.Runtimert=Runtime.getRuntime();
65.Processp=rt.exec("top-b-n1");//调用系统的“top"命令
66.
67.
68.BufferedReaderin=null;
69.try{
70.in=newBufferedReader(newInputStreamReader(p.getInputStream()));
71.Stringstr=null;
72.String[]strArray=null;
73.
74.while((str=in.readLine())!
=null){
75.intm=0;
76.
77.if(str.indexOf("R")!
=-1){//只分析正在运行的进程,top进程本身除外&&
78.//
79.//System.out.println("------------------3-----------------");
80.strArray=str.split("");
81.for(Stringtmp:
strArray){
82.if(tmp.trim().length()==0)
83.continue;
84.
85.if(++m==10){
86.//9)--第10列为mem的使用百分比(RedHat9)
87.
88.menUsed+=Double.parseDouble(tmp);
89.
90.}
91.}
92.
93.}
94.}
95.}catch(Exceptione){
96.e.printStackTrace();
97.}finally{
98.in.close();
99.}
100.returnmenUsed;
101.}
102.
103./**
104.*获取磁盘空间大小
105.*
106.*@return
107.*@throwsException
108.*/
109.publicdoublegetDeskUsage()throwsException{
110.doubletotalHD=0;
111.doubleusedHD=0;
112.Runtimert=Runtime.getRuntime();
113.Processp=rt.exec("df-hl/home");//df-hl查看硬盘空间
114.
115.
116.
117.BufferedReaderin=null;
118.try{
119.in=newBufferedReader(newInputStreamReader(p.getInputStream()));
120.Stringstr=null;
121.String[]strArray=null;
122.while((str=in.readLine())!
=null){
123.intm=0;
124.strArray=str.split("");
125.for(Stringtmp:
strArray){
126.if(tmp.trim().length()==0)
127.continue;
128.++m;
129.System.out.println("----tmp----"+tmp);
130.if(tmp.indexOf("G")!
=-1){
131.if(m==2){
132.System.out.println("---G----"+tmp);
133.if(!
tmp.equals("")&&!
tmp.equals("0"))
134.totalHD+=Double.parseDouble(tmp
135..substring(0,tmp.length()-1))*1024;
136.
137.}
138.if(m==3){
139.System.out.println("---G----"+tmp);
140.if(!
tmp.equals("none")&&!
tmp.equals("0"))
141.usedHD+=Double.parseDouble(tmp.substring(
142.0,tmp.length()-1))*1024;
143.
144.}
145.}
146.if(tmp.indexOf("M")!
=-1){
147.if(m==2){
148.System.out.println("---M---"+tmp);
149.if(!
tmp.equals("")&&!
tmp.equals("0"))
150.totalHD+=Double.parseDouble(tmp
151..substring(0,tmp.length()-1));
152.
153.}
154.if(m==3){
155.System.out.println("---M---"+tmp);
156.if(!
tmp.equals("none")&&!
tmp.equals("0"))
157.usedHD+=Double.parseDouble(tmp.substring(
158.0,tmp.length()-1));
159.System.out.println("----3----"+usedHD);
160.}
161.}
162.
163.}
164.
165.}
166.}catch(Exceptione){
167.e.printStackTrace();
168.}finally{
169.in.close();
170.}
171.//上面写在userd和total写反了,懒得改了,就反着用了
172.System.out.println("----totalHD----"+usedHD);
173.System.out.println("----usedHD----"+totalHD);
174.return(totalHD/usedHD)*100;
175.}
176.
177.
178.publicstaticvoidmain(String[]args)throwsException{
179.DiskSpacecpu=newDiskSpace();
180.System.out.println("---------------cpuused:
"+cpu.getCpuUsage()+"%");
181.System.out.println("---------------memused:
"+cpu.getMemUsage()+"%");
182.System.out.println("---------------HDused:
"+cpu.getDeskUsage()+"%");
183.System.out.println("------------jvm监控----------------------");
184.
185.RuntimelRuntime=Runtime.getRuntime();
186.System.out.println("--------------FreeMomery:
"+lRuntime.freeMemory()+"K");
187.System.out.println("--------------MaxMomery:
"+lRuntime.maxMemory()+"K");
188.System.out.println("--------------TotalMomery:
"+lRuntime.totalMemory()+"K");
189.System.out.println("---------------AvailableProcessors:
"
190.+lRuntime.availableProcessors());
191.}
192.}