1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
46
47
48
49
50
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));
58
59 if (processname.contains("\\")) {
60 String s = processname.substring(processname.lastIndexOf("\\"));
61 if (Utility.stringIsNullOrEmpty(s)) {
62 pids.addAll(getPidsByName(s, false));
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));
72 }
73 }
74 return pids;
75 }
76
77
78
79
80
81
82
83
84
85 public List<Long> getDataWindows(String servicename, String altname, boolean IsWindowsService) throws SigarException {
86 List<Long> pids = new ArrayList<Long>();
87
88
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
102 pids.addAll(getPidsByName(exe, true));
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
115
116 Service svc = new Service(servicename);
117 String exe = svc.getConfig().getExe();
118 int status = svc.getStatus();
119
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
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());
136 test = test.substring(0, test.lastIndexOf("."));
137 } else if ((test.lastIndexOf("\\") >= 0)) {
138 test = test.substring(test.lastIndexOf("\\") + 1, test.length());
139
140 }
141 long[] pids = this.proxy.getProcList();
142
143
144
145
146 for (int i = 0; i < pids.length; i++) {
147
148 try {
149
150 ProcExe pe = sigar.getProcExe(pids[i]);
151
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
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
174
175
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
185
186
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
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 }