@Override public void getValues(MeasurementReport report, Set<MeasurementScheduleRequest> requests) throws Exception { Set<MeasurementScheduleRequest> metrics = new HashSet<MeasurementScheduleRequest>(requests.size()); for (MeasurementScheduleRequest request : requests) { if (request.getName().equals("connectionAvailable")) { report.addData(getConnectionAvailable(request)); } else if (request.getName().equals(MAX_POOL_SIZE_ATTRIBUTE)) { getRCAsMetric(report, request); } else if (request.getName().equals(MIN_POOL_SIZE_ATTRIBUTE)) { getRCAsMetric(report, request); } else { metrics.add(request); } } /* * Remainder here are metrics that can be read from the resource. * Those */ ReadResource op = new ReadResource(address); op.includeRuntime(true); op.recursive(true); ComplexResult res = getASConnection().executeComplex(op); if (!res.isSuccess()) return; Map<String, Object> results = new HashMap<String, Object>(); @SuppressWarnings("unchecked") Map<String, Map<String, Object>> statistics = (Map<String, Map<String, Object>>) res.getResult().get("statistics"); if (statistics != null) { results.putAll(statistics.get("pool")); results.putAll(statistics.get("jdbc")); for (MeasurementScheduleRequest metric : metrics) { String name = metric.getName(); Object o = results.get(name); if (o != null) { Double val; if (o instanceof Integer) { val = ((Integer) o).doubleValue(); } else if (o instanceof Double) { val = (Double) o; } else { String tmp = (String) o; val = Double.valueOf(tmp); } MeasurementDataNumeric data = new MeasurementDataNumeric(metric, val); report.addData(data); } } } }
private void getRCAsMetric(MeasurementReport report, MeasurementScheduleRequest request) { ReadMetricResult result = getMetricValue(report, request, EXPRESSION_METRICS); if (result.equals(ReadMetricResult.Null)) { // server Double val = Double.valueOf(-1); if (request.getName().equals(MAX_POOL_SIZE_ATTRIBUTE)) val = Double.valueOf(20); // The default value else if (request.getName().equals(MIN_POOL_SIZE_ATTRIBUTE)) val = Double.valueOf(0); // The default value MeasurementDataNumeric data = new MeasurementDataNumeric(request, val); report.addData(data); } }
@Override protected void getValues( ManagedComponent managedComponent, MeasurementReport report, Set<MeasurementScheduleRequest> metrics) throws Exception { Set<MeasurementScheduleRequest> uncollectedMetrics = new HashSet<MeasurementScheduleRequest>(); for (MeasurementScheduleRequest request : metrics) { String metricName = request.getName(); if (metricName.equals("custom.connectionAvailable")) { try { Configuration parameters = new Configuration(); OperationResult result = invokeOperation(managedComponent, "testConnection", parameters); PropertySimple resultProp = result.getComplexResults().getSimple("result"); boolean connectionAvailable = resultProp.getBooleanValue(); MeasurementDataTrait trait = new MeasurementDataTrait(request, connectionAvailable ? "yes" : "no"); report.addData(trait); } catch (Exception e) { log.error("Failed to collect trait [" + metricName + "].", e); } } else { uncollectedMetrics.add(request); } } super.getValues(managedComponent, report, uncollectedMetrics); }
/** * Gather measurement data * * @see * org.rhq.core.pluginapi.measurement.MeasurementFacet#getValues(org.rhq.core.domain.measurement.MeasurementReport, * java.util.Set) */ @Override public void getValues(MeasurementReport report, Set<MeasurementScheduleRequest> metrics) { boolean trace = log.isTraceEnabled(); if (trace) log.trace("Get values metrics"); for (MeasurementScheduleRequest req : metrics) { if (trace) log.trace("Inspect metric " + req); String metric = req.getName(); try { EmsBean bean = queryComponentBean(metric); if (bean != null) { if (trace) log.trace("Retrieved mbean with name " + bean.getBeanName()); bean.refreshAttributes(); String attName = metric.substring(metric.indexOf(".") + 1); EmsAttribute att = bean.getAttribute(attName); // Attribute values are of various data types if (att != null) { Object o = att.getValue(); Class<?> attrType = att.getTypeClass(); DataType type = req.getDataType(); if (type == DataType.MEASUREMENT) { if (o != null) { MeasurementDataNumeric res = constructNumericMeasure(attrType, o, req); if (res != null) report.addData(res); } else { if (log.isDebugEnabled()) log.debug("Metric (" + req.getName() + ") has null value, do not add to report"); } } else if (type == DataType.TRAIT) { String value = (String) o; if (trace) log.trace("Metric (" + req.getName() + ") is trait with value " + value); MeasurementDataTrait res = new MeasurementDataTrait(req, value); report.addData(res); } } else { if (log.isWarnEnabled()) { log.warn("Attribute " + attName + " not found"); } } } } catch (Exception e) { if (log.isWarnEnabled()) { log.warn("getValues failed for " + metric, e); } } } }
/** * Gather measurement data * * @see * org.rhq.core.pluginapi.measurement.MeasurementFacet#getValues(org.rhq.core.domain.measurement.MeasurementReport, * java.util.Set) */ public void getValues(MeasurementReport report, Set<MeasurementScheduleRequest> metrics) throws Exception { for (MeasurementScheduleRequest req : metrics) { if (req.getName().startsWith(INTERNAL)) processPluginStats(req, report); else { // Metrics from the application server Operation op = new ReadAttribute(address, req.getName()); // TODO batching Result res = connection.execute(op, false); if (!res.isSuccess()) { log.warn( "Getting metric [" + req.getName() + "] at [ " + address + "] failed: " + res.getFailureDescription()); continue; } String val = (String) res.getResult(); if (val == null) // One of the AS7 ways of telling "This is not implemented" See also AS7-1454 continue; if (req.getDataType() == DataType.MEASUREMENT) { if (!val.equals("no metrics available")) { // AS 7 returns this try { Double d = Double.parseDouble(val); MeasurementDataNumeric data = new MeasurementDataNumeric(req, d); report.addData(data); } catch (NumberFormatException e) { log.warn("Non numeric input for [" + req.getName() + "] : [" + val + "]"); } } } else if (req.getDataType() == DataType.TRAIT) { MeasurementDataTrait data = new MeasurementDataTrait(req, val); report.addData(data); } } } }
/** * Return internal statistics data * * @param req Schedule for the requested data * @param report report to add th data to. */ private void processPluginStats(MeasurementScheduleRequest req, MeasurementReport report) { String name = req.getName(); if (!name.startsWith(INTERNAL)) return; name = name.substring(INTERNAL_SIZE); PluginStats stats = PluginStats.getInstance(); MeasurementDataNumeric data; Double val; if (name.equals("mgmtRequests")) { val = (double) stats.getRequestCount(); } else if (name.equals("requestTime")) { val = (double) stats.getRequestTime(); } else if (name.equals("maxTime")) { val = (double) stats.getMaxTime(); } else val = Double.NaN; data = new MeasurementDataNumeric(req, val); report.addData(data); }
public void getValues(MeasurementReport report, Set<MeasurementScheduleRequest> metrics) { if (cpuInformation != null) { cpuInformation.refresh(); Cpu cpu = null; CpuEntry currentCpu = null; CpuInfo cpuInfo = null; // this is probably gonna be used for traits only for (MeasurementScheduleRequest request : metrics) { String property = request.getName(); if (property.startsWith("Cpu.")) { // Grab the current cpu info from SIGAR, only once for this cpu for all processed // schedules if (cpu == null) { cpu = cpuInformation.getCpu(); } property = property.substring(property.indexOf(".") + 1); Long longValue = (Long) ObjectUtil.lookupAttributeProperty(cpu, property); // A value of -1 indicates SIGAR does not support the metric on the Agent platform type. if (longValue != null && longValue != -1) { report.addData(new MeasurementDataNumeric(request, longValue.doubleValue())); } } else if (property.startsWith("CpuPerc.")) { /* * ! we no longer use the SIGAR CpuPerc object to report cpu percentage metrics. See * ! RHQ-245 for an explanation. We now calculate our own percentages using * ! the current raw cpu info from Sigar and the previous cpu numbers, cached per metric. * ! This allows us to avoid the problem in RHQ-245 while handling perCpu-perMetric schedule * ! granularity. */ // Grab the current cpu info from SIGAR, only once for this cpu for all processed // schedules if (null == cpu) { cpu = cpuInformation.getCpu(); } // Create a Cpu cacheEntry only once for this cpu for all processed schedules if (null == currentCpu) { currentCpu = new CpuEntry(cpu); } // Get the previous cpu numbers to be used for this metric and update the cache for the // next go. CpuEntry previousCpu = cpuCache.put(property, currentCpu); previousCpu = (null == previousCpu) ? startCpuEntry : previousCpu; // if for some reason the delta time is excessive then toss // the metric, since it depends on a reasonable interval between // prev and curr. This can happen due to avail down or a newly // activated metric. Allow up to twice the metric interval. Number num = null; long deltaTime = currentCpu.getTimestamp() - previousCpu.getTimestamp(); if (deltaTime <= (2 * request.getInterval())) { // Use the same calculation that SIGAR uses to generate the percentages. The difference // is that // we use a safe "previous" cpu record. num = getPercentage(previousCpu.getCpu(), cpu, property); } // Uncomment to see details about the calculations. // System.out.println("\nCPU-" + cpuInformation.getCpuIndex() + " Interval=" // + ((currentCpu.getTimestamp() - previousCpu.getTimestamp()) / 1000) + " " + property // + "=" // + num + "\n Prev=" + previousCpu + "\n Curr=" + currentCpu); if (num != null) { report.addData(new MeasurementDataNumeric(request, num.doubleValue())); } } else if (property.startsWith("CpuInfo.")) { if (cpuInfo == null) { cpuInfo = cpuInformation.getCpuInfo(); } property = property.substring(property.indexOf(".") + 1); Number num = ((Number) ObjectUtil.lookupAttributeProperty(cpuInfo, property)); if (num != null) { report.addData(new MeasurementDataNumeric(request, num.doubleValue())); } } else if (property.startsWith("CpuTrait.")) { if (cpuInfo == null) { cpuInfo = cpuInformation.getCpuInfo(); } property = property.substring(property.indexOf(".") + 1); Object o = ObjectUtil.lookupAttributeProperty(cpuInfo, property); if (o != null) { String res; if ("model".equals(property) || "vendor".equals(property)) { res = (String) o; } else { res = String.valueOf(o); } report.addData(new MeasurementDataTrait(request, res)); } } } } return; }
@Override public void getValues(MeasurementReport report, Set<MeasurementScheduleRequest> metrics) throws Exception { // we'll handling the rest of the metrics using the super method, but we may leave out some of // the requests // if we handle them here. Right now, just use the obtained set. We only create a copy of the // (unmodifiable) set // of requests if necessary. Set<MeasurementScheduleRequest> metricsToPassDown = metrics; for (MeasurementScheduleRequest request : metrics) { if (request.getDataType() == DataType.CALLTIME) { ensureGlobalEJB3StatisticsEnabled(); // make a copy to pass down to super class if necessary if (metricsToPassDown == metrics) { metricsToPassDown = new HashSet<MeasurementScheduleRequest>(metrics); } metricsToPassDown.remove(request); // handle this ourselves // the name of the metric is actually the name of the stat collected for each method. we // then provide // the calltime data for each method. Result result = getASConnection().execute(new ReadAttribute(address, METHODS_ATTRIBUTE)); Object value = result.getResult(); if (value instanceof Map) { @SuppressWarnings("unchecked") Map<String, Map<String, Number>> allMethodStats = (Map<String, Map<String, Number>>) value; if (allMethodStats.isEmpty()) { continue; } // first we need to know since when the values were collected result = getASConnection() .execute(new ReadAttribute(RUNTIME_MBEAN_ADDRESS, START_TIME_ATTRIBUTE)); long serverStartTime = (Long) result.getResult(); // now process the calltime value String requestedMetric = request.getName().substring(CALLTIME_METRIC_NAME_PREFIX_LENGTH); Stats lastCollection = getLastCallTimeCollection(requestedMetric, allMethodStats, serverStartTime); Stats thisCollection = Stats.fromMap( allMethodStats, requestedMetric, System.currentTimeMillis(), serverStartTime); CallTimeData callTime = new CallTimeData(request); fillCallTimeData(callTime, thisCollection, lastCollection); saveCallTimeCollection(requestedMetric, thisCollection); report.addData(callTime); } else { OSGiVersion currentAsVersion = getASVersion(); if (currentAsVersion == null) { getLog() .warn( "Could not determine the AS version while reporting unexpected result of method" + " stats. Request: " + request); } else if (FIRST_VERSION_SUPPORTING_METHOD_STATS.compareTo(currentAsVersion) <= 0) { getLog() .error( "Unexpected type of results when querying method stats for measurement request " + request + ". Expected map but got " + (value == null ? "null" : value.getClass().getName())); } } } } super.getValues(report, metricsToPassDown); }