6.19.6 Coding example for a user command
This coding example for a user command changes the IP addresses of servers. Part of the coding of the user command is shown and explained in (1). The entire coding is shown in (2).
You can also change the IP addresses of servers by specifying a LAN status settings file. If you specify a LAN status settings file, there is no need to create a user command.
- Organization of this subsection
(1) Explanation of the user command shown in the coding example
This example adds and deletes IP addresses that are used by servers at the following times:
-
An IP address is added on the active server when the server starts (when the server startup processing begins) and nothing is performed on the standby server.
-
An IP address is deleted from the active server in the event of an active server failure (when error processing begins) and an IP address is added on the standby server.
The figure below shows the timing of these operations. The circled numbers in the figure correspond to the numbers in the partial coding example.
The following figure shows the correspondence between the coding example for a user command and the issuance timing.
Specify in (1) the processing that you want to perform on the active server when the server starts (when server startup processing begins). This example specifies ipaddr_add as the command used to add IP addresses.
Specify in (2) the processing that you want to perform on the standby server when the server starts (when server startup processing begins). This example specifies nothing because no action is to be performed.
Specify in (3) the processing that you want to perform on the active server in the event of a failure on the active server (when error processing begins). This example specifies ipaddr_delete as the command for deleting IP addresses.
Specify in (4) the processing that you want to perform on the standby server in the event of a failure on the active server (when error processing begins). This example specifies ipaddr_add as the command for adding IP addresses.
The following table shows the correspondence between the variables passed from HA Monitor and the user commands issued by HA Monitor for each of the issuance timings (1) through (4).
Issuance timing |
$4 |
$5 |
$6 |
User command issued by HA Monitor# |
---|---|---|---|---|
(1) |
online |
-s |
start |
/usr/bin/usrcmd -n server -k online -s start |
(2) |
standby |
-s |
start |
/usr/bin/usrcmd -n server -k standby -s start |
(3) |
online |
-a |
start |
/usr/bin/usrcmd -n server -k online -a start |
(4) |
standby |
-a |
start |
/usr/bin/usrcmd -n server -k standby -a start |
#: This example uses the following user command name and server alias name:
-
User command name: /usr/bin/usrcmd
-
Server alias name: server
(2) Coding example
This code is stored as a sample file under HA Monitor's sample file directory.
#!/bin/sh # The definition of the 1st server SERV1="server1" IPADDR_SERV1="a.b.c.d" BROADCAST_SERV1="e.f.g.h" IFNAME_SERV1="ethX:Y1" # The definition of the 2nd server SERV2="server2" IPADDR_SERV2="i.j.k.l" BROADCAST_SERV2="m.n.o.p" IFNAME_SERV2="ethX:Y2" # The alias IP address is added to the LAN interface. # The ARP caches are updated. ipaddr_add() { /sbin/ifconfig $WIFNAME inet $WIPADDR netmask 255.255.255.0 broadcast $WBROADCAST ARPIFNAME=`echo $WIFNAME | /bin/sed -e 's/:[0-9]*$//'` /sbin/arping -U -c 2 -I $ARPIFNAME $WIPADDR /bin/echo 0 > /proc/sys/net/ipv4/route/flush return 0 } # The alias IP address is deleted from the LAN interface. ipaddr_delete() { /sbin/ifconfig $WIFNAME down /bin/echo 0 > /proc/sys/net/ipv4/route/flush return 0 } # Main KIND_ONLINE="online" KIND_STANDBY="standby" SERV_START="-s" SERV_END="-e" SERV_PLANEND="-p" SERV_ABORT="-a" SERV_ABORT_NS="-o" SERV_FAULT="-f" SERV_HOSTDOWN="-h" SERV_PLANSWAP="-w" STATUS_START="start" STATUS_END="end" STATUS_SBYEND="sbyend" # This user command terminates, when the state of HAmonitor has changed. if [ "$1" = "-m" ] then exit 1 fi # Processing of the server for serv in $SERV1 $SERV2 do if [ "$2" = "$serv" ] then case "$serv" in "$SERV1" ) WIPADDR="$IPADDR_SERV1" WBROADCAST="$BROADCAST_SERV1" WIFNAME="$IFNAME_SERV1" ;; "$SERV2" ) WIPADDR="$IPADDR_SERV2" WBROADCAST="$BROADCAST_SERV2" WIFNAME="$IFNAME_SERV2" ;; esac if [ "$4" = "$KIND_ONLINE" ] then case "$5" in "$SERV_START" ) [ "$6" = "$STATUS_START" ] && ipaddr_add ;; "$SERV_END" ) ipaddr_delete ;; "$SERV_PLANEND" ) ipaddr_delete ;; "$SERV_ABORT" ) [ "$6" = "$STATUS_START" ] && ipaddr_delete ;; "$SERV_ABORT_NS" ) [ "$6" = "$STATUS_START" ] && ipaddr_delete ;; "$SERV_PLANSWAP" ) [ "$6" = "$STATUS_START" ] && ipaddr_delete ;; esac else case "$5" in "$SERV_START" ) ;; "$SERV_END" ) ;; "$SERV_PLANEND" ) ;; "$SERV_ABORT" ) [ "$6" = "$STATUS_START" ] && ipaddr_add ;; "$SERV_FAULT" ) ipaddr_delete ;; "$SERV_HOSTDOWN" ) [ "$6" = "$STATUS_START" ] && ipaddr_add ;; "$SERV_PLANSWAP" ) [ "$6" = "$STATUS_START" ] && ipaddr_add ;; esac fi fi done exit 0