Skip to content

Commit a219da1

Browse files
update/patch elasticsearch container and metrics on server
1 parent 060f400 commit a219da1

File tree

4 files changed

+241
-0
lines changed

4 files changed

+241
-0
lines changed

scripts/deploy_elk_beats.sh

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/bin/bash
2+
3+
##################################################################################
4+
# Name : deploy__elk_beats.sh #
5+
# Author : Ibrahim Musayev #
6+
# Purpose : Downloading of beats rpm files from artifactory and installing #
7+
# them on servers by using sudo privileges in install_root.sh #
8+
# script. #
9+
# History : 18.02.22 Ibrahim Musayev, creation #
10+
##################################################################################
11+
12+
DATE=`date +'%Y%m%d'`
13+
TIMESTAMP=`date +'%Y%m%d_%H%M%S'`
14+
BEATS_SETUP=/opt/setup/beats
15+
OLD_VERSION_FILEBEAT=`/usr/share/filebeat/bin/filebeat version| awk '{print $3 }' 2>/dev/null`
16+
OLD_VERSION_METRICBEAT=`/usr/share/metricbeat/bin/metricbeat version| awk '{print $3 }' 2>/dev/null`
17+
LOGFILE=/opt/operations/logs/deploy_beats.log
18+
INSTALL_ROOT=/opt/source/custom/scripts/install_root.sh
19+
MASK=$1
20+
21+
if [ ! -d ${BEATS_SETUP} ]; then
22+
mkdir -p ${BEATS_SETUP}
23+
elif [ -d ${BEATS_SETUP} ] && [ $(ls "$BEATS_SETUP" | wc -l) -ne 0 ]; then
24+
rm -r ${BEATS_SETUP}/*
25+
fi
26+
27+
if [[ ! -d $(dirname ${LOGFILE}) ]]; then
28+
mkdir -p $(dirname ${LOGFILE})
29+
fi
30+
31+
cd $BEATS_SETUP
32+
echo "${TIMESTAMP} WARN Stop filebeat and metricbeat on all nodes with operate_services.sh." | tee -a ${LOGFILE}
33+
read -p 'Enter the APITOKEN: ' APITOKEN
34+
read -p 'Enter desired filebeat & metricbeat version: ' VERSION_BEATS
35+
36+
curl -H X-JFrog-Art-Api:${APITOKEN} -O -s "https://artifactory.$MASK/elastic/${VERSION_BEATS}/filebeat-${VERSION_BEATS}-x86_64.rpm" # masking the domain regarding security.
37+
curl -H X-JFrog-Art-Api:${APITOKEN} -O -s "https://artifactory.$MASK/elastic/${VERSION_BEATS}/metricbeat-${VERSION_BEATS}-x86_64.rpm" # masking the domain regarding security.
38+
39+
if [ $(ls "$BEATS_SETUP" | wc -l) -ne 0 ]; then
40+
echo "${TIMESTAMP} INFO filebeat-${VERSION_BEATS} and metricbeat-${VERSION_BEATS} downloads completed." | tee -a ${LOGFILE}
41+
else
42+
echo "${TIMESTAMP} ERROR Something went wrong. Please retry to download update again!" | tee -a ${LOGFILE}
43+
exit 1
44+
fi
45+
46+
if [[ "${VERSION_BEATS}" == "${OLD_VERSION_FILEBEAT}" ]]; then
47+
echo "${TIMESTAMP} INFO filebeat-${VERSION_BEATS} is already installed. Nothing to do. Complete!" | tee -a ${LOGFILE}
48+
exit 0
49+
else
50+
sudo ${INSTALL_ROOT} install_filebeat
51+
fi
52+
53+
if [[ "${VERSION_BEATS}" == "${OLD_VERSION_METRICBEAT}" ]]; then
54+
echo "${TIMESTAMP} INFO metricbeat-${VERSION_BEATS} is already installed. Nothing to do. Complete!" | tee -a ${LOGFILE}
55+
exit 0
56+
else
57+
sudo ${INSTALL_ROOT} install_metricbeat
58+
fi
59+
60+
CHECK_FILEBEAT_VERSION=`/usr/share/filebeat/bin/filebeat version| awk '{print $3 }' 2>/dev/null`
61+
CHECK_METRICBEAT_VERSION=`/usr/share/metricbeat/bin/metricbeat version| awk '{print $3 }' 2>/dev/null`
62+
TIMESTAMP=`date +'%Y%m%d_%H%M%S'`
63+
64+
if [[ "${CHECK_FILEBEAT_VERSION}" == "${VERSION_BEATS}" ]]; then
65+
echo "${TIMESTAMP} INFO filebeat-${VERSION_BEATS} is successfully installed."
66+
else
67+
echo "${TIMESTAMP} ERROR Fatal error during filebeat-${VERSION_BEATS} installation. Exiting..." | tee -a ${LOGFILE}
68+
exit 1
69+
fi
70+
71+
if [[ "${CHECK_METRICBEAT_VERSION}" == "${VERSION_BEATS}" ]]; then
72+
echo "${TIMESTAMP} INFO metricbeat-${VERSION_BEATS} is successfully installed."
73+
else
74+
echo "${TIMESTAMP} ERROR Fatal error during metricbeat-${VERSION_BEATS} installation. Exiting..." | tee -a ${LOGFILE}
75+
exit 1
76+
fi
77+
78+
EXITCODE=$?
79+
TIMESTAMP=`date +'%Y%m%d_%H%M%S'`
80+
if [[ ${EXITCODE} -ne 0 ]]; then
81+
echo "${TIMESTAMP} ERROR Got exit code ${EXITCODE} on installation. Exiting ..." | tee -a ${LOGFILE}
82+
exit 1
83+
else
84+
echo "${TIMESTAMP} INFO Starting to copy parameters in installation folders" | tee -a ${LOGFILE}
85+
sudo ${INSTALL_ROOT} sync_source ${ENV} ${VERSION_BEATS}
86+
fi
87+
88+
exit 0

scripts/install_root.sh

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/bin/bash
2+
3+
# Purpose:
4+
# wrapper script for install commands that require a root shell
5+
# the script should support a list of options to run
6+
7+
echo "Running with root shell"
8+
echo "Arguments given for options $1 $@"
9+
10+
OPS_CONF="/opt/operations/conf/opt_elastic.conf"
11+
if [[ -f ${OPS_CONF} ]]; then
12+
. ${OPS_CONF}
13+
else
14+
echo "ERROR: Could not find ${OPS_CONF}"
15+
exit 99
16+
fi
17+
18+
DATE=`date +'%Y%m%d'`
19+
ELK_ROOT=${GLOBAL_ELK_YAML_DIR}
20+
BEATS_SETUP=${GLOBAL_BEATS_SETUP_DIR}
21+
ENV=$2
22+
NEW_VERSION_BEATS=$3
23+
24+
case $1 in
25+
install_filebeat)
26+
yum install -y ${BEATS_SETUP}/filebeat-*.rpm 2>/dev/null
27+
;;
28+
29+
install_metricbeat)
30+
yum install -y ${BEATS_SETUP}/metricbeat-*.rpm 2>/dev/null
31+
;;
32+
33+
sync_source) # copy/change parameters and ownerships and restart services
34+
cd /etc/filebeat
35+
mv filebeat.yml.rpmnew filebeat.yml.${NEW_VERSION_BEATS} 2>/dev/null
36+
cp -p ${ELK_ROOT}/filebeat.yml filebeat.yml.server 2>/dev/null
37+
38+
if [[ "${ENV}" == "dev"]]; then
39+
cp -p ${ELK_ROOT}/filebeat_${ENV}.yml /etc/filebeat/filebeat.yml.server 2>/dev/null
40+
elif [[ "${ENV}" == "prod"]]; then
41+
cp -p ${ELK_ROOT}/filebeat_${ENV}.yml /etc/filebeat/filebeat.yml.server 2>/dev/null
42+
fi
43+
44+
chown root:root filebeat.yml.server 2>/dev/null
45+
chmod 600 filebeat.yml.server 2>/dev/null
46+
cp -p filebeat.yml.server filebeat.yml 2>/dev/null
47+
cd /etc/metricbeat/
48+
mv metricbeat.yml.rpmnew metricbeat.yml.${NEW_VERSION_BEATS} 2>/dev/null
49+
cp -p ${ELK_ROOT}/metricbeat_${ENV}.yml metricbeat.yml.server 2>/dev/null
50+
# change ownerships and restart services
51+
chown root:root metricbeat.yml.server 2>/dev/null
52+
chmod 600 metricbeat.yml.server 2>/dev/null
53+
cp -p metricbeat.yml.server metricbeat.yml 2>/dev/null
54+
systemctl restart filebeat.service
55+
systemctl restart metricbeat.service
56+
;;
57+
58+
*) # unsupported arguments for operation provided
59+
echo -e "\n unsupported arguments for operation provided."
60+
help
61+
;;
62+
63+
esac
64+
65+
exit 0

scripts/opt_elastic.conf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#! /bin/ksh
2+
3+
#
4+
# install_root.sh
5+
#
6+
7+
GLOBAL_ELK_YAML_DIR="/opt/operations/elastic"
8+
GLOBAL_BEATS_SETUP_DIR="/opt/setup/beats"
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/bin/bash
2+
3+
#################################################################################
4+
# Name : update_elastic_container.sh #
5+
# Author : Ibrahim Musayev #
6+
# Purpose : Automating the setup of docker elastic container #
7+
# Prerequisites : create elk data snapshots, #
8+
# define repository and policy and run an index snapshot #
9+
# History : 08.02.22 Ibrahim Musayev, creation #
10+
#################################################################################
11+
12+
DATE=`date +'%Y%m%d'`
13+
TIMESTAMP=`date +'%Y%m%d_%H%M%S'`
14+
OPT_SETUP=/opt/setup/elastic
15+
LOGFILE=/opt/operations/logs/update_elastic_container.log
16+
17+
if [ ! -d ${OPT_SETUP} ]; then
18+
mkdir -p ${OPT_SETUP}
19+
elif [ -d ${OPT_SETUP} ] && [ $(ls "$OPT_SETUP" | wc -l) -ne 0 ]; then
20+
rm -r ${OPT_SETUP}/* 2>/dev/null
21+
fi
22+
23+
if [[ ! -d $(dirname ${LOGFILE}) ]]; then
24+
mkdir -p $(dirname ${LOGFILE})
25+
fi
26+
27+
cd $OPT_SETUP
28+
29+
echo "${TIMESTAMP} WARN Stop filebeat and metricbeat on all nodes with operate_services.sh." | tee -a ${LOGFILE}
30+
read -p 'Enter filebeat & metricbeat version: ' VERSION_BEATS
31+
32+
function downloadElk {
33+
wget "https://artifactory.wdt.six-group.net:443/artifactory/cscao-generic-release-remote-cache/docker/elk_${VERSION_BEATS}.tar.gz"
34+
gzip -d elk_${VERSION_BEATS}.tar.gz
35+
36+
if [ $(ls -l "$OPT_SETUP" | wc -l) -ne 0 ]; then
37+
echo "${TIMESTAMP} INFO RPM files are downloaded." | tee -a ${LOGFILE}
38+
else
39+
echo "${TIMESTAMP} ERROR Downloads are not completed. Please try manually!" | tee -a ${LOGFILE}
40+
exit 1
41+
fi
42+
}
43+
44+
function loadNewImage {
45+
docker stop elk
46+
docker container rm -v $(docker ps -aqf "name=elk") 2>/dev/null
47+
docker load -i ${OPT_SETUP}/elk_${VERSION_BEATS}.tar 2>/dev/null
48+
EXITCODE=$?
49+
}
50+
51+
function runNewContainer {
52+
if [[ ${EXITCODE} -ne 0 ]]; then
53+
echo "`date +'%Y%m%d_%H%M%S'` ERROR Got exit code ${EXITCODE} on docker load. Exiting ..." | tee -a ${LOGFILE}
54+
exit 1
55+
else
56+
TIMESTAMP=`date +'%Y%m%d_%H%M%S'`
57+
echo "${TIMESTAMP} INFO Container build is being loaded..." | tee -a ${LOGFILE}
58+
docker run --name elk -d --restart=always -p 4560:4560 -it -v /opt/source/docker/bsicrm-json.conf:/etc/logstash/conf.d/05-bsicrm-json.conf -v /opt/source/docker/kibana.yml:/opt/kibana/config/kibana.yml -v /opt/data/elasticsearch:/var/log/elasticsearch sebp/elk:elk_${VERSION_BEATS}
59+
echo "${TIMESTAMP} INFO Container customization is being handled..." | tee -a ${LOGFILE}
60+
echo -e "-Xms4g\n-Xmx4g" > /opt/source/docker/heap.options
61+
docker cp /opt/source/docker/heap.options elk:/etc/elasticsearch/jvm.options.d/
62+
docker exec elk sed -i '0,/#bootstrap.memory_lock/s//bootstrap.memory_lock/' /etc/elasticsearch/elasticsearch.yml
63+
docker exec elk sed -i 's|/var/backups|/var/log/elasticsearch|' /etc/elasticsearch/elasticsearch.yml
64+
EXITCODE=$?
65+
fi
66+
67+
TIMESTAMP=`date +'%Y%m%d_%H%M%S'`
68+
if [[ ${EXITCODE} -ne 0 ]]; then
69+
echo "${TIMESTAMP} ERROR Got exit code ${EXITCODE} on container build setup. Exiting ..." | tee -a ${LOGFILE}
70+
exit 1
71+
else
72+
echo "${TIMESTAMP} INFO Container build setup is successfully completed. Please continue with defined Post Action tasks" | tee -a ${LOGFILE}
73+
echo "${TIMESTAMP} INFO Update metricbeat and filebeat to release ${VERSION_BEATS}"
74+
exit 0
75+
fi
76+
}
77+
78+
downloadElk
79+
loadNewImage
80+
runNewContainer

0 commit comments

Comments
 (0)