马哥Linux运维原创作者投稿

zabbix的编译安装并发送通知邮件

2017-04-23  本文已影响484人  天之蓝色

1.写在前面

本文主要介绍的是zabbix的编译安装过程,包含它的基础环境LNMP,虽然zabbix官方一般推荐的环境是LAMP。以及实现简单的监控任务,在发现问题时能够发送通知邮件。

2.系统环境

软件包 版本
OS CentOS6.8-2.6.32-642.el6.x86_64
nginx nginx-1.10.3.tar.gz
mysql mysql-5.5.53-linux2.6-x86_64.tar.gz
php php-5.5.38.tar.gz
zabbix zabbix-3.2.4.tar.gz

提前安装好如下两个包组:yum groupinstall "Development Tools" "Server Platform Development"

3.搭建web服务

zabbix作为监控系统需要有图形化的页面呈现给运维人员,它使用web+php的方式来实现。这里首先搭建其需要的web服务。我使用的是nginx-1.10.3的源码包编译安装的方式。相应步骤如下:

useradd nginx -s /sbin/nologin -M   #不让此用户通过shell登录系统
#-M不在home下创建此用户名同名的文件夹
./configure --prefix=/usr/local/nginx-1.10.3 --with-http_ssl_module 
--with-http_stub_status_module --with-pcre  --with-user=nginx   
 #提前yum安装好pcre,pcre-devel,openssl,openssl-devl
make
make install
 ln -s /usr/local/nginx-1.10.3/ /usr/local/nginx  
vim /etc/profile.d/nginx.sh
export PATH=/usr/local/nginx/sbin:$PATH
source /etc/profile.d/nginx.sh    #加载文件中的参数到当前的shell环境中

在Linux中,通过源码安装的各类软件包的执行程序都可以都通过这种方式放入PATH环境变量中方便使用,且相应的文件不需要赋予执行权限。

chown -R nginx.nginx /usr/local/nginx-1.10.3 
[root@monitor-server2 ~]# nginx -t
nginx: the configuration file /usr/local/nginx-1.10.3/conf/nginx.conf syntax
 is ok
nginx: configuration file /usr/local/nginx-1.10.3/conf/nginx.conf test is 
successful
[root@monitor-server2 ~]# nginx   #启动nginx程序
[root@monitor-server2 ~]# pidof nginx   #检查nginx启动的进程,默认
#nginx一个master进程,4个worker进程,此处共5个进程ID,正常
1642 1641 1640 1639 1634
[root@monitor-server2 ~]# curl -I 127.0.0.1   #进一步验证web service是
#否已正常
HTTP/1.1 200 OK                        #此处结果为正常
Server: nginx/1.10.3
Date: Sat, 22 Apr 2017 10:52:45 GMT
Content-Type: text/html
Connection: keep-alive
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local
tail /etc/rc.local  #验证下是否正常添加上去

至此web service的搭建工作完成。下面继续mysql的搭建。

4. 搭建mysql服务

zabbix的server和proxy是需要连接数据库的,zabbix支持多种类型的数据库,本文只涉及到mysql。

useradd -s /sbin/nologin -M mysql
tar xf mysql-5.5.53-linux2.6-x86_64.tar.gz -C /usr/local   
#解压出来的文件会自动放置于/usr/local/mysql-5.5.53-linux2.6-x86_64中
 ln -s /usr/local/mysql-5.5.53-linux2.6-x86_64/ /usr/local/mysql
chown -R mysql.mysql /usr/local/mysql/
scripts/mysql_install_db --user=mysql -datadir =/usr/local/mysql/data  
#执行此语句后出现两个ok,并有一些安全方面和配置mysql自启服务的提示则为正常
tree /usr/local/mysql/data   #检查一下是否生成了相关的数据库文件,初始状态下为空
cp support-files/mysql.server /etc/init.d/mysqld 
chkconfig --add mysqld
chkconfig mysqld on
cp support-files/my-small.cnf /etc/my.cnf     #更名为my.ncf
service mysqld start   
lsof -i :3306   #检查mysql是否启动成功
mysqladmin -uroot -p<password>

然后在使用root用户登录。

5.搭建php-fpm环境

 ./configure --prefix=/usr/local/php-5.5.38 --with-config-file-path=/usr/local \  
/php-5.5.38/etc --with-bz2 --with-curl --enable-ftp --enable-sockets \
--disable-ipv6 --with-gd --with-jpeg-dir=/usr/local --with-png-dir=/usr/local \
--with-freetype-dir=/usr/local --enable-gd-native-ttf --with-iconv-dir=/usr \
/local --enable-mbstring --enable-calendar --with-gettext --with-libxml- \
dir=/usr/local --with-zlib --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd \
 --with-mysql=mysqlnd --enable-dom --enable-xml --enable-fpm --with- \
libdir=lib64 --enable-bcmath --enable-ctype --without-pear --disable-phar

configure完成以后,会提示进行make test,这是一个非常耗时间的过程,最后的结果有可能有一些警告,但不一定会有很大的影响,可以忽略。我在操作过程中遇到如下提示,没有去深究它,后面编译安装正常完成,没有错误提示,使用也还未发现问题。

FAILED TEST SUMMARY
\---------------------------------------------------------------------
Bug #64267 (CURLOPT_INFILE doesn't allow reset) [ext/curl/tests/bug64267.phpt]
\#66265: gettext doesn't switch locales within the same script [ext/gettext/tests/bug66267.phpt]
Bug #32001 (xml_parse*() goes into infinite loop when autodetection in effect), using UTF-* [ext/xml/tests/bug32001.phpt]
WARNED TEST SUMMARY
zend multibyte (2) [ext/mbstring/tests/zend_multibyte-02.phpt] (warn: XFAIL section but test passes)
zend multibyte (6) [ext/mbstring/tests/zend_multibyte-06.phpt] (warn: XFAIL section but test passes)
zend multibyte (8) [ext/mbstring/tests/zend_multibyte-08.phpt] (warn: XFAIL section but test passes)
Bug #70172 - Use After Free Vulnerability in unserialize() [ext/standard/tests/serialize/bug70172.phpt] (warn: XFAIL section but test passes)

configure过程中如果碰到的有错误提示,请根据提示信息予以解决,查看是否上述指定的包并未安装所致。

make
make install
ln -s /usr/local/php-5.5.38/ /usr/local/php
echo "export PATH=/usr/local/php/sbin:/usr/local/php/bin:$PATH
" > /etc/profile.d/php.sh
source /etc/profile.d/php.sh
cp php.ini-production /usr/local/php-5.5.38/etc/php.ini  
#在解压后的源码包中实际有两个配置文件,但我们使用这个适用生产环境的这个即可
cd /usr/local/php/etc
cp php-fpm.conf.default php-fpm.conf  
php-fpm   #因为已将php执行程序路径放入PATH变量中,所以此处可以直接使用php-fpm指令  
lsof -i :9000          #查看9000端口是否被php-fpm占用,是则表明php-fpm启动成功
cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm   #当前的工作目录为编译
#安装后的php源码目录php-5.5.38
chmod +x /etc/init.d/php-fpm   #赋予服务脚本可执行权限
chkconfig --add php-fpm
chkconfig php-fpm on   #设置php-fpm服务开机自启动
service php-fpm start    #启动php-fpm服务  
service php-fpm status   #检查php-fpm服务状态,或者使用之前的lsof -i :9000查看  
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

        location ~ \.php$ {                                
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  SCRIPT_NAME      $fastcgi_script_name;
            include        fastcgi_params;
        }

可以直接将原nginx.conf文件中与php-fpm相关的部分的注释直接去掉,并修改成如上所示内容。

        location / {
            root   html;
            index  index.php index.html index.htm;
        }
echo "<?php phpinfo(); ?>" > index.php
nginx -t
nginx -s reload
service php-fpm restart

6.搭建zabbix server

useradd -s /sbin/nologin -M zabbix
  ./configure  --prefix=/usr/local/zabbix-3.2.4 --enable-server \
--enable-agent --with-mysql  --with-net-snmp --with-libcurl --with-libxml2 

因为zabbix server也需要被监控,所以其也需要agent。
configure时使用了--with-net-snmp --with-libcurl --with-libxml2所以需要先在系统中使用yum安装net-snmp-devel、libcurl-devel、libxml2-devel包,使用yum安装会自动解决依赖关系将net-snmp、libcurl、libxml2一并安装上。
正常完成configure后,会显示如下汇总信息:

  Configuration:

   Detected OS:           linux-gnu
   Install path:          /usr/local
   Compilation arch:      linux

   Compiler:              gcc
   Compiler flags:         -g -O2

    Library-specific flags:
   database:               -I/usr/local/mysql/include  -fPIC  -g -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing -DMY_PTHREAD_FASTMUTEX=1   
   libXML2:               -I/usr/include/libxml2
   Net-SNMP:               -I/usr/include/rpm -I/usr/local/include -I/usr/lib64/perl5/CORE -I. -I/usr/include

  Enable server:         yes
  Server details:
   With database:         MySQL
   WEB Monitoring:        cURL
    Native Jabber:         no
    SNMP:                  yes
   IPMI:                  no
   SSH:                   no
   TLS:                   no
   ODBC:                  no
  Linker flags:             -L/usr/local/mysql/lib        -L/usr/lib64      -rdynamic  
  Libraries:               -lmysqlclient      -lxml2    -lnetsnmp      -lcurl -lm -ldl -lrt  -lresolv 

  Enable proxy:          no

  Enable agent:          yes
  Agent details:
   TLS:                   no
   Linker flags:              -rdynamic  
  Libraries:                 -lcurl -lm -ldl -lrt  -lresolv 

  Enable Java gateway:   no

  LDAP support:          no
  IPv6 support:          yes

   ***********************************************************
   *            Now run 'make install'                       *
   *                                                         *
   *            Thank you for using Zabbix!                  *
   *              <http://www.zabbix.com>                    *
  ***********************************************************

7.建立给zabbix使用的数据库

zabbix要实现将其相关的配置信息,监控的信息都存储于后台的数据库中,自然需要建立相应的数据库,并配置相应的账号。zabbix支持多种类型的数据库,这里只以mysql为例,且是在zabbix server上的mysql上需要做的。

mysql -uroot -p<password>    #登录mysql后继续以下操作
mysql> create database zabbix character set utf8 collate utf8_bin;
mysql> grant all privileges on zabbix.* to zabbix@localhost identified by '<password>';
mysql> quit;  
cd database/mysql
mysql -uzabbix -p<password> zabbix < schema.sql
# 如果是在zabbix proxy服务器上初始化mysql数据库,则只导入上面一条sql语句即可,切记!
mysql -uzabbix -p<password> zabbix < images.sql
mysql -uzabbix -p<password> zabbix < data.sql
#在zabbix server上将三条语句全部导入
DBPassword=<password>
Server=127.0.0.1   #设置允许哪些zabbix server连接此agent
ServerActive=127.0.0.1  #设置为zabbix server地址,用于agent主动发送监控数据给server端
Hostname=Zabbix server   #这个地方设置要和configuration-->host设置的host名称一致  
[root@monitor-server2 core]# pwd
/root/softwares/zabbix-3.2.4/misc/init.d/fedora/core
[root@monitor-server2 core]# ll
total 8
-rwxr-xr-x 1 1000 1000 2170 Feb 27 17:22 zabbix_agentd
-rwxr-xr-x 1 1000 1000 2181 Feb 27 17:22 zabbix_server

很明显分别有针对server和agent的脚本,把它们复制到/etc/init.d/目录下:

cp zabbix_agentd /etc/init.d
cp zabbix_server /etc/init.d 

打开这两个文件(zabbix_agentd和zabbix_server)查看其内容,根据实际情况进行修改,一般需要更改的只是如下内容:

    # Zabbix-Directory
    BASEDIR=/usr/local     #将此处改为/usr/local/zabbix即可
#congifure如果不指定zabbix的prefix,它默认直接安装到/usr/local目录下  

将这两个服务安装添加到自启动中:

chkconfig --add zabbix_agentd
chkconfig --add zabbix_server
chkconfig zabbix_agentd on
chkconfig zabbix_server on
service zabbix_server start
service zabbix_anentd start
[root@monitor-server2 ~]# ss -tnl | grep -E "10050|10051"    
LISTEN     0      128                       *:10050                    *:*     
LISTEN     0      128                       *:10051                    *:* 
#zabbix server和agent启动成功

8.安装zabbix web程序

zabbix对于监控的结果可以通过web页面来进行展示和查看,它需要安装一个使用php程序来运行的web站点,根据zabbix的官方要求,还有如下php的参数需要提前修改:

max_execution_time = 300
max_input_time = 300
memory_limit = 128M
post_max_size = 16M
upload_max_filesize = 2M
date.timezone = Asia/Shanghai    #建议设成国内标准时区

上述修改都在php.ini文件中完成。在本文的环境中它位于/usr/local/php/etc/php.ini。另外需要补充一下,zabbix3.2.4它要求的php版本最低为5.4。 修改完php.ini文件后,重启php-fpm服务service php-fpm restart

安装zabbix web程序操作步骤如下:

mkidr /usr/local/nginx/html/zabbix    #nginx默认的站点根目录为其安装路径下的html
cd frontends/php 
cp -a . /usr/local/nginx/html/zabbix
Pre-requisite Minimum value Description
PHP version 5.4.0
PHP memory_limit option 128MB In php.ini:memory_limit = 128M
PHP post_max_size option 16MB In php.ini:post_max_size = 16M
PHP upload_max_filesize option 2MB In php.ini:upload_max_filesize = 2M
PHP max_execution_time option 300 seconds (values 0 and -1 are allowed) In php.ini:max_execution_time = 300
PHP max_input_time option 300 seconds (values 0 and -1 are allowed) In php.ini:max_input_time = 300
PHP session.auto_start option must be disabled In php.ini:session.auto_start = 0
Database support One of: IBM DB2, MySQL, Oracle, PostgreSQL, SQLite One of the following modules must be installed:ibm_db2, mysql, oci8, pgsql, sqlite3
bcmath php-bcmath
mbstring php-mbstring
PHP mbstring.func_overload option must be disabled In php.ini:mbstring.func_overload = 0
PHP always_populate_raw_post_data option must be disabled Required only for PHP versions 5.6.0 or newer.In php.ini:always_populate_raw_post_data = -1
sockets php-net-socket. Required for user script support.
gd 2.0 or higher php-gd. PHP GD extension must support PNG images (--with-png-dir), JPEG (--with-jpeg-dir) images and FreeType 2 (--with-freetype-dir).
libxml 2.6.15 php-xml or php5-dom
xmlwriter php-xmlwriter
xmlreader php-xmlreader
ctype php-ctype
session php-session
gettext php-gettext,Since Zabbix 2.2.1, the PHP gettext extension is not a mandatory requirement for installing Zabbix. If gettext is not installed, the frontend will work as usual, however, the translations will not be available.

php环境检测通过的结果如下图所示:

zabbix web php环境检测通过图1 zabbix web php环境检测通过图2 zabbix web DB配置 zabbix server详细信息 zabbix web安装信息汇总 zabbix web conf install
文件导入完成后,点击Finish,会出现以下信息,即表示zabbix web安装成功。 zabbix web安装成功提示信息

初始情况下,只配置了让zabbix server监控它自己,下面继续配置一台Linux server,让zabbix server可以监控它,并可以发送监控通知邮件。

9.被监控端安装zabbix agent

安装过程还是使用源码编译安装的方式,先创建zabbix用户。

useradd -s /sbin/nologin -M zabbix

在被监控的Linunx server上,只需要安装zabbix agent,在configure时使用如下参数即可:

./configure  --prefix=/usr/local/zabbix-3.2.4 --enable-agent

configure完成以后,注意观察汇总信息是否有错误提示,是否只启用了agent,有问题根据实际情况去排查并解决。如果没有错误,则根据提示信息执行make install完成安装过程。
被监控端设置软链接,添加zabbix执行程序路径至PATH变量,修改zabbix_agentd.conf文件以及zabbix_agentd服务开机自启的过程参考前文相关内容即可,此处不在赘述。
最后启动被监控端的zabbix_agentd服务。

service zabbix_agentd start

10.zabbix server上配置监控内容

在zabbix server上配置刚安装了zabbix agent的主机,并设置要监控的内容。

zabbix web添加主机1
根据实际情况填写主机名,选择主机分组,主机ip,对于主机分组此处我选择Linux servers作为示例。
然后可以选择Host旁的Templates,zabbix内置了很多种不同的模板,它包含的有监控条目item,告警触发器trigger,图形展示Graphs,屏幕Screens等。以下为给主机应用模板的操作过程: 给主机应用监控模板1 选择相应的监控模板 确认主机是否应用了监控模板
示例中的server3上我启用的有web服务,所以这里选择了zabbix内置的Template App HTTP Serivice这一模板对http服务进行监控。 等待片刻,ZBX会显示成绿色即表示zabbix server能和server3上的zabbix agent正常通信了,如下所示: zabbix server已可监控到server3

11.配置zabbix server发送通知邮件

zabbix server默认支持多种发送通知的方式,其中就包含的有名为Email的方式,但经过多次尝试都未能实现让zabbix server发送通知邮件,所以这里使用zabbix调用外部脚本且借助公网smtp服务器的方式来发送通知邮件。详细过程如下:

zabbix自定义media type
#!/bin/bash
#
file=/tmp/mailtmp.txt
echo "$3" > $file         #$3为上一步中定义的{ALERT.MESSAGE},即告警详细信息
dos2unix -k $file         #实际使用中发现zabbix所发邮件的正文会以一
#个.dat结尾的文件作为附件来进行发送,使用dos2unix将邮件正文进行格
#式转换,这样在收到邮件后正文直接显示通知的正文,-k是保持文件的时
#间戳不变。
/bin/mail -s "$2" $1 < $file  #$2为上一步中定义的{ALERT.SUBJECT},即
#邮件主题,$1为上一步中定义的{ALERT.SENDTO}即收件人。  
给用户关联相应的Media-1 给用户关联相应的Media-2 给用户关联相应的Media-3 定义action名称及触发条件 定义故障时action的具体行为-1 定义故障时action的具体行为-2 定义故障恢复后action的具体行为 查看刚定义的action
从上述过程也可以看到其实这些action定义的是由Trigger来触发的,因为前面我是直接给主机应用了zabbix内置的http service的模板,它里面已经关联的有相应的trigger了,所以当http service发生变化,会自动触发相应的trigger,而后又触发zabbix 执行相应的action。这是zabbix发送通知的一个大致的过程。
set from=zabbix@163.com    #提供一个可用的163邮箱
set smtp=smtp.163.com
set smtp-auth-user=zabbix
set smtp-auth-password=xxxxxx   #163邮箱的授权码,在邮箱网页中去设置
set smtp-auth=login
zabbix通知邮件

12.小结

以上即为zabbix实现监控Linux服务器并发送通知邮件的实现过程小记。只是zabbix的基础入门,还有很多内容需要在不断的使用中熟悉,也可以参考官网手册,会有很多收获。

上一篇 下一篇

猜你喜欢

热点阅读