net-analyzer/suricata: Updated init script
authorSlawomir Lis <slis@gentoo.org>
Tue, 27 Dec 2016 10:43:03 +0000 (11:43 +0100)
committerSlawomir Lis <slis@gentoo.org>
Tue, 27 Dec 2016 10:43:03 +0000 (11:43 +0100)
Now it's able to override config filename and point to log directory.
Related to #602590

Thanks to Vieri <rentorbuy@yahoo.com>

Package-Manager: Portage-2.3.3, Repoman-2.3.1

net-analyzer/suricata/files/suricata-3.2-conf
net-analyzer/suricata/files/suricata-3.2-init

index 61715ba3c55e6748f0831289794de14aa384a55d..d900ade852585428d4bcbc54d529d5fec62cba23 100644 (file)
 # ln -s /etc/init.d/suricata /etc/init.d/suricata.q1
 # cp /etc/suricata/suricata.yaml /etc/suricata/suricata-q0.yaml
 # cp /etc/suricata/suricata.yaml /etc/suricata/suricata-q1.yaml
+#
 # Edit both suricata-q{0,1}.yaml files and set values accordingly.
-# 
-# You can then define the following options here:
+# You can override these yaml config file names with SURICATA_CONF* below (optional).
+# This allows you to use the same yaml config file for multiple instances as long as you override
+# sensible options such as the log file paths.
+# SURICATA_CONF_q0="suricata-queues.yaml"
+# SURICATA_CONF_q1="suricata-queues.yaml"
+# SURICATA_CONF="suricata.yaml"
+
+# You can define the options here:
+# NB: avoid using -l, -c and setting logging.outputs.1.file.filename as the init script will try to set them for you.
 
 # SURICATA_OPTS_q0="-q 0"
 # SURICATA_OPTS_q1="-q 1"
 # then you can set:
 
 SURICATA_OPTS="-i eth0"
+
+# Log paths listed here will be created by the init script and will override the log path
+# set in the yaml file, if present.
+# SURICATA_LOG_PATH_q0="/var/log/suricata/q0"
+# SURICATA_LOG_PATH_q1="/var/log/suricata/q1"
+# SURICATA_LOG_PATH="/var/log/suricata"
+# SURICATA_LOG_FILE="suricata.log"
+
+# You can view all the available options you can set with --set
+# and check the full config settings in an easily parsable format.
+# SURICATA_DUMP=1
index d6128159e1b19ce0d5ce5f57314de3b2e9f5cbfa..3ec6afd68f7248c0b73d4a54cd1d1624c75c971e 100644 (file)
@@ -8,13 +8,17 @@ SURICATA_DIR=${SURICATA_DIR:-/etc/suricata}
 SURICATA=${SVCNAME#*.}
 SURICATAID=$(shell_var "${SURICATA}")
 if [ -n "${SURICATA}" ] && [ ${SVCNAME} != "suricata" ]; then
-    SURICATACONF="${SURICATA_DIR}/suricata-${SURICATA}.yaml"
+    eval SURICATACONF=\$SURICATA_CONF_${SURICATAID}
+    [ ${#SURICATACONF} -eq 0 ] && SURICATACONF="${SURICATA_DIR}/suricata-${SURICATA}.yaml" || SURICATACONF="${SURICATA_DIR}/${SURICATACONF}"
     SURICATAPID="/var/run/suricata/suricata.${SURICATA}.pid"
     eval SURICATAOPTS=\$SURICATA_OPTS_${SURICATAID}
+    eval SURICATALOGPATH=\$SURICATA_LOG_PATH_${SURICATAID}
 else
-    SURICATACONF="${SURICATA_DIR}/suricata.yaml"
+    SURICATACONF=${SURICATA_CONF}
+    [ ${#SURICATACONF} -eq 0 ] && SURICATACONF="${SURICATA_DIR}/suricata.yaml" || SURICATACONF="${SURICATA_DIR}/${SURICATACONF}"
     SURICATAPID="/var/run/suricata/suricata.pid"
     SURICATAOPTS=${SURICATA_OPTS}
+    SURICATALOGPATH=${SURICATA_LOG_PATH}
 fi
 [ -e ${SURICATACONF} ] && SURICATAOPTS="${SURICATAOPTS} -c ${SURICATACONF}"
 
@@ -36,6 +40,14 @@ checkconfig() {
        if [ ! -d "/var/run/suricata" ] ; then
                checkpath -d /var/run/suricata
        fi
+       if [ ${#SURICATALOGPATH} -gt 0 ]; then
+               if [ ! -d "${SURICATALOGPATH}" ] ; then
+                       checkpath -d "${SURICATALOGPATH}"
+               fi
+               SURICATALOGFILE=${SURICATA_LOG_FILE:-suricata.log}
+               SURICATAOPTS="${SURICATAOPTS} --set logging.outputs.1.file.filename=${SURICATALOGPATH}/${SURICATALOGFILE}"
+               SURICATALOGPATH="-l ${SURICATALOGPATH}"
+       fi
 }
 
 initpidinfo() {
@@ -65,13 +77,19 @@ checkpidinfo() {
 
 start() {
        checkconfig || return 1
+       if [ $((SURICATA_DUMP)) -eq 1 ]; then
+           einfo "Dumping ${SVCNAME} config values and quitting."
+           ${SURICATA_BIN} --dump-config --pidfile ${SURICATAPID} ${SURICATAOPTS} ${SURICATALOGPATH}
+           einfo "You need to disable SURICATA_DUMP to start ${SVCNAME}."
+           return 1
+       fi
        ebegin "Starting ${SVCNAME}"
        start-stop-daemon --start --quiet --exec ${SURICATA_BIN} \
-               -- --pidfile ${SURICATAPID} -D ${SURICATAOPTS} >/dev/null 2>&1
+               -- --pidfile ${SURICATAPID} -D ${SURICATAOPTS} ${SURICATALOGPATH} >/dev/null 2>&1
        local SUR_EXIT=$?
        if [ $((SUR_EXIT)) -ne 0 ]; then
            einfo "Could not start ${SURICATA_BIN} with:"
-           einfo "--pidfile ${SURICATAPID} -D ${SURICATAOPTS}"
+           einfo "--pidfile ${SURICATAPID} -D ${SURICATAOPTS} ${SURICATALOGPATH}"
            einfo "Exit code ${SUR_EXIT}"
        fi
        eend ${SUR_EXIT}
@@ -87,7 +105,7 @@ stop() {
        while [ -e ${SURICATAPID} ] && [ $cnt -gt 0 ]; do
            cnt=$(expr $cnt - 1)
            sleep 1
-           einfo -ne "$cnt seconds left before we give up checking the PID file...\r"
+           echo -ne "$cnt seconds left before we give up checking the PID file...\r"
        done
        # under certain conditions suricata can be pretty slow and the PID can persist long after the pidfile has been removed
        # max wait for process to terminate: 1 minute
@@ -105,7 +123,7 @@ stop() {
                    break
                fi
                sleep 1
-               einfo -ne "$cnt seconds left before we give up checking PID ${SUR_PID}...\r"
+               echo -ne "$cnt seconds left before we give up checking PID ${SUR_PID}...\r"
                SUR_PID_CHECK="$(ps -eo pid | grep -c ${SUR_PID})"
            done
        fi