1 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 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 ("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 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 values = memoryProbe.getValue(memoryResourceIds); 96 result[1] = (values[1] * 100) / values[0]; 97 result[2] = values[2]; 99 result[3] = values[3]; 100 return new SystemEvent( 101 System.currentTimeMillis(), 102 probeId, 103 result); 104 } 105 catch (Exception ex) 106 { 107 ex.printStackTrace(System.err); 108 return null; 109 } 110 } 111 } 112 | Popular Tags |