Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@
true => Class['logstashforwarder::service'],
false => undef,
}

file { "/etc/init.d/${logstashforwarder::service_name}":
ensure => file,
content => template("${module_name}/logstashforwarder.Debian.erb"),
}
file { $logstashforwarder::configdir:
ensure => directory,
mode => '0644',
Expand Down Expand Up @@ -123,7 +126,7 @@
logstashforwarder_config { 'lsf-config':
ensure => 'present',
config => $main_config,
path => '/etc/logstash-forwarder.conf',
path => "${logstashforwarder::configdir}/${logstashforwarder::lsf_name}.conf",
tag => "LSF_CONFIG_${::fqdn}",
owner => $logstashforwarder::logstashforwarder_user,
group => $logstashforwarder::logstashforwarder_group,
Expand Down
3 changes: 2 additions & 1 deletion manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
#
class logstashforwarder(
$ensure = $logstashforwarder::params::ensure,
$lsf_name = $logstashforwarder::params::lsf_name,
$servers = undef,
$ssl_cert = undef,
$ssl_key = undef,
Expand All @@ -176,7 +177,7 @@
$init_defaults = $logstashforwarder::params::init_defaults,
$init_defaults_file = undef,
$init_template = undef,
$manage_repo = false
$manage_repo = true,
) inherits logstashforwarder::params {

anchor {'logstashforwarder::begin': }
Expand Down
7 changes: 5 additions & 2 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
# ensure
$ensure = 'present'

# Name to use instead of hardcoded name
$lsf_name = 'logstash-forwarder'

# autoupgrade
$autoupgrade = false

Expand Down Expand Up @@ -123,15 +126,15 @@
# service parameters
case $::operatingsystem {
'RedHat', 'CentOS', 'Fedora', 'Scientific', 'Amazon', 'OracleLinux': {
$service_name = 'logstash-forwarder'
$service_name = $lsf_name
$service_hasrestart = true
$service_hasstatus = true
$service_pattern = $service_name
$service_providers = [ 'init' ]
$defaults_location = '/etc/sysconfig'
}
'Debian', 'Ubuntu': {
$service_name = 'logstash-forwarder'
$service_name = $lsf_name
$service_hasrestart = true
$service_hasstatus = true
$service_pattern = $service_name
Expand Down
298 changes: 122 additions & 176 deletions templates/etc/init.d/logstashforwarder.Debian.erb
Original file line number Diff line number Diff line change
@@ -1,203 +1,149 @@
#!/bin/bash
#
# /etc/init.d/logstash -- startup script for LogStash.
#!/bin/sh
# Init script for logstash-forwarder
# Maintained by
# Generated by pleaserun.
# Implemented based on LSB Core 3.1:
# * Sections: 20.2, 20.3
#
### BEGIN INIT INFO
# Provides: logstash
# Required-Start: $all
# Required-Stop: $all
# Provides: logstash-forwarder
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts logstash
# Description: Starts logstash using start-stop-daemon
# Short-Description:
# Description: no description given
### END INIT INFO

set -e

PATH=/bin:/usr/bin:/sbin:/usr/sbin
NAME=logstash
DESC="Logstash Daemon"
DEFAULT=/etc/default/$NAME

if [ `id -u` -ne 0 ]; then
echo "You need root privileges to run this script"
exit 1
fi

. /lib/lsb/init-functions

if [ -r /etc/default/rcS ]; then
. /etc/default/rcS
fi

# The following variables can be overwritten in $DEFAULT
PATH=/sbin:/usr/sbin:/bin:/usr/bin
export PATH

# Run logstash as this user ID and group ID
LS_USER=logstash
LS_GROUP=logstash
name=<%= scope.lookupvar('logstashforwarder::lsf_name' %>)
program=<%= @installpath %>/bin/$name
args=-config\ <%= @configdir %>/$name.conf
pidfile="/var/run/$name.pid"

JAVA=/usr/bin/java
[ -r /etc/default/$name ] && . /etc/default/$name
[ -r /etc/sysconfig/$name ] && . /etc/sysconfig/$name

# Directory where the logstash all in one jar lives
LS_HOME=/var/lib/logstash

# Additional Java OPTS
LS_JAVA_OPTS=" -Djava.io.tmpdir=/var/logstash/"
trace() {
logger -t "/etc/init.d/$name" "$@"
}

# logstash log directory
LOG_DIR=/var/log/logstash
emit() {
trace "$@"
echo "$@"
}

# logstash configuration directory
CONF_DIR=/etc/logstash/conf.d
start() {

# logstash log file
LOG_FILE=$LOG_DIR/$NAME.log

# Open File limit
OPEN_FILES=2048
# Setup any environmental stuff beforehand

# LogStash options
LS_OPTS="--log ${LOG_DIR}/${NAME}.log"

# Nice level
NICE=19
# Run the program!

# End of variables that can be overwritten in $DEFAULT
chroot --userspec "$user":"$group" "$chroot" sh -c "

cd \"$chdir\"
exec \"$program\" $args
" >> /var/log/$name/$name.log 2>> /var/log/$name/$name.err &

# overwrite settings from default file
if [ -f "$DEFAULT" ]; then
. "$DEFAULT"
fi
# Generate the pidfile from here. If we instead made the forked process
# generate it there will be a race condition between the pidfile writing
# and a process possibly asking for status.
echo $! > $pidfile
emit "$name started"
return 0
}

# Define other required variables
PID_FILE=/var/run/$NAME.pid
DAEMON=$LS_JAR
DAEMON_OPTS="agent -f ${CONF_DIR} ${LS_OPTS}"
stop() {
# Try a few times to kill TERM the program
if status ; then
pid=$(cat "$pidfile")
trace "Killing $name (pid $pid) with SIGTERM"
kill -TERM $pid
# Wait for it to exit.
for i in 1 2 3 4 5 ; do
trace "Waiting $name (pid $pid) to die..."
status || break
sleep 1
done
if status ; then
emit "$name stop failed; still running."
else
emit "$name stopped."
fi
fi
}

is_true() {
if [ "x$1" = "xtrue" -o "x$1" = "xyes" -o "x$1" = "x1" ] ; then
return 0
status() {
if [ -f "$pidfile" ] ; then
pid=$(cat "$pidfile")
if ps -p $pid > /dev/null 2> /dev/null ; then
# process by this pid is running.
# It may not be our pid, but that's what you get with just pidfiles.
# TODO(sissel): Check if this process seems to be the same as the one we
# expect. It'd be nice to use flock here, but flock uses fork, not exec,
# so it makes it quite awkward to use in this case.
return 0
else
return 1
return 2 # program is dead but pid file exists
fi
else
return 3 # program is not running
fi
}

# Check DAEMON exists
if ! test -e $DAEMON; then
log_failure_msg "Daemon $DAEMON doesn't exist"
exit 1
fi
force_stop() {
if status ; then
stop
status && kill -KILL $(cat "$pidfile")
fi
}

case "$1" in
force-start|start|stop|force-stop|restart)
trace "Attempting '$1' on $name"
;;
esac

case "$1" in
start)
if ! is_true "$START" ; then
echo "logstash not configured to start, please edit /etc/default/logstash to enable"
exit 0
fi

if [ -z "$JAVA" ]; then
log_failure_msg "no JDK found - $JAVA"
exit 1
fi

# Check if a config file exists
if [ ! "$(ls -A $CONF_DIR/*.conf 2> /dev/null)" ]; then
log_failure_msg "There aren't any configuration files in $CONF_DIR"
exit 1
fi

log_daemon_msg "Starting $DESC"

if start-stop-daemon --test --start --pidfile "$PID_FILE" \
--user "$LS_USER" --exec "$JAVA" \
>/dev/null; then
# Prepare environment
ulimit -n $OPEN_FILES

# Start Daemon
start-stop-daemon --start -b --user "$LS_USER" -c "$LS_USER":"$LS_GROUP" \
-d "$LS_HOME" --pidfile "$PID_FILE" --make-pidfile \
-N $NICE \
--exec "$JAVA" -- $LS_JAVA_OPTS -jar $DAEMON $DAEMON_OPTS

sleep 1

if start-stop-daemon --test --start --pidfile "$PID_FILE" \
--user "$LS_USER" --exec "$JAVA" \
>/dev/null; then

if [ -f "$PID_FILE" ]; then
rm -f "$PID_FILE"
fi

log_end_msg 1
else
log_end_msg 0
fi
else
log_progress_msg "(already running)"
log_end_msg 0
fi
;;
stop)
log_daemon_msg "Stopping $DESC"

set +e

if [ -f "$PID_FILE" ]; then
start-stop-daemon --stop --pidfile "$PID_FILE" \
--user "$LS_USER" \
--retry=TERM/20/KILL/5 >/dev/null

if [ $? -eq 1 ]; then
log_progress_msg "$DESC is not running but pid file exists, cleaning up"
elif [ $? -eq 3 ]; then
PID="`cat $PID_FILE`"
log_failure_msg "Failed to stop $DESC (pid $PID)"
exit 1
fi

rm -f "$PID_FILE"
else
log_progress_msg "(not running)"
fi

log_end_msg 0
set -e
;;
status)
set +e

start-stop-daemon --test --start --pidfile "$PID_FILE" \
--user "$LS_USER" --exec "$JAVA" \
>/dev/null 2>&1

if [ "$?" = "0" ]; then
if [ -f "$PID_FILE" ]; then
log_success_msg "$DESC is not running, but pid file exists."
exit 1
else
log_success_msg "$DESC is not running."
exit 3
fi
else
log_success_msg "$DESC is running with pid `cat $PID_FILE`"
fi

set -e
;;
restart|force-reload)
if [ -f "$PID_FILE" ]; then
$0 stop
sleep 1
fi

$0 start
;;
*)
log_success_msg "Usage: $0 {start|stop|restart|force-reload|status}"
exit 1
;;
force-start)
PRESTART=no
exec "$0" start
;;
start)
status
code=$?
if [ $code -eq 0 ]; then
emit "$name is already running"
exit $code
else
start
exit $?
fi
;;
stop) stop ;;
force-stop) force_stop ;;
status)
status
code=$?
if [ $code -eq 0 ] ; then
emit "$name is running"
else
emit "$name is not running"
fi
exit $code
;;
restart)

stop && start
;;
*)
echo "Usage: $SCRIPTNAME {start|force-start|stop|force-start|force-stop|status|restart}" >&2
exit 3
;;
esac

exit 0
exit $?