/** * This gathers more information about a host than is available by querying the host list table * directly. * * @param host The host to populate with more information * @param items The data items to enrich the host with * @return The host object with more information attached. */ private Host fullyDescribeHost(Host host, Collection<MetricValue> items) { for (MetricValue item : items) { if (item.getKey().equals(MEMORY_TOTAL_KPI_NAME)) { // Convert to Mb // Original value given in bytes. 1024 * 1024 = 1048576 host.setRamMb((int) (item.getValue() / 1048576)); } if (item.getKey().equals(DISK_TOTAL_KPI_NAME)) { // Convert to Mb // Original value given in bytes. 1024 * 1024 * 1024 = 1073741824 host.setDiskGb((item.getValue() / 1073741824)); } } return host; }
@Override public double getCpuUtilisation(Host host, int durationSeconds) { long currentTime = TimeUnit.MILLISECONDS.toSeconds(new GregorianCalendar().getTimeInMillis()); long timeInPast = currentTime - durationSeconds; List<Double> spotCpuData = getHistoryDataItems(CPU_SPOT_USAGE_KPI_NAME, host.getId(), timeInPast, currentTime); if (spotCpuData != null && !spotCpuData.isEmpty()) { double usage = removeNaN(sumArray(spotCpuData) / ((double) spotCpuData.size())); return usage / 100; } List<Double> idleData = getHistoryDataItems(CPU_IDLE_KPI_NAME, host.getId(), timeInPast, currentTime); double idle = removeNaN(sumArray(idleData) / ((double) idleData.size())); return 1 - ((idle) / 100); }
@Override public HostMeasurement getHostData(Host host) { HostMeasurement answer = new HostMeasurement(host); long clock = 0; connection = getConnection(connection); if (connection == null) { return null; } for (String historyTable : HISTORY_TABLES) { String query = QUERY_DATA_BY_ID.replace("XXXX", historyTable); try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { preparedStatement.setInt(1, host.getId()); try (ResultSet resultSet = preparedStatement.executeQuery()) { ArrayList<ArrayList<Object>> results = resultSetToArray(resultSet); for (ArrayList<Object> dataItem : results) { if ((int) dataItem.get(1) > clock) { clock = (int) dataItem.get(1); answer.setClock(clock); } // itemid | clock | name | key_ | value MetricValue value = new MetricValue( (String) dataItem.get(2), (String) dataItem.get(3), dataItem.get(4) + "", (Integer) dataItem.get(1)); answer.addMetric(value); } } } catch (SQLException ex) { DB_LOGGER.log(Level.SEVERE, null, ex); } } return answer; }
@Override public double getHighestHostPowerUsage(Host host) { long currentTime = TimeUnit.MILLISECONDS.toSeconds(new GregorianCalendar().getTimeInMillis()); long timeInPast = currentTime - TimeUnit.MINUTES.toSeconds(10); // NOTE: The semantics do not match the other Zabbix Datasource adaptor List<Double> energyData = getHistoryDataItems(POWER_KPI_NAME, host.getId(), timeInPast, currentTime); double highestValue = Double.MIN_VALUE; for (Double current : energyData) { if (current > highestValue) { highestValue = current; } } return highestValue; }