@Test public void testGetCpuUtilisation() { System.out.println("getCpuUtilisation"); Host host = CHOSEN_HOST; int duration = (int) TimeUnit.MINUTES.toSeconds(2); ZabbixDataSourceAdaptor instance = new ZabbixDataSourceAdaptor(); double result = instance.getCpuUtilisation(host, duration); assert(result >= 0); assert(result <= 1.0); System.out.println("CPU Utilisation Last " + duration + " minutes: " + result); }
@Override public double getCpuUtilisation(eu.ascetic.asceticarchitecture.iaas.energymodeller.types.energyuser.Host host, int lastNSeconds) { long currentTime = new GregorianCalendar().getTimeInMillis(); long timeInPast = currentTime - TimeUnit.SECONDS.toMillis(lastNSeconds); List<HistoryItem> spotCpuData = client.getHistoryDataFromItem(CPU_SPOT_USAGE_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); if (spotCpuData != null && !spotCpuData.isEmpty()) { double answer = removeNaN(sumArray(spotCpuData) / ((double) spotCpuData.size())); return answer / 100; } else { List<HistoryItem> interruptData = client.getHistoryDataFromItem(CPU_INTERUPT_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> iowaitData = client.getHistoryDataFromItem(CPU_IO_WAIT_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> niceData = client.getHistoryDataFromItem(CPU_NICE_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> softirqData = client.getHistoryDataFromItem(CPU_SOFT_IRQ_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> stealData = client.getHistoryDataFromItem(CPU_STEAL_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> systemData = client.getHistoryDataFromItem(CPU_SYSTEM_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> userData = client.getHistoryDataFromItem(CPU_USER_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); double interrupt = removeNaN(sumArray(interruptData) / ((double) interruptData.size())); double iowait = removeNaN(sumArray(iowaitData) / ((double) iowaitData.size())); double nice = removeNaN(sumArray(niceData) / ((double) niceData.size())); double softirq = removeNaN(sumArray(softirqData) / ((double) softirqData.size())); double steal = removeNaN(sumArray(stealData) / ((double) stealData.size())); double system = removeNaN(sumArray(systemData) / ((double) systemData.size())); double user = removeNaN(sumArray(userData) / ((double) userData.size())); return (system + user + interrupt + iowait + nice + softirq + steal) / 100; } }
ZabbixDataSourceAdaptor implements HostDataSource { @Override public double getCpuUtilisation(eu.ascetic.asceticarchitecture.iaas.energymodeller.types.energyuser.Host host, int lastNSeconds) { long currentTime = new GregorianCalendar().getTimeInMillis(); long timeInPast = currentTime - TimeUnit.SECONDS.toMillis(lastNSeconds); List<HistoryItem> spotCpuData = client.getHistoryDataFromItem(CPU_SPOT_USAGE_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); if (spotCpuData != null && !spotCpuData.isEmpty()) { double answer = removeNaN(sumArray(spotCpuData) / ((double) spotCpuData.size())); return answer / 100; } else { List<HistoryItem> interruptData = client.getHistoryDataFromItem(CPU_INTERUPT_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> iowaitData = client.getHistoryDataFromItem(CPU_IO_WAIT_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> niceData = client.getHistoryDataFromItem(CPU_NICE_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> softirqData = client.getHistoryDataFromItem(CPU_SOFT_IRQ_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> stealData = client.getHistoryDataFromItem(CPU_STEAL_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> systemData = client.getHistoryDataFromItem(CPU_SYSTEM_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> userData = client.getHistoryDataFromItem(CPU_USER_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); double interrupt = removeNaN(sumArray(interruptData) / ((double) interruptData.size())); double iowait = removeNaN(sumArray(iowaitData) / ((double) iowaitData.size())); double nice = removeNaN(sumArray(niceData) / ((double) niceData.size())); double softirq = removeNaN(sumArray(softirqData) / ((double) softirqData.size())); double steal = removeNaN(sumArray(stealData) / ((double) stealData.size())); double system = removeNaN(sumArray(systemData) / ((double) systemData.size())); double user = removeNaN(sumArray(userData) / ((double) userData.size())); return (system + user + interrupt + iowait + nice + softirq + steal) / 100; } } }
ZabbixDataSourceAdaptor implements HostDataSource { @Override public double getCpuUtilisation(eu.ascetic.asceticarchitecture.iaas.energymodeller.types.energyuser.Host host, int lastNSeconds) { long currentTime = new GregorianCalendar().getTimeInMillis(); long timeInPast = currentTime - TimeUnit.SECONDS.toMillis(lastNSeconds); List<HistoryItem> spotCpuData = client.getHistoryDataFromItem(CPU_SPOT_USAGE_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); if (spotCpuData != null && !spotCpuData.isEmpty()) { double answer = removeNaN(sumArray(spotCpuData) / ((double) spotCpuData.size())); return answer / 100; } else { List<HistoryItem> interruptData = client.getHistoryDataFromItem(CPU_INTERUPT_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> iowaitData = client.getHistoryDataFromItem(CPU_IO_WAIT_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> niceData = client.getHistoryDataFromItem(CPU_NICE_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> softirqData = client.getHistoryDataFromItem(CPU_SOFT_IRQ_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> stealData = client.getHistoryDataFromItem(CPU_STEAL_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> systemData = client.getHistoryDataFromItem(CPU_SYSTEM_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> userData = client.getHistoryDataFromItem(CPU_USER_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); double interrupt = removeNaN(sumArray(interruptData) / ((double) interruptData.size())); double iowait = removeNaN(sumArray(iowaitData) / ((double) iowaitData.size())); double nice = removeNaN(sumArray(niceData) / ((double) niceData.size())); double softirq = removeNaN(sumArray(softirqData) / ((double) softirqData.size())); double steal = removeNaN(sumArray(stealData) / ((double) stealData.size())); double system = removeNaN(sumArray(systemData) / ((double) systemData.size())); double user = removeNaN(sumArray(userData) / ((double) userData.size())); return (system + user + interrupt + iowait + nice + softirq + steal) / 100; } }  }
ZabbixDataSourceAdaptor implements HostDataSource { @Override public double getCpuUtilisation(eu.ascetic.asceticarchitecture.iaas.energymodeller.types.energyuser.Host host, int lastNSeconds) { long currentTime = new GregorianCalendar().getTimeInMillis(); long timeInPast = currentTime - TimeUnit.SECONDS.toMillis(lastNSeconds); List<HistoryItem> spotCpuData = client.getHistoryDataFromItem(CPU_SPOT_USAGE_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); if (spotCpuData != null && !spotCpuData.isEmpty()) { double answer = removeNaN(sumArray(spotCpuData) / ((double) spotCpuData.size())); return answer / 100; } else { List<HistoryItem> interruptData = client.getHistoryDataFromItem(CPU_INTERUPT_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> iowaitData = client.getHistoryDataFromItem(CPU_IO_WAIT_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> niceData = client.getHistoryDataFromItem(CPU_NICE_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> softirqData = client.getHistoryDataFromItem(CPU_SOFT_IRQ_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> stealData = client.getHistoryDataFromItem(CPU_STEAL_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> systemData = client.getHistoryDataFromItem(CPU_SYSTEM_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> userData = client.getHistoryDataFromItem(CPU_USER_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); double interrupt = removeNaN(sumArray(interruptData) / ((double) interruptData.size())); double iowait = removeNaN(sumArray(iowaitData) / ((double) iowaitData.size())); double nice = removeNaN(sumArray(niceData) / ((double) niceData.size())); double softirq = removeNaN(sumArray(softirqData) / ((double) softirqData.size())); double steal = removeNaN(sumArray(stealData) / ((double) stealData.size())); double system = removeNaN(sumArray(systemData) / ((double) systemData.size())); double user = removeNaN(sumArray(userData) / ((double) userData.size())); return (system + user + interrupt + iowait + nice + softirq + steal) / 100; } }  static void main(String[] args); @Override eu.ascetic.asceticarchitecture.iaas.energymodeller.types.energyuser.Host getHostByName(String hostname); @Override GeneralPurposePowerConsumer getGeneralPowerConsumerByName(String hostname); @Override VmDeployed getVmByName(String name); @Override List<eu.ascetic.asceticarchitecture.iaas.energymodeller.types.energyuser.Host> getHostList(); List<eu.ascetic.asceticarchitecture.iaas.energymodeller.types.energyuser.Host> getHostList(String groupName); @Override List<GeneralPurposePowerConsumer> getGeneralPowerConsumerList(); @Override List<VmDeployed> getVmList(); List<VmDeployed> getVmList(String groupName); @Override List<EnergyUsageSource> getHostAndVmList(); @Override List<HostMeasurement> getHostData(); @Override HostMeasurement getHostData(eu.ascetic.asceticarchitecture.iaas.energymodeller.types.energyuser.Host host); @Override List<HostMeasurement> getHostData(List<eu.ascetic.asceticarchitecture.iaas.energymodeller.types.energyuser.Host> hostList); @Override List<VmMeasurement> getVmData(); @Override VmMeasurement getVmData(VmDeployed vm); @Override List<VmMeasurement> getVmData(List<VmDeployed> vmList); ZabbixClient getClient(); void setClient(ZabbixClient client); @Override CurrentUsageRecord getCurrentEnergyUsage(eu.ascetic.asceticarchitecture.iaas.energymodeller.types.energyuser.Host host); @Override double getLowestHostPowerUsage(eu.ascetic.asceticarchitecture.iaas.energymodeller.types.energyuser.Host host); @Override double getHighestHostPowerUsage(eu.ascetic.asceticarchitecture.iaas.energymodeller.types.energyuser.Host host); @Override double getCpuUtilisation(eu.ascetic.asceticarchitecture.iaas.energymodeller.types.energyuser.Host host, int lastNSeconds); ZabbixHostVMFilter getHostFilter(); void setHostFilter(ZabbixHostVMFilter hostFilter); }
ZabbixDataSourceAdaptor implements HostDataSource { @Override public double getCpuUtilisation(eu.ascetic.asceticarchitecture.iaas.energymodeller.types.energyuser.Host host, int lastNSeconds) { long currentTime = new GregorianCalendar().getTimeInMillis(); long timeInPast = currentTime - TimeUnit.SECONDS.toMillis(lastNSeconds); List<HistoryItem> spotCpuData = client.getHistoryDataFromItem(CPU_SPOT_USAGE_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); if (spotCpuData != null && !spotCpuData.isEmpty()) { double answer = removeNaN(sumArray(spotCpuData) / ((double) spotCpuData.size())); return answer / 100; } else { List<HistoryItem> interruptData = client.getHistoryDataFromItem(CPU_INTERUPT_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> iowaitData = client.getHistoryDataFromItem(CPU_IO_WAIT_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> niceData = client.getHistoryDataFromItem(CPU_NICE_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> softirqData = client.getHistoryDataFromItem(CPU_SOFT_IRQ_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> stealData = client.getHistoryDataFromItem(CPU_STEAL_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> systemData = client.getHistoryDataFromItem(CPU_SYSTEM_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); List<HistoryItem> userData = client.getHistoryDataFromItem(CPU_USER_KPI_NAME, host.getHostName(), Dictionary.HISTORY_ITEM_FORMAT_FLOAT, timeInPast, currentTime); double interrupt = removeNaN(sumArray(interruptData) / ((double) interruptData.size())); double iowait = removeNaN(sumArray(iowaitData) / ((double) iowaitData.size())); double nice = removeNaN(sumArray(niceData) / ((double) niceData.size())); double softirq = removeNaN(sumArray(softirqData) / ((double) softirqData.size())); double steal = removeNaN(sumArray(stealData) / ((double) stealData.size())); double system = removeNaN(sumArray(systemData) / ((double) systemData.size())); double user = removeNaN(sumArray(userData) / ((double) userData.size())); return (system + user + interrupt + iowait + nice + softirq + steal) / 100; } }  static void main(String[] args); @Override eu.ascetic.asceticarchitecture.iaas.energymodeller.types.energyuser.Host getHostByName(String hostname); @Override GeneralPurposePowerConsumer getGeneralPowerConsumerByName(String hostname); @Override VmDeployed getVmByName(String name); @Override List<eu.ascetic.asceticarchitecture.iaas.energymodeller.types.energyuser.Host> getHostList(); List<eu.ascetic.asceticarchitecture.iaas.energymodeller.types.energyuser.Host> getHostList(String groupName); @Override List<GeneralPurposePowerConsumer> getGeneralPowerConsumerList(); @Override List<VmDeployed> getVmList(); List<VmDeployed> getVmList(String groupName); @Override List<EnergyUsageSource> getHostAndVmList(); @Override List<HostMeasurement> getHostData(); @Override HostMeasurement getHostData(eu.ascetic.asceticarchitecture.iaas.energymodeller.types.energyuser.Host host); @Override List<HostMeasurement> getHostData(List<eu.ascetic.asceticarchitecture.iaas.energymodeller.types.energyuser.Host> hostList); @Override List<VmMeasurement> getVmData(); @Override VmMeasurement getVmData(VmDeployed vm); @Override List<VmMeasurement> getVmData(List<VmDeployed> vmList); ZabbixClient getClient(); void setClient(ZabbixClient client); @Override CurrentUsageRecord getCurrentEnergyUsage(eu.ascetic.asceticarchitecture.iaas.energymodeller.types.energyuser.Host host); @Override double getLowestHostPowerUsage(eu.ascetic.asceticarchitecture.iaas.energymodeller.types.energyuser.Host host); @Override double getHighestHostPowerUsage(eu.ascetic.asceticarchitecture.iaas.energymodeller.types.energyuser.Host host); @Override double getCpuUtilisation(eu.ascetic.asceticarchitecture.iaas.energymodeller.types.energyuser.Host host, int lastNSeconds); ZabbixHostVMFilter getHostFilter(); void setHostFilter(ZabbixHostVMFilter hostFilter);  }