zabbix agent shell一键安装脚本

时间:2022-10-25 21:30:51
#!/bin/bash

basepath=$(cd `dirname $0`; pwd)
SHELL_DIR="${basepath}/shell"
PACKAGE_DIR="${basepath}/package"


#获取参数
while getopts "i:" opt; do
    case $opt in
        i)
            ipaddr=$OPTARG
            ;;
        \?)
            ;;
    esac
done

echo "ip NO:"
echo $ipaddr

rpm -ivh  $PACKAGE_DIR/zabbix-release-2.4-1.el6.noarch.rpm

yum install -y zabbix-agent

if [ -e /etc/zabbix/zabbix_agentd.conf ];then
echo
echo "##########begin modify zabbix_agentd.conf##########"
sed -i 's/^Server=127.0.0.1/Server=116.*******28/g' /etc/zabbix/zabbix_agentd.conf
sed -i 's/^ServerActive=127.0.0.1/ServerActive=116.*******.28/g' /etc/zabbix/zabbix_agentd.conf
sed -i "s/Hostname=Zabbix server/Hostname=${ipaddr}/g" /etc/zabbix/zabbix_agentd.conf
#sed -i 's/^Hostname=Zabbix server/Hostname=$ip/g' /etc/zabbix/zabbix_agentd.conf
#sed -i '/ListenPort=10050$/a\ListenPort=21224' /etc/zabbix/zabbix_agentd.conf
#sed -i "/ListenIP=0.0.0.0$/a\ListenIP=${ip}" /etc/zabbix/zabbix_agentd.conf
sed -i '/Timeout=3$/a\Timeout=30' /etc/zabbix/zabbix_agentd.conf
grep -v '^#' /etc/zabbix/zabbix_agentd.conf |grep -v '^$'
echo "##########end modify zabbix_agentd.conf##########"
echo
else
echo
echo "##########zabbix_agentd.conf is not exit##########"
echo
exit 2
fi


if [ -d /etc/zabbix/zabbix_agentd.d ];then
cp -r $PACKAGE_DIR/yuwang.conf /etc/zabbix/zabbix_agentd.d
else
echo "##########/etc/zabbix/zabbix_agentd.d is not exist##########"
echo
exit 3
fi


echo "##########begin move scripts to /etc/zabbix/scripts##########"
if [ -d /etc/zabbix/scripts ];then
    cp -r $PACKAGE_DIR/scripts/* /etc/zabbix/scripts
    chmod +x /etc/zabbix/scripts/*
else
    mkdir /etc/zabbix/scripts
    cp -r $PACKAGE_DIR/scripts/* /etc/zabbix/scripts
    chmod +x /etc/zabbix/scripts/*
fi
echo "##########end move scripts to /etc/zabbix/scripts##########"

echo "##########begin modify /etc/sudoers##########"
sed -i 's/\(Defaults.*requiretty\)/#\1/g' /etc/sudoers
echo '...'
sed -i 's/!visiblepw/visiblepw/g' /etc/sudoers
echo '...'
echo 'zabbix ALL=(ALL) NOPASSWD:/etc/zabbix/scripts/*' >> /etc/sudoers
echo '...'
echo "##########end modify /etc/sudoers##########"


service zabbix-agent restart
chkconfig zabbix-agent on

zabbixagentpid=`ps aux|grep zabbix_agentd|grep -v "grep"|awk '{print $2}'`

if [ "$zabbixagentpid" ];then
      echo "success ! zabbix agent is running now"
fi

 

配置文件内容
cat yuwang.conf 
UserParameter=iostat[*],/etc/zabbix/scripts/iostat.sh $1 $2
UserParameter=discovery.disk,/etc/zabbix/scripts/discovery.disk.sh
UserParameter=net.if.discovery2,/etc/zabbix/scripts/net.if.discovery2.sh

 

脚本网卡发现,磁盘io监控,网卡流量监控

[root@pro- scripts]# cat discovery.disk.sh 
#!/bin/bash

count=`cat /proc/diskstats |grep '[vs]d[a-z] ' |wc -l`
i=1

echo '{'
echo '    "data":['

for disk in `cat /proc/diskstats |grep '[vs]d[a-z] ' |awk '{print $3}'`
do
        if [ $i -lt $count ] ; then
                echo '        {"{#DISK}":"'$disk'"},'
        else
                echo '        {"{#DISK}":"'$disk'"}'
        fi
        i=`echo $(($i+1))`
done

echo '    ]'
echo '}'

[root@pro- scripts]# cat iostat.sh 
#!/bin/bash
result=`iostat -k -x $1 1 2|grep $1 |tail -n1`
#echo "$result"
#Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await  svctm  %util
#sda               0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00
function readkB(){
    echo "$result" |awk '{print $6}'
}
function writekB(){
    echo "$result" |awk '{print $7}'
}
function avgrq-sz(){
    echo "$result" |awk '{print $8}'
}
function avgqu-sz(){
    echo "$result" |awk '{print $9}'
}
function await(){
    echo "$result" |awk '{print $10}'
}
function svctm(){
    echo "$result" |awk '{print $11}'
}
function util(){
    echo "$result" |awk '{print $12}'
}
$2
[root@ scripts]# cat net.if.discovery2.sh 
#/bin/bash

i=0
names=()
for interface in `cat /proc/net/dev |tail -n +3 |grep -v 'lo\|vir\|vnet' |grep -oP '\w+\d*(?=:)'`
#for interface in `cat /proc/net/dev |tail -n +3 |grep -v 'lo' |grep -oP '\w+\d*(?=:)'`
do
    c=`ifconfig $interface 2>/dev/null|grep 'inet ' |grep -v -c '127.0.0.1'`
    if [ $c -gt 0 ];then
        names[$i]=$interface
        i=`echo $((i+1))`
    fi
done


length=${#names[*]}
i=1
echo '{'
echo '    "data":['
for interface in ${names[*]}
do
    if [ $i -lt $length ];then
        echo '        {"{#IFNAME}":"'$interface'"},'
        i=`echo $((i+1))`
    else
        echo '        {"{#IFNAME}":"'$interface'"}'
        break
    fi
done

echo '    ]'
echo '}'