From 42f1e8817885123171ea7ef698cad1eaaf5cd1cc Mon Sep 17 00:00:00 2001
From: Erick Hitter <git-contrib@ethitter.com>
Date: Sun, 16 Jul 2017 13:31:52 -0700
Subject: [PATCH] Init script and updated readme

---
 README.md | 25 +++++++++-------
 defaults  |  3 ++
 init.sh   | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 105 insertions(+), 10 deletions(-)
 create mode 100644 defaults
 create mode 100755 init.sh

diff --git a/README.md b/README.md
index 9995f57..47b68b5 100644
--- a/README.md
+++ b/README.md
@@ -3,15 +3,20 @@ Log Alerting
 
 Pipe logs to Mattermost (or Slack) webhooks
 
-# Requirements
+## Requirements
+
 * Go 1.8.3
 
-# Installation
-1. TBD
-<!-- 1. `git clone https://git.ethitter.com/debian/eth-log-alerting.git /usr/local/eth-log-alerting`
-2. `cp /usr/local/eth-log-alerting/systemd.service /etc/systemd/system/eth_log_alerting.service`
-3. `cp /usr/local/eth-log-alerting/defaults /etc/default/eth_log_alerting`
-4. Edit `/etc/default/eth_log_alerting`
-5. `systemctl daemon-reload`
-6. `systemctl enable eth_log_alerting.service`
-6. `systemctl start eth_log_alerting.service` -->
+## Installation
+
+1. `git clone https://git.ethitter.com/debian/eth-log-alerting.git /usr/local/bin/eth-log-alerting`
+1. `cd /usr/local/bin/eth-log-alerting`
+1. `go build eth-log-alerting.go`
+1. `cp /usr/local/bin/eth-log-alerting/init.sh /etc/init.d/eth-log-alerting`
+1. `chmod +x /etc/init.d/eth-log-alerting`
+1. `cp /usr/local/bin/eth-log-alerting/config-sample.json /usr/local/bin/eth-log-alerting/config.json`
+1. Edit `/usr/local/bin/eth-log-alerting/config.json`
+1. `cp /usr/local/bin/eth-log-alerting/defaults /etc/default/eth_log_alerting`
+1. Edit `/etc/default/eth_log_alerting`
+1. `update-rc.d eth-log-alerting defaults`
+1. `/etc/init.d/eth-log-alerting start`
diff --git a/defaults b/defaults
new file mode 100644
index 0000000..b39a993
--- /dev/null
+++ b/defaults
@@ -0,0 +1,3 @@
+# DAEMON="/usr/local/bin/eth-log-alerting/eth-log-alerting"
+# DAEMON_ARGS="-config /usr/local/bin/eth-log-alerting/config.json"
+# USER="www-data"
diff --git a/init.sh b/init.sh
new file mode 100755
index 0000000..68a58b4
--- /dev/null
+++ b/init.sh
@@ -0,0 +1,87 @@
+#!/bin/sh
+### BEGIN INIT INFO
+# Provides:          eth-log-alerting
+# Required-Start:    $network $local_fs
+# Required-Stop:     $network $local_fs
+# Default-Start:     2 3 4 5
+# Default-Stop:      0 1 6
+# Short-Description: Log alerting via Mattermost
+# Description:       Pipe myriad logs to Mattermost webhooks
+### END INIT INFO
+
+. /lib/lsb/init-functions
+
+NAME=eth-log-alerting
+USER=root
+DAEMON="/usr/local/bin/${NAME}/${NAME}"
+DAEMON_ARGS="-config /usr/local/bin/${NAME}/config.json"
+PIDFILE="/var/run/${NAME}.pid"
+
+# Overrides
+[ -f "/etc/default/$NAME" ] && . /etc/default/$NAME
+
+# If the daemon is not there, then exit.
+test -x $DAEMON || exit 5
+
+case $1 in
+ start)
+  # Checked the PID file exists and check the actual status of process
+  if [ -e $PIDFILE ]; then
+   status_of_proc -p $PIDFILE $DAEMON "$NAME process" && status="0" || status="$?"
+   # If the status is SUCCESS then don't need to start again.
+   if [ $status = "0" ]; then
+    exit # Exit
+   fi
+  fi
+  # Start the daemon.
+  log_daemon_msg "Starting the process" "$NAME"
+  # Start the daemon with the help of start-stop-daemon
+  # Log the message appropriately
+  if start-stop-daemon --start --chuid $USER --background --oknodo --pidfile $PIDFILE --make-pidfile --exec $DAEMON -- "$DAEMON_ARGS"; then
+   log_end_msg 0
+  else
+   log_end_msg 1
+  fi
+  ;;
+ stop)
+  # Stop the daemon.
+  if [ -e $PIDFILE ]; then
+   status_of_proc -p $PIDFILE $DAEMON "Stoppping the $NAME process" && status="0" || status="$?"
+   if [ "$status" = 0 ]; then
+    start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
+    /bin/rm -rf $PIDFILE
+   fi
+  else
+   log_daemon_msg "$NAME process is not running"
+   log_end_msg 0
+  fi
+  ;;
+ restart)
+  # Restart the daemon.
+  $0 stop && sleep 2 && $0 start
+  ;;
+ status)
+  # Check the status of the process.
+  if [ -e $PIDFILE ]; then
+   status_of_proc -p $PIDFILE $DAEMON "$NAME process" && exit 0 || exit $?
+  else
+   log_daemon_msg "$NAME Process is not running"
+   log_end_msg 0
+  fi
+  ;;
+ reload)
+  # Reload the process. Basically sending some signal to a daemon to reload
+  # it configurations.
+  if [ -e $PIDFILE ]; then
+   start-stop-daemon --stop --signal USR1 --quiet --pidfile $PIDFILE --name $NAME
+   log_success_msg "$NAME process reloaded successfully"
+  else
+   log_failure_msg "$PIDFILE does not exists"
+  fi
+  ;;
+ *)
+  # For invalid arguments, print the usage message.
+  echo "Usage: $0 {start|stop|restart|reload|status}"
+  exit 2
+  ;;
+esac
-- 
GitLab