一、准备LAMP环境
[root@zabbix ~]# yum install mariadb-devel mariadb-server -y
[root@zabbix ~]# systemctl enable mariadb.service ##注意启动的时候是service而不是server
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@zabbix ~]# systemctl start mariadb.service
[root@zabbix ]# mysql
MariaDB [(none)]> create database zabbix default character set utf8;
MariaDB [(none)]> grant all on zabbix.* to 'zabbix'@'192.168.%.%' identified by 'zabbix'; ##在授权的同时创建了该用户
MariaDB [(none)]> grant all on zabbix.* to 'zabbix'@'localhost' identified by 'zabbix'; ##使用socket连接,因为数据库也在同一台机器上
MariaDB [(none)]> flush privileges;
[root@zabbix ~]# mysql -uzabbix -pzabbix ##测试zabbix用户登录
[root@zabbix mariadb]# yum install httpd httpd-devel httpd-manual php -y
[root@zabbix httpd]# cat /var/www/html/index.html
<h1>
this is Zabbix Server,and IP is 192.168.0.24
</h1>
# systemctl stop firewalld
# setenforce 0
[root@zabbix ~]# vim /etc/php.ini ##
date.timezone =Asia/Shanghai
[root@zabbix httpd]# service httpd start
[root@zabbix ~]# curl -I http://localhost
[root@zabbix ~]# systemctl enable httpd
二、安装zabbix-server
[root@zabbix ~]# rpm -ivh http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm ##安装zabbix的yum仓库
[root@zabbix ~]# ll /etc/yum.repos.d/zabbix.repo
-rw-r--r--. 1 root root 410 Sep 14 2016 /etc/yum.repos.d/zabbix.repo
[root@zabbix yum.repos.d]# yum list|grep zabbix
[root@zabbix yum.repos.d]# yum install zabbix-server zabbix-server-mysql zabbix-get zabbix zabbix-web zabbix-web-mysql zabbix-agent zabbix-sender -y
[root@zabbix doc]# rpm -ql zabbix-server-mysql
[root@zabbix ~]# cd /usr/share/doc/zabbix-server-mysql-3.2.4/
[root@zabbix ~]# gunzip create.sql.gz
[root@zabbix ~]# mysql zabbix <create.sql ##默认root密码为空
[root@zabbix zabbix-server-mysql-3.2.3]# mysql
MariaDB [(none)]> use zabbix;
MariaDB [zabbix]> show tables;
[root@zabbix ~]# vim /etc/zabbix/zabbix_server.conf
## DBHost=zabbix ##如果指定为localhost的意思是,数据库也在本机上,那么我们是不需要修改接下来的DBsocket的
DBPassword=zabbix
[root@zabbix ~]# service zabbix-server start
[root@zabbix ~]# systemctl enable zabbix-server
[root@zabbix ~]# netstat -tunlp ##并没有察觉到10051端口处于监听状态
[root@zabbix ~]# vim /etc/httpd/conf.d/zabbix.conf
php_value date.timezone Asia/Shanghai
[root@zabbix ~]# service httpd restart
三、配置zabbix web界面,zabbix server本身安装zabbix-agnet,监控自己。
http://[zabbix-ip]/zabbix 填入数据库的密码是zabbix,zabbix服务登录的用户名是admin,密码是zabbix
[root@zabbix ~]# yum install zabbix-agent zabiix-sender -y ##centos7.2中不需要安装zabbix公共组件
[root@zabbix ~]# vim /etc/zabbix/zabbix_agentd.conf
Server=127.0.0.1
ServerActive=192.168. 1.63 ##申明zabbix服务监控端的IP地址或者域名,也可以是127.0.0.1
Hostname=Zabbix Server ##申明被监控端的主机名,必须唯一
[root@zabbix ~]# service zabbix-agent start
[root@zabbix ~]# systemctl enable zabbix-agent
[root@zabbix ~]# netstat -tnulp|grep 10050
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 3166/zabbix_agentd
tcp6 0 0 :::10050 :::* LISTEN 3166/zabbix_agentd
而后,登录web界面,在[configuration]-[host]右侧创建主机[create host],填入相关信息
/////////////////////////////
批量部署源码包zabbix-agent的脚本,因为其他节点是访问不了外网的。这个脚本不适用,不用添加了!
#!/bin/bash
read -p "请输入zabbix-agent所在的IP地址: " Host
read -s -p "请输入zabbix-agent的root用户密码: " Password
echo
ep=`rpm -qa | grep expect`
if [ -z $ep ] ; then
echo "检测到你的系统,没有安装expect模块,准备执行安装"
yum install -y expect --quiet
if [ $? -eq 0 ];then
echo "\033[32m 成功安装expect模块,开始执行ssh文件同步! \033[0m"
fi
else
echo -e "\033[32m 你已经安装过了expect模块,开始执行ssh文件同步! \033[0m"
fi
#spawn ssh-copy-id -i .ssh/id_rsa.pub $Host
spawn ssh root@$Host
expect {
"*yes/no" {send "yes\r" ;exp_continue}
"*password:" {send "$Password\r" ;exp_continue }
}
//////////////////////////
四、ansbile批量部署zabbix-agent rpm包。 参考文档:http://yangfannie.com/1120.html http://www.cnblogs.com/wsl222000/p/5724938.html
[root@zabbix ~]# yum install -y epel-release
[root@zabbix ~]# yum install ansible -y
[root@zabbix ansible]# tree
├── ansible.cfg
├── hosts
├── install_zabbix_agent.yml
└── roles
└── install_zabbix_agent
├── files
│?? ├── zabbix-agent-3.2.4-2.el7.x86_64.rpm
│?? └── zabbix-sender-3.2.4-1.el7.x86_64.rpm
├── tasks
│?? └── main.yml
├── templates
│?? └── zabbix-agentd.conf
└── vars
└── main.yml
[root@zabbix ansible]# vim /etc/ansible/hosts ##添加agent的IP地址
[Zabbix-Agent]
172.16.1.8
10.20.0.3
10.20.0.4
10.20.0.5
# 192.168.1.25 ansible_ssh_user=root ansible_ssh_pass=openstack
[root@zabbix ]# ansible Zabbix-Agent -a 'cat /etc/redhat-release' --ask-pass ##测试的第一步,也可以不加ask-pass,像上面的配置那样,手动加密码
[root@zabbix ]# cd /etc/ansible && vim install_zabbix_agent.yml
- hosts: Zabbix-Agent
roles:
- install_zabbix_agent
[root@zabbix ansible]# vim ansible.cfg
host_key_checking = False ##取消注释,否则会报错
[root@zabbix ]# mkdir -p /etc/ansible/roles/install_zabbix_agent/{files,tasks,templates,vars}
[root@zabbix files]# wget http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-agent-3.2.4-2.el7.x86_64.rpm ##放置files目录下
[root@zabbix files]# wget http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-sender-3.2.4-1.el7.x86_64.rpm
[root@zabbix tasks]# cat main.yml ##如果对方已经安装过了zabbix-agent,是会报错的,需要进行判断
==============================================
- name: scp zabbix agent rpm
copy:
src=zabbix-agent-3.2.4-2.el7.x86_64.rpm dest=/tmp
- name: scp zabbix sender rpm ##经测试,一次不能传输多个文件
copy:
src=zabbix-sender-3.2.4-1.el7.x86_64.rpm dest=/tmp
- name: install software
shell: cd /tmp && rpm -ivh zabbix-* ##不知道为什么不能用command模块,应该是"/"这个符号的影响
- name: change zabbix agent conf
template: src={{ item }} dest=/etc/zabbix/zabbix_agentd.conf
with_items:
- zabbix_agentd.conf
- name: start zabbix service
service: name=zabbix-agent state=started enabled=yes
=============================================================
[root@zabbix templates]# cat zabbix-agentd.conf !!这一步还是不要这么做,在zabbix server上拷贝一份完整的zabbix agent为好
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server={{ ZabbixServerIP }}
ServerActive={{ ZabbixServerIP }} ##变量名不能有”-“符号,例如Zabbix-Server-IP的形式
Hostname=Zabbix-Agent ##我暂时没想到如何收集不同客户端的信息,然后发过去,这个Hostname跟web界面不一样的话,会出现问题
Include=/etc/zabbix/zabbix_agentd.d/*.conf
[root@agent vars]# ansible Zabbix-Agent -m setup ##收集变量信息
[root@zabbix ansible]# cat roles/install_zabbix_agent/vars/main.yml
ZabbixServerIP: 192.168.1.63
五、playbook任务执行。
[root@zabbix ansible]# cd /etc/ansible
[root@zabbix ansible]# ansible-playbook install_zabbix_agent.yml --limit=Zabbix-Agent --ask-pass
剩下的就是zabbix web配置了!
六、zabbix配置的过程中出现的错误。
1、Zabbix agent on agent-172.16.1.8 is unreachable for 5 minutes
分析:[root@zabbix ~]# zabbix_get -s 172.16.1.8 -p 10050 -k agent.ping
zabbix_get [20097]: Get value error: cannot connect to [[172.16.1.8]:10050]: [113] No route to host
单独手工命令去Ping 172.16.1.8是通的,但是用zabbix检测就不行。
[root@zabbix ~]# zabbix_get -s 10.20.0.3 -p 10050 -k agent.ping 于是再使用zabbix get另一台agent,同样报错,说明是server端问题了。
[root@zabbix ~]# zabbix_get -s 127.0.0.1 -p 10050 -k agent.ping ##zabbix_get自己可以成功
1
[root@compute2 ~]# tail /var/log/zabbix/zabbix_agentd.log
1354:20170419:161933.716 active check configuration update from [192.168.1.63:10051] started to fail (cannot connect to [[192.168.1.63]:10051]: [4] Interrupted system call)
解决:防火墙问题,这个问题实在又忘了!
[root@controller1 ~]# iptables -I INPUT -p tcp -m tcp --dport 10050 -m comment --comment "zabbix_agentd listen " -j ACCEPT
[root@controller1 ~]# yum install iptables-services
[root@controller1 ~]# service iptables save
2、[root@zabbix ~]# zabbix_get -s 172.16.1.8 -k agent.ping ##这是云主机才出现的问题,其他服务器正常
zabbix_get [21468]: Check access restrictions in Zabbix agent configuration
[root@vm2 ~]# tail /var/log/zabbix/zabbix_agentd.log
686:20170419:180136.315 failed to accept an incoming connection: connection from "172.16.1.6" rejected, allowed hosts: "192.168.1.63"
解决:# vim /etc/zabbix/zabbix_agentd.conf
把Server=浮动IP改为zabbix server的虚拟网络的私有IP地址,同时改动的还有ServerActive,这个问题可害苦我了,明明都是能通的
3、no active checks on server [192.168.3.108:10051]: host [192.168.3.108] not found
解决:zabbix agent.conf配置文件的Hostname要与Zabbix web界面的主机名要相同,否则客户端无法进行主动报告数据。
ansible模块知识:http://www.itshuji.com/technical-article/1818.html