1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.miloss.fgsms.osagent;
17
18 import org.hyperic.sigar.Sigar;
19 import org.hyperic.sigar.SigarProxy;
20 import org.hyperic.sigar.SigarException;
21 import org.hyperic.sigar.ProcCredName;
22 import org.hyperic.sigar.ProcMem;
23 import org.hyperic.sigar.ProcTime;
24 import org.hyperic.sigar.ProcState;
25 import org.hyperic.sigar.ProcUtil;
26
27 import java.util.ArrayList;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.text.SimpleDateFormat;
31 import java.util.*;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
34 import org.miloss.fgsms.common.Utility;
35 import org.miloss.fgsms.services.interfaces.common.ProcessPerformanceData;
36 import org.hyperic.sigar.*;
37 import org.hyperic.sigar.cmd.Shell;
38
39
40
41
42
43 public class Ps extends SigarCommandBase implements Closable {
44
45 public Ps(Shell shell) {
46 super(shell);
47 }
48
49 public Ps() {
50 super();
51 }
52
53
54 public boolean isPidCompleter() {
55 return true;
56 }
57
58 public static List getInfo(SigarProxy sigar, long pid)
59 throws SigarException {
60
61 ProcState state = sigar.getProcState(pid);
62 ProcTime time = null;
63 String unknown = "???";
64
65 List info = new ArrayList();
66 info.add(String.valueOf(pid));
67
68 try {
69 ProcCredName cred = sigar.getProcCredName(pid);
70 info.add(cred.getUser());
71 } catch (SigarException e) {
72 info.add(unknown);
73 }
74
75 try {
76 time = sigar.getProcTime(pid);
77 info.add(getStartTime(time.getStartTime()));
78 } catch (SigarException e) {
79 info.add(unknown);
80 }
81
82 try {
83 ProcMem mem = sigar.getProcMem(pid);
84 info.add(Sigar.formatSize(mem.getSize()));
85 info.add(Sigar.formatSize(mem.getRss()));
86 info.add(Sigar.formatSize(mem.getShare()));
87 } catch (SigarException e) {
88 info.add(unknown);
89 }
90
91 info.add(String.valueOf(state.getState()));
92
93 if (time != null) {
94 info.add(getCpuTime(time));
95 } else {
96 info.add(unknown);
97 }
98
99 String name = ProcUtil.getDescription(sigar, pid);
100 info.add(name);
101
102 return info;
103 }
104
105
106 public List<String> runningProcesses() throws SigarException {
107 List<String> ret = new ArrayList<String>();
108 long[] pids = this.proxy.getProcList();
109
110
111
112
113 for (int i = 0; i < pids.length; i++) {
114
115
116
117
118
119
120
121
122 try {
123 String name = ProcUtil.getDescription(sigar, pids[i]);
124 if (!Utility.stringIsNullOrEmpty(name)) {
125 ret.add(name);
126 }
127 } catch (Exception ex) {
128 }
129 }
130 return ret;
131 }
132
133 public static String getCpuTime(long total) {
134 long t = total / 1000;
135 return t / 60 + ":" + t % 60;
136 }
137
138 public static String getCpuTime(ProcTime time) {
139 return getCpuTime(time.getTotal());
140 }
141
142 private static String getStartTime(long time) {
143 if (time == 0) {
144 return "00:00";
145 }
146 long timeNow = System.currentTimeMillis();
147 String fmt = "MMMd";
148
149 if ((timeNow - time) < ((60 * 60 * 24) * 1000)) {
150 fmt = "HH:mm";
151 }
152
153 return new SimpleDateFormat(fmt).format(new Date(time));
154 }
155
156 public long GetSystemBootTimeInMS() {
157 try {
158 double timesincebootupinseconds = sigar.getUptime().getUptime();
159 long l = (long) (timesincebootupinseconds * 1000);
160 return l;
161
162 } catch (SigarException e) {
163 return System.currentTimeMillis();
164 }
165
166 }
167
168 public double GetCurrentCPUUsage() throws SigarException {
169 CpuPerc c = sigar.getCpuPerc();
170 return ((1 - c.getIdle()) * 100.0);
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204 }
205
206 public long CurrentThreadCount() throws SigarException {
207 return sigar.getProcStat().getThreads();
208 }
209
210 ProcessPerformanceData GetProcessData(List<Long> pids) {
211 ProcessPerformanceData ppd = new ProcessPerformanceData();
212 long theads = 0;
213 long memb = 0;
214 double cpu = 0;
215 long files = 0;
216 if (pids.isEmpty()) {
217 ppd.setOperationalstatus(false);
218 ppd.setStatusmessage("Stopped");
219 return ppd;
220 }
221 ppd.setOperationalstatus(true);
222 long startTimeinseconds = 0;
223 double systemuptime = 0;
224 for (int i = 0; i < pids.size(); i++) {
225
226 try {
227
228 ProcCpu cpu2 = sigar.getProcCpu(pids.get(i));
229 long time = cpu2.getTotal();
230 Thread.sleep(1000);
231 cpu2 = sigar.getProcCpu(pids.get(i));
232 time = cpu2.getTotal() - time;
233 float p = (float) ((float) (time) / 1000F) * 100;
234 p = p / GetCPUCores();
235 if (p < 0) {
236 p = 0;
237 }
238 cpu += p;
239
240
241 } catch (Exception ex) {
242
243 }
244 try {
245 files += (sigar.getProcFd(pids.get(i)).getTotal());
246 } catch (SigarException ex) {
247
248 }
249 try {
250 systemuptime = sigar.getUptime().getUptime();
251 } catch (SigarException ex) {
252
253 }
254 try {
255 startTimeinseconds = sigar.getProcCpu(pids.get(i)).getStartTime();
256 } catch (SigarException ex) {
257
258 }
259
260
261
262 try {
263 ProcState state = sigar.getProcState(pids.get(i));
264 char sta = state.getState();
265 String stastr = "" + sta;
266 ppd.setStatusmessage(stastr);
267 } catch (SigarException ex) {
268
269 }
270 try {
271
272
273
274
275
276
277
278
279
280
281
282 theads += (sigar.getProcState(pids.get(i)).getThreads());
283 } catch (SigarException ex) {
284
285 }
286 try {
287 ProcMem mem = sigar.getProcMem(pids.get(i));
288 memb += (mem.getSize());
289
290 } catch (SigarException e) {
291 }
292
293
294
295
296
297
298
299
300
301 }
302 GregorianCalendar gcal = new GregorianCalendar();
303 if (startTimeinseconds > 0) {
304 gcal.setTimeInMillis(System.currentTimeMillis() - (startTimeinseconds * 1000));
305 }
306 if (systemuptime > 0) {
307 gcal.setTimeInMillis(System.currentTimeMillis() - (new Double(systemuptime).longValue() * 1000));
308 }
309 try {
310 ppd.setStartedAt((gcal));
311 } catch (Exception ex) {
312
313 }
314 ppd.setPercentusedCPU(cpu);
315 ppd.setOpenFileHandles(files);
316 ppd.setBytesusedMemory(memb);
317 ppd.setNumberofActiveThreads(theads);
318 return ppd;
319 }
320
321 public int GetCPUCores() {
322 int cores = 0;
323 try {
324 org.hyperic.sigar.CpuInfo[] c = sigar.getCpuInfoList();
325 if (c == null) {
326 return 1;
327 }
328 if (c.length > 0) {
329 cores = c[0].getTotalCores();
330 }
331
332
333
334 } catch (SigarException ex) {
335 Logger.getLogger(Ps.class.getName()).log(Level.SEVERE, null, ex);
336 }
337 return cores;
338 }
339
340 @Override
341 public void close() throws Exception {
342 if (sigar != null) {
343 sigar.close();
344 sigar = null;
345 }
346 }
347 @Override
348 protected void finalize() throws Throwable
349 {
350
351 if (sigar != null) {
352 System.out.println("WARN,. finalize called without closing sigar first"+ this.getClass().getCanonicalName());
353 sigar.close();
354 }
355 super.finalize();
356 }
357 }