Wednesday, November 27, 2013

Ubuntu - Run Logstash as a service

In the previous blog post, I had setup a log management system with logstash, elasticsearch, and redis. But I started logstash by running the java command for demonstration purpose. So, to run Logstash as a service in a Ubuntu machine:


1. Create a init script for logstash at /etc/init.d/logstash: (This will also start the Kibana web interface)

This script is for logstash in the indexer server. For the shipper machine, change "mode", "logstash_conf" and "command" definition:

#! /bin/sh

### BEGIN INIT INFO
# Provides:          logstash-indexer
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO

. /lib/lsb/init-functions

mode="indexer"
name="logstash-$mode"
logstash_bin="/usr/bin/java -- -jar /opt/logstash/logstash-1.2.2-flatjar.jar"
logstash_conf="/etc/logstash/logstash-indexer.conf"
logstash_log="/var/log/$name.log"
pid_file="/var/run/$name.pid"

NICE_LEVEL="-n 19"

start () {
    command="/usr/bin/nice ${NICE_LEVEL} ${logstash_bin} agent -f $logstash_conf --log ${logstash_log} -- web"

    log_daemon_msg "Starting $mode" "$name"
    if start-stop-daemon --start --quiet --oknodo --pidfile "$pid_file" -b -m --exec $command; then
        log_end_msg 0
    else
        log_end_msg 1
    fi
}

stop () {
    echo "Stoping $name"
    start-stop-daemon --stop --quiet --oknodo --pidfile "$pid_file"
    echo "$name stopped"
}

status () {
    status_of_proc -p $pid_file "" "$name"
}

case $1 in
    start)
        if status; then exit 0; fi
        start
        ;;
    stop)
        stop
        ;;
    reload)
        stop
        start
        ;;
    restart)
        stop
        start
        ;;
    status)
        status && exit 0 || exit $?
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|reload|status}"
        exit 1
        ;;
esac

exit 0

(source: https://gist.github.com/jippi/1065761)


2. Add the new Logstash init script to all the default runlevels using the following command:

$ sudo update-rc.d logstash defaults


3. Restart