KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > probe > system > Insert


1 /*
2 * CLIF is a Load Injection Framework
3 * Copyright (C) 2004 France Telecom R&D
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * CLIF $Name: $
20 *
21 * Contact: clif@objectweb.org
22 */

23
24 package org.objectweb.clif.probe.system;
25
26 import org.objectweb.clif.supervisor.api.ClifException;
27 import org.objectweb.clif.probe.util.AbstractDumbInsert;
28 import org.objectweb.clif.storage.api.ProbeEvent;
29 import org.objectweb.lewys.probe.Probe;
30 import org.objectweb.lewys.common.exceptions.NoResourceToProbeException;
31 import org.objectweb.lewys.common.exceptions.NoSuchResourceException;
32 import org.objectweb.lewys.common.exceptions.ProbeException;
33
34
35 /**
36  *
37  * @author Bruno Dillenseger
38  */

39 public class Insert extends AbstractDumbInsert
40 {
41     static private final int[] cpuResourceIds = new int[] { 0, 2, 3 };
42     static private final int[] memoryResourceIds = new int[] { 0, 1, 7, 8 };
43
44     private Probe cpuProbe, memoryProbe;
45     private long[] cpuLastTicks = new long[cpuResourceIds.length];
46
47     public Insert()
48         throws ClifException
49     {
50         try
51         {
52             if (System.getProperty("os.name").equalsIgnoreCase("linux"))
53             {
54                 memoryProbe = new org.objectweb.lewys.probe.linux.MemoryProbe();
55                 cpuProbe = new org.objectweb.lewys.probe.linux.CpuProbe();
56             }
57             else if (System.getProperty("os.name").startsWith("Windows"))
58             {
59                 memoryProbe = new org.objectweb.lewys.probe.windows.MemoryProbe();
60                 cpuProbe = new org.objectweb.lewys.probe.windows.CpuProbe();
61             }
62             else
63             {
64                 throw new ClifException("No system probe available for " + System.getProperty("os.name") + " in blade " + getId());
65             }
66             cpuLastTicks = cpuProbe.getValue(cpuResourceIds);
67         }
68         catch (NoResourceToProbeException ex)
69         {
70             throw new ClifException("Can't set system probe in blade " + getId(), ex);
71         }
72         catch (NoSuchResourceException ex)
73         {
74             throw new Error JavaDoc("Incompatible system probe implementation in blade " + getId(), ex);
75         }
76         catch (ProbeException ex)
77         {
78             throw new ClifException("Can't run system probe in blade " + getId(), ex);
79         }
80     }
81
82
83     public ProbeEvent doProbe()
84     {
85         long[] result = new long[SystemEvent.getFieldLabels().length];
86         try
87         {
88             long[] values;
89             // CPU usage %
90
values = cpuProbe.getValue(cpuResourceIds);
91             result[0] = values[0] - cpuLastTicks[0] + values[1] - cpuLastTicks[1];
92             result[0] = (100 * result[0]) / (result[0] + values[2] - cpuLastTicks[2]);
93             cpuLastTicks = values;
94             // memory usage %
95
values = memoryProbe.getValue(memoryResourceIds);
96             result[1] = (values[1] * 100) / values[0];
97             // swap usage
98
result[2] = values[2];
99             result[3] = values[3];
100             return new SystemEvent(
101                 System.currentTimeMillis(),
102                 probeId,
103                 result);
104         }
105         catch (Exception JavaDoc ex)
106         {
107             ex.printStackTrace(System.err);
108             return null;
109         }
110     }
111 }
112
Popular Tags