zabbix agent 安装脚本

#!/bin/bash

#define color variables
RED=’\E[1;31m’
GRN=’\E[1;32m’
YEL=’\E[1;33m’
MAG=’\E[1;35m’
RES=’\E[0m’
#define environment variables
ZB_NAME=”zabbix”
ZB_VERSION=”3.2.4″
ZB_SOFT=”${ZB_NAME}-${ZB_VERSION}.tar.gz”
ZB_SOURCE_DIR=”/data/usr/src”
ZB_INSTALL_DIR=”/data/usr/local”
ZB_SOFT_DIR=”/data/soft”
ZB_CONF_FILE=”${ZB_INSTALL_DIR}/${ZB_NAME}/etc/zabbix_agentd.conf”
ZB_SER_IP=’10.26.83.53′
ZB_PUBLIC_DIR=”/mnt/zabbix”
ZB_LOG_DIR=”${ZB_PUBLIC_DIR}/zabbix_agentd.log”
ZB_PID_DIR=”${ZB_PUBLIC_DIR}/zabbix_agentd.pid”
ZB_PORT=’10050′
ZB_HOSTNAME=`hostname`
ZB_USER=`cat /etc/passwd |grep zabbix| wc -l`

check_dependency () {

NUM=`lsof -i :${ZB_PORT} | grep “LISTEN” | grep -v grep | wc -l`
if [ ${NUM} -ne 0 ];then
echo -e “${GRN}zabbix agent is install,${RES}”
exit 1
fi

if [ ${ZB_USER} -eq 0 ];then
useradd -s /sbin/nologin zabbix
fi
[ ${UID} -ne 0 ] && echo -e “${RED}need to be root to that${RES}” && exit 1
[ ! -d ${ZB_SOFT_DIR} ] && echo -e “${RED}no ${ZB_SOFT_DIR},Create a directory…${RES}” && mkdir -pv ${ZB_SOFT_DIR}
[ ! -f ${ZB_SOFT_DIR}/${ZB_SOFT} ] && echo -e “${RED}not fount file ${ZB_SOFT}… , please check…${RES}” && exit 1
[ ! -d ${ZB_SOURCE_DIR} ] && echo -e “${RED}no ${ZB_SOURCE_DIR},Create a directory…${RES}” && mkdir -pv ${ZB_SOURCE_DIR}
[ ! -d ${ZB_INSTALL_DIR} ] && echo -e “${RED}no ${ZB_INSTALL_DIR},Create a directory…${RES}” && mkdir -pv ${ZB_INSTALL_DIR}
[ ! -d ${ZB_PUBLIC_DIR} ] && echo -e “${RED}no ${ZB_PUBLIC_DIR},Create a directory…${RES}” && mkdir -pv ${ZB_PUBLIC_DIR} && chown -R ${ZB_NAME}.${ZB_NAME} ${ZB_PUBLIC_DIR}
PUBLIC_DIR_OMNER=`stat -c %G ${ZB_PUBLIC_DIR}`
PUBLIC_DIR_GROUP=`stat -c %G ${ZB_PUBLIC_DIR}`
if [ “${PUBLIC_DIR_OMNER}” != “${ZB_NAME}” ] || [ “${PUBLIC_DIR_GROUP}” != “${ZB_NAME}” ];then
chown -R ${ZB_NAME}.${ZB_NAME} ${ZB_PUBLIC_DIR}
fi
}

start_install () {
[ -d ${ZB_SOURCE_DIR}/${ZB_NAME}-${ZB_VERSION} ] && rm -rf ${ZB_SOURCE_DIR}/${ZB_NAME}-${ZB_VERSION}
[ -d ${ZB_INSTALL_DIR}/${ZB_NAME} ] && rm -rf ${ZB_INSTALL_DIR}/${ZB_NAME}
tar xf ${ZB_SOFT_DIR}/${ZB_SOFT} -C ${ZB_SOURCE_DIR}
cd ${ZB_SOURCE_DIR}/${ZB_NAME}-${ZB_VERSION}
if [ $? -eq 0 ];then
yum -y install gcc curl lsof
if [ $? -eq 0 ];then
./configure –prefix=${ZB_INSTALL_DIR}/${ZB_NAME} –enable-agent
if [ $? -eq 0 ];then
make
if [ $? -eq 0 ];then
make install
if [ $? -eq 0 ];then
echo -e “${GRN}zabbix agent install success${RES}”
else
echo -e “${RED}zabbix agent install faild,MAKE INSTALL faild,please check…${RES}”
exit 1
fi
else
echo -e “${RED}zabbix agent install faild,MAKE faild,please check…${RES}”
exit 1
fi
else
echo -e “${RED}zabbix agent install faild,CONFIGURE faild,please check…${RES}”
exit 1
fi
else
echo -e “${RED}yum set faild…,please check yum…${RES}”
exit 1
fi
else
echo -e “${RED}directory change faild or zabbix source directory not fount,please check…${RES}”
exit 1
fi
}

start_configure () {
# backup zabbix agent configure file
cp ${ZB_CONF_FILE} ${ZB_CONF_FILE}\-bak
sed -i “s#\# PidFile=/tmp/zabbix_agentd.pid#PidFile=${ZB_PID_DIR}#g” ${ZB_CONF_FILE}
sed -i “s#LogFile=/tmp/zabbix_agentd.log#LogFile=${ZB_LOG_DIR}#g” ${ZB_CONF_FILE}
sed -i “s#Server=127.0.0.1#Server=${ZB_SER_IP}#g” ${ZB_CONF_FILE}
sed -i “s#ServerActive=127.0.0.1#ServerActive=${ZB_SER_IP}#g” ${ZB_CONF_FILE}
sed -i “s#Hostname=Zabbix server#Hostname=${ZB_HOSTNAME}#g” ${ZB_CONF_FILE}
}

start_zabbix_agent () {
${ZB_INSTALL_DIR}/${ZB_NAME}/sbin/zabbix_agentd
NUM=`lsof -i :${ZB_PORT} | grep “LISTEN” | grep -v grep | wc -l`
if [ ${NUM} -ne 0 ];then
echo -e “${GRN}zabbix agent start success${RES}”
else
echo -e “${RED}zabbix agent start faild${RES}”
fi
}

check_dependency
start_install
start_configure
start_zabbix_agent

发表评论

电子邮件地址不会被公开。 必填项已用*标注