View Javadoc
1   /*
2    * Copyright (c) 2006 Hyperic, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.miloss.fgsms.osagent;
17  
18  import org.hyperic.sigar.SigarException;
19  import org.hyperic.sigar.SigarNotImplementedException;
20  import org.hyperic.sigar.SigarPermissionDeniedException;
21  import org.hyperic.sigar.ProcFd;
22  import org.hyperic.sigar.ProcExe;
23  import org.hyperic.sigar.cmd.Shell;
24  
25  /**
26   * Display process file information.
27   */
28  public class ProcFileInfo extends SigarCommandBase implements Closable{
29  
30      public ProcFileInfo(Shell shell) {
31          super(shell);
32      }
33  
34      public ProcFileInfo() {
35          super();
36      }
37  
38  
39      public long GetOpenFileCount(String processname) throws SigarException {
40          long[] pids = this.shell.findPids(processname);
41          if (pids == null || pids.length == 0) {
42              return -1;
43          }
44          long openfiles = 0;
45  
46          for (int i = 0; i < pids.length; i++) {
47              try {
48                  ProcFd fd = sigar.getProcFd(pids[i]);
49                  println("open file descriptors=" + fd.getTotal());
50              } catch (SigarNotImplementedException e) {
51              }
52          }
53          return openfiles;
54      }
55  
56          @Override
57      public void close() throws Exception{
58          if (sigar != null) {
59              sigar.close();
60              sigar = null;
61          }
62      }
63      @Override
64      protected void finalize() throws Throwable
65      {
66          
67            if (sigar != null) {
68                System.out.println("WARN,. finalize called without closing sigar first"+ this.getClass().getCanonicalName());
69              sigar.close();
70          }
71          super.finalize();
72      }
73  }