Error: Password file read access must be restricted: /etc/cassandra/jmxremote.password

在配置JMX远程访问的时候,设置jmxremote.password文件权限,修改该文件时添加写权限,chmod +w jmxremote.password ,放开角色信息那俩行的注释,保存,再使用chmod 0400 jmxremote.password

这样就是它正确的权限设置

jmxremote.password 在jdk/jre/lib/management/下,jmxremote.password.template复制,去掉.template后缀

转载请注明出处:http://blog.csdn.net/l1028386804/article/details/51547408

2016年5月的最后一天,今天我将和大家分享Java中如何使用JMX来监控Tomcat的各种状态。好了,不多说了,我们直接进入主题

一、激活Tomcat的JMX远程配置

要通过JMX远程监控Tomcat,首先需要激活Tomcat的JMX远程配置。

① 修改脚本

先修改Tomcat的启动脚本,windows下为bin/catalina.bat(linux下为catalina.sh),添加以下内容,8999是jmxremote使用的端口号,第二个false表示不需要鉴权:

[plain] view plain copy
 
  1. set JMX_REMOTE_CONFIG=-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false  
  2. set CATALINA_OPTS=%CATALINA_OPTS% %JMX_REMOTE_CONFIG%  

要注意以上语句的位置不能太后面,可以加在【if "%OS%" == "Windows_NT" setlocal】一句后的大段的注释后面。

参考官方说明:

http://tomcat.apache.org/tomcat-6.0-doc/monitoring.html#Enabling_JMX_Remote

http://tomcat.apache.org/tomcat-7.0-doc/monitoring.html#Enabling_JMX_Remote

http://tomcat.apache.org/tomcat-8.0-doc/monitoring.html#Enabling_JMX_Remote

http://tomcat.apache.org/tomcat-9.0-doc/monitoring.html#Enabling_JMX_Remote

② 鉴权

上面的配置是不需要鉴权的,如果需要鉴权则添加的内容为:

[plain] view plain copy
 
  1. set JMX_REMOTE_CONFIG=-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.password.file=../conf/jmxremote.password -Dcom.sun.management.jmxremote.access.file=../conf/jmxremote.access  
  2. set CATALINA_OPTS=%CATALINA_OPTS% %JMX_REMOTE_CONFIG%  

③ 复制并修改授权文件

$JAVA_HOME/jre/lib/management下有jmxremote.access和jmxremote.password的模板文件,将两个文件复制到$CATALINA_BASE/conf目录下
◆ 修改$CATALINA_BASE/conf/jmxremote.access 添加内容:
     monitorRole readonly
     controlRole readwrite
◆ 修改$CATALINA_BASE/conf/jmxremote.password 添加内容:
     monitorRole chenfeng
     controlRole chenfeng
注意: 如果进行了以上步骤导致Tomcat启动不了,那么很可能是密码文件的权限问题
需要修改jmxremote.password文件的访问权限,只有运行Tomcat的用户才能拥有访问权限 :
      Windows的NTFS文件系统下,选中文件,点右键 -->“属性”-->“安全”--> 点“高级”--> 点“更改权限”--> 去掉“从父项继承....”--> 弹出窗口中选“删除”,这样就删除了所有访问权限。再选“添加”--> “高级”--> “立即查找”,选中你的用户(或用户组,如果选用户不行那就选用户组),例administrator,点“确定",“确定"。来到权限项目窗口,勾选“完全控制”,点“确定”,OK了。
官方的提示:
      The password file should be read-only and only accessible by the operating system user Tomcat is running as.

④验证配置

重新启动Tomcat,在Windows命令行输入“netstat -a”查看配置的端口号是否已打开,如果打开,说明上面的配置成功了。

⑤ 使用jconsole测试JMX

运行$JAVA_HOME/bin目录下的jconsole.exe,打开J2SE监视和管理控制台,然后建立连接,如果是本地的Tomcat则直接选择然后点击连接,如果是远程的,则进入远程选项卡,填写地址、端口号、用户名、口令即可连接。。Mbean属性页中给出了相应的数据,Catalina中是tomcat的,java.lang是jvm的。对于加粗的黑体属性值,需双击一下才可看内容。

二、使用JMX监控Tomcat示例代码

[java] view plain copy
 
  1. String jmxURL = "service:jmx:rmi:///jndi/rmi://192.168.10.93:8999/jmxrmi";  
  2. JMXServiceURL serviceURL = new JMXServiceURL(jmxURL);  
  3.   
  4. Map map = new HashMap();  
  5. // 用户名密码,在jmxremote.password文件中查看  
  6. String[] credentials = new String[] { "monitorRole", "tomcat" };  
  7. map.put("jmx.remote.credentials", credentials);  
  8. JMXConnector connector = JMXConnectorFactory.connect(serviceURL, map);  
  9. MBeanServerConnection mbsc = connector.getMBeanServerConnection();  
  10.   
  11. // 端口最好是动态取得  
  12. ObjectName threadObjName = new ObjectName("Catalina:type=ThreadPool,name=http-8080");  
  13. MBeanInfo mbInfo = mbsc.getMBeanInfo(threadObjName);  
  14.   
  15. // tomcat的线程数对应的属性值  
  16. String attrName = "currentThreadCount";  
  17. MBeanAttributeInfo[] mbAttributes = mbInfo.getAttributes();  
  18. System.out.println("currentThreadCount:" + mbsc.getAttribute(threadObjName, attrName));  

三、完整的示例代码文件

[java] view plain copy
 
    1. import java.lang.management.MemoryUsage;  
    2. import java.text.SimpleDateFormat;  
    3. import java.util.Date;  
    4. import java.util.Formatter;  
    5. import java.util.HashMap;  
    6. import java.util.Iterator;  
    7. import java.util.Map;  
    8. import java.util.Set;  
    9.   
    10. import javax.management.MBeanAttributeInfo;  
    11. import javax.management.MBeanInfo;  
    12. import javax.management.MBeanServerConnection;  
    13. import javax.management.ObjectInstance;  
    14. import javax.management.ObjectName;  
    15. import javax.management.openmbean.CompositeDataSupport;  
    16. import javax.management.remote.JMXConnector;  
    17. import javax.management.remote.JMXConnectorFactory;  
    18. import javax.management.remote.JMXServiceURL;  
    19. /** 
    20.  * @author liuyazhuang 
    21.  * @date 2016-05-31  
    22.  */  
    23. public class JMXTest {  
    24.   
    25.     /** 
    26.      * main方法 
    27.      * @param args 
    28.      */  
    29.     public static void main(String[] args) {  
    30.         try {  
    31.   
    32.             String jmxURL = "service:jmx:rmi:///jndi/rmi://127.0.0.1:8999/jmxrmi";  
    33.   
    34.             JMXServiceURL serviceURL = new JMXServiceURL(jmxURL);  
    35.   
    36.             Map map = new HashMap();  
    37.             String[] credentials = new String[] { "monitorRole", "tomcat" };  
    38.             map.put("jmx.remote.credentials", credentials);  
    39.             JMXConnector connector = JMXConnectorFactory.connect(serviceURL,  
    40.                     map);  
    41.             MBeanServerConnection mbsc = connector.getMBeanServerConnection();  
    42.   
    43.             // 端口最好是动态取得  
    44.             ObjectName threadObjName = new ObjectName(  
    45.                     "Catalina:type=ThreadPool,name=http-8080");  
    46.             MBeanInfo mbInfo = mbsc.getMBeanInfo(threadObjName);  
    47.   
    48.             String attrName = "currentThreadCount";// tomcat的线程数对应的属性值  
    49.             MBeanAttributeInfo[] mbAttributes = mbInfo.getAttributes();  
    50.             System.out.println("currentThreadCount:"  
    51.                     + mbsc.getAttribute(threadObjName, attrName));  
    52.   
    53.             // heap  
    54.             for (int j = 0; j < mbsc.getDomains().length; j++) {  
    55.                 System.out.println("###########" + mbsc.getDomains()[j]);  
    56.             }  
    57.             Set MBeanset = mbsc.queryMBeans(null, null);  
    58.             System.out.println("MBeanset.size() : " + MBeanset.size());  
    59.             Iterator MBeansetIterator = MBeanset.iterator();  
    60.             while (MBeansetIterator.hasNext()) {  
    61.                 ObjectInstance objectInstance = (ObjectInstance) MBeansetIterator  
    62.                         .next();  
    63.                 ObjectName objectName = objectInstance.getObjectName();  
    64.                 String canonicalName = objectName.getCanonicalName();  
    65.                 System.out.println("canonicalName : " + canonicalName);  
    66.                 if (canonicalName  
    67.                         .equals("Catalina:host=localhost,type=Cluster")) {  
    68.                     // Get details of cluster MBeans  
    69.                     System.out.println("Cluster MBeans Details:");  
    70.                     System.out  
    71.                             .println("=========================================");  
    72.                     // getMBeansDetails(canonicalName);  
    73.                     String canonicalKeyPropList = objectName  
    74.                             .getCanonicalKeyPropertyListString();  
    75.                 }  
    76.             }  
    77.             // ------------------------- system ----------------------  
    78.             ObjectName runtimeObjName = new ObjectName("java.lang:type=Runtime");  
    79.             System.out.println("厂商:"  
    80.                     + (String) mbsc.getAttribute(runtimeObjName, "VmVendor"));  
    81.             System.out.println("程序:"  
    82.                     + (String) mbsc.getAttribute(runtimeObjName, "VmName"));  
    83.             System.out.println("版本:"  
    84.                     + (String) mbsc.getAttribute(runtimeObjName, "VmVersion"));  
    85.             Date starttime = new Date((Long) mbsc.getAttribute(runtimeObjName,  
    86.                     "StartTime"));  
    87.             SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    88.             System.out.println("启动时间:" + df.format(starttime));  
    89.   
    90.             Long timespan = (Long) mbsc.getAttribute(runtimeObjName, "Uptime");  
    91.             System.out.println("连续工作时间:" + JMXTest.formatTimeSpan(timespan));  
    92.             // ------------------------ JVM -------------------------  
    93.             // 堆使用率  
    94.             ObjectName heapObjName = new ObjectName("java.lang:type=Memory");  
    95.             MemoryUsage heapMemoryUsage = MemoryUsage  
    96.                     .from((CompositeDataSupport) mbsc.getAttribute(heapObjName,  
    97.                             "HeapMemoryUsage"));  
    98.             long maxMemory = heapMemoryUsage.getMax();// 堆最大  
    99.             long commitMemory = heapMemoryUsage.getCommitted();// 堆当前分配  
    100.             long usedMemory = heapMemoryUsage.getUsed();  
    101.             System.out.println("heap:" + (double) usedMemory * 100  
    102.                     / commitMemory + "%");// 堆使用率  
    103.   
    104.             MemoryUsage nonheapMemoryUsage = MemoryUsage  
    105.                     .from((CompositeDataSupport) mbsc.getAttribute(heapObjName,  
    106.                             "NonHeapMemoryUsage"));  
    107.             long noncommitMemory = nonheapMemoryUsage.getCommitted();  
    108.             long nonusedMemory = heapMemoryUsage.getUsed();  
    109.             System.out.println("nonheap:" + (double) nonusedMemory * 100  
    110.                     / noncommitMemory + "%");  
    111.   
    112.             ObjectName permObjName = new ObjectName(  
    113.                     "java.lang:type=MemoryPool,name=Perm Gen");  
    114.             MemoryUsage permGenUsage = MemoryUsage  
    115.                     .from((CompositeDataSupport) mbsc.getAttribute(permObjName,  
    116.                             "Usage"));  
    117.             long committed = permGenUsage.getCommitted();// 持久堆大小  
    118.             long used = heapMemoryUsage.getUsed();//  
    119.             System.out.println("perm gen:" + (double) used * 100 / committed  
    120.                     + "%");// 持久堆使用率  
    121.   
    122.             // -------------------- Session ---------------  
    123.             ObjectName managerObjName = new ObjectName(  
    124.                     "Catalina:type=Manager,*");  
    125.             Set<ObjectName> s = mbsc.queryNames(managerObjName, null);  
    126.             for (ObjectName obj : s) {  
    127.                 System.out.println("应用名:" + obj.getKeyProperty("path"));  
    128.                 ObjectName objname = new ObjectName(obj.getCanonicalName());  
    129.                 System.out.println("最大会话数:"  
    130.                         + mbsc.getAttribute(objname, "maxActiveSessions"));  
    131.                 System.out.println("会话数:"  
    132.                         + mbsc.getAttribute(objname, "activeSessions"));  
    133.                 System.out.println("活动会话数:"  
    134.                         + mbsc.getAttribute(objname, "sessionCounter"));  
    135.             }  
    136.   
    137.             // ----------------- Thread Pool ----------------  
    138.             ObjectName threadpoolObjName = new ObjectName(  
    139.                     "Catalina:type=ThreadPool,*");  
    140.             Set<ObjectName> s2 = mbsc.queryNames(threadpoolObjName, null);  
    141.             for (ObjectName obj : s2) {  
    142.                 System.out.println("端口名:" + obj.getKeyProperty("name"));  
    143.                 ObjectName objname = new ObjectName(obj.getCanonicalName());  
    144.                 System.out.println("最大线程数:"  
    145.                         + mbsc.getAttribute(objname, "maxThreads"));  
    146.                 System.out.println("当前线程数:"  
    147.                         + mbsc.getAttribute(objname, "currentThreadCount"));  
    148.                 System.out.println("繁忙线程数:"  
    149.                         + mbsc.getAttribute(objname, "currentThreadsBusy"));  
    150.             }  
    151.   
    152.         } catch (Exception e) {  
    153.             e.printStackTrace();  
    154.         }  
    155.     }  
    156.   
    157.     public static String formatTimeSpan(long span) {  
    158.         long minseconds = span % 1000;  
    159.   
    160.         span = span / 1000;  
    161.         long seconds = span % 60;  
    162.   
    163.         span = span / 60;  
    164.         long mins = span % 60;  
    165.   
    166.         span = span / 60;  
    167.         long hours = span % 24;  
    168.   
    169.         span = span / 24;  
    170.         long days = span;  
    171.         return (new Formatter()).format("%1$d天 %2$02d:%3$02d:%4$02d.%5$03d",  
    172.                 days, hours, mins, seconds, minseconds).toString();  
    173.     }  
    174. }  
原文地址:https://www.cnblogs.com/diyunpeng/p/7955314.html