美文网首页
zabbix agent安装脚本

zabbix agent安装脚本

作者: Daisy小朋友 | 来源:发表于2019-05-17 15:45 被阅读0次

此脚本适用于centos6/7
并且主机PSK加密设置,TCP连接数设置

#!/usr/bin/env bash
#zabbix-agent install scripts
#This script only works with Centos6 and Centos7
#author:daisy 
#date:20190422

#get intranet IP
hostname=`ip address |grep inet |grep -v inet6 |grep -v 127.0.0.1 |awk '{print $2}' |awk -F "/" '{print $1}'`
#get extranet Ip
#hostname=`curl ip.cip.cc`

HOST=`hostname`
DATE=`date +%s`
#zabbix server IP
ip='xx.xx.xx.xx:10091'  
#ip='xx.xx.xx.xx:10091'
#get os_version
OS=`rpm -qa|grep centos-release>>os.txt`
OS_VERSION=`cat os.txt|awk -F "-" '{print $3}'`

##TCP connections func
function TCP_connections()
{
 touch /tmp/netstat.tmp
 #monitor script
 cat << EOF > /etc/zabbix/zabbix_agentd.d/zabbix_linux_plugin.sh
#!/bin/bash
tcp_status_fun(){
    TCP_STAT=\$1
    #netstat -n | awk '/^tcp/ {++state[\$NF]} END {for(key in state) print key,state[key]}' > /tmp/netstat.tmp
    ss -ant | awk 'NR>1 {++s[\$1]} END {for(k in s) print k,s[k]}' > /tmp/netstat.tmp
    TCP_STAT_VALUE=\$(grep "\$TCP_STAT" /tmp/netstat.tmp | cut -d ' ' -f2)
    if [ -z \$TCP_STAT_VALUE ];then
        TCP_STAT_VALUE=0
    fi
    echo \$TCP_STAT_VALUE
}
main(){
    case \$1 in
        tcp_status)
            tcp_status_fun \$2;
            ;;

        *)
            echo $"Usage: \$0 {tcp_status key}"
    esac
}
main \$1 \$2 \$3
EOF
## add zabbix_agent parameter
 cat << EOF > /etc/zabbix/zabbix_agentd.d/userparameter_linux.conf
UserParameter=linux_status[*],/etc/zabbix/zabbix_agentd.d/zabbix_linux_plugin.sh "\$1" "\$2"
EOF
## grant authority
chown -R zabbix:zabbix /tmp/netstat.tmp
chmod 755 /etc/zabbix/zabbix_agentd.d/zabbix_linux_plugin.sh

}

##zabbix PSK&CERT
function Security()
{
#zabbix agent PSK
/usr/bin/openssl rand -hex 32 >>/etc/zabbix/zabbix_agentd.d/zabbix_agentd.psk
##modify zabbix_agented.conf file
cp /etc/zabbix/zabbix_agentd.conf /etc/zabbix/zabbix_agentd.confPSK.bak
cat >>/etc/zabbix/zabbix_agentd.conf <<EOF
#psk encryption
TLSConnect=psk
TLSAccept=psk
TLSPSKFile=/etc/zabbix/zabbix_agentd.d/zabbix_agentd.psk
##unique psk value
TLSPSKIdentity=PSK $HOST$DATE
EOF
}
##install func
function install_agent(){

            #install zabbix_agent
            yum clean all
            yum -y install zabbix-agent
            #modify zabbix_agentd.conf 
            cp /etc/zabbix/zabbix_agentd.conf /etc/zabbix/zabbix_agentd.conf.bak
            sed -i "s/Server=127.0.0.1/# Server=127.0.0.1/g" /etc/zabbix/zabbix_agentd.conf
            sed -i "/# StartAgents=3/a\StartAgents=0" /etc/zabbix/zabbix_agentd.conf
            sed -i "s/ServerActive=127.0.0.1/ServerActive=$ip/g" /etc/zabbix/zabbix_agentd.conf
            sed -i "s/Hostname=Zabbix server/Hostname=$hostname/g" /etc/zabbix/zabbix_agentd.conf
            sed -i "/# RefreshActiveChecks=120/a\RefreshActiveChecks=120" /etc/zabbix/zabbix_agentd.conf
            sed -i "/# BufferSize=100/a\BufferSize=300" /etc/zabbix/zabbix_agentd.conf
            sed -i "/# Timeout=3/a\Timeout=5" /etc/zabbix/zabbix_agentd.conf

}


#main
if [ $OS_VERSION = 6 ]; then
    #modify file attributes
    chattr -i /etc/passwd /etc/shadow /etc/group /etc/gshadow
    #install zabbix_agent epel
    rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/6/x86_64/zabbix-release-4.0-1.el6.noarch.rpm
    #use func
    install_agent
    #encryption
    Security
    ##add TCP connections
    TCP_connections
    #grant
    chown -R zabbix:zabbix /etc/zabbix
    #centos 6 start
    service zabbix-agent start && chkconfig zabbix-agent on  
    #modify file attributes
    chattr +i /etc/passwd /etc/shadow /etc/group /etc/gshadow
    #echo 
    ##PSK value
    PSK_Identityfy=`cat /etc/zabbix/zabbix_agentd.conf|grep "TLSPSKIdentity=PSK"|cut -d "=" -f2`
    PSK_Value=`cat /etc/zabbix/zabbix_agentd.d/zabbix_agentd.psk`
    echo -e "Use----\"service zabbix-agent status\"----check zabbix agent status"
    echo "IP值:$hostname"
    echo "PSK_Identityfy值:$PSK_Identityfy"
    echo "PSK_Value值:$PSK_Value"

elif [ $OS_VERSION = 7 ]; then
    #modify file attributes
    chattr -i /etc/passwd /etc/shadow /etc/group /etc/gshadow
    #install zabbix_agent epel
    rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
    #use func
    install_agent
    #encryption
    Security
    ##add TCP connections
    TCP_connections
    #grant
    chown -R zabbix:zabbix /etc/zabbix
    #centos 7 start
    systemctl start zabbix-agent && systemctl enable zabbix-agent && systemctl daemon-reload
    #modify file attributes
    chattr +i /etc/passwd /etc/shadow /etc/group /etc/gshadow
    #echo 
    ##PSK value
    PSK_Identityfy=`cat /etc/zabbix/zabbix_agentd.conf|grep "TLSPSKIdentity=PSK"|cut -d "=" -f2`
    PSK_Value=`cat /etc/zabbix/zabbix_agentd.d/zabbix_agentd.psk`
    echo -e "Use----\"systemctl status zabbix-agent\"----check zabbix agent status"
    echo "IP值:$hostname"
    echo "PSK_Identityfy值:$PSK_Identityfy"
    echo "PSK_Value值:$PSK_Value"
else 
    echo "This script only works with Centos6 and Centos7!"
fi
#delete os.txt
rm -rf os.txt

相关文章

网友评论

      本文标题:zabbix agent安装脚本

      本文链接:https://www.haomeiwen.com/subject/zbjoaqtx.html