Project

General

Profile

How to Remote Manage » History » Version 7

Henning Blohm, 08.03.2020 18:09

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 6 Henning Blohm
<pre><code class="bash">
32 5 Henning Blohm
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 6 Henning Blohm
<pre><code class="bash">
38 5 Henning Blohm
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 6 Henning Blohm
<pre><code class="bash">
61 5 Henning Blohm
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 6 Henning Blohm
<pre><code class="bash">
74 5 Henning Blohm
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 6 Henning Blohm
<pre><code class="bash">
89 5 Henning Blohm
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 7 Henning Blohm
Supposedly, 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
111
In addition, it will not hurt to force the hostname used in RMI access to localhost via setting 
112
113
<pre><code class="bash">
114
java.rmi.server.hostname=localhost
115
</code></pre>
116
117
in @Z2_HOME/bin/runtime.properties@.
118 2 Henning Blohm
119
h2. Remote Synchronization and Log Streaming
120
121
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:
122
123
<pre><code class="bash">
124
java -cp z.jar com.zfabrik.launch.Manage -?
125
</code></pre>
126
127
for the usage:
128
129
<pre><code class="bash">
130
SYNOPSIS
131
132
java -cp z.jar Manage <command> <options>
133
134
COMMANDS
135
    
136
    sync        The sync performs a synchronization of a running (remote) z2 Home.  
137
    
138
    showlog     Continuously stream the z2 Home log of a running z2 Home to the current stderr.  
139
140
OPTIONS
141
142
    -url <url>
143
        JMX URL identifying the target z2 Home. Defaults to service:jmx:rmi:///jndi/rmi://localhost:7777/jmxrmi
144
        The URL may be shortened to <host>:<port> (e.g. localhost:7777)
145
        
146
147
    -user <username> 
148
    
149
        Username used for JMX authentication. Optional.
150
        
151
    -pass <password> 
152
    
153
        Password used for JMX authentication. Optional. Mandatory when a username has been set
154
        
155
    -b <n>
156
    
157
        Number of lines to read before current (if available) when running showlog. 
158
        
159
EXAMPLE
160
161
    java -cp z.jar com.zfabrik.launch.Manage showlog -url host:7777 -user admin -pass admin 
162
</code></pre>