引言: zabbix是目前系统监控领域的佼佼者,其开源且提供了强大完善的Web界面,并且兼容各种不同的设备和系统,支持多种平台,其背后的公司为Zabbix持续演进提供了技术支持,总之,zabbix是目前流行的一线监控解决方案。本文将详细描述一下整个搭建Zabbix服务的过程以及其中出现的若干问题。


1. 环境准备

 Zabbix版本: 2.4.4        系统: Centos 7          Agent:  Linux/Window 2008

 官方网站:  www.zabbix.com

2. Zabbix服务端的安装

  2.1  检查Apache是否安装

       如需安装apache, 则执行: yum install httpd -y

       参考资料: http://www.server-world.info/en/note?os=CentOS_7&p=httpd

   2.2 安装zabbix

      >>    rpm -ivh http://repo.zabbix.com/zabbix/2.4/rhel/7/x86_64/zabbix-release-2.4-1.el7.noarch.rpm

      >>    yum install zabbix zabbix-get zabbix-server zabbix-web-mysql zabbix-web zabbix-agent

   2.3 安装mysql数据库, 并初始化zabbixdb

        关于mysql的安装,可以参考其他相关资料,这里不再赘述。关于zabbix初始化中所需的sql脚本,请从zabbix官方网站上下载源代码,其中包含所需SQL.

      以下是mysql命令行下执行的脚本:

> mysql -u root -p password
  > use zabbixdb;
  > source /root/path/schema.sql
  > source /root/path/images.sql
  > source /root/path/data.sql
  > show tables;

    这里我设置db的信息: db: zabbixdb, user: zabbixuser password: password


  2.4 配置zabbix_server.conf文件

       将其中关于数据库方面的信息替换为在2.3中设置的信息:

[...]
    DBName=zabbixdb
   [...]
    DBUser=zabbixuser
   [...]
    DBPassword=password
    [...]

  2.5 启动zabbix-server服务


> service zabbix-server start
 > service httpd start
 > service enable httpd

   可选将服务添加进入自动执行:


chkconfig zabbix-server on
 chkconfig httpd on

  2.6 修改IPTable策略,开放所需的服务端口

    由于Centos 6 和Centos 7两个系统之间,针对iptables的指令发生了较大变化,故针对两者的指令是不同的,需要注意。

   在CentOS 6之下,指令如下:

vi /etc/sysconfig/iptable-configure     #在其中新增下列信息
  -A INPUT -m state --state NEW -m tcp -p --dport 22 -j ACCEPT
  -A INPUT -m state --state NEW -m tcp -p --dport 80 -j ACCEPT
  -A INPUT -m state --state NEW -m tcp -p --dport 10051 -j ACCEPT
  -A OUTPUT -m state --state NEW -m tcp -p --dport 10050 -j ACCEPT

  在CentOS 7之下,指令如下:


firewall-cmd --permanent --add-port=10050/tcp
  firewall-cmd --permanent --add-port=10051/tcp
  firewall-cmd --permanent --add-port=80/tcp
  firewall-cmd --permanent --add-port=3306/tcp

 10050 是Agent的端口,Agent采用被动式。 10051是Server的端口,Agent采用主动或者tapper的方式连接server的端口。

  关于selinux的安全控制系统,需要自行检查是否已经关闭

   检查getenforce, 获取selinux的状态。 编辑config命令,停用selinux

    vi /etc/selinux/config
    SELINUX=disabled

   关于firewalld服务的状态:

    serivce firewalld start/stop
    firewall-cmd --reload  ### reload the configuration info

   systemctl restart firewalld

   2.6  修改php.ini的设置

     vi /etc/php.ini
  => date.timezone =Asia/Shanghai

      vi /etc/httpd/conf.d/zabbix.conf
  + Allow from All
 
  Alias /zabbix /usr/share/zabbix
<Directory "/usr/share/zabbix">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>

 2.7 启动服务

systemctl restart zabbix-server
   systemctl start zabbix-agent
   systemctl restart httpd
   systemctl restart mariadb/mysql
   systemctl enable zabbix-server
   systemctl enable zabbix-agent

   check=> systemctl status xxx

  2.8 设置Zabbix Agent客户端

    ============================================
  sudo vi /etc/zabbix/zabbix_agentd.conf   #设置正确的ip地址和主机名称
   server
   serverActive
   serverName
 
 ===============================================
 systemctl start zabbix-agent
 systemctl enable zabbix-agent

设置主机名称 in centos7:
 hostnamectl set-hostname xxx --pretty/static

2.7 停用zabbix服务

systemctl stop zabbix-agent
   systemctl stop zabbix-server
   systemctl stop httpd   
   chkconfig zabbix-server off
<pre name="code" class="html">   chkconfig httpd off
   systemctl disable zabbix-agent

systemctl disable zabbix-server




</pre></p><p>  实际执行中,会发现zabbix-server经常无法正常的关闭,需要杀死进程方可。</p><p>3. Zabbix在window下的安装</p><p>  3.1 下载安装文件</p><p>      http://www.zabbix.com/downloads/2.4.4/zabbix_agents_2.4.4.win.zip</p><p>  3.2 解压缩至某个目录</p><p>  3.3 修改zabbix_agentd.win.conf</p><p>    Server/ServerActive:  设置为目标的服务器</p><p>  3.4 安装Zabbix服务</p><p>     zabbix_agentd.exe --config "C:\zabbix-agents\conf\zabbix_agentd.win.conf"  --install</p><p>     --uninstall  卸载服务</p><p>  3.5 启停zabbix服务</p><p>     zabbix_agentd.exe --config "C:\zabbix-agents\conf\zabbix_agentd.win.conf"  --start/--stop</p><p>  3.6 参考资料</p><p>     http://www.tecmint.com/install-zabbix-agent-and-add-windows-host-to-zabbix-monioring/</p><p>4. 常见问题的解决</p><p>   4.1 访问页面403错误</p><p>      错误信息:</p><pre code_snippet_id="1571404" snippet_file_name="blog_20160131_8_9280193" name="code" class="html">[root@AY131203102210033c39Z ~]# wget http://127.0.0.1:80
--2016-01-02 21:39:57--  http://127.0.0.1/
Connecting to 127.0.0.1:80... connected.
HTTP request sent, awaiting response... 403 Forbidden
2016-01-02 21:39:57 ERROR 403: Forbidden.

    如何解决:

       a.  telnet ip port   # 验证端口是否可以访问

       b.  卸载apache, 重新安装即可解决。

       c.   原因是 apache未能正确启动和设置

    4.2  无法正确安装服务

       zabbix_agentd.exe --config "D:\Program Files\zabbix-agent-2.4.4\conf\zabbix_agentd.win.conf"  --install
       错误信息:
           zabbix_agentd.exe [33256]: ERROR: cannot connect to Service Manager: [0x00000005] 鎷掔粷璁块棶銆

        如何解决:

               在启动cmd之时,基于右键的run as administrator(以管理员来运行)

      4.3 如何从zabbix db获取单个监控项的数值

         基于主机ip,获取主机信息:   select * from zabbixdb.hosts where hostid = 10081    # windows.agent 10106, 获取主机信息

         基于主机ip或者itemid, 获取监控项信息: select * from zabbixdb.items where itemid = 23292(hostid=10081)

          基于监控项,获取具体的监控值信息: select * from zabbixdb.history_uint hu where hu.itemid = 23292; (23292为具体的item id)

       4.4 在执行第三方自定义的script之时,报出如下错误信息, windows2008

           错误信息:

                 [t|无法加载文件 C:\zabbix-agents\scripts\listallprocesses.ps1,因为在此系统中禁止执行脚本。有关详细信息,请参阅 "get-help about_signing"。
                + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
                + FullyQualifiedErrorId : RuntimeException]

            解决办法:

              >>get-ExecutionPolicy

    >> set-ExecutionPolicy RemoteSigned

          参考文档:   

        4.5 在设置ExecutionPolicy为RemoteSigned之后,仍然报出ParentContainsErrorRecordException,FullyQualifiedErrorId : RuntimeException之类的错误信息。

             错误信息:

                           [t|无法加载文件 C:\zabbix-agents\scripts\listallprocesses.ps1,因为在此系统中禁止执行脚本。有关详细信息,请参阅 "get-help about_signing"。
                + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
                + FullyQualifiedErrorId : RuntimeException]

               分析路径: get-ExecutionPolicy已经设置为了RemoteSigned,但是仍然报出这类的错误;操作系统为:windows 2008 R2. 最后确认问题仍然是ps1脚本本身在操作系统范围上的执行控制。

               选中ps1文件,解除Block文件的阻止,然后就可以在powershell的执行。

5.   访问监控web工具

      基于http://ip/zabbix   即可访问Web控制台。这里不再详述如何基于控制台配置zabbix agent和监控项,我们将另起一篇blog来介绍。

 6.  总结

     利用周末2天时间,搭建起来了zabbix的监控环境,其中碰到最多的问题是centos6 和centos7之间的变更引发的诸多问题,在搭建起来之后,也加深了对centos 7的理解。