kuyjhtgrf 发表于 2017-1-11 09:26:15

centos7 搭建zabbix3.0.5监控系统

一、实验环境:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
# uname -r
3.10.0-327.el7.x86_64
#
# ifconfig eno16777736
eno16777736: flags=4163mtu 1500
      inet 10.0.0.98netmask 255.255.255.0broadcast 10.0.0.255
      inet6 fe80::20c:29ff:fef2:d583prefixlen 64scopeid 0x20
      ether 00:0c:29:f2:d5:83txqueuelen 1000(Ethernet)
      RX packets 150143bytes 218651391 (208.5 MiB)
      RX errors 0dropped 0overruns 0frame 0
      TX packets 30015bytes 2046005 (1.9 MiB)
      TX errors 0dropped 0 overruns 0carrier 0collisions 0
#






二、安装软件包:

1
2
3
4
5
6
7
8
# ll
total 65776
-rw-r--r-- 1 root root   523321 Jan 10 10:27 libmcrypt-2.5.7.tar.gz
-rw-r--r-- 1 root root 32167628 Nov 28 07:46 mysql-5.6.35.tar.gz
-rw-r--r-- 1 root root   910812 Oct 18 11:14 nginx-1.10.2.tar.gz
-rw-r--r-- 1 root root 18331135 Aug62015 php-5.6.12.tar.gz
-rw-r--r-- 1 root root 15412400 Jan 10 12:01 zabbix-3.0.5.tar.gz
#





三、安装步骤
(1)创建用户zabbix

1
2
3
4
# useradd zabbix -s /sbin/nologin
# id zabbix
uid=1000(zabbix) gid=1000(zabbix) groups=1000(zabbix)
#





(2)安装执行编译MySQL的编译工具cmake,(从MySQL5.5以后编译用工具cmake)

1
2
# yum -y install cmake cmake-devel
#





(3)创建MySQL用户

1
2
3
4
# useradd mysql -s /sbin/nologin
# id mysql
uid=1001(mysql) gid=1001(mysql) groups=1001(mysql)
#





(4)安装MySQL,用cmake编译,先安装依赖包openssl openssl-devel ncurses ncurses-devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# yum install -y openssl openssl-devel ncurses ncurses-devel
# cd tools
# tar xf mysql-5.6.35.tar.gz
# cd mysql-5.6.35
#
# cmake
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql
-DMYSQL_DATADIR=/usr/local/mysql/data
-DSYSCONFDIR=/etc
-DWITH_MYISAM_STORAGE_ENGINE=1
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_MEMORY_STORAGE_ENGINE=1
-DWITH_READLINE=1
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock
-DMYSQL_TCP_PORT=3306
-DENABLED_LOCAL_INFILE=1
-DWITH_PARTITION_STORAGE_ENGINE=1
-DEXTRA_CHARSETS=all
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
-DMYSQL_USER=mysql
-DWITH_DEBUG=0
-DWITH_SSL=system
# make && make install





(5)配置MySQL
#修改MySQL安装目录权限

1
# chown -R mysql.mysql /usr/local/mysql/





#拷贝配置文件

1
# cp support-files/my-default.cnf /etc/my.cnf





#修改配置文件

1
2
3
4
5
6
7
8
9
10
11
# vim/etc/my.cnf

port = 3306
socket = /usr/local/mysql/data/socket

basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
server_id = 1
socket = /usr/local/mysql/data/socket
#





#初始化数据库生成数据库基本数据结构

1
# /usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql





#拷贝MySQL数据库启动脚本,赋可执行权限并设置开机启动

1
2
3
4
5
6
# cp support-files/mysql.server /etc/init.d/mysqld
# chmod +x /etc/init.d/mysqld
# ll /etc/init.d/mysqld
-rwxr-xr-x 1 root root 10875 Jan 10 18:11 /etc/init.d/mysqld
#
# chkconfig mysqld on





#修改PATH变量

1
2
3
4
5
# echo 'export PATH=/usr/local/mysql/bin:$PATH'>>/etc/profile
# tail -1 /etc/profile
export PATH=/usr/local/mysql/bin:$PATH
# source /etc/profile
#





#启动MySQL 测试是否登录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
# mysql
Welcome to the MySQL monitor.Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.6.35 Source distribution
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql> s
--------------
mysqlVer 14.14 Distrib 5.6.35, for Linux (x86_64) usingEditLine wrapper
Connection id:          1
Current database:
Current user:         root@localhost
SSL:                  Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:      ;
Server version:         5.6.35 Source distribution
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    utf8
Db   characterset:    utf8
Client characterset:    utf8
Conn.characterset:    utf8
UNIX socket:            /usr/local/mysql/data/socket
Uptime:               8 sec
Threads: 1Questions: 5Slow queries: 0Opens: 70Flush tables: 1Open tables: 63Queries per second avg: 0.625
--------------
mysql>





#修改数据库密码,默认密码为空


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('root');
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
#删除不必要的用户
mysql> select user,host from user;
+------+--------------+
| user | host         |
+------+--------------+
| root | 127.0.0.1    |
| root | ::1          |
|      | localhost    |
| root | localhost    |
|      | zabbixserver |
| root | zabbixserver |
+------+--------------+
6 rows in set (0.00 sec)
mysql>
mysql> delete from user where user="root" and host="::1";
Query OK, 1 row affected (0.00 sec)
mysql> delete from user where user="" and host="zabbixserver";
Query OK, 1 row affected (0.00 sec)
mysql> select user,host from user;
+------+--------------+
| user | host         |
+------+--------------+
| root | 127.0.0.1    |
|      | localhost    |
| root | localhost    |
| root | zabbixserver |
+------+--------------+
4 rows in set (0.00 sec)
mysql> delete from user where user="" and host="localhost";
Query OK, 1 row affected (0.00 sec)
mysql> select user,host from user;
+------+--------------+
| user | host         |
+------+--------------+
| root | 127.0.0.1    |
| root | localhost    |
| root | zabbixserver |
+------+--------------+
3 rows in set (0.00 sec)
mysql>





#创建zabbix访问MySQL的用户和存放zabbix的数据库zabbix


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
mysql> GRANT ALL PRIVILEGES ON zabbix.* TO 'zabbix'@'127.0.0.1' IDENTIFIED BY 'zabbix' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
mysql> create database zabbix default charset utf8;
Query OK, 1 row affected (0.00 sec)
mysql> select user,host from user;
+--------+--------------+
| user   | host         |
+--------+--------------+
| root   | 127.0.0.1    |
| zabbix | 127.0.0.1    |
| root   | localhost    |
| root   | zabbixserver |
+--------+--------------+
4 rows in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database         |
+--------------------+
| information_schema |
| mysql            |
| performance_schema |
| test               |
| zabbix             |
+--------------------+
5 rows in set (0.00 sec)
mysql>




(6)安装PHP
#安装依赖包


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# yum install -y libxml2-devel openssl-devel libcurl-devel libjpeg-turbo-devel libpng-devel freetype-devel libmcrypt-devel
#创建启动PHP的用户和用户组,解压PHP压缩包进入压缩目录进行编译安装
# useradd www -s /sbin/nologin
# id www
uid=1002(www) gid=1002(www) groups=1002(www)
#
# cd tools
# tar xf php-5.6.12.tar.gz
# cd php-5.6.12
#
./configure --prefix=/usr/local/php
--with-config-file-path=/usr/local/php/etc
--enable-fpm
--with-fpm-user=www
--with-fpm-group=www
--with-mysql=mysqlnd
--with-mysqli=mysqlnd
--with-pdo-mysql=mysqlnd
--with-iconv-dir
--with-freetype-dir
--with-jpeg-dir
--with-png-dir
--with-zlib
--with-libxml-dir=/usr
--enable-xml
--disable-rpath
--enable-bcmath
--enable-shmop
--enable-sysvsem
--enable-inline-optimization
--with-curl
--enable-mbregex
--enable-mbstring
--with-mcrypt
--enable-ftp
--with-gd
--enable-gd-native-ttf
--with-openssl
--with-mhash
--enable-pcntl
--enable-sockets
--with-xmlrpc
--enable-zip
--enable-soap
--without-pear
--with-gettext
--disable-fileinfo
--enable-maintainer-zts
# make && make install





#配置PHP相关服务

1
2
3
4
5
6
7
8
9
10
11
12
13
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
# cp php.ini-production /usr/local/php/etc/php.ini
# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod +x /etc/init.d/php-fpm
# ll /etc/init.d/php-fpm
-rwxr-xr-x 1 root root 2354 Jan 10 18:54 /etc/init.d/php-fpm
#
# chkconfig php-fpm on
# service php-fpm start
Starting php-fpmdone
# netstat -lnupt|grep php-fpm
tcp      0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      51556/php-fpm: mast
#





(7)安装Nginx


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# tar xf nginx-1.10.2.tar.gz
# cd nginx-1.10.2
# ./configure --prefix=/usr/local/nginx
--pid-path=/usr/local/nginx/nginx.pid
--error-log-path=/usr/local/nginx/error.log
--http-log-path=/usr/local/nginx/access.log
--with-http_ssl_module --with-mail
--with-mail_ssl_module --with-stream
--with-threads
# make && make install
# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# /usr/local/nginx/sbin/nginx
# netstat -lnupt|grep nginx
tcp      0      0 0.0.0.0:80            0.0.0.0:*               LISTEN      54318/nginx: master
#





(8)安装zabbix

#安装依赖包

1
# yum install -y net-snmp-devel libssh2-devel





#解压编译安装

1
2
3
4
5
6
7
8
# tar xf zabbix-3.0.5.tar.gz
# cd zabbix-3.0.5
# ./configure --prefix=/usr/local/zabbix
--enable-server --enable-agent
--with-mysql --with-net-snmp
--with-libcurl --with-libxml2
--with-ssh2
# make && make install





#验证安装结果

1
2
3
4
5
6
7
8
# /usr/local/zabbix/sbin/zabbix_server -V
zabbix_server (Zabbix) 3.0.5
Revision 62889 30 September 2016, compilation time: Jan 10 2017 19:04:09
Copyright (C) 2016 Zabbix SIA
License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it according to
the license. There is NO WARRANTY, to the extent permitted by law.
#





#查看配置文件默认设置

1
2
3
4
5
6
7
# egrep -v "^#|^$" /usr/local/zabbix/etc/zabbix_server.conf
LogFile=/tmp/zabbix_server.log
DBName=zabbix
DBUser=zabbix
Timeout=4
LogSlowQueries=3000
#





#修改后为

1
2
3
4
5
6
7
8
9
10
# egrep -v "^#|^$" /usr/local/zabbix/etc/zabbix_server.conf
LogFile=/usr/local/zabbix/etc/log/zabbix_server.log
PidFile=/usr/local/zabbix/etc/zabbix_server.pid
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix
Timeout=4
LogSlowQueries=3000
#





#创建对应目录并修改属主和属组

1
2
3
# mkdir -p /usr/local/zabbix/etc/log
# chown -R zabbix.zabbix /usr/local/zabbix/*
#





#导入zabbix数据到zabbix数据库,注意顺序,一定是schema.sql、images.sql、data.sql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# mysql -uzabbix -pzabbix -h127.0.0.1 zabbix < /root/tools/zabbix-3.0.5/database/mysql/schema.sql
# mysql -uzabbix -pzabbix -h127.0.0.1 zabbix < /root/tools/zabbix-3.0.5/database/mysql/images.sql
# mysql -uzabbix -pzabbix -h127.0.0.1 zabbix < /root/tools/zabbix-3.0.5/database/mysql/data.sql
#
mysql> use zabbix
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
mysql> show tables;
+----------------------------+
| Tables_in_zabbix         |
+----------------------------+
| acknowledges               |
| actions                  |
| alerts                     |
| application_discovery      |
| application_prototype      |
| application_template       |
| applications               |
| auditlog                   |
| auditlog_details         |
| autoreg_host               |
| conditions               |
| config                     |
| dbversion                  |
| dchecks                  |
| dhosts                     |
| drules                     |
| dservices                  |
| escalations                |
| events                     |
| expressions                |
| functions                  |
| globalmacro                |
| globalvars               |
| graph_discovery            |
| graph_theme                |
| graphs                     |
| graphs_items               |
| group_discovery            |
| group_prototype            |
| groups                     |
| history                  |
| history_log                |
| history_str                |
| history_text               |
| history_uint               |
| host_discovery             |
| host_inventory             |
| hostmacro                  |
| hosts                      |
| hosts_groups               |
| hosts_templates            |
| housekeeper                |
| httpstep                   |
| httpstepitem               |
| httptest                   |
| httptestitem               |
| icon_map                   |
| icon_mapping               |
| ids                        |
| images                     |
| interface                  |
| interface_discovery      |
| item_application_prototype |
| item_condition             |
| item_discovery             |
| items                      |
| items_applications         |
| maintenances               |
| maintenances_groups      |
| maintenances_hosts         |
| maintenances_windows       |
| mappings                   |
| media                      |
| media_type               |
| opcommand                  |
| opcommand_grp            |
| opcommand_hst            |
| opconditions               |
| operations               |
| opgroup                  |
| opinventory                |
| opmessage                  |
| opmessage_grp            |
| opmessage_usr            |
| optemplate               |
| profiles                   |
| proxy_autoreg_host         |
| proxy_dhistory             |
| proxy_history            |
| regexps                  |
| rights                     |
| screen_user                |
| screen_usrgrp            |
| screens                  |
| screens_items            |
| scripts                  |
| service_alarms             |
| services                   |
| services_links             |
| services_times             |
| sessions                   |
| slides                     |
| slideshow_user             |
| slideshow_usrgrp         |
| slideshows               |
| sysmap_element_url         |
| sysmap_url               |
| sysmap_user                |
| sysmap_usrgrp            |
| sysmaps                  |
| sysmaps_elements         |
| sysmaps_link_triggers      |
| sysmaps_links            |
| timeperiods                |
| trends                     |
| trends_uint                |
| trigger_depends            |
| trigger_discovery          |
| triggers                   |
| users                      |
| users_groups               |
| usrgrp                     |
| valuemaps                  |
+----------------------------+
113 rows in set (0.01 sec)
mysql>





#添加启动脚本赋予执行权限并修改


1
2
3
4
5
6
7
8
9
# cp misc/init.d/fedora/core/zabbix_agentd /etc/init.d/zabbix_agentd
# cp misc/init.d/fedora/core/zabbix_server /etc/init.d/zabbix_server
#
# sed -i "s#BASEDIR=/usr/local#BASEDIR=/usr/local/zabbix#g" /etc/init.d/zabbix_server
# sed -i "s#BASEDIR=/usr/local#BASEDIR=/usr/local/zabbix#g" /etc/init.d/zabbix_agentd
#
# chmod +x /etc/init.d/zabbix_agentd
# chmod +x /etc/init.d/zabbix_server
#





#设置开机启动


1
2
3
# chkconfig zabbix_server on
# chkconfig zabbix_agentd on
#





#设置zabbix支持Nginx+PHP组合访问


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# mkdir -p /data/web/site/www
# cp -rfp frontends/php/* /data/web/site/www/ -R
# chown -R root.root /data/web/site/www/
#
# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
# vim /usr/local/nginx/conf/nginx.conf
userroot;
worker_processes4;
#error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;
#pid      logs/nginx.pid;
events {
    worker_connections1024;
}
http {
    include       mime.types;
    default_typeapplication/octet-stream;
    #log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    #access_loglogs/access.logmain;
    sendfile      on;
    #tcp_nopush   on;
    #keepalive_timeout0;
    keepalive_timeout65;
    #gzipon;
    server {
      listen       80;
      server_namelocalhost;
      #charset koi8-r;
      #access_loglogs/host.access.logmain;
      location / {
            root   /data/web/site/www;
            indexindex.php index.html index.htm;
      }
      error_page   500 502 503 504/50x.html;
      location = /50x.html {
            root   html;
      }
      # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

      location ~ .php$ {
            root         /data/web/site/www;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_indexindex.php;
            fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
            #fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
            include      fastcgi_params;
      }
    }
}
# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# /usr/local/nginx/sbin/nginx -s reload
#





#清除防火墙策略


1
2
3
4
# iptables -F
# iptables -X
# iptables -Z
#





通过浏览器输入http://10.0.0.98进入安装zabbix界面

点击“Next step”

遇到上述错误是因为PHP配置文件/usr/local/php/etc/php.ini的中的默认参数不能满足zabbix要求,这里我们做如下调整:

1
2
3
4
5
6
7
8
9
post_max_size = 32M
max_execution_time = 300
max_input_time = 300
date.timezone = "Asia/Shanghai"
always_populate_raw_post_data = -1
# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpmdone
#





然后刷新页面即可,然后下一步

输入密码"zabbix",下一步

输入"127.0.0.1"之后直接下一步

接着下一步

再下一步

按照提示下载zabbix.conf.php文件放置于/data/web/site/www/conf目录下即可

1
2
3
4
5
6
7
8
9
10
11
# cd /data/web/site/www/conf
# rz
rz waiting to receive.
Starting zmodem transfer.Press Ctrl+C to cancel.
100%   415 bytes415 bytes/s 00:00:01       0 Errors
# ll
total 12
-rw-r--r-- 1 root root 1036 Sep 30 05:42 maintenance.inc.php
-rw-r--r-- 1 root root415 Jan 10 10:48 zabbix.conf.php
-rw-r--r-- 1 root root411 Sep 30 05:42 zabbix.conf.php.example
#





刷新页面,点击“finish”完成zabbix服务端的安装


注意:在安装PHP时会出现一些错误,下面是一些常见错误解决方法
安装php遇到问题解决办法:
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
解决办法
wget ftp://mcrypt.hellug.gr/pub/crypt ... mcrypt-2.5.7.tar.gz

#解压
tar -zxvf libmcrypt-2.5.7.tar.gz   

#进入目录
cd libmcrypt-2.5.7

#编译(默认安装到/usr/local/lib/)
./configure --prefix=/usr/local/libmcrypt

#执行安装
make && make install


configure: error: Don't know how to define struct flock on this system, set --enable-opcache=no

解决方法一:

查看php官方文档解决. 在configure 里面加上 --with-libdir=lib64

解决方法二:

编辑/etc/ld.so.conf

根据系统,加入include /etc/ld.so.conf,

然后执行ldconfig,使其重新加载一次;(如果出错改为vi /etc/ld.so.conf >> include /usr/local/libmcrytp/lib/)


configure: error: Don't know how to define struct flock on this system, set --enable-opcache=no


解决方法是:
echo /usr/local/mysql/lib >> /etc/ld.so.conf.d/mysql-x86_64.conf
ldconfig -v

blueghost 发表于 2017-3-1 19:51:22

非常感谢分享
页: [1]
查看完整版本: centos7 搭建zabbix3.0.5监控系统