0
votes

I am trying to write a startup script that starts elastalert as a service. elastalert uses python and starts as follows:

python -m elastalert --verbose --rule rules_folder/frequency.yaml --config config.yaml

I've been trying to make it work with the following:

#!/bin/bash
# elastalert   startup script for elastalert
# pidfile:           /var/run/elastalert.pid
# chkconfig: 2345 99 01

NAME=elastalert
PIDFILE=/var/run/$NAME.pid
ELASTALERT_DIR=/elastalert/elastalert
ELASTALERT_USER=elastalert
CONFIG_FILE=$ELASTALERT_DIR/config.yaml
ELASTALERT=$ELASTALERT_DIR/$NAME

. /etc/rc.d/init.d/functions

case $1 in
   start)
      echo -n $"Starting $NAME: "
      cd $ELASTALERT_DIR
      daemon --pidfile="$PIDFILE" "$ELASTALERT --rule rules_folder/frequency.yaml --config $CONFIG_FILE &"
      RETVAL=$?
      pid=`ps -ef | grep python | grep elastalert | awk '{print $2}'`
      if [ -n "$pid" ]; then
        echo $pid > "$PIDFILE"
      fi
   ;;
       stop)
      echo -n $"Stopping $NAME: "
      killproc -p "$PIDFILE" -d 10 "$ELASTALERT"
      RETVAL="$?"
      echo
      [ $RETVAL = 0 ] && rm -f "$PIDFILE"
   ;;
   *)
      echo "Usage: /etc/init.d/elastalert {start|stop}" ;;
esac
exit 0

When I try to run the script I get the following error:

virtualenvelastalert) [root@mplinux scripts]# elastalert Traceback (most recent call last): File "/virtualenvelastalert/bin/elastalert", line 11, in load_entry_point('elastalert==0.0.95', 'console_scripts', 'elastalert')() File "/virtualenvelastalert/lib/python2.7/site-packages/elastalert-0.0.95-py2.7.egg/elastalert/elastalert.py", line 1426, in main client = ElastAlerter(args) File "/virtualenvelastalert/lib/python2.7/site-packages/elastalert-0.0.95-py2.7.egg/elastalert/elastalert.py", line 94, in init self.conf = load_rules(self.args) File "/virtualenvelastalert/lib/python2.7/site-packages/elastalert-0.0.95-py2.7.egg/elastalert/config.py", line 373, in load_rules conf = yaml_loader(filename) File "/virtualenvelastalert/lib/python2.7/site-packages/staticconf/loader.py", line 161, in yaml_loader with open(filename) as fh: IOError: [Errno 2] No such file or directory: 'config.yaml'

2

2 Answers

0
votes

I get that error message when attempting to start ElastAlert from the wrong directory. You do not need to pass in the location of the config.yaml as long as it is in the standard location. Assuming your directories you have defined are correct, try executing without the config flag and path to config:

--config $CONFIG_FILE
0
votes

Can you please try out this with service unit? There you'll have to write the Exec section with the command you want to execute