001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.monitoring.reporting.web.plugin.jvm;
018
019 import org.apache.commons.monitoring.reporting.web.handler.api.Regex;
020 import org.apache.commons.monitoring.reporting.web.handler.api.Template;
021 import org.apache.commons.monitoring.reporting.web.plugin.json.Jsons;
022 import org.apache.commons.monitoring.reporting.web.plugin.jvm.gauges.CPUGauge;
023 import org.apache.commons.monitoring.reporting.web.plugin.jvm.gauges.UsedMemoryGauge;
024 import org.apache.commons.monitoring.reporting.web.template.MapBuilder;
025 import org.apache.commons.monitoring.repositories.Repository;
026
027 import java.lang.management.ManagementFactory;
028 import java.lang.management.MemoryMXBean;
029 import java.lang.management.OperatingSystemMXBean;
030 import java.util.Iterator;
031 import java.util.Map;
032
033 public class JVMEndpoints {
034 @Regex
035 public Template home() {
036 final OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
037 final MemoryMXBean memory = ManagementFactory.getMemoryMXBean();
038
039 return new Template("jvm/jvm.vm", new MapBuilder<String, Object>()
040 .set("architecture", os.getArch())
041 .set("name", os.getName())
042 .set("version", os.getVersion())
043 .set("numberProcessor", os.getAvailableProcessors())
044 .set("maxMemory", memory.getHeapMemoryUsage().getMax())
045 .set("initMemory", memory.getHeapMemoryUsage().getInit())
046 .build());
047 }
048
049 @Regex("/cpu/([0-9]*)/([0-9]*)")
050 public String cpu(final long start, final long end) {
051 return "{ \"data\": " + Jsons.toJson(Repository.INSTANCE.getGaugeValues(start, end, CPUGauge.CPU)) + ", \"label\": \"CPU Usage\", \"color\": \"#317eac\" }";
052 }
053
054 @Regex("/memory/([0-9]*)/([0-9]*)")
055 public String memory(final long start, final long end) {
056 return "{ \"data\": " + Jsons.toJson(Repository.INSTANCE.getGaugeValues(start, end, UsedMemoryGauge.USED_MEMORY)) + ", \"label\": \"Used Memory\", \"color\": \"#317eac\" }";
057 }
058 }