脚本部署zabbix_agentd

传送门:
Zabbix的Server端的源码安装:http://www.jianshu.com/p/a861afe42394
LNMP的源码安装:http://www.jianshu.com/p/4699bcb04633
Zabbix-3.0的Agent端在CentOS 7.3的源码安装实践:http://www.jianshu.com/p/2e4c535ca53a

Zabbix_agent的脚本安装

环境准备:

1.提前下载zabbix软件包


软件获取
Zabbix官网下载地址:http://www.zabbix.com/download.php

获取3.0.0的LTS版地址
[root@localhost ~]# wget http://jaist.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/3.0.0/zabbix-3.0.0.tar.gz

2.依赖软件

[root@localhost ~]# yum -y install net-tools gcc gcc-c++ vim
    ```
3.修改主机名,关闭selinux,配置iptables

[root@localhost ~]# hostnamectl set-hostname agent2
[root@agent2 ~]# shutdown -r now
[root@agent2 ~]# setenforce 0
[root@agent2 ~]# systemctl stop firewalld.service
[root@agent2 ~]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
[root@agent2 ~]# yum -y install iptables-services

配置iptables

[root@agent2 ~]# vim /etc/sysconfig/iptables

sample configuration for iptables service

you can edit this manually or use system-config-firewall

please do not ask us to add additional ports/services to this default configuration

*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -s 192.168.81.11 -p tcp -m state --state NEW -m tcp --dport 10050 -j ACCEPT
-A OUTPUT -d 192.168.81.11 -p tcp -m state --state NEW -m tcp --dport 10051 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

[root@agent2 ~]# service iptables save
[root@agent2 ~]# service iptables restart
[root@agent2 ~]# iptables -L
[root@agent2 ~]# systemctl enable iptables.service

######创建脚本(ps:如果zabbix的软件版本号不一样,需要修改)

[root@agent2 ~]# vim zabbix_agentd.sh

!/bin/bash

Program:

Automatic install zabbix_agentd-3.0.0 in centos-6.x-x86_64 by the scripts.

Usage:

It relate to chage directory, please use source or . to execute this scripts, the others methods will fail.

History:

2017/04/22 v0.1

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

Check if user is root.

if [ $(id -u) != "0" ]; then
echo "Error: You must be root to run this script, please use root to install zabbix_agentd!"
exit 1
fi

Function: check the zabbix_server ip address which has be inputed.

checkip() {
echo $1 | egrep -q '^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$' > /dev/null
if [ $? -ne 0 ]; then
echo "Error: Please input correct format IP address!"
exit 1
fi

ipaddr=$1
ip1=echo $ipaddr | awk -F. '{print $1}'
ip2=echo $ipaddr | awk -F. '{print $2}'
ip3=echo $ipaddr | awk -F. '{print $3}'
ip4=echo $ipaddr | awk -F. '{print $4}'

for num in $ip1 $ip2 $ip3 $ip4; do
if [ $num -ge 255 ] || [ $num -lt 0 ]; then
echo "Error: Please input correct format IP address!"
exit 1
fi
done

return 0
}

Input zabbix_server's ip address.

read -p "Please input zabbix_server's ip address[ie: 192.168.1.1]: " zabbixserverip
checkip $zabbixserverip
echo "OK! Your zabbix_server is ${zabbixserverip}!"

Set iptables rules, zabbix server will detect agentd by tcp 10050, and zabbix_agentd will send trapper to server by tcp 10051.

iptables -I INPUT -s $zabbixserverip -p tcp -m state --state NEW -m tcp --dport 10050 -j ACCEPT
iptables -I OUTPUT -d $zabbixserverip -p tcp -m state --state NEW -m tcp --dport 10051 -j ACCEPT
service iptables save

Check selinux.

if [ $(getenforce) = "Enforcing" ]; then
sed -i 's|SELINUX=enforcing|SELINUX=disabled|g' /etc/selinux/config ; sed -i 's|SELINUXTYPE=targeted|#SELINUXTYPE=targeted|g' /etc/selinux/config && setenforce 0
fi

Create zabbix group and user.

groupadd zabbix
useradd -g zabbix -s /sbin/nologin zabbix

Install zabbix_agentd

cur_dir=$(pwd)
tar -zxvf $cur_dir/zabbix-3.0.0.tar.gz -C /usr/local/src/
cd /usr/local/src/zabbix-3.0.0
./configure --prefix=/usr/local/zabbix --enable-agent && make && make install
cd ~

Add soft link to zabbix_agentd execute file.

ln -s /usr/local/zabbix/sbin/* /usr/local/sbin/
ln -s /usr/local/zabbix/bin/* /usr/local/bin/

Modify zabbix_agentd config file.

sed -i "s|Server=127.0.0.1|Server=${zabbixserverip}|g" /usr/local/zabbix/etc/zabbix_agentd.conf
sed -i '262s|# Include=/usr/local/etc/zabbix_agentd.conf.d/|Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/|g' /usr/local/zabbix/etc/zabbix_agentd.conf
sed -i 's|# UnsafeUserParameters=0|UnsafeUserParameters=1|g' /usr/local/zabbix/etc/zabbix_agentd.conf

Set zabbix_agentd automatic start scripts.

cp /usr/local/src/zabbix-3.0.0/misc/init.d/fedora/core/zabbix_agentd /etc/rc.d/init.d/zabbix_agentd
chown zabbix:zabbix /etc/rc.d/init.d/zabbix_agentd
chmod +x /etc/rc.d/init.d/zabbix_agentd
sed -i 's|BASEDIR=/usr/local|BASEDIR=/usr/local/zabbix|g' /etc/rc.d/init.d/zabbix_agentd
chkconfig --level 35 zabbix_agentd on

Start zabbix_agentd service.

service zabbix_agentd start

Check zabbix_agentd service.

if [ $(netstat -tnlp | grep zabbix_agentd | awk '{print $7}' | awk -F/ '{print $2}') = "zabbix_agentd" ]; then
echo -e "\033[32m [INFO]Zabbix_agentd has installed and started! \033[0m"
else
echo -e "\033[31m [ERROR]Zabbix_agentd has not started! \033[0m"
fi

Clean install package.

rm -rf /usr/local/src/zabbix-3.0.0

#######运行脚本(脚本需要和zabbix安装包在一个目录下)

[root@agent2 ~]# sh zabbix_agentd.sh
Please input zabbix_server's ip address[ie: 192.168.1.1]: 192.168.81.11


![image.png](http://upload-images.jianshu.io/upload_images/5314324-c422955165bc2d8a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容