Skip to content

Instantly share code, notes, and snippets.

@mainframe
Last active September 13, 2021 01:54
Show Gist options
  • Save mainframe/5646082 to your computer and use it in GitHub Desktop.
Save mainframe/5646082 to your computer and use it in GitHub Desktop.
Install and configure zabbix agent on OpenNode or CentOS/RHEL host
#!/bin/bash
# Copyright (C) 2013, OpenNode LLC. All rights reserved.
# Email: info@opennodecloud.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# DESCRIPTION:
# Install and configure zabbix agent on OpenNode or CentOS/RHEL host
# Config
CONFIG_FILE=/etc/zabbix/zabbix_agentd.conf
ZBX_REPO_KEY=http://repo.zabbixzone.com/centos/RPM-GPG-KEY-zabbixzone
ZBX_REPO_RPM=http://repo.zabbixzone.com/centos/zabbixzone-release-0.0-1.noarch.rpm
usage()
{
echo 'Usage: on-zbx-agentinstall.sh -h -e -s <zabbix_server>'
cat << EOF
Options:
-h help
-e start and enable zabbix agent on boot
-s zabbix server hostname/ip
EOF
exit 1
}
chkZbxAgent()
{
[ -z "$(rpm -qa|grep zabbix-agent)" ] && ZBX_AGENT="no" || ZBX_AGENT="yes"
if [ "x${ZBX_AGENT}" = "xno" ]; then
# Check for EPEL zabbix20 version of agent package
if [ -z "$(rpm -qa|grep zabbix20-agent)" ]; then
ZBX_AGENT="no"
else
ZBX_AGENT="yes"
CONFIG_FILE=/etc/zabbix_agentd.conf
fi
fi
echo "Zabbix agent installed: ${ZBX_AGENT}"
}
chkZbxAgentVersion()
{
ZBX_AGENT_VERSION=`zabbix_agent -V | head -1 | awk '{ print $3 }'`
echo "Zabbix agent version: ${ZBX_AGENT_VERSION}"
echo "Zabbix agent config: ${CONFIG_FILE}"
}
chkOnHost()
{
[ -z "$(rpm -qa|grep opennode)" ] && ON_HOST="no" || ON_HOST="yes"
echo "OpenNode OS host: ${ON_HOST}"
}
setZbxServer()
{
echo "Setting Zabbix server to: ${ZBX_SERVER}"
sed -i "s/\(^Server *= *\).*/\1$ZBX_SERVER/" $CONFIG_FILE
}
setZbxServerActive()
{
echo "Setting Zabbix active checks server to: ${ZBX_SERVER}"
sed -i "s/\(^ServerActive *= *\).*/\1$ZBX_SERVER/" $CONFIG_FILE
}
setZbxAgentHostname()
{
echo "Setting Zabbix agent hostname to: ${HOSTNAME}"
sed -i "s/\(^Hostname *= *\).*/\1$HOSTNAME/" $CONFIG_FILE
}
installZbxAgent()
{
echo "Installing zabbix agent..."
yum install zabbix-agent -y
}
installZbxRepo()
{
echo "Installing zabbixzone yum repository..."
rpm --import $ZBX_REPO_KEY
rpm -Uv $ZBX_REPO_RPM
}
enableZbxAgent()
{
echo "Enabling Zabbix agent..."
service zabbix-agent restart && chkconfig zabbix-agent on
}
# MAIN
while getopts "hes:" opt; do
case $opt in
h)
usage >&2
;;
e)
ENABLE_AGENT=yes
;;
s)
ZBX_SERVER=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
;;
:)
echo "Option -$OPTARG requires an argument." >&2
usage
;;
esac
done
if [ -z "$ZBX_SERVER" ]; then
echo "Zabbix server name option missing!"
usage
exit 1
fi
chkZbxAgent
if [ "x${ZBX_AGENT}" = "xno" ]; then
chkOnHost
if [ "x${ON_HOST}" = "xno" ]; then
installZbxRepo
fi
installZbxAgent
fi
chkZbxAgentVersion
setZbxServer
setZbxServerActive
setZbxAgentHostname
if [ "x${ENABLE_AGENT}" = "xyes" ]; then
enableZbxAgent
fi
exit 0
@mainframe
Copy link
Author

wget -q -O - https://gist.github.com/mainframe/5646082/raw/on-zbx-agentinstall.sh > on-zbx-agentinstall.sh
chmod 700 on-zbx-agentinstall.sh
./on-zbx-agentinstall.sh -s 10.0.0.4 -e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment