You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ranger.apache.org by me...@apache.org on 2019/10/11 05:17:42 UTC

[ranger] branch master updated: RANGER-2589 : Introduce Ranger API to return Ranger's JVM resource status metric

This is an automated email from the ASF dual-hosted git repository.

mehul pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ranger.git


The following commit(s) were added to refs/heads/master by this push:
     new 3f468ba  RANGER-2589 : Introduce Ranger API to return Ranger's JVM resource status metric
3f468ba is described below

commit 3f468bab324b78de86e1efa38bb823c685c2b077
Author: fatimaawez <fa...@gmail.com>
AuthorDate: Thu Oct 10 13:51:04 2019 +0530

    RANGER-2589 : Introduce Ranger API to return Ranger's JVM resource status metric
    
    Signed-off-by: Mehul Parikh <me...@apache.org>
---
 .../apache/ranger/plugin/model/RangerMetrics.java  |  56 +++++++++
 .../java/org/apache/ranger/rest/MetricsREST.java   |  80 ++++++++++++
 .../org/apache/ranger/util/RangerMetricsUtil.java  | 135 +++++++++++++++++++++
 .../conf.dist/security-applicationContext.xml      |   2 +-
 4 files changed, 272 insertions(+), 1 deletion(-)

diff --git a/agents-common/src/main/java/org/apache/ranger/plugin/model/RangerMetrics.java b/agents-common/src/main/java/org/apache/ranger/plugin/model/RangerMetrics.java
new file mode 100644
index 0000000..2ee756f
--- /dev/null
+++ b/agents-common/src/main/java/org/apache/ranger/plugin/model/RangerMetrics.java
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.ranger.plugin.model;
+
+import java.util.Map;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.codehaus.jackson.annotate.JsonAutoDetect;
+import org.codehaus.jackson.annotate.JsonIgnoreProperties;
+import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
+import org.codehaus.jackson.map.annotate.JsonSerialize;
+
+@JsonAutoDetect(fieldVisibility=Visibility.ANY)
+@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
+@JsonIgnoreProperties(ignoreUnknown=true)
+@XmlRootElement
+@XmlAccessorType(XmlAccessType.FIELD)
+public class RangerMetrics {
+
+    private Map<String, Object> data;
+
+    public RangerMetrics() {
+        setData(null);
+    }
+    public RangerMetrics(Map<String, Object> data) {
+        setData(data);
+    }
+
+    public Map<String, Object> getData() {
+        return data;
+    }
+
+    public void setData(Map<String, Object> data) {
+        this.data = data;
+    }
+}
diff --git a/security-admin/src/main/java/org/apache/ranger/rest/MetricsREST.java b/security-admin/src/main/java/org/apache/ranger/rest/MetricsREST.java
new file mode 100644
index 0000000..9944bad
--- /dev/null
+++ b/security-admin/src/main/java/org/apache/ranger/rest/MetricsREST.java
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.ranger.rest;
+
+import java.lang.management.ManagementFactory;
+import java.lang.management.RuntimeMXBean;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+
+import org.apache.log4j.Logger;
+import org.apache.ranger.plugin.model.RangerMetrics;
+import org.apache.ranger.util.RangerMetricsUtil;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Scope;
+import org.springframework.stereotype.Component;
+import org.springframework.transaction.annotation.Propagation;
+import org.springframework.transaction.annotation.Transactional;
+
+@Path("metrics")
+@Component
+@Scope("request")
+@Transactional(propagation = Propagation.REQUIRES_NEW)
+public class MetricsREST {
+    private static final Logger LOG = Logger.getLogger(MetricsREST.class);
+    private static final RuntimeMXBean RUNTIME = ManagementFactory.getRuntimeMXBean();
+    private static final String JVM_MACHINE_ACTUAL_NAME = RUNTIME.getVmName();
+    private static final String VERSION = RUNTIME.getVmVersion();
+    private static final String JVM_MACHINE_REPRESENTATION_NAME = RUNTIME.getName();
+    private static final long UP_TIME_OF_JVM = RUNTIME.getUptime();
+    private static final String JVM_VENDOR_NAME =  RUNTIME.getVmVendor();
+
+    @Autowired
+    RangerMetricsUtil jvmMetricUtil;
+
+    @GET
+    @Path("/status")
+    @Produces({ "application/json", "application/xml" })
+    public RangerMetrics getStatus() {
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("==> MetricsREST.getStatus()");
+        }
+
+        Map<String, Object> jvm = new LinkedHashMap<>();
+        Map<String, Object> vmDetails = new LinkedHashMap<>();
+        vmDetails.put("JVM Machine Actual Name", JVM_MACHINE_ACTUAL_NAME);
+        vmDetails.put("version", VERSION);
+        vmDetails.put("JVM Machine Representation Name", JVM_MACHINE_REPRESENTATION_NAME);
+        vmDetails.put("Up time of JVM", UP_TIME_OF_JVM);
+        vmDetails.put("JVM Vendor Name", JVM_VENDOR_NAME);
+        vmDetails.putAll(jvmMetricUtil.getValues());
+        jvm.put("jvm",vmDetails);
+
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("<== MetricsREST.getStatus() " + jvm);
+        }
+
+        return new RangerMetrics(jvm);
+    }
+}
diff --git a/security-admin/src/main/java/org/apache/ranger/util/RangerMetricsUtil.java b/security-admin/src/main/java/org/apache/ranger/util/RangerMetricsUtil.java
new file mode 100644
index 0000000..7f8ac9b
--- /dev/null
+++ b/security-admin/src/main/java/org/apache/ranger/util/RangerMetricsUtil.java
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.ranger.util;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.log4j.Logger;
+
+import org.springframework.stereotype.Component;
+
+import java.lang.management.ManagementFactory;
+import java.lang.management.MemoryMXBean;
+import java.lang.management.OperatingSystemMXBean;
+import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.lang.management.MemoryPoolMXBean;
+import java.lang.management.MemoryType;
+import java.lang.management.MemoryUsage;
+
+/**
+ * Connect Worker system and runtime information.
+ */
+@Component
+public class RangerMetricsUtil {
+
+    private static final Logger LOG = Logger.getLogger(RangerMetricsUtil.class);
+    private static final OperatingSystemMXBean OS;
+    private static final MemoryMXBean MEM_BEAN;
+    public static final String NL = System.getProperty("line.separator");
+
+    static {
+        OS = ManagementFactory.getOperatingSystemMXBean();
+        MEM_BEAN = ManagementFactory.getMemoryMXBean();
+    }
+
+    public Map<String, Object> getValues() {
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("==> RangerJVMMetricUtil.getValues()");
+        }
+        
+        Map<String, Object> values = new LinkedHashMap<>();
+        values.put("os.spec", StringUtils.join(Arrays.asList(addSystemInfo()), ", "));
+        values.put("os.vcpus", String.valueOf(OS.getAvailableProcessors()));
+        values.put("memory", addMemoryDetails());
+
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("<== RangerJVMMetricUtil.getValues()" + values);
+        }
+
+        return values;
+    }
+
+    /**
+     * collect the pool division of java
+     */
+    protected Map<String, Object> getPoolDivision() {
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("==> RangerJVMMetricUtil.getPoolDivision()");
+        }
+
+        Map<String, Object> poolDivisionValues = new LinkedHashMap<>();
+        for (MemoryPoolMXBean mpBean : ManagementFactory.getMemoryPoolMXBeans()) {
+            if (mpBean.getType() == MemoryType.HEAP) {
+                poolDivisionValues.put(mpBean.getName(), mpBean.getUsage());
+            }
+        }
+
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("<== RangerJVMMetricUtil.getPoolDivision()" + poolDivisionValues);
+        }
+
+        return poolDivisionValues;
+    }
+
+    /**
+     * Add memory details
+     */
+    protected Map<String, Object> addMemoryDetails() {
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("==> RangerJVMMetricUtil.addMemoryDetails()");
+        }
+
+        Map<String, Object> memory  = new LinkedHashMap<>();
+        MemoryUsage memHeapUsage = MEM_BEAN.getHeapMemoryUsage();
+        MemoryUsage nonHeapUsage = MEM_BEAN.getNonHeapMemoryUsage();
+        memory.put("heapInit", String.valueOf(memHeapUsage.getInit()));
+        memory.put("heapMax", String.valueOf(memHeapUsage.getMax()));
+        memory.put("heapCommitted", String.valueOf(memHeapUsage.getCommitted()));
+        memory.put("heapUsed", String.valueOf(memHeapUsage.getUsed()));
+        memory.put("nonHeapInit", String.valueOf(nonHeapUsage.getInit()));
+        memory.put("nonHeapMax", String.valueOf(nonHeapUsage.getMax()));
+        memory.put("nonHeapCommitted", String.valueOf(nonHeapUsage.getCommitted()));
+        memory.put("nonHeapUsed", String.valueOf(nonHeapUsage.getUsed()));
+        memory.put("memory_pool_usages", getPoolDivision());
+
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("<== RangerJVMMetricUtil.addMemoryDetails()" + memory);
+        }
+
+        return memory;
+    }
+
+    /**
+     * Collect system information.
+     */
+    protected String[] addSystemInfo() {
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("==> RangerJVMMetricUtil.addSystemInfo()");
+        }
+
+        String[] osInfo = { OS.getName(), OS.getArch(), OS.getVersion() };
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("<== RangerJVMMetricUtil.addSystemInfo()" + osInfo);
+        }
+
+        return osInfo;
+    }
+}
diff --git a/security-admin/src/main/resources/conf.dist/security-applicationContext.xml b/security-admin/src/main/resources/conf.dist/security-applicationContext.xml
index 672d4a6..24be67d 100644
--- a/security-admin/src/main/resources/conf.dist/security-applicationContext.xml
+++ b/security-admin/src/main/resources/conf.dist/security-applicationContext.xml
@@ -44,7 +44,7 @@ http://www.springframework.org/schema/security/spring-security-oauth2-2.0.xsd">
 	<security:http pattern="/service/plugins/services/grant/*" security="none"/>
 	<security:http pattern="/service/plugins/services/revoke/*" security="none"/>
 	<security:http pattern="/service/tags/download/*" security="none"/>
-
+	<security:http pattern="/service/metrics/status" security="none" />
 	<security:http disable-url-rewriting="true" use-expressions="true" create-session="always" entry-point-ref="authenticationProcessingFilterEntryPoint">
 		<csrf disabled="true"/>
 		<security:headers>