Skip to content

Instantly share code, notes, and snippets.

@wittling
Created November 8, 2017 17:16
Show Gist options
  • Save wittling/b8ad09ac909cdce2eb70f246ad442c7d to your computer and use it in GitHub Desktop.
Save wittling/b8ad09ac909cdce2eb70f246ad442c7d to your computer and use it in GitHub Desktop.
user-data.sh - customized
#!/bin/sh
# The orchestrator sets these dynamically at runtime. Do not set them here.
# In fact, if you attempt to override them by setting them here, the parameter
# is not replaced - but in fact is written twice over (i.e. TIMEZONE=CDTEDT, USERNAME=adminadmin) in the VM.
#
export MONITORING_IP=
export TIMEZONE=
export BROKER_IP=
export BROKER_PORT=
export USERNAME=
export PASSWORD=
export EXCHANGE_NAME=
export EMS_HEARTBEAT=
export EMS_AUTODELETE=
export EMS_VERSION=
export #Hostname=
export ENDPOINT=
# Hostname/IP and path of the EMS repository
export UBUNTU_EMS_REPOSITORY_HOSTNAME_OR_IP="get.openbaton.org"
export UBUNTU_EMS_REPOSITORY_PATH="repos/apt/debian/"
export CENTOS_EMS_REPOSITORY_HOSTNAME_OR_IP="get.openbaton.org"
export CENTOS_EMS_REPOSITORY_PATH="repos/rpm/"
export OS_DISTRIBUTION_RELEASE_MAJOR=7
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
export LC_COLLATE=C
export LC_CTYPE=en_US.UTF-8
source /etc/bashrc
################
#### Ubuntu ####
################
install_ems_on_ubuntu () {
result=$(dpkg -l | grep "ems" | grep -i "open baton\|openbaton" | wc -l)
if [ ${result} -eq 0 ]; then
echo "Downloading EMS from ${UBUNTU_EMS_REPOSITORY_HOSTNAME_OR_IP}"
echo "deb http://${UBUNTU_EMS_REPOSITORY_HOSTNAME_OR_IP}/${UBUNTU_EMS_REPOSITORY_PATH} ems main" >> /etc/apt/sources.list
wget -O - http://get.openbaton.org/public.gpg.key | apt-key add -
apt-get update
cp /usr/share/zoneinfo/$TIMEZONE /etc/localtime
apt-get install -y git
apt-get install -y --force-yes ems-$EMS_VERSION
else
echo "EMS is already installed"
fi
}
install_zabbix_on_ubuntu () {
result=$(dpkg -l | grep "zabbix-agent" | wc -l)
if [ ${result} -eq 0 ]; then
echo "Installing zabbix-agent for server at $MONITORING_IP"
apt-get install -y zabbix-agent
else
echo "Zabbix-agent is already installed"
fi
}
################
#### CentOS ####
################
install_ems_on_centos () {
result=$(yum list installed | grep "ems" | grep -i "open baton\|openbaton" | wc -l)
if [ ${result} -eq 0 ]; then
echo "Downloading EMS from ${CENTOS_EMS_REPOSITORY_HOSTNAME_OR_IP}"
echo "[openbaton]" >> /etc/yum.repos.d/OpenBaton.repo
echo "name=Open Baton Repository" >> /etc/yum.repos.d/OpenBaton.repo
echo "baseurl=http://${CENTOS_EMS_REPOSITORY_HOSTNAME_OR_IP}/${CENTOS_EMS_REPOSITORY_PATH}" >> /etc/yum.repos.d/OpenBaton.repo
echo "gpgcheck=0" >> /etc/yum.repos.d/OpenBaton.repo
echo "enabled=1" >> /etc/yum.repos.d/OpenBaton.repo
cp /usr/share/zoneinfo/$TIMEZONE /etc/localtime
yum install -y git
yum install -y ems
systemctl enable ems
#systemctl start ems
else
echo "EMS is already installed"
fi
}
install_zabbix_on_centos () {
result=$( yum list installed | grep zabbix-agent | wc -l )
if [ ${result} -ne 0 ]; then
logger "Zabbix-agent is already installed"
logger "Removing legacy zabbix packages"
yum -y remove zabbix*
# We could check return code above but lets make double sure
rpm -qa | grep -i zabbix
if [ $? -eq 0 ]; then
logger "ERR:Failed to remove prior zabbix packages"
exit 1
else
logger "INFO:Prior Zabbix packages removed"
fi
fi
# The 3.2 is a hard-code based on the version of server we run. Probably should be at least a variable
# if not determined at runtime somehow.
rpm -ivh http://repo.zabbix.com/zabbix/3.2/rhel/${OS_DISTRIBUTION_RELEASE_MAJOR}/x86_64/zabbix-release-3.2-1.el7.noarch.rpm
logger "INFO:Installing zabbix-agent"
yum -y install zabbix-agent
rpm -qa | grep -i zabbix-agent
if [ $? -ne 0 ]; then
logger "ERR:Failed to install new version of zabbix agent"
exit 1
fi
}
#############
#### EMS ####
#############
configure_ems () {
mkdir -p /etc/openbaton/ems
echo [ems] > /etc/openbaton/ems/conf.ini
echo broker_ip=$BROKER_IP >> /etc/openbaton/ems/conf.ini
echo broker_port=$BROKER_PORT >> /etc/openbaton/ems/conf.ini
echo username=$USERNAME >> /etc/openbaton/ems/conf.ini
echo password=$PASSWORD >> /etc/openbaton/ems/conf.ini
echo exchange=$EXCHANGE_NAME >> /etc/openbaton/ems/conf.ini
echo heartbeat=$EMS_HEARTBEAT >> /etc/openbaton/ems/conf.ini
echo autodelete=$EMS_AUTODELETE >> /etc/openbaton/ems/conf.ini
echo type=$ENDPOINT >> /etc/openbaton/ems/conf.ini
echo hostname=$Hostname >> /etc/openbaton/ems/conf.ini
service ems restart
}
################
#### Zabbix ####
################
configure_zabbix () {
logger "user-data.sh:INFO:Configuring Zabbix"
ZABX_AGNT_CONF=/etc/zabbix/zabbix_agentd.conf
ZABX_AGNT_LOG=/var/log/zabbix/zabbix_agentd.log
ZABBIXCLEAN=true
# Back up the config file
if [ -f ${ZABX_AGNT_CONF} ]; then
DIRNM=`dirname ${ZABX_AGNT_CONF}`
FLNM=`basename ${ZABX_AGNT_CONF}`
pushd ${DIRNM}
cp ${FLNM} ${FLNM}.bak
popd
else
logger "ERR:FileNotExists:Zabbix Config File ${ZABX_AGNT_CONF}"
exit 1
fi
# just in case the installation launches it.
systemctl stop zabbix-agent
# They have this sed statement, which looks similar to one in the cloudinit script. We will disable both and
# use our own.
#sed -i -e "s|ServerActive=127.0.0.1|ServerActive=${MONITORING_IP}:10051|g" -e "s|Server=127.0.0.1|Server=${MONITORING_IP}|g" -e "s|Hostname=Zabbix server|#Hostname=|g" /etc/zabbix/zabbix_agentd.conf
# Based on testing a new deployment uncomments and sets this parameter - assumes active by default.
# We will assume that the VM needs to be an active agent and not a passive agent.
(sed -i 's+^ServerActive=127\.0\.0\.1+#&\nServerActive='"${MONITORING_IP}"'+' ${ZABX_AGNT_CONF})
if [ $? -ne 0 ]; then
logger "ERR:Error configuring zabbix server active parm in zabbix agent"
ZABBIXCLEAN=false
fi
# A brand spanking new deployment always has the Zabbix Server directive uncommented and set to local host.
# We will need to set that to the appropriate server.
(sed -i 's+^Server=127\.0\.0\.1+#&\nServer='"${MONITORING_IP}"'+' ${ZABX_AGNT_CONF})
if [ $? -ne 0 ]; then
logger "ERR:Error configuring zabbix server parm in zabbix agent"
ZABBIXCLEAN=false
fi
# Hostname is an env var but at this stage it should be set and we can use the command.
(HOSTNAME=`hostname` && sed -i 's+^Hostname=Zabbix [s|S]erver+Hostname='$HOSTNAME'+' ${ZABX_AGNT_CONF})
# We could also just pound it out and let it use system hostname. That is an option also.
# (sed -i 's+^Hostname+# &+' $ZABX_AGNT_CONF})
if [ $? -ne 0 ]; then
logger "ERR:Error configuring zabbix hostname"
ZABBIXCLEAN=false
fi
# During testing we found that if we do not set the ListenIP and or SourceIP, the IP address comes in as the
# external GW IP OpenStack router IP for that port. The net effect of this is that the Zabbix server starts
# complaining about the fact that it cannot reach agents at that router port IP address.
# Which means no monitoring happens.
#
# NOTE: We tested this with Discovery turned OFF and an Auto Registration Action rule that adds the hosts upon
# registration and adds them to Linux and VM groups so that the templates get applied that enable monitoring.
# AES will not work without these steps taken!!!
LISTENIP=`hostname -I`
(sed -i 's+^#*.ListenIP=0\.0\.0\.0*.$+&\nListenIP='"${LISTENIP}"'+' ${ZABX_AGNT_CONF})
if [ $? -ne 0 ]; then
logger "ERR:Error configuring zabbix ListenIP paramater"
ZABBIXCLEAN=false
fi
(sed -i 's+^#*.SourceIP=0\.0\.0\.0*.$+&\SourceIP='"${LISTENIP}"'+' ${ZABX_AGNT_CONF})
if [ $? -ne 0 ]; then
logger "ERR:Error configuring zabbix SourceIP paramater"
ZABBIXCLEAN=false
fi
# If we cannot ping but got the variable we will assume maybe server is down and stay legitimate w a warning.
ping -c 10 -q ${MONITORING_IP}
if [ $? -ne 0 ]; then
logger "WARN:Could not ping zabbix server at ${MONITORING_IP}!"
fi
if [ ${ZABBIXCLEAN} ]; then
logger "INFO:Zabbix looks clean. Starting agent."
# If SELinux is enforcing you may need to kick a policy in to get agent to run.
# This requires the policycoreutils-python package.
semanage permissive -a zabbix_agent_t
if [ $? -ne 0 ]; then
logger "WARN:Zabbix Agent may not start because SELinux may prevent it!"
fi
systemctl enable zabbix-agent
systemctl start zabbix-agent
# I do not know why zabbix agent sometimes starts with a host not found. If it does, kick it. Usually fixes it.
sleep 10
grep -i "host not found" ${ZABBIX_AGNT_LOG}
if [ $? -eq 0 ]; then
systemctl stop zabbix-agent
sleep 3
logger "" > ${ZABBIX_AGNT_LOG}
systemctl start zabbix-agent
fi
else
logger "WARN:Not starting Zabbix Agent due to configuration errors."
exit 1
fi
}
##############
#### Main ####
##############
if [ $(cat /etc/os-release | grep -i "ubuntu" | wc -l) -gt 0 ]; then
os=ubuntu
elif [ $(cat /etc/os-release | grep -i "centos" | wc -l) -gt 0 ]; then
os=centos
else
os=undefined
fi
case ${os} in
ubuntu)
install_ems_on_ubuntu
if [ -z "${MONITORING_IP}" ]; then
echo "No MONITORING_IP is defined, I will not download zabbix-agent"
else
install_zabbix_on_ubuntu
fi
;;
centos)
logger "user-data.sh:INFO:Installing ems on centos"
install_ems_on_centos
if [ -z "${MONITORING_IP}" ]; then
logger "No MONITORING_IP is defined, I will not download zabbix-agent"
else
#yum install -y */lsb-release
#OS_DISTRIBUTION_RELEASE_MAJOR=$( lsb_release -a | grep "Release:" | awk -F'\t' '{ print $2 }' | awk -F'.' '{ print $1 }' )
logger "user-data.sh:INFO:Installing zabbix on centos"
install_zabbix_on_centos
fi
;;
*)
echo "OS not recognized"
exit 1
;;
esac
logger "user-data.sh:INFO:configuring ems"
configure_ems
if [ -n "${MONITORING_IP}" ]; then
logger "user-data.sh:INFO:configuring zabbix"
configure_zabbix
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment