MPI Control Script (mpictl.sh)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/bin/bash
#
# MPI control script.
 
usage()
{
cat <<EOF
 
Usage: $0 <options> <command>
 
This script allows to control MPI instances (i.e. start/stop them).
 
OPTIONS:
 
   -c      The configuration home directory (defaults to $MPI_HOME/conf)
 
   -n      The node ID (defaults to 1)
           The node ID is used to select:
           - the server configuration in the configuration home directory (e.g. server-1.xml)
           - the extract directory (e.g. node-1)
           - the startup log file to write in the out directory (e.g. catalina-1.out)
           - the PID file to write in the pid directory (e.g. node-1.pid)
           - the server configuration to parse for host and port for config test/reload
             (used to send a configuration reload HTTP request to the MPI)
 
   -j      The Java options to set (optional). See examples below...
 
   -f      Keep the started java process in the foreground (optional).
 
   -h      Show this message
 
COMMANDS:
 
   start          Starts an MPI node
   stop           Stops an MPI node
   showlog        Shows the log of an MPI node with tail -f
   logless        Shows the log of an MPI node with less
   showout        Shows the startup log of an MPI node with tail -f
   showstats      Display timing statistics for an MPI node
   configtest     Validates the mpi configuration without actually reloading it
   configreload   Reloads the mpi configuration (only if valid)
 
Java option examples:
 
   Set the maximum heap space for the MPI to 1024 MB and the maximum PermGen space to 256 MB:
     -Xmx1024m -XX:MaxPermSize=256m
 
   Use a custom log4j configuration located on the filesystem:
     -Dlog4j.configuration=file:/etc/mpi/log4j.xml
 
   Enable JSSE (Java Secure Socket Extension) debug output
   (Check http://docs.oracle.com/javase/7/docs/technotes/guides/security/jsse/JSSERefGuide.html#Debug for details):
     -Djavax.net.debug=all
 
   Enable remote monitoring the MPI using JMX
   (Check http://docs.oracle.com/javase/7/docs/technotes/guides/management/agent.html for details):
     -Dcom.sun.management.jmxremote.port=3000
     -Dcom.sun.management.jmxremote.password.file=password.properties
     -Dcom.sun.management.jmxremote.access.file=access.properties
     -Djavax.net.ssl.keyStore=keystore
     -Djavax.net.ssl.keyStorePassword=password
 
   To configure the MPI to prefer IPv4 over IPv6
   (e.g. when running into trouble with a IP multicasting setup for session replication) use:
     -Djava.net.preferIP4Stack=true
 
EOF
}
 
# Set defaults for command line arguments
NODE_ID="1"
MPI_CONFIG_HOME=""
JAVA_OPTS=""
DAEMON_MODE="true"
 
# Read command line arguments
while getopts "fn:c:j:h" OPTION
do
  case $OPTION in
    n)
      NODE_ID=$OPTARG
      ;;
    c)
      MPI_CONFIG_HOME=$OPTARG
      ;;
    j)
      JAVA_OPTS=$OPTARG
      ;;
    f)
      DAEMON_MODE="false";
      ;;
    h)
      usage
      exit
      ;;
    \?)
      echo "Unknown option: -$OPTARG" >&2
      exit 1
      ;;
    :)
      echo "Missing option argument for -$OPTARG" >&2
      exit 1
      ;;
    *)
      echo "Unimplemented option: -$OPTARG" >&2
      exit 1
      ;;
  esac
done
shift $(($OPTIND-1))
 
# Set variables
MPI_HOME=$(cd $(dirname "$0")/..; pwd)
if [[ ! $MPI_CONFIG_HOME ]]; then
  MPI_CONFIG_HOME="$MPI_HOME/conf"
fi
MPI_JAR="$MPI_HOME/lib/mpi-exec-war.jar"
PERF4J_JAR="$MPI_HOME/lib/perf4j.jar"
SERVER_XML="$MPI_CONFIG_HOME/server-$NODE_ID.xml"
EXTRACT_DIR="$MPI_HOME/node-$NODE_ID"
CATALINA_OUT="$MPI_HOME/out/catalina-$NODE_ID.out"
PID_FILE="$MPI_HOME/pid/node-$NODE_ID.pid"
CURRENT_DATE=`date +"%Y-%m-%d"`
CONFIGURATION_RELOAD_PATH="admin/config/reload"
LICENSE_RELOAD_PATH="admin/license/reload"
DRY_RUN_PARAM="?dryRun=true"
NODE_HOST=""
NODE_PORT=""
 
# Read node port and hostname
function read_node_host_and_port() {
  NODE_HOST=$(cat $SERVER_XML | tr '\n' ' ' | grep -o '<Host [^>]*' | sed 's/.*name="\([^"]*\)".*/\1/')
  NODE_PORT=$(cat $SERVER_XML | tr '\n' ' ' | grep -o '<Connector [^>]*protocol="HTTP[^>]*' | sed 's/.*port="\([^"]*\)".*/\1/' | head -n 1)
}
 
# Check files and directories
assert_file_exists_and_is_readable()
{
  if [[ ! -f $1 || ! -r $1 ]]; then
    echo "File $1 doesn't exist or is not readable" >&2
    exit 1
  fi
}
assert_directory_exists_and_is_writable()
{
  if [[ ! -d $1 || ! -w $1 ]]; then
    echo "Directory $1 doesn't exist or is not writable" >&2
    exit 1
  fi
}
assert_file_exists_and_is_readable $MPI_JAR
assert_file_exists_and_is_readable $PERF4J_JAR
assert_file_exists_and_is_readable $SERVER_XML
assert_directory_exists_and_is_writable "$MPI_HOME"
assert_directory_exists_and_is_writable "$MPI_HOME/pid/"
assert_directory_exists_and_is_writable "$MPI_HOME/out/"
 
# Execute command
COMMAND=$1
case $COMMAND in
  start)
    if [[ -e $PID_FILE ]]; then
      PID=$(<$PID_FILE)
      CURRENT_PID=`ps aux | awk '{print $2}'  | grep "^$PID$"`
        if [[ "$CURRENT_PID" -eq "$PID" ]]; then
        echo "MPI node $NODE_ID seems to be already started (there is a active PID for this node)"
        exit 1
      else
        rm $PID_FILE
      fi
    fi
    if [[ $DAEMON_MODE = "true" ]]; then
      java $JAVA_OPTS -Dmpi.home=$MPI_HOME -Dmpi.config.home=$MPI_CONFIG_HOME -jar $MPI_JAR -serverXmlPath=$SERVER_XML -extractDirectory=$EXTRACT_DIR > $CATALINA_OUT 2>&1 &
      echo $! > $PID_FILE
    else
      java $JAVA_OPTS -Dmpi.home=$MPI_HOME -Dmpi.config.home=$MPI_CONFIG_HOME -jar $MPI_JAR -serverXmlPath=$SERVER_XML -extractDirectory=$EXTRACT_DIR
    fi
    ;;
  stop)
    if [[ ! -e $PID_FILE ]]; then
      echo "MPI node $NODE_ID seems not to be started (there is no PID file for this node)"
      exit 1
    fi
    PID=$(<$PID_FILE)
    kill $PID
    rm $PID_FILE
    ;;
  showlog)
    tail -100f $MPI_HOME/node-$NODE_ID/logs/mpi.$CURRENT_DATE.log
    ;;
  logless)
    less $MPI_HOME/node-$NODE_ID/logs/mpi.$CURRENT_DATE.log
    ;;
  showout)
    tail -100f $MPI_HOME/out/catalina-$NODE_ID.out
    ;;
  showstats)
    java $JAVA_OPTS -jar $PERF4J_JAR $MPI_HOME/node-$NODE_ID/logs/mpi-statistics.$CURRENT_DATE.log -t 86400000
    ;;
  configtest)
    read_node_host_and_port
    curl -X POST http://$NODE_HOST:$NODE_PORT/mpi/$CONFIGURATION_RELOAD_PATH$DRY_RUN_PARAM --header "Accept:application/json" -k
    echo
    ;;
  configreload)
    read_node_host_and_port
    curl -X POST http://$NODE_HOST:$NODE_PORT/mpi/$CONFIGURATION_RELOAD_PATH --header "Accept:application/json" -k
    echo
    ;;
  licensetest)
    read_node_host_and_port
    curl -X POST http://$NODE_HOST:$NODE_PORT/mpi/$LICENSE_RELOAD_PATH$DRY_RUN_PARAM --header "Accept:application/json" -k
    echo
    ;;
  licensereload)
    read_node_host_and_port
    curl -X POST http://$NODE_HOST:$NODE_PORT/mpi/$LICENSE_RELOAD_PATH --header "Accept:application/json" -k
    echo
    ;;
  *)
    usage
    exit 1
    ;;
esac