1. 程式人生 > >Zabbix4.0 for PostgreSQL (上篇):基礎環境的搭建部署

Zabbix4.0 for PostgreSQL (上篇):基礎環境的搭建部署

文章目錄

概述

Zabbix大家都很熟悉,一般的是使用MySQL作為儲存庫來使用的,而我看使用手冊上是使用postgresql也是可以的,所以就做了一次基於pg的zabbix搭建測試,記錄下來,分享給大家

Server端配置

配置PG環境

安裝好pg,且在root和postgres下都配置好pg的環境變數,以供後續使用

在hba控制檔案中直接加入這一行

host    zabbix          zabbix          192.168.0.0/24          trust
host    all             all             127.0.0.1/32            md5

配置apache

建立user

[[email protected]_GW_TEST opt]# adduser apache

準備好安裝包並解壓

[[email protected]_GW_TEST opt]# ls httpd-2.4.37.tar.gz 
httpd-2.4.37.tar.gz
[[email protected]_GW_TEST opt]# tar -xvzf httpd-2.4.37.tar.gz 

編譯安裝

執行編譯命令

[[email protected]_GW_TEST httpd-2.4.37]# ./configure --prefix=/usr/local/apache  --enable-rewrite --enable-deflate --enable-expires --enable-headers --enable-modules=most --with-mpm=worker --enable-so

報錯:

configure: error: APR not found.  Please read the documentation.

解決:

yum install apr
yum install apr-util-devel

報錯:

configure: error: in `/opt/httpd-2.4.37':
configure: error: C compiler cannot create executables
See `config.log' for more details

解決:

yum install gcc

問題:

configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/

解決:

yum -y install pcre-devel

問題:

configure: error: mod_deflate has been requested but can not be built due to prerequisite failures

解決:

yum install  -y zlib-devel

以下畫面說明編譯成功

configure: summary of build options:

    Server Version: 2.4.37
    Install prefix: /usr/local/apache
    C compiler:     gcc -std=gnu99
    CFLAGS:           -pthread  
    CPPFLAGS:        -DLINUX -D_REENTRANT -D_GNU_SOURCE  
    LDFLAGS:           
    LIBS:             
    C preprocessor: gcc -E

繼續安裝

gmake
gmake install

安裝成功之後安裝目錄也就生成檔案了

[[email protected]_GW_TEST apache]# ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs  man  manual  modules

建立指令碼根目錄

用來存放html資訊

mkdir /home/www
chown -R apache:apache /home/www

配置執行引數

[[email protected] httpd-2.2.31]# vim /usr/local/apache/conf/httpd.conf

#監聽port
Listen 80

#運營apache服務的使用者及使用者組,為apache
User apache
Group apache

#管理員的郵箱是 
ServerAdmin [email protected]

#配置指令碼根目錄
DocumentRoot "/home/www"

#配置
ServerName 127.0.0.1:80
<Directory />
    Options FollowSymLinks
    AllowOverride None
    Order deny,allow
    allow from all
</Directory>

<Directory "/home/www">
    Options Indexes FollowSymLinks    
    AllowOverride None   
    Order allow,deny
    Allow from all
</Directory>

配置環境變數

新增一行環境變數到檔案中

export PATH=/usr/local/apache/bin:$PATH
[[email protected]_GW_TEST apache]# source ~/.bash_profile
[[email protected]_GW_TEST apache]# which apachectl
/usr/local/apache/bin/apachectl

啟動apache服務

apachectl start

順便配置開機自動

vim /etc/rc.d/rc.local
/usr/local/apache/bin/apachectl start

至此,Apache配置完成

安裝配置php

安裝php依賴包

wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar zxf libiconv-1.14.tar.gz
./configure --prefix=/usr/local/libiconv
make
make install

準備好包並解壓

 tar -xvzf php-5.5.38.tar.gz

編譯

./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache/bin/apxs --with-xmlrpc --with-openssl --with-zlib --with-freetype-dir --with-gd --with-jpeg-dir --with-png-dir --with-iconv=/usr/local/libiconv --enable-short-tags --enable-sockets --enable-soap --enable-mbstring --enable-static --enable-gd-native-ttf --with-curl --with-xsl --enable-ftp --with-libxml-dir  --with-pgsql=/usr/postgresql --enable-fpm --enable-bcmath --with-gettext

報錯

Sorry, I cannot run apxs.  Possible reasons follow:

1. Perl is not installed
2. apxs was not found. Try to pass the path using --with-apxs2=/path/to/apxs
3. Apache was not built using --enable-so (the apxs usage page is displayed)

The output of /usr/local/apache/bin/apxs follows:
./configure: /usr/local/apache/bin/apxs: /replace/with/path/to/perl/interpreter: bad interpreter: No such file or directory
configure: error: Aborting

解決:將apache的bin檔案下的apxs檔案第一行改為

#!/usr/bin/perl -w

報錯:

configure: error: xml2-config not found. Please check your libxml2 installation

yum install libxml2-devel

報錯

configure: error: Cannot find OpenSSL's <evp.h>

yum install openssl openssl-devel

報錯

configure: error: Please reinstall the libcurl distribution -
    easy.h should be in <curl-dir>/include/curl/

yum install curl curl-devel

報錯

configure: error: There is something wrong. Please check config.log for more information.

curl-devel-7.15.5-2.el5安裝

報錯

configure: error: jpeglib.h not found.

yum -y install libjpeg-devel

報錯

configure: error: png.h not found.

yum install libpng libpng-devel

報錯:

configure: error: freetype-config not found.

yum install freetype-devel

報錯

configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution

yum -y install libxslt libxslt-devel

顯示如下,則編譯完成

creating main/internal_functions_cli.c
+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE.  By continuing this installation |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.

config.status: creating php5.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

安裝

make
make install

準備配置檔案

[[email protected]_GW_TEST php-5.5.38]# cp php.ini-development /usr/local/php/lib/php.ini
[[email protected]_GW_TEST php-5.5.38]# vi /usr/local/php/lib/php.ini 

修改配置檔案

[[email protected] php-5.5.38]# cp php.ini-development /usr/local/php/lib/php.ini
[[email protected] php-5.5.38]# vim /usr/local/php/lib/php.ini

error_reporting = E_ALL & ~E_NOTICE

#報告所有的錯誤,但除了E_NOTICE這一種
display_errors = On
#在終端顯示出錯資訊,這個在生產環境最好設定成off
track_errors = Off
#是否在變數$php_errormsg中儲存最近一個錯誤或警告訊息
post_max_size = 16M
#表單最大提交的資料量大小
upload_max_filesize = 16M
#准許上傳檔案大小限制為16M

date.timezone = PRC
#中華人民共和國時區

expose_php = Off
#隱藏php版本資訊

curl -I www.ha97.com
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 20 Jul 2010 05:45:13 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
#已經徹底隱藏了PHP版本。

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20121212/"
#擴充套件模組存放目錄

修改Apache的檔案,在重啟

[[email protected] php-5.5.38]# vim /usr/local/apache/conf/httpd.conf
增加下面內容

<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>

<IfModule mime_module>
    AddType application/x-httpd-php-source .phps
    AddType application/x-httpd-php .php .phtml   
</IfModule>

換一個思路的nginx搭建

概述

在apache配置解析php的操作種,遇到種種坑,Apache的配置不太熟悉,介面打不開php檔案,理論上問題是出在Apache配置訪問php上。但是我能力有限,解決不來,所以就換一個NGINX試試。

編譯安裝

上傳解壓,並編譯。NGINX的編譯相對簡單,就不做詳細記錄

tar -xvzf nginx-1.6.0.tar.gz
cd nginx-1.6.0

./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
make
make install

配置NGINX訪問PHP

這個是NGINX的關鍵,因為zabbix的介面全部都是PHP語言寫的,必須使用PHP軟體才能解析應用

server {
			listen       80;
			server_name  localhost;
			
			
			location ~ \.(php|php5)?$ {
				root /data/html;
				fastcgi_pass 127.0.0.1:9000;
				fastcgi_index  index.php;
				fastcgi_param SCRIPT_FILENAME /data/html$fastcgi_script_name;
				include fastcgi_params;
			}
			
			
			location ~* ^.+\.(ico|gif|jpg|jpeg|png|html|css|htm|bmp|js|svg)$ {
				root  /data/html;
			}
		}

配置nginx.conf檔案。增加兩個location,第一個location增加的是站點的地址資訊。第二個是增加解析各種圖片樣式的功能(因為在沒有加的時候,我開啟的zabbix介面全部都是隻有文字,沒有任何圖片與樣式,如下。原因就是PHP沒有解析這些內容)

建立好站點目錄

瀏覽器通過埠來訪問伺服器的檔案,並用解析器解析,我們需要先準備好這些檔案。zabbix的PHP檔案是放在原始碼包裡的,解壓後就可以看到可以在上面的配置檔案中可以看到我已經選擇了/data/html

cp -r  /opt/zabbix-4.0.2/frontends/php/*   /data/html

注意賦權

啟動nginx

把環境變數配置在root下

export PATH=/usr/local/nginx/sbin:$PATH

啟動nginx,沒有報錯則啟動成功,看監聽埠和後臺程序

[[email protected]_GW_TEST ~]# netstat -lnp|grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      25373/nginx: master 


[[email protected]_GW_TEST ~]# ps -ef|grep nginx
root     12817 10266  0 18:11 pts/1    00:00:00 grep --color=auto nginx
root     25373     1  0 01:21 ?        00:00:00 nginx: master process nginx
www      27717 25373  0 02:07 ?        00:00:00 nginx: worker process

Zabbix配置

使用者環境配置好

使用者

adduser zabbix

編譯zabbix

編譯

./configure --prefix=/usr/local/zabbix --enable-server --enable-proxy --enable-agent --enable-ipv6 --with-postgresql=/usr/local/pgsql/bin/pg_config --with-net-snmp --with-ssh2 --with-openipmi --with-ldap --with-libcurl --with-iconv

安裝依賴

yum install net-snmp-devel
yum install libssh2-devel
yum install OpenIPMI-devel
yum install openldap-devel
yum install libevent-devel
yum install curl-devel

編譯到此介面則安裝成功


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

執行make install

配置好環境變數

將此行寫進配置檔案中

PATH=/usr/local/zabbix/bin:/usr/local/zabbix/sbin:$PATH

建立zabbix 資料庫

postgres=# create role zabbix login
postgres-# ;
CREATE ROLE
postgres=# \password zabbix
Enter new password: 
Enter it again: 
postgres=#  create database zabbix with template template0 encoding 'UTF8' ;
CREATE DATABASE
postgres=#  grant all on database zabbix to zabbix;
GRANT
postgres=# \q

匯入zabbix資料

在原始碼下,有zabbix資料庫的資料

cd /opt/zabbix-4.0.2/database/postgresql/
psql -Uzabbix -dzabbix -h127.0.0.1 -f schema.sql
psql -Uzabbix -dzabbix -h127.0.0.1 -f images.sql
psql -Uzabbix -dzabbix -h127.0.0.1 -f data.sql

配置zabbix_server引數

[[email protected]_GW_server ~]# vim /usr/local/zabbix/etc/zabbix_server.conf

#監聽port
ListenPort=10051

#日誌存放地點及大小
LogFile=/tmp/zabbix_server.log
LogFileSize=10

#pid檔案存放地點
PidFile=/tmp/zabbix_server.pid

#下面幾個引數是配置資料庫連線引數
DBHost=127.0.0.1
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix
DBPort=5432

#監聽ip範圍
ListenIP=0.0.0.0

#media types中使用script存放目錄
AlertScriptsPath=/usr/local/zabbix/alertscripts

啟動服務

zabbix_server

檢視是否有錯,就檢視zabbix_server 的啟動日誌,如果有報錯,按照報錯修改。反正我安裝的時候真的報錯頻出,經歷過,懂得都懂

檢視web介面

此時就可以通過web介面來訪問zabbix了
輸入地址

可以看到,配存在一些問題也還是需要修改的,不然的話無法安裝

agent端配置

概述

agnet端就是被監控的資料庫主機,也是需要做一些安裝

建立使用者

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

編譯安裝

 ./configure --prefix=/usr/local/zabbix-agent --enable-agent
make install

無需指定其他的編譯引數

修改配置檔案

 vi /usr/local/zabbix-agent/etc/zabbix_agentd.conf
 
LogFile=/tmp/zabbix_agentd.log
Server=192.168.6.17/32
Hostname=Zabbix agent

配置到環境變數

PATH=/usr/local/zabbix-agent/bin:/usr/local/zabbix-agent/sbin:$PATH

啟動zabbix_agent

直接執行zabbix_agent命令,檢視日誌,觀察是否正常

至此

至此,zabbix的基本配置完成。後續還要配置監控等操作。後面慢慢再說