Commit 0268a24c authored by Volodymyr Melnyk's avatar Volodymyr Melnyk

Implement CentOS 6 compatibility

parent f31a2c08
......@@ -8,9 +8,15 @@ io_stats_dir="${_spool_dir}/io"
io_stats_timestamp="${io_stats_dir}/.timestamp"
_log_file="${_log_dir}/save_io_stats.log"
_lock_file="${_lock_dir}/save_io_stats.pid"
_log 1 "On duty!"
lock_file_set=$(_lock_file_set "${_lock_file}")
if [[ "${lock_file_set}" != "${_lock_file}" ]]; then
_die "Can't set lock file ${_lock_file}"
fi
while :; do
touch "${io_stats_timestamp}"
......
......@@ -5,13 +5,13 @@ _root_dir='/opt/snmp_stats'
_lib_dir="${_root_dir}/lib"
_var_dir="${_root_dir}/var"
_log_dir="${_var_dir}/log"
_log_file="${_log_dir}/snmp_stats_default.log"
_lock_dir="${_var_dir}/run"
_spool_dir="${_var_dir}/spool"
_share_dir="${_root_dir}/share"
_log_file="${_log_dir}/snmp_stats.log"
_mib_root=".1.3.6.1.4.1.53033.100"
trap "exit ${_exit_status_error}" TERM
_mib_root=".1.3.6.1.4.1.53045.100"
_my_name="$(basename "${0}")"
_top_pid="${$}"
function _log {
......@@ -39,3 +39,83 @@ function _die {
_warn $*
kill -s TERM "${_top_pid}"
}
function _lock_file_get_pid {
local lock_file="${1}"
if [[ -z "${lock_file}" ]]; then
_warn "Lock file isn't defined" >/dev/null 2>&1
return 1
fi
if [[ ! -f "${lock_file}" ]]; then
_warn "Lock file ${lock_file} isn't present" >/dev/null 2>&1
return 2
fi
local pid=$(cat "${lock_file}")
if [[ -z "${pid}" ]]; then
_warn "Can't read lock file ${lock_file}" >/dev/null 2>&1
return 2
fi
local pcmd=$(ps -o cmd= "${pid}")
if [[ -z "${pcmd}" ]]; then
_warn "Can't find process ${pid}, lock file ${lock_file} is stale" >/dev/null 2>&1
return 3
fi
if [[ "${pcmd}" != *"${_my_name}"* ]]; then
_warn "${pid} is running, but it doesn't look like ${_my_name}" >/dev/null 2>&1
return 4
fi
echo "${pid}"
return 0
}
function _lock_file_set {
local lock_file="${1}"
if [[ -z "${lock_file}" ]]; then
_warn "Lock file isn't defined"
return 1
fi
local lock_file_pid="$(_lock_file_get_pid "${lock_file}")"
if [[ ! -z "${lock_file_pid}" ]]; then
_warn "Lock file ${lock_file} exists, PID is ${lock_file_pid}"
return 2
fi
_log 0 "Setting lock-file ${lock_file}"
echo "${_top_pid}" >"${lock_file}"
if [[ "${?}" -ne 0 ]]; then
_warn "Can't write lock file ${lock_file}"
return 3
fi
_lock_file="${lock_file}"
echo "${lock_file}"
return 0
}
function _lock_file_unset {
local lock_file="${1}"
if [[ -z "${lock_file}" ]]; then
_warn "Lock file isn't defined"
return 1
fi
local lock_file_pid="$(_lock_file_get_pid "${lock_file}")"
if [[ -z "${lock_file_pid}" ]]; then
_warn "Lock file ${lock_file} can't be removed"
return 2
fi
if [[ -f "${lock_file}" ]]; then
_log 0 "Removing lock_file ${lock_file}"
rm -f "${lock_file}"
if [[ "${?}" -ne 0 ]]; then
_warn "Can't remove lock file ${lock_file}"
return 3
fi
fi
return 0
}
function _exit {
_lock_file_unset "${_lock_file}"
exit "${1}"
}
trap "_exit ${_exit_status_error}" TERM
trap "_exit ${_exit_status_error}" INT
......@@ -12,6 +12,26 @@ fi
_log_file='/dev/null'
centos_release=$(cat /etc/centos-release)
case "${centos_release}" in
CentOS Linux release 6.*)
syvinit_script='/etc/init.d/save_io_stats'
if [[ ! -f "${syvinit_script}" ]]; then
_log 1 "Installing System V init script"
cat "${_share_dir}/sysvinit/save_io_stats" >"${syvinit_script}"
fi
;;
CentOS Linux release 7.*)
systemd_service_config='/etc/systemd/system/save_io_stats.service'
if [[ ! -f "${systemd_service_config}" ]]; then
_log 1 "Installing systemd service configuration file"
cat "${_share_dir}/systemd/save_io_stats.service" >"${systemd_service_config}"
fi
;;
*)
_die "Can't install on this distro"
;;
esac
systemd_service_config='/etc/systemd/system/save_io_stats.service'
if [[ ! -f "${systemd_service_config}" ]]; then
_log 1 "Installing systemd service configuration file"
......@@ -30,12 +50,20 @@ if [[ ! -f "${logrotate_config}" ]]; then
cat "${_share_dir}/logrotate/snmp_stats" >"${logrotate_config}"
fi
io_stats_spool="${_spool_dir}/io"
if [[ ! -f "${io_stats_spool}" ]]; then
io_stats_lock_dir="${_lock_dir}"
if [[ ! -f "${io_stats_lock_dir}" ]]; then
_log 1 "Creating the lock directory"
mkdir -p "${io_stats_lock_dirl}"
chown root:nobody "${io_stats_lock_dir}"
chmod 0770 "${io_stats_lock_dir}"
fi
io_stats_spool_dir="${_spool_dir}/io"
if [[ ! -f "${io_stats_spool_dir}" ]]; then
_log 1 "Creating the spool directory"
mkdir -p "${io_stats_spool}"
chown root:nobody "${io_stats_spool}"
chmod 0770 "${io_stats_spool}"
mkdir -p "${io_stats_spool_dir}"
chown root:nobody "${io_stats_spool_dir}"
chmod 0770 "${io_stats_spool_dir}"
fi
_log 1 "Installation steps have been completed"
#!/bin/bash
# pidfile: /opt/snmp_stats/var/run/save_io_stats.pid
### BEGIN INIT INFO
# Provides:
# Required-Start: $snmpd
# Required-Stop: $snmpd
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: IO stats saver
# Description: Saves IO stats to them available for SNMP-agent.
### END INIT INFO
. /opt/snmp_stats/lib/common.sh
. /etc/init.d/functions
name="save_io_stats"
pid_file="${_lock_dir}/${name}.pid"
start() {
running_pid=$(_lock_file_get_pid "${pid_file}")
if [[ -z ${running_pid} ]]; then
echo -n "Starting ${name}: "
cd "${_root_dir}"
daemon --check "${name}" --user nobody "${_root_dir}/bin/save_io_stats.sh >/dev/null 2>&1 &"
touch "/var/lock/subsys/${name}"
success
else
_warn "$name is already running"
failure
fi
echo
}
stop() {
echo -n "Shutting down ${name}: "
for i in $(seq 1 13); do
running_pid=$(_lock_file_get_pid "${pid_file}")
if [[ -z ${running_pid} ]]; then
break
elif [[ "${1}" -eq 13 ]]; then
echo -n '!'
kill -KILL "${running_pid}"
else
echo -n '.'
kill -TERM "${running_pid}"
fi
sleep 2
done
rm -f "/var/lock/subsys/${name}"
success
echo
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
exit 0
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment