天天看點

擷取Linux/Unix檔案系統資訊 設計一套SAP監控系統

 設計一套SAP監控系統

http://blog.csdn.net/bayaci/archive/2009/10/13/4666456.aspx

顯示系統df(linux )/ bdf (hpunix)的資訊

擷取Linux/Unix檔案系統資訊 設計一套SAP監控系統

執行代碼,擷取諸如

bdf /usr

指令後的資訊:

DiskInfo diskInfo = new DiskInfo();

diskInfo.info(directory);

long used = diskInfo.getUsed();

long free = diskInfo.getAvailable();

long kbyte = diskInfo.getKbyte();

代碼如下:

import java.io.IOException;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.SQLException;

import java.util.StringTokenizer;

public class DiskInfo {

     static final String NL = System.getProperty( "line.separator" );

   private static long getLong( String value ) throws NumberFormatException {

       try {

           return Long.parseLong( value );

       }

       catch( NumberFormatException nfe ) {

           throw new NumberFormatException( "On value "+value+": "+nfe.getMessage() );

       }

   }

   private String fileSystem ;

   private String mounted ;

private  long used = 0L;

private long kbyte = 0L;

private long available = 0L;

   public   void info(String directory  ) throws IOException {

      InputStream stream = null;

     // Connection connection = null; // Database Connection Object

         // to handle standard outputs

         final byte[] buf = new byte[1024];

         String[] strings;

         int i;

         int size;

         // note OS name

         final String osName = System.getProperty( "os.name" );

        // System.out.println( "system is  " + osName);

         if( "Linux".equals( osName ) ) {

            final Process p = Runtime.getRuntime().exec( "df " + directory );

            stream = p.getInputStream();

            final int count = stream.read( buf, 0, buf.length );

            String delim = NL;

            final StringTokenizer st = new StringTokenizer( new String( buf, 0, count ), delim,

true );

            int lineNum = 1;

            int field = 0;

            while( st.hasMoreTokens() ) {

                final String token = st.nextToken(delim);

                //System.out.println( "token is  " + token);

                if( lineNum < 2 ) {

                    if( NL.equals( token ) ) {

                        lineNum++;

                        if( lineNum == 2 ) {

                            delim = " " + NL;

                        }

                    }

                }// else

                if( !" ".equals( token ) ) {

                    field++;

                    if( field == 0 ) {

                        setFileSystem (   token  );

                    } else

                    if( field == 2 ) {

                        setUsed ( getLong( token ));

                    } else

                    if( field == 3 ) {

                        setAvailable( getLong( token ));

                    }

                }

            }

         }

         else

         if( "HP-UX".equals( osName ) ) {

            final Process p = Runtime.getRuntime().exec( "bdf " + directory );

            stream = p.getInputStream();

            final int count = stream.read( buf, 0, buf.length );

            String delim = NL;

            final StringTokenizer st = new StringTokenizer( new String( buf, 0, count ), delim,

true );

            int lineNum = 1;

            int field = 0;

            while( st.hasMoreTokens() ) {

                final String token = st.nextToken(delim);

                if( lineNum < 2 ) {

                    if( NL.equals( token ) ) {

                        lineNum++;

                        if( lineNum == 2 ) {

                            delim = " " + NL;

                        }

                    }

                } else

                if( !" ".equals( token ) ) {

                    field++;

                    if( field == 0 ) {

                        setFileSystem (   token  );

                    } else

                    if( field == 3 ) {

                        setUsed ( getLong( token ));

                    } else

                    if( field == 4 ) {

                        setAvailable( getLong( token ));

                    }

                }

            }

         } else {

            throw new RuntimeException( "Unknown OS name: " + osName + " to implement..." );

         }

         setKbyte ( getUsed() + getAvailable());

         setMounted(directory );

   }

   private void setUsed(long used) {

    this.used = used;

}

public long getUsed() {

    return used;

}

private void setKbyte(long kbyte) {

    this.kbyte = kbyte;

}

public long getKbyte() {

    return kbyte;

}

private void setAvailable(long available) {

    this.available = available;

}

public long getAvailable() {

    return available;

}

private void setFileSystem(String fileSystem) {

    this.fileSystem = fileSystem;

}

public String getFileSystem() {

    return fileSystem;

}

private void setMounted(String mounted) {

    this.mounted = mounted;

}

public String getMounted() {

    return mounted;

}

}

擷取Linux/Unix檔案系統資訊 設計一套SAP監控系統

繼續閱讀