Project

General

Profile

How to Remote Manage » History » Version 5

Henning Blohm, 08.03.2020 18:00

1 1 Henning Blohm
h1. How to Remote Manage
2 2 Henning Blohm
3 4 Henning Blohm
A significant amount of internal state and operations of a z2 system can be accessed via JMX using tools such as jconsole or jvisualvm. In order to access z2 via JMX some JMX related configuration should be applied.
4 2 Henning Blohm
5 3 Henning Blohm
h2. Configuring JMX access in Z2
6
7
h2. Accessing a Java VM remotely via JMX
8
9 5 Henning Blohm
It should not be necessary, but unfortunately, as JMX in based on RMI, accessing to a Java VM via JMX remotely can be non-trivial, if
10 1 Henning Blohm
11 5 Henning Blohm
a) You have port restrictions
12
b) The externally used hostname is not the same as the internally used hostname
13
14
Both are typical, if you need to manage machines remotely.
15
16 3 Henning Blohm
Here are two practical approaches: 
17
18 1 Henning Blohm
h3. Accessing a Remote JVM via JMX directly
19 4 Henning Blohm
20
The tricky thing about JMX over RMI is that knowing the host name and the JMX port is merely sufficient to request RMI access that then uses a different (random) port and a host name defined by the JMX endpoint. That is, when accessing to the JMX endpoint we get a redirect to a different host and port combination that may not make sense from where we are calling, either because the host name is not resolvable or wrong (e.g. localhost), or because the port is not accessible. 
21
22
However, the following two system properties can be used to solve these problems:
23 3 Henning Blohm
24
| com.sun.management.jmxremote.rmi.port | Defines the RMI port to use and can be set to be the same as the JMX port |
25 1 Henning Blohm
| java.rmi.server.hostname | Defines the RMI host name to use |
26
27 5 Henning Blohm
h4. If hostnames differ
28 1 Henning Blohm
29 5 Henning Blohm
So, for example, assuming your host is accessible as myhost externally, but has a different name by itself, adding
30 1 Henning Blohm
31 5 Henning Blohm
<pre><code class="java">
32
java.rmi.server.hostname=myhost
33
</code></pre>
34 1 Henning Blohm
35 5 Henning Blohm
to @Z2_HOME/bin/runtime.properties@ (which is one way to make sure the system property is set on all z2 processes), will make sure that JMX access to myhost works. However, when doing so and checking for the ports used by the JVM you might see something like this:
36 1 Henning Blohm
37 5 Henning Blohm
<pre><code class="java">
38
netstat -nltp | grep java
39
40
tcp6       0      0 :::42149                :::*                    LISTEN      2775/java
41
tcp6       0      0 :::8080                 :::*                    LISTEN      2775/java
42
tcp6       0      0 :::38291                :::*                    LISTEN      2775/java
43
tcp6       0      0 :::37237                :::*                    LISTEN      2735/java
44
tcp6       0      0 :::43767                :::*                    LISTEN      2735/java
45
tcp6       0      0 :::7800                 :::*                    LISTEN      2775/java
46
tcp6       0      0 :::7777                 :::*                    LISTEN      2735/java
47
</code></pre>
48
49
Note the expected "unexpected" ports 42149 and 38291 for the web worker process and 37237 and 43767 for the home process.
50
51
h4. Using the same port for JMX and RMI
52
53
By default, z2 sets uses the JMX port 7777 for the <home> process and 7800 for the web worker process. If not all ports are accessible but you are willing to allow to make dedicated ports accessible, it could
54
be a good idea to make sure that these ports are also used for JMX over RMI.
55
56
In that case, we need to set the property @com.sun.management.jmxremote.rmi.port@ for each process invidually.
57
58
For the home process, that is started directly from the command line, we configure startup settings in @Z2_HOME/bin/launch.properties@. We enhance @home.vmopts@ to include the new property:
59
60
<pre><code class="java">
61
home.vmopts=\
62
        -Xmx64M -cp z.jar \
63
        -Dcom.sun.management.config.file=management.properties \
64
        -Dworker.remoteJmx=true \
65
        -Djava.util.logging.config.file=logging.properties \
66
        -Dcom.zfabrik.home.concurrency=5 \
67
        -Dcom.sun.management.jmxremote.rmi.port=7700 \
68
        -Dcom.zfabrik.home.start=environment/home
69
</code></pre>
70
71
In addition in @Z2_HOME/base/environment.base/webWorker.properties@ we change the VM options to 
72
73
<pre><code class="java">
74
worker.process.vmOptions\:JEXL3=`\
75
 -Xmx128m -Xms128m -XX:+HeapDumpOnOutOfMemoryError \
76
 -Dderby.system.home=../../data/derby \
77
 -Dsvnkit.symlinks=false \
78
 -Duser.language=en \
79
 -Dcom.sun.management.config.file=management.properties \
80
 -Dcom.sun.management.jmxremote.rmi.port=${this["worker.jmx.port"]}\
81
`
82
</code></pre>
83
84
so that the same port is used for JMX over RMI as for the initial JMX access.
85
86
After that we will see two of the obscure ports gone. For example this:
87
88
<pre><code class="java">
89
netstat -nltp | grep java
90
91
tcp6       0      0 :::44809                :::*                    LISTEN      8980/java           
92
tcp6       0      0 :::35691                :::*                    LISTEN      8956/java           
93
tcp6       0      0 :::8080                 :::*                    LISTEN      8980/java           
94
tcp6       0      0 :::7700                 :::*                    LISTEN      8956/java           
95
tcp6       0      0 :::7800                 :::*                    LISTEN      8980/java           
96
tcp6       0      0 :::7777                 :::*                    LISTEN      8956/java    
97
</code></pre>
98
99
The remaining two random ports are due to the "Java Attach API":https://docs.oracle.com/javase/8/docs/technotes/guides/attach/index.html.
100
101
h3. Using jstatd 
102
103
Another way of allowing access to all Java VMS on some machine is by running "jstatd":https://docs.oracle.com/javase/6/docs/technotes/tools/share/jstatd.html .
104
105
h3. Accessing a Remote JVM via JMX via an SSH tunnel
106
107
Another option is to access JMX via an SSH tunnel. Given you made sure the ports for JMX and JMX RMI are identical, this works and has the additional benefits, that you could protect access to JMX completely by limiting it to SSH access. 
108
109
All you need to do for that is to add the property @com.sun.management.jmxremote.local.only=true@ to Z2_HOME/bin/management.properties
110 2 Henning Blohm
111
h2. Remote Synchronization and Log Streaming
112
113
Starting with version 2.8, Z2 offers a built-in command line utility to synchronize (with log output) and provide streaming access to the z2 home log. When in Z2_HOME/bin run:
114
115
<pre><code class="bash">
116
java -cp z.jar com.zfabrik.launch.Manage -?
117
</code></pre>
118
119
for the usage:
120
121
<pre><code class="bash">
122
SYNOPSIS
123
124
java -cp z.jar Manage <command> <options>
125
126
COMMANDS
127
    
128
    sync        The sync performs a synchronization of a running (remote) z2 Home.  
129
    
130
    showlog     Continuously stream the z2 Home log of a running z2 Home to the current stderr.  
131
132
OPTIONS
133
134
    -url <url>
135
        JMX URL identifying the target z2 Home. Defaults to service:jmx:rmi:///jndi/rmi://localhost:7777/jmxrmi
136
        The URL may be shortened to <host>:<port> (e.g. localhost:7777)
137
        
138
139
    -user <username> 
140
    
141
        Username used for JMX authentication. Optional.
142
        
143
    -pass <password> 
144
    
145
        Password used for JMX authentication. Optional. Mandatory when a username has been set
146
        
147
    -b <n>
148
    
149
        Number of lines to read before current (if available) when running showlog. 
150
        
151
EXAMPLE
152
153
    java -cp z.jar com.zfabrik.launch.Manage showlog -url host:7777 -user admin -pass admin 
154
</code></pre>