#!/bin/sh # # # ntp # Description: Manages the ntp service as a stateful ntp OCF resource # Master -> ntp runs as server # Slave -> ntp runs as client # # # This program is free software; you can redistribute it and/or modify # it under the terms of version 2 of the GNU General Public License as # published by the Free Software Foundation. # # This program is distributed in the hope that it would be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # # Further, this software is distributed without any warranty that it is # free of the rightful claim of any third person regarding infringement # or the like. Any license provided herein, whether implied or # otherwise, applies only to this software file. Patent licenses, if # any, provided herein do not apply to combinations of this program with # other software, or any other product whatsoever. # # You should have received a copy of the GNU General Public License # along with this program; if not, write the Free Software Foundation, # Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA. # # Copyright (c) 2010 T-Systems GEI GmbH, Simon Jansen # All Rights Reserved. # ####################################################################### # # OCF Parameters: # OCF_RESKEY_binfile : Executable file # OCF_RESKEY_user : NTP user # OCF_RESKEY_master_config : Configuration file for master # OCF_RESKEY_slave_config : Configuration file for slave # OCF_RESKEY_port : UDP port ntp is listening on # OCF_RESKEY_pidfile : Process id file # OCF_RESKEY_lockfile : Lock file # OCF_RESKEY_further_params : Further parameters # OCF_RESKEY_nmap_binfile : Executable file of nmap # # Parameters with default values: OCF_RESKEY_binfile, # OCF_RESKEY_user, # OCF_RESKEY_master_config, # OCF_RESKEY_slave_config, # OCF_RESKEY_port, # OCF_RESKEY_pidfile, # OCF_RESKEY_lockfile, # OCF_RESKEY_further_params, # OCF_RESKEY_nmap_binfile # ####################################################################### # Initialization: : ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/resource.d/heartbeat} . ${OCF_FUNCTIONS_DIR}/.ocf-shellfuncs CRM_MASTER="${HA_SBIN_DIR}/crm_master -l reboot" ####################################################################### #---------------------------------------------------------------------- # Definitions and default values #---------------------------------------------------------------------- # Define default values for parameters OCF_RESKEY_binfile_default="/usr/sbin/ntpd" OCF_RESKEY_user_default="ntp" OCF_RESKEY_master_config_default="/etc/ntp_master.conf" OCF_RESKEY_slave_config_default="/etc/ntp_slave.conf" OCF_RESKEY_port_default="123" OCF_RESKEY_pidfile_default="/var/run/ntpd.pid" OCF_RESKEY_lockfile_default="/var/lock/ntpdate" OCF_RESKEY_nmap_binfile_default="/usr/bin/nmap" # Fill in defaults if no values are defined : ${OCF_RESKEY_binfile=${OCF_RESKEY_binfile_default}} : ${OCF_RESKEY_user=${OCF_RESKEY_user_default}} : ${OCF_RESKEY_master_config=${OCF_RESKEY_master_config_default}} : ${OCF_RESKEY_slave_config=${OCF_RESKEY_slave_config_default}} : ${OCF_RESKEY_port=${OCF_RESKEY_port_default}} : ${OCF_RESKEY_pidfile=${OCF_RESKEY_pidfile_default}} : ${OCF_RESKEY_lockfile=${OCF_RESKEY_lockfile_default}} : ${OCF_RESKEY_nmap_binfile=${OCF_RESKEY_nmap_binfile_default}} #---------------------------------------------------------------------- # Functions #---------------------------------------------------------------------- meta_data() { cat < 1.0 This is an OCF stateful resource agent to manage the ntp service in a high-availability setup. In the master role the node starts the service with the master config and in the slave role with the slave config. The agent monitors the service in the master role via nmap by checking the status of the port ntp is listening on. So you have to install nmap to use this RA. Manages a ntp instance as a master/slave resource This parameter defines the executable file of the ntp service. The default value is ${OCF_RESKEY_binfile_default}. Executable file This parameter defines the user of the ntp service. The default value is ${OCF_RESKEY_user_default}. Executable file This parameter defines the configuration file of the ntp service in the master role. The default value is ${OCF_RESKEY_master_config_default}. Master configuration file This parameter defines the configuration file of the ntp service in the slave role. The default value is ${OCF_RESKEY_slave_config_default}. Slave configuration file This parameter defines the UDP port ntp is listening on. The port is defined in the configuration file. This value is only used to monitor the service via nmap. The default value is ${OCF_RESKEY_port_default}. UDP port ntp is listening on This parameter defines the process id file of the ntp service. The default value is ${OCF_RESKEY_pidfile_default}. Process id file This parameter defines the lock file of the ntp service. The default value is ${OCF_RESKEY_lockfile_default}. Lock file This parameter defines additional parameters that are passed to the ntp service during the startup. Further parameters This parameter defines the executable file of nmap. The default value is ${OCF_RESKEY_nmap_binfile_default}. Nmap executable file END } ####################################################################### ntp_usage() { cat < /dev/null 2>&1; then # Determine state with the output of ps PS_OUTPUT=`ps aux | grep $pid` # Check if ntp is running with master config if [ `expr match "$PS_OUTPUT" ".*$OCF_RESKEY_master_config.*"` != "0" ]; then # Check if ntp is listening on port NMAP_OUTPUT=`${OCF_RESKEY_nmap_binfile} -sU -p ${OCF_RESKEY_port} 127.0.0.1 | grep $OCF_RESKEY_port` if [ `expr match "$NMAP_OUTPUT" ".*open.*"` != "0" ]; then return $OCF_RUNNING_MASTER else ocf_log err "NTP seems to be running, but doesn't listen on UDP port ${OCF_RESKEY_port}." return $OCF_ERR_GENERIC fi elif [ `expr match "$PS_OUTPUT" ".*$OCF_RESKEY_slave_config.*"` != "0" ]; then ocf_log info "NTP $PS_OUTPUT" # ntp is running as slave return $OCF_SUCCESS fi else ocf_log err "NTP service is not running but the process id file exists." return $OCF_ERR_GENERIC fi ocf_log info "NTP $PS_OUTPUT" return $OCF_NOT_RUNNING } ntp_running_undefined() { # Check if NTP is running # Check if process id file exists if [ ! -r ${OCF_RESKEY_pidfile} ]; then return 1 fi if read pid < ${OCF_RESKEY_pidfile} && ps -p $pid > /dev/null 2>&1; then # Determine state with the output of ps PS_OUTPUT=`ps aux | grep $pid` # Check if NTP is running with master config if [ `expr match "$PS_OUTPUT" ".*$OCF_RESKEY_master_config.*"` != "0" ]; then # NTP is running as master return 1 elif [ `expr match "$PS_OUTPUT" ".*$OCF_RESKEY_slave_config.*"` != "0" ]; then # NTP is running as slave return 1 else # NTP is running in an undefined state return 0 fi else return 0 fi } ntp_validate() { # Test if ntp executable file exists and is executable if [ ! -x ${OCF_RESKEY_binfile} ]; then ocf_log err "NTP binary $OCF_RESKEY_binfile does not exist or is not executable." return $OCF_ERR_INSTALLED fi # Test if configs exist if [ ! -f ${OCF_RESKEY_slave_config} ]; then ocf_log err "NTP slave config file $OCF_RESKEY_slave_config does not exist." return $OCF_ERR_CONFIGURED fi # Test if directory for pid file exists PID_DIR=`dirname ${OCF_RESKEY_pidfile}` if [ ! -d ${PID_DIR} ]; then ocf_log err "The directory to store the pid file does not exist." return $OCF_ERR_CONFIGURED fi # Test if portnumber is valid if ! ocf_is_decimal "$OCF_RESKEY_port" || [ $OCF_RESKEY_port -le 0 ]; then ocf_log err "Portnumber is invalid." return $OCF_ERR_CONFIGURED fi # Test if nmap executable file exists and is executable if [ ! -x ${OCF_RESKEY_nmap_binfile} ]; then ocf_log err "nmap binary $OCF_RESKEY_nmap_binfile does not exist or is not executable." return $OCF_ERR_INSTALLED fi return $OCF_SUCCESS } #---------------------------------------------------------------------- # Main script #---------------------------------------------------------------------- case $__OCF_ACTION in meta-data) meta_data exit $OCF_SUCCESS ;; usage|help) ntp_usage exit $OCF_SUCCESS esac #ntp_validate case $__OCF_ACTION in start) ntp_start ;; promote) ntp_promote ;; demote) ntp_demote ;; stop) ntp_stop ;; monitor) ntp_monitor ;; validate-all) ntp_validate ;; *) ntp_usage exit $OCF_ERR_UNIMPLEMENTED;; esac rc=$? ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc" exit $rc