View Javadoc
1   /*
2    * Copyright (c) 2006-2007 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 java.util.ArrayList;
19  import java.util.List;
20  import org.hyperic.sigar.ProcExe;
21  import org.hyperic.sigar.ProcState;
22  import org.hyperic.sigar.ProcUtil;
23  import org.hyperic.sigar.SigarException;
24  import org.miloss.fgsms.common.Utility;
25  import org.miloss.fgsms.services.interfaces.common.ProcessPerformanceData;
26  
27  import org.hyperic.sigar.cmd.Shell;
28  import org.hyperic.sigar.win32.Service;
29  import org.hyperic.sigar.win32.ServiceConfig;
30  
31  /**
32   * Display all process information.
33   */
34  public class ProcInfo extends SigarCommandBase implements Closable {
35  
36      public ProcInfo(Shell shell) {
37          super(shell);
38      }
39  
40      public ProcInfo() {
41          super();
42      }
43  
44      /**
45       * gets the list of PIDs for a specific process merged by name and altname
46       *
47       * @param processname
48       * @param altname
49       * @return
50       * @throws SigarException
51       */
52      public List<Long> GetData(String processname, String altname) throws SigarException {
53          List<Long> pids = new ArrayList<Long>();
54  
55          ProcessPerformanceData ppd = new ProcessPerformanceData();
56  
57          pids.addAll(getPidsByName(processname, false)); //this.shell.findPids(processname);
58  
59          if (processname.contains("\\")) {
60              String s = processname.substring(processname.lastIndexOf("\\"));
61              if (Utility.stringIsNullOrEmpty(s)) {
62                  pids.addAll(getPidsByName(s, false)); //this.shell.findPids(processname);
63              }
64          }
65          if (Utility.stringIsNullOrEmpty(altname)) {
66              pids.addAll(getPidsByName(altname, false));
67          }
68          if (processname.contains("/")) {
69              String s = processname.substring(processname.lastIndexOf("/"));
70              if (Utility.stringIsNullOrEmpty(s)) {
71                  pids.addAll(getPidsByName(s, false)); //this.shell.findPids(processname);
72              }
73          }
74          return pids;
75      }
76  
77      /**
78       * gets the list of PIDs for a specific process merged by name and altname
79       *
80       * @param processname
81       * @param altname
82       * @return
83       * @throws SigarException
84       */
85      public List<Long> getDataWindows(String servicename, String altname, boolean IsWindowsService) throws SigarException {
86          List<Long> pids = new ArrayList<Long>();
87          //System.out.println("service name" + servicename + " " + altname);
88          // ServiceConfig serviceConfig = new ServiceConfig(servicename);
89          if (!Utility.stringIsNullOrEmpty(altname)) {
90              pids.addAll(getPidsByName(altname, IsWindowsService));
91          }
92          try {
93              Service svc = new Service(servicename);
94              ServiceConfig config = svc.getConfig();
95              String exe = svc.getConfig().getExe();
96              String path = svc.getConfig().getPath();
97              String statusstr = svc.getStatusString();
98              String x = svc.getConfig().getStartName();
99              int status = svc.getStatus();
100 
101             //ProcessPerformanceData ppd = new ProcessPerformanceData();
102             pids.addAll(getPidsByName(exe, true)); //this.shell.findPids(processname);
103         } catch (org.hyperic.sigar.win32.Win32Exception ex) {
104             throw ex;
105         } catch (Exception ex) {
106             ex.printStackTrace();
107         }
108 
109         return pids;
110     }
111    
112 
113     public void restartWindowsService(String servicename) throws SigarException {
114         // List<Long> pids = new ArrayList<Long>();
115         // ServiceConfig serviceConfig = new ServiceConfig(servicename);
116         Service svc = new Service(servicename);
117         String exe = svc.getConfig().getExe();
118         int status = svc.getStatus();
119         //only stop if it's in a stoppable state
120         if (status == 4) {
121             svc.stop();
122         }
123         svc.start();
124     }
125 
126     private List<Long> getPidsByName(String name, boolean isWindowsService) throws SigarException {
127 
128         //List<String> ret = new ArrayList<String>();
129         List<Long> list = new ArrayList<Long>();
130         if (Utility.stringIsNullOrEmpty(name)) {
131             return list;
132         }
133         String test = name;
134         if (isWindowsService && (test.lastIndexOf("\\") >= 0)) {
135             test = test.substring(test.lastIndexOf("\\") + 1, test.length());  //trim off the path name
136             test = test.substring(0, test.lastIndexOf("."));    //trim off the extension
137         } else if ((test.lastIndexOf("\\") >= 0)) {
138             test = test.substring(test.lastIndexOf("\\") + 1, test.length());  //trim off the path name
139             //   test = test.substring(0, test.lastIndexOf("."));    //trim off the extension
140         }
141         long[] pids = this.proxy.getProcList();
142         //   ProcState state = sigar.getProcState(pid);
143         //   ProcTime time = null;
144         //  String unknown = "???";
145 
146         for (int i = 0; i < pids.length; i++) {
147 
148             try {
149                 //ProcCredName cred = sigar.getProcCredName(pids[i]);
150                 ProcExe pe = sigar.getProcExe(pids[i]);
151                 //System.out.println(pe.getName());
152                 if (pe.getName().equalsIgnoreCase(test) || pe.getName().endsWith(test)) {
153 
154                     list.add(pids[i]);
155                 }
156             } catch (Exception e) {
157             }
158             try {
159                 ProcState ps = sigar.getProcState(pids[i]);
160 
161                 //  System.out.println(ps.getName());
162                 if (ps.getName().equalsIgnoreCase(test) || ps.getName().toLowerCase().endsWith(test.toLowerCase())) {
163                     list.add(pids[i]);
164                 }
165             } catch (Exception e) {
166             }
167         }
168         return list;
169     }
170 
171 
172     List<Long> getDataJava(String name, String alsoKnownAs) {
173         //throw new UnsupportedOperationException("Not yet implemented");
174 
175         //List<String> ret = new ArrayList<String>();
176         List<Long> list = new ArrayList<Long>();
177         if (Utility.stringIsNullOrEmpty(name)) {
178             return list;
179         }
180         String test = name.replace("java|", "java:");
181         try {
182 
183             long[] pids = this.proxy.getProcList();
184             //   ProcState state = sigar.getProcState(pid);
185             //   ProcTime time = null;
186             //  String unknown = "???";
187 
188             for (int i = 0; i < pids.length; i++) {
189                 try {
190                     String value = ProcUtil.getDescription(sigar, pids[i]);
191                     if (value.equalsIgnoreCase(test)) {
192                         list.add(pids[i]);
193                     }
194                 } catch (Exception e) {
195                 }
196 
197                 try {
198                     //ProcCredName cred = sigar.getProcCredName(pids[i]);
199                     ProcExe pe = sigar.getProcExe(pids[i]);
200                     if (pe.getName().equalsIgnoreCase(test)) {
201                         list.add(pids[i]);
202                     }
203                 } catch (Exception e) {
204                 }
205                 try {
206                     ProcState ps = sigar.getProcState(pids[i]);
207                     if (ps.getName().equalsIgnoreCase(test)) {
208                         list.add(pids[i]);
209                     }
210                 } catch (Exception e) {
211                 }
212             }
213         } catch (Exception ex) {
214         }
215         return list;
216     }
217 
218     @Override
219     public void close() throws Exception {
220         if (sigar != null) {
221             sigar.close();
222             sigar = null;
223         }
224     }
225 
226     @Override
227     protected void finalize() throws Throwable {
228 
229         if (sigar != null) {
230             System.out.println("WARN,. finalize called without closing sigar first" + this.getClass().getCanonicalName());
231             sigar.close();
232         }
233         super.finalize();
234     }
235 }