Skip to content

Instantly share code, notes, and snippets.

@PowerWagon
Forked from SeonghoonKim/graylog2-bootstrap.sh
Last active November 11, 2017 01:57
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save PowerWagon/9694436 to your computer and use it in GitHub Desktop.
Save PowerWagon/9694436 to your computer and use it in GitHub Desktop.
Graylog2 0.20.1 install script for RHEL/OEL/CentOS 6.5
#!/bin/bash
# Created Fri Mar 21 2014
# This is a fork of https://gist.github.com/SeonghoonKim/4378896
# This script will download, install and start
# the following items on CentOS 6.5:
# MongoDB
# OpenJDK 1.7.0
# ElasticSearch 0.90.1
# Graylog2 server 0.20.1
# Graylog2 web interface 0.20.1
#
# This script should be safe to run more than one time. YMMV
# You can change the password for graylog2-web-interface, here:
PASSWD="password"
# Change the root install dir for elasticsearch and graylog2, here:
APP_ROOT="/opt"
# If all goes well, nothing should need to be changed below here
ORIG_WKDIR=$(pwd)
MONGODB_REPO="/etc/yum.repos.d/10gen-mongodb.repo"
CENTOS_REPO="/etc/yum.repos.d/CentOS-Base.repo"
EPEL_REPO="/etc/yum.repos.d/epel.repo"
[ -f "$APP_ROOT" ] || mkdir -p $APP_ROOT
if [ -f ${MONGODB_REPO} ]; then
echo "$MONGODB_REPO found"
else
cat << 'EOF' > ${MONGODB_REPO}
[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/$basearch
gpgcheck=0
EOF
fi
if [ -f ${CENTOS_REPO} ]; then
echo "$CENTOS_REPO found"
else
echo "Creating $CENTOS_REPO"
cat << 'EOF' > ${CENTOS_REPO}
# CentOS-Base.repo for RHEL6
[base]
name=CentOS-6 - Base
mirrorlist=http://mirrorlist.centos.org/?release=6&arch=$basearch&repo=os
#baseurl=http://mirror.centos.org/centos/6/os/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6
#released updates
[update]
name=CentOS-6 - Updates
mirrorlist=http://mirrorlist.centos.org/?release=6&arch=$basearch&repo=updates
#baseurl=http://mirror.centos.org/centos/6/updates/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6
EOF
fi
if [ -f ${EPEL_REPO} ]; then
echo "$EPEL_REPO found"
else
echo "Creating $EPEL_REPO"
cat << 'EOF' > ${EPEL_REPO}
[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=0
EOF
fi
echo "Stopping running services..."
service graylog2-server stop
service graylog2-web stop
service mongod stop
service elasticsearch stop
#killall java
MYRANDOM=$RANDOM
echo "Moving old files to $APP_ROOT/OLD_$MYRANDOM..."
mkdir $APP_ROOT/OLD_$MYRANDOM
mv $APP_ROOT/graylog2* $APP_ROOT/elasticsearch* $APP_ROOT/OLD_$MYRANDOM
rmdir $APP_ROOT/OLD_$MYRANDOM >/dev/null 2>&1
echo "Checking and/or getting files..."
if [ ! -d "$APP_ROOT/sources" ]; then mkdir "$APP_ROOT/sources"; fi
if [ ! -f "$APP_ROOT/sources/elasticsearch-0.90.10.noarch.rpm" ]; then
curl -O "$APP_ROOT/sources/elasticsearch-0.90.10.noarch.rpm" https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.10.noarch.rpm
fi
if [ ! -f "$APP_ROOT/sources/graylog2-server-0.20.1.tgz" ]; then
curl -O "$APP_ROOT/sources/graylog2-server-0.20.1.tgz" https://github.com/Graylog2/graylog2-server/releases/download/0.20.1/graylog2-server-0.20.1.tgz
fi
if [ ! -f "$APP_ROOT/sources/graylog2-web-interface-0.20.1.tgz" ]; then
curl -O "$APP_ROOT/sources/graylog2-web-interface-0.20.1.tgz" https://github.com/Graylog2/graylog2-web-interface/releases/download/0.20.1/graylog2-web-interface-0.20.1.tgz
fi
echo "installing openjdk 1.7.0 and Dev tools..."
yum -y install java-1.7.0-openjdk
#yum -y groupinstall "Development tools"
yum -y install pwgen
echo "Installing MongoDB..."
yum install -y mongo-10gen mongo-10gen-server
echo "Installing init script of MongoDB..."
chkconfig --level 2345 mongod on
echo "Starting MongoDB..."
service mongod start
echo "Installing ElasticSearch..."
yum -y localinstall "$APP_ROOT/sources/elasticsearch-0.90.10.noarch.rpm"
echo "Coniguring ElasticSearch for graylog2..."
sed -i -e 's|# cluster.name: elasticsearch|cluster.name: graylog2|' /etc/elasticsearch/elasticsearch.yml
sed -i '/# index.number_of_replicas: 0/a \
index.auto_expand_replicas: 0-all' /etc/elasticsearch/elasticsearch.yml
echo "Starting ElasticSearch..."
service elasticsearch start
echo "Installing graylog2-server..."
cd $APP_ROOT
tar -xf "$APP_ROOT/sources/graylog2-server-0.20.1.tgz"
ln -sf graylog2-server-0.20.1 graylog2-server
cd graylog2-server
cp graylog2.conf.example graylog2.conf
cd /etc
if [ ! -L /etc/graylog2.conf ]; then
mv /etc/graylog2.conf $APP_ROOT/OLD_$MYRANDOM 2> /dev/null
ln -sf $APP_ROOT/graylog2-server/graylog2.conf /etc/graylog2.conf
fi
echo "Changing graylog2 mongo db auth..."
sed -i -e 's/\(mongodb_useauth\ =\ \)true/\1false/' /etc/graylog2.conf
echo "Setting graylog2 password secret..."
APP_SECRET=$(pwgen -s 96)
sed -i -e 's|password_secret\ =|password_secret\ =\ '$APP_SECRET'|' /etc/graylog2.conf
echo "Setting graylog2 app password..."
APP_PASSWD=$(echo -n $PASSWD | sha256sum |awk '{ print $1 }' )
sed -i -e 's|root_password_sha2\ =|root_password_sha2\ =\ '$APP_PASSWD'|' /etc/graylog2.conf
echo "Installing init script of graylog2-server..."
cat << 'EOF' > /etc/init.d/graylog2-server
#! /bin/sh
### BEGIN INIT INFO
# Provides: graylog2-server
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Graylog2-server init script
# Description: Graylog2-server init script
### END INIT INFO
## Source function library.
. /etc/init.d/functions
NAME="graylog2-server"
GRAYLOG2_HOME=
GRAYLOG2_JAR=$GRAYLOG2_HOME/graylog2-server.jar
GRAYLOG2_CONF="/etc/graylog2.conf"
GRAYLOG2_OUT="/var/log/graylog2.log"
PID_DIR="/var/run/graylog2"
PID_FILE="${PID_DIR}/${NAME}.pid"
LOCK_FILE="/var/lock/subsys/${NAME}"
JAVA="/usr/bin/java"
JAVA_OPTS="-server -Xms512m -Xmx512m"
RUN_AS_USER=root
if [ ! -d "$PID_DIR" ]; then
mkdir "$PID_DIR"
chown $RUN_AS_USER "$PID_DIR"
fi
if [ ! -r "$GRAYLOG2_JAR" ]; then
echo "Cannot find $GRAYLOG2_JAR"
echo "${GRAYLOG2_JAR} is absent or does not have read permission"
exit 1
fi
touch "$GRAYLOG2_OUT"
chown $RUN_AS_USER "$GRAYLOG2_OUT"
start() {
echo "Starting $NAME: "
COMMAND="$JAVA $JAVA_OPTS -jar $GRAYLOG2_JAR -f $GRAYLOG2_CONF -p $PID_FILE >> \"$GRAYLOG2_OUT\" 2>&1 &"
daemon --check=${NAME} --user=${RUN_AS_USER} --pidfile=${PID_FILE} ${COMMAND}
RETVAL=$?
[ $RETVAL -eq 0 ] && touch $LOCK_FILE
return $RETVAL
}
stop() {
echo "Stopping $NAME: "
killproc -p ${PID_FILE} -d 5 ${NAME}
RETVAL=$?
[ $RETVAL -eq 0 ] && rm -f $LOCK_FILE
return $RETVAL
}
restart() {
stop
sleep 1
start
}
dump() {
echo "Dumping $NAME: "
PID=`cat $PID_FILE`
kill -3 $PID
RETVAL=$?
[ $RETVAL -eq 0 ] && success $"Dumped $NAME." || failure $"Failed to dump $NAME."
return $RETVAL
}
rh_status() {
status -p ${PID_FILE} ${NAME}
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
start
;;
stop)
rh_status_q || exit 0
stop
;;
restart)
restart
;;
status)
rh_status
;;
dump)
rh_status_q || exit 0
dump
;;
condrestart|try-restart)
rh_status_q || exit 0
restart
;;
*)
echo "Usage: $0 {start|stop|status|restart|condrestart|try-restart|dump}"
exit 1
esac
exit $?
EOF
sed -i -e "s|GRAYLOG2_HOME=|GRAYLOG2_HOME=$APP_ROOT/graylog2-server|" /etc/init.d/graylog2-server
chmod u+x /etc/init.d/graylog2-server
chkconfig graylog2-server on
echo "Starting graylog2-server..."
service graylog2-server start
echo "Installing graylog2-web-interface..."
cd $APP_ROOT
tar -xf "$APP_ROOT/sources/graylog2-web-interface-0.20.1.tgz"
ln -s graylog2-web-interface-0.20.1 graylog2-web-interface
cd graylog2-web-interface
APP_SECRET=$(pwgen -s 96)
sed -i -e 's|application.secret=""|application.secret="'$APP_SECRET'"|' conf/graylog2-web-interface.conf
sed -i -e 's|graylog2-server.uris=""|graylog2-server.uris="http://127\.0\.0\.1:12900/"|' conf/graylog2-web-interface.conf
cat << 'EOF' > /etc/init.d/graylog2-web
#! /bin/sh
### BEGIN INIT INFO
# Provides: graylog2-web
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Graylog2-web-interface init script
# Description: Graylog2-web-interface init script
### END INIT INFO
## Source function library.
. /etc/init.d/functions
NAME="graylog2-web-interface"
GRAYLOG2_WEB_HOME=
GRAYLOG2_WEB=$GRAYLOG2_WEB_HOME/bin/graylog2-web-interface
GRAYLOG2_WEB_OUT="/var/log/graylog2-web.log"
PID_FILE="$GRAYLOG2_WEB_HOME/RUNNING_PID"
LOCK_FILE="/var/lock/subsys/${NAME}"
JAVA="/usr/bin/java"
JAVA_OPTS="-Xms${mem}m -Xmx${mem}m -XX:MaxPermSize=${perm}m -XX:ReservedCodeCacheSize=${codecache}m"
RUN_AS_USER=root
if [ ! -r "$GRAYLOG2_WEB" ]; then
echo "Cannot find $GRAYLOG2_WEB"
echo "${GRAYLOG2_WEB} is absent or does not have read permission"
exit 1
fi
touch "$GRAYLOG2_WEB_OUT"
chown $RUN_AS_USER "$GRAYLOG2_WEB_OUT"
start() {
echo "Starting $NAME: "
COMMAND="$GRAYLOG2_WEB >> \"$GRAYLOG2_WEB_OUT\" 2>&1 &"
daemon --check=${NAME} --user=${RUN_AS_USER} --pidfile=${PID_FILE} ${COMMAND}
RETVAL=$?
[ $RETVAL -eq 0 ] && touch $LOCK_FILE
return $RETVAL
}
stop() {
echo "Stopping $NAME: "
killproc -p ${PID_FILE} -d 5 ${NAME}
RETVAL=$?
[ $RETVAL -eq 0 ] && rm -f $LOCK_FILE
return $RETVAL
}
restart() {
stop
sleep 1
start
}
dump() {
echo "Dumping $NAME: "
PID=`cat $PID_FILE`
kill -3 $PID
RETVAL=$?
[ $RETVAL -eq 0 ] && success $"Dumped $NAME." || failure $"Failed to dump $NAME."
return $RETVAL
}
rh_status() {
status -p ${PID_FILE} ${NAME}
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
start
;;
stop)
rh_status_q || exit 0
stop
;;
restart)
restart
;;
status)
rh_status
;;
dump)
rh_status_q || exit 0
dump
;;
condrestart|try-restart)
rh_status_q || exit 0
restart
;;
*)
echo "Usage: $0 {start|stop|status|restart|condrestart|try-restart|dump}"
exit 1
esac
exit $?
EOF
sed -i -e "s|GRAYLOG2_WEB_HOME=|GRAYLOG2_WEB_HOME=$APP_ROOT/graylog2-web-interface|" /etc/init.d/graylog2-web
chmod u+x /etc/init.d/graylog2-web
echo "Starting graylog2-web..."
chkconfig graylog2-web on
service graylog2-web start
IP_ADDR=$(ip a |grep inet\ |grep -v 127.0.0.1 |awk '{ print $2 }'| cut -d/ -f1)
echo
echo "Now, log into http://$IP_ADDR:9000/"
echo "with user name: admin and password: $PASSWD"
echo "If there were any, your old files should be in $APP_ROOT/OLD_$MYRANDOM"
echo
echo "You can now use the service command on:"
echo "mongod, elasticsearch, graylog2-server and graylog2-web"
echo "e.g. service graylog2-web restart"
echo
cd $ORIG_WKDIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment