|
From: <do...@hy...> - 2007-03-24 20:55:39
|
Author: dougm Date: 2007-03-24 12:55:36 -0800 (Sat, 24 Mar 2007) New Revision: 3864 URL: http://svn.hyperic.org/?view=rev&root=Hyperic+HQ&revision=3864 Added: trunk/plugins/system/src/org/hyperic/hq/plugin/system/ProcessData.java trunk/plugins/system/src/org/hyperic/hq/plugin/system/TopData.java trunk/plugins/system/src/org/hyperic/hq/plugin/system/UptimeData.java Log: wrapper classes for top command Added: trunk/plugins/system/src/org/hyperic/hq/plugin/system/ProcessData.java =================================================================== --- trunk/plugins/system/src/org/hyperic/hq/plugin/system/ProcessData.java (rev 0) +++ trunk/plugins/system/src/org/hyperic/hq/plugin/system/ProcessData.java 2007-03-24 20:55:36 UTC (rev 3864) @@ -0,0 +1,150 @@ +/* + * NOTE: This copyright does *not* cover user programs that use HQ + * program services by normal system calls through the application + * program interfaces provided as part of the Hyperic Plug-in Development + * Kit or the Hyperic Client Development Kit - this is merely considered + * normal use of the program, and does *not* fall under the heading of + * "derived work". + * + * Copyright (C) [2004, 2005, 2006], Hyperic, Inc. + * This file is part of HQ. + * + * HQ is free software; you can redistribute it and/or modify + * it under the terms version 2 of the GNU General Public License as + * published by the Free Software Foundation. This program is distributed + * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + */ +//XXX move this class to sigar +package org.hyperic.hq.plugin.system; + +import org.hyperic.sigar.ProcCpu; +import org.hyperic.sigar.ProcCredName; +import org.hyperic.sigar.ProcMem; +import org.hyperic.sigar.ProcState; +import org.hyperic.sigar.ProcTime; +import org.hyperic.sigar.ProcUtil; +import org.hyperic.sigar.Sigar; +import org.hyperic.sigar.SigarException; + +public class ProcessData { + + private long _pid; + private String _owner; + private long _startTime; + private long _size; + private long _resident; + private long _share; + private char _state; + private long _cpuTotal; + private double _cpuPerc; + private String _name; + + public ProcessData() {} + + public void populate(Sigar sigar, long pid) + throws SigarException { + + ProcState state = sigar.getProcState(pid); + ProcTime time = null; + final String unknown = "???"; + + _pid = pid; + + try { + ProcCredName cred = sigar.getProcCredName(pid); + _owner = cred.getUser(); + } catch (SigarException e) { + _owner = unknown; + } + + try { + time = sigar.getProcTime(pid); + _startTime = time.getStartTime(); + } catch (SigarException e) { + _startTime = Sigar.FIELD_NOTIMPL; + } + + try { + ProcMem mem = sigar.getProcMem(pid); + _size = mem.getSize(); + _resident = mem.getResident(); + _share = mem.getShare(); + } catch (SigarException e) { + _size = _resident = _share = Sigar.FIELD_NOTIMPL; + } + + _state = state.getState(); + + if (time != null) { + _cpuTotal = time.getTotal(); + } + else { + _cpuTotal = Sigar.FIELD_NOTIMPL; + } + + try { + ProcCpu cpu = sigar.getProcCpu(pid); + _cpuPerc = cpu.getPercent(); + } catch (SigarException e) { + _cpuPerc = Sigar.FIELD_NOTIMPL; + } + + _name = ProcUtil.getDescription(sigar, pid); + } + + public static ProcessData gather(Sigar sigar, long pid) + throws SigarException { + + ProcessData data = new ProcessData(); + data.populate(sigar, pid); + return data; + } + + public long getPid() { + return _pid; + } + + public String getOwner() { + return _owner; + } + + public long getStartTime() { + return _startTime; + } + + public long getSize() { + return _size; + } + + public long getShare() { + return _share; + } + + public long getResident() { + return _resident; + } + + public char getState() { + return _state; + } + + public long getCpuTotal() { + return _cpuTotal; + } + + public double getCpuPerc() { + return _cpuPerc; + } + + public String getName() { + return _name; + } +} Added: trunk/plugins/system/src/org/hyperic/hq/plugin/system/TopData.java =================================================================== --- trunk/plugins/system/src/org/hyperic/hq/plugin/system/TopData.java (rev 0) +++ trunk/plugins/system/src/org/hyperic/hq/plugin/system/TopData.java 2007-03-24 20:55:36 UTC (rev 3864) @@ -0,0 +1,113 @@ +/* + * NOTE: This copyright does *not* cover user programs that use HQ + * program services by normal system calls through the application + * program interfaces provided as part of the Hyperic Plug-in Development + * Kit or the Hyperic Client Development Kit - this is merely considered + * normal use of the program, and does *not* fall under the heading of + * "derived work". + * + * Copyright (C) [2004, 2005, 2006], Hyperic, Inc. + * This file is part of HQ. + * + * HQ is free software; you can redistribute it and/or modify + * it under the terms version 2 of the GNU General Public License as + * published by the Free Software Foundation. This program is distributed + * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + */ +//XXX move this class to sigar +package org.hyperic.hq.plugin.system; + +import java.util.ArrayList; +import java.util.List; + +import org.hyperic.sigar.CpuPerc; +import org.hyperic.sigar.CurrentProcessSummary; +import org.hyperic.sigar.Mem; +import org.hyperic.sigar.Sigar; +import org.hyperic.sigar.SigarException; +import org.hyperic.sigar.Swap; +import org.hyperic.sigar.ptql.ProcessFinder; + +public class TopData { + + private UptimeData _uptime; + private CurrentProcessSummary _currentProcessSummary; + private CpuPerc _cpu; + private Mem _mem; + private Swap _swap; + private List _processes; + + public TopData() {} + + public void populate(Sigar sigar, String filter) + throws SigarException { + + _uptime = UptimeData.gather(sigar); + _currentProcessSummary = CurrentProcessSummary.get(sigar); + _cpu = sigar.getCpuPerc(); + _mem = sigar.getMem(); + _swap = sigar.getSwap(); + + ps(sigar, filter); + } + + private void ps(Sigar sigar, String filter) throws SigarException { + _processes = new ArrayList(); + long[] pids; + if (filter == null) { + pids = sigar.getProcList(); + } + else { + pids = ProcessFinder.find(sigar, filter); + } + + for (int i=0; i<pids.length; i++) { + long pid = pids[i]; + try { + _processes.add(ProcessData.gather(sigar, pid)); + } catch (SigarException e) { + + } + } + } + + public static TopData gather(Sigar sigar, String filter) + throws SigarException { + + TopData data = new TopData(); + data.populate(sigar, filter); + return data; + } + + public UptimeData getUptime() { + return _uptime; + } + + public CurrentProcessSummary getCurrentProcessSummary() { + return _currentProcessSummary; + } + + public CpuPerc getCpu() { + return _cpu; + } + + public Mem getMem() { + return _mem; + } + + public Swap getSwap() { + return _swap; + } + + public List getProcesses() { + return _processes; + } +} Added: trunk/plugins/system/src/org/hyperic/hq/plugin/system/UptimeData.java =================================================================== --- trunk/plugins/system/src/org/hyperic/hq/plugin/system/UptimeData.java (rev 0) +++ trunk/plugins/system/src/org/hyperic/hq/plugin/system/UptimeData.java 2007-03-24 20:55:36 UTC (rev 3864) @@ -0,0 +1,67 @@ +/* + * NOTE: This copyright does *not* cover user programs that use HQ + * program services by normal system calls through the application + * program interfaces provided as part of the Hyperic Plug-in Development + * Kit or the Hyperic Client Development Kit - this is merely considered + * normal use of the program, and does *not* fall under the heading of + * "derived work". + * + * Copyright (C) [2004, 2005, 2006], Hyperic, Inc. + * This file is part of HQ. + * + * HQ is free software; you can redistribute it and/or modify + * it under the terms version 2 of the GNU General Public License as + * published by the Free Software Foundation. This program is distributed + * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + */ +//XXX move this class to sigar +package org.hyperic.hq.plugin.system; + +import org.hyperic.sigar.Sigar; +import org.hyperic.sigar.SigarException; +import org.hyperic.sigar.SigarNotImplementedException; + +public class UptimeData { + + private long _time; + private double _uptime; + private double[] _loadavg; + + public UptimeData() {} + + public void populate(Sigar sigar) throws SigarException { + _time = System.currentTimeMillis(); + _uptime = sigar.getUptime().getUptime(); + try { + _loadavg = sigar.getLoadAverage(); + } catch (SigarNotImplementedException e) { + _loadavg = null; + } + } + + public static UptimeData gather(Sigar sigar) throws SigarException { + UptimeData data = new UptimeData(); + data.populate(sigar); + return data; + } + + public long getTime() { + return _time; + } + + public double getUptime() { + return _uptime; + } + + public double[] getLoadavg() { + return _loadavg; + } +} |