Zabbix-Centos7-源码包安装Server及异常记录
Part1.安装操作系统Centos7
1.最小化安装
2.虚拟机安装
3.修改网络参数
# yum -y install vim #vim /etc/sysconfig/network-scripts/ifcfg-ens33 BOOTPROTO=static ONBOOT=yes IPADDR=192.168.0.88 NETMASK=255.255.255.0 GATEWAY=192.168.0.1 DNS1=114.114.114.114 #systemctl restart network
4.安装一些常用包
# yum install -y tree # yum install -y lrzsz
Part3.编译安装zabbix源码包
1.解压包
# tar -zxvf zabbix-4.0.13.tar.gz
2.创建zabbix用户
# groupadd zabbix # useradd -g zabbix zabbix -s /sbin/nologin # cat /etc/passwd |grep zabbix zabbix:x:1001:1001::/home/zabbix:/sbin/nologin
3.尝试编译zabbix
创建zabbix安装目录 # cd /usr/local/ # mkdir zabbix 进入zabbix解压目录 # cd /usr/local/src/zabbix-4.0.13 查看编译前准备帮助 # ./configure --help 尝试编译 --prefix=/usr/local/zabbix //指定安装目录 --enable-static //静态库安装 --enable-server //安装zabbix-server --enable-proxy //安装zabbix-proxy --enable-agent //安装zabbix-agent --with-mysql //使用数据库mysql相关 --with-libxml2 //xml库 --with-unixodbc //使用odbc连接数据库 --with-net-snmp //支持snmp --with-ssh2 //支持ssh2 --with-openipmi //支持硬件ipmi监控 --with-ldap //支持使用LDAP认证 --with-libcurl //支持url检测 --with-sqlite3 //proxy可以使用sqlite数据库 --with-iconv //支持字符集转换 # ./configure --prefix=/usr/local/zabbix --enable-static --enable-server --enable-proxy --enable-agent --with-mysql --with-libxml2 --with-unixodbc --with-net-snmp --with-ssh2 --with-openipmi --with-ldap --with-libcurl --with-sqlite3 --with-iconv 错误1: configure: error: no acceptable C compiler found in $PATH 解决方法:安装gcc编译器 yum -y install gcc 错误2: checking if static linking is possible... no configure: error: static linking is not possible on this system 解决方法:系统不支持静态库安装,去掉 --enable-static 未找到具体不支持原因,有待研究; 错误3: configure: error: MySQL library not found 解决方法:安装mysql-devel(mysql库和文件) yum install mysql-devel -y 错误4: configure: error: SQLite is not supported as a main Zabbix database backend. 解决方法:sqlite与zabbix-server貌似无法共存,去掉该项 错误5: configure: error: LIBXML2 library not found 解决方法:安装libxml2-devel # yum -y install libxml2-devel 错误6: configure: error: unixODBC library not found 解决方法:安装unixODBC-devel # yum install unixODBC-devel -y 错误7: configure: error: Invalid Net-SNMP directory - unable to find net-snmp-config 解决方法:安装net-snmp-devel # yum install net-snmp-devel -y 错误8: configure: error: SSH2 library not found 解决方法:安装libssh2 # yum install libssh2 -y未解决 # yum install libssh2-devel 解决 错误9: configure: error: Invalid OPENIPMI directory - unable to find ipmiif.h 解决方法:安装OpenIPMI-devel # yum -y install OpenIPMI-devel 错误10: configure: error: Unable to use libevent (libevent check failed) 解决方法:安装libevent-devel # yum install -y libevent-devel 错误11: configure: error: Invalid LDAP directory - unable to find ldap.h 解决方法:安装openldap-devel # yum install -y openldap-devel 错误12: configure: error: Curl library not found 解决方法:安装curl-devel # yum install -y curl-devel 以上编译前准备完成
4.编译并安装zabbix
# make # make install # cd /usr/local/zabbix/ # tree . . ├── bin │ ├── zabbix_get │ └── zabbix_sender ├── etc │ ├── zabbix_agentd.conf //agentd配置文件 │ ├── zabbix_agentd.conf.d │ ├── zabbix_proxy.conf //proxy配置文件 │ ├── zabbix_proxy.conf.d │ ├── zabbix_server.conf //server配置文件 │ └── zabbix_server.conf.d ├── lib │ └── modules ├── sbin │ ├── zabbix_agentd │ ├── zabbix_proxy │ └── zabbix_server └── share ├── man │ ├── man1 │ │ ├── zabbix_get.1 │ │ └── zabbix_sender.1 │ └── man8 │ ├── zabbix_agentd.8 │ ├── zabbix_proxy.8 │ └── zabbix_server.8 └── zabbix ├── alertscripts └── externalscripts
# /usr/local/zabbix/sbin/zabbix_server -V
zabbix_server (Zabbix) 4.0.13 //版本为4.0.13
Revision 4e383bb6c5 2 October 2019, compilation time: Oct 26 2019 15:04:41
Copyright (C) 2019 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.
5.配置zabbix-server数据库
二进制安装mysql数据库 # yum install mariadb-server -y 启动数据库 # systemctl start mariadb # systemctl status mariadb 数据库初始化 # mysql_secure_installation Set root password? [Y/n] y Remove anonymous users? [Y/n] y Disallow root login remotely? [Y/n] n Remove test database and access to it? [Y/n] y Reload privilege tables now? [Y/n] y # ps aux |grep mysql 连接mysql创建zabbix并导入相关表 mysql -uroot -p123.com MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin; //创建数据库zabbix并设置字符集为utf8 MariaDB [(none)]> show databases; MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@zabbixldp identified by "123.com"; MariaDB [(none)]> flush privileges; MariaDB [(none)]>quit # mysql -uzabbix -p123.com ERROR 1045 (28000): Access denied for user 'zabbix'@'localhost' (using password: YES)
//说明grant内的zabbix@localhost其中localhost代指本地而非就是本机的主机名 # mysql -uroot -p123.com MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@localhost identified by "123.com"; MariaDB [(none)]> flush privileges; MariaDB [(none)]> quit # mysql -uzabbix -p123.com Welcome to the MariaDB monitor. Commands end with ; or \g. MariaDB [(none)]> use zabbix; MariaDB [zabbix]> source //usr/local/src/zabbix-4.0.13/database/mysql/schema.sql MariaDB [zabbix]> source //usr/local/src/zabbix-4.0.13/database/mysql/images.sql MariaDB [zabbix]> source //usr/local/src/zabbix-4.0.13/database/mysql/data.sql MariaDB [zabbix]>show tables;
6.配置zabbix-server与agent的配置文件并启动
# vim /usr/local/zabbix/etc/zabbix_server.conf DBHost=localhost DBName=zabbix DBuser=zabbix DBPassword=123.com
# /usr/local/zabbix/sbin/zabbix_server -c /usr/local/zabbix/etc/zabbix_server.conf
# ps aux |grep zabbix
# tailf /tmp/zabbix_server.log
# vim /usr/local/zabbix/etc/zabbix_agentd.conf
Server=192.168.0.88
Hostname=Zabbix server
# /usr/local/zabbix/sbin/zabbix_agent -c /usr/local/zabbix/etc/zabbix_agent.conf
# ps aux |grep zabbix_agent
# tailf /tmp/zabbix_agent.log
7.二进制安装zabbix前端
# rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm //安装yum仓库 # yum -y install zabbix-web-mysql # vim /etc/php.ini
# vi /etc/httpd/conf.d/zabbix.conf
php_value max_execution_time 300
php_value memory_limit 128M
php_value post_max_size 16M
php_value upload_max_filesize 2M
php_value max_input_time 300
php_value always_populate_raw_post_data -1
php_value date.timezone Asia/Shanghai
# systemctl start httpd
# systemctl status httpd
# ps aux |grep httpd
# netstat -lntp
8.登录http://192.168.0.88/zabbix
进入配置页面 1.检查当前的环境,全OK; 2.配置mysql相关参数 3.配置zabbix-server相关参数 host=zabbixldp //必须能够通过host通信,否则异常,也可填写IP地址; port=10051//默认 name=ZabbixServer/首次安装时影响生成的zabbix服务器自身的host(主机),造成Server报错;
/再次修改目前看到的影响是web一些标注不同,如网页标题及按钮行最右边标识,并没有再次造成Server报错;
4.生成的配置文件为/etc/zabbix/web/zabbix.conf.php
9.web页面异常
检查进程和端口10051均正常开启 在zabbix_server上ping zabbixldp无法解析域名 方法一:修改前端php配置文件 # vim /etc/zabbix/web/zabbix.conf.php ZBX_SERVER = '192.168.0.11';
# systemctl restart zabbix_server 方法二:修改host文件 # vim /etc/hosts 127.0.0.1 新增 zabbixldp(当前主机名)
猜测原因缺少host Zabbix server,因为名字被我改成ZabbixServer
#vim /usr/local/zabbix/etc/zabbix_agentd.conf
Hostname=Zabbix server
#/etc/init.d/zabbix_agentd restart
修改web页面host相关配置
hostname=改为Zabbix server后日志报错停止
更改中文显示时图表(graph)显示异常 -原因为图标字体中没有中文字体造成的,添加相应的中文字体即可,这用simhei.ttf用于添加(copy到/usr/share/zabbix/assets/fonts/目录下,各版本可能不同,可以在defines.inc.php文件内找) # vim /usr/share/zabbix/include/defines.inc.php define('ZBX_FONTPATH', realpath('assets/fonts'));//字体所在目录 define('ZBX_GRAPH_FONT_NAME', 'simhei'); // font file name//字体名称 原字体名称为‘graphfont’
刷新页面后正常,如下:
10.开机启动及启动项修改
配置zabbix_server及zabbix_agentd开机启动
# cp /usr/local/src/zabbix-4.0.13/misc/init.d/fedora/core5/zabbix_server /etc/init.d/ # vim /etc/init.d/zabbix_server ZABBIX_BIN="/usr/local/zabbix/sbin/zabbix_server" # cp /usr/local/src/zabbix-4.0.13/misc/init.d/fedora/core5/zabbix_agentd /etc/init.d/ # vim /etc/init.d/zabbix_agentd ZABBIX_BIN="/usr/local/zabbix/sbin/zabbix_agentd"
# chkconfig --add zabbix_agentd
# chkconfig --add zabbix_server
# chkconfig zabbix_agentd on
# chkconfig zabbix_server on
# chkconfig --list
配置apache开启启动
# cd /etc/init.d/
# ls | grep httpd
# cp /usr/sbin/apachectl /etc/init.d/httpd
# chkconfig --add httpd
服务 httpd 不支持 chkconfig
# vim /etc/init.d/httpd
在#!/bin/sh下添加2行如下,增加chkconfig支持
#chkconfig:345 85 15
#description:Start and stop the Apache HTTP Server
然后再次添加
# chkconfig --add httpd
注意:正在将请求转发到“systemctl enable httpd.service”。
所以直接用systemctl就可以了,费啥劲呢
# systemctl enable httpd
# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
开机不启动selinux
# vim /etc/selinux/config
SELINUX=disabled
关闭防火墙开启激动
# systemctl disable firewalld
# reboot now
# systemctl status httpd && systemctl status mariadb && systemctl status zabbix_server && systemctl status zabbix_agentd
11.修改zabbix日志存放位置
创建日志目录 # mkdir /var/log/zabbix/ # chown zabbix.zabbix /var/log/zabbix/ 修改zabbix_server配置文件 # vim /usr/local/zabbix/etc/zabbix_server.conf LogFile=/var/log/zabbix/zabbix_server.log # systemctl restart zabbix_server 修改zabbix_agentr配置文件 # vim /usr/local/zabbix/etc/zabbix_agentd.conf LogFile=/var/log/zabbix/zabbix_agentd.log # systemctl restart zabbix_agentd 修改zabbix_proxy配置文件 # vim /usr/local/zabbix/etc/zabbix_proxy.conf LogFile=/var/log/zabbix/zabbix_proxy.log # 未启动prox
Zabbix使用过程中的一些异常
数据库连接数异常 Too many connections:
# 日志中大量如下异常: [Z3001]connection to database 'zabbix' failed: [1040] Too many connections
原因为Zabbix连接Mariadb的请求比较多,而Mariadb默认安装时,最大连接数默认值为151,解决该问题的办法就是把Mariadb的最大连接数修改大一点,比如改到1000,修改方法如下,详细点这里
# 查看mariadb数据库连接数(Threads): mysqladmin -uroot -p status #查看默认最大连接数,默认是151 mysql -uroot -p show variables like 'max_connections'; #修改/etc/my.cnf配置文件 vim /etc/my.cnf #[mysqld]后面添加 max_connections=1000 #配置/usr/lib/systemd/system/mariadb.service来调大打开文件数目 vim /usr/lib/systemd/system/mariadb.service # 在[Service]后添加两行: LimitNOFILE=10000 LimitNPROC=10000 #重新加载系统服务,并重启mariadb服务 systemctl --system daemon-reload systemctl restart mariadb.service #查看默认最大连接数,最大连接数应已经变更为1000 mysql -uroot -p show variables like 'max_connections';
数据库housekeeper及history syncer process使用率过高问题:
排错过程:
1.使用iostat -x 1 10 查看硬盘读写,发现硬盘读写使用率均趋于100%:
2.使用iotop查看发现,mysql数据库读写进程占用大量的io,怀疑是数据库中大表的条目过多造成读写太慢;
3.mysql中查看表格条目数,发现单张history_unit的表就有几十亿条记录,这造成了查询与读写的缓慢;
select table_name, (data_length+index_length)/1024/1024 as total_mb, table_rows from information_schema.tables where table_schema='zabbix';
修复过程,具体参考这里:
1. 直接删除大表中的数据,这样可以暂时解决问题,也为下面的大表分区做准备,过大的数据分区使用的时间太长;
truncate table history_text;
truncate table history_str;
truncate table history_uint;
truncate table history;
truncate table trends;
truncate table trends_uint;
2. 针对几个大表做表分区:
3.查看分区是否成功;
mysql> show create table history_uint;
脚本如下:
DELIMITER $$ CREATE PROCEDURE `partition_create`(SCHEMANAME varchar(64), TABLENAME varchar(64), PARTITIONNAME varchar(64), CLOCK int) BEGIN /* SCHEMANAME = The DB schema in which to make changes TABLENAME = The table with partitions to potentially delete PARTITIONNAME = The name of the partition to create */ /* Verify that the partition does not already exist */ DECLARE RETROWS INT; SELECT COUNT(1) INTO RETROWS FROM information_schema.partitions WHERE table_schema = SCHEMANAME AND table_name = TABLENAME AND partition_description >= CLOCK; IF RETROWS = 0 THEN /* 1. Print a message indicating that a partition was created. 2. Create the SQL to create the partition. 3. Execute the SQL from #2. */ SELECT CONCAT( "partition_create(", SCHEMANAME, ",", TABLENAME, ",", PARTITIONNAME, ",", CLOCK, ")" ) AS msg; SET @sql = CONCAT( 'ALTER TABLE ', SCHEMANAME, '.', TABLENAME, ' ADD PARTITION (PARTITION ', PARTITIONNAME, ' VALUES LESS THAN (', CLOCK, '));' ); PREPARE STMT FROM @sql; EXECUTE STMT; DEALLOCATE PREPARE STMT; END IF; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `partition_drop`(SCHEMANAME VARCHAR(64), TABLENAME VARCHAR(64), DELETE_BELOW_PARTITION_DATE BIGINT) BEGIN /* SCHEMANAME = The DB schema in which to make changes TABLENAME = The table with partitions to potentially delete DELETE_BELOW_PARTITION_DATE = Delete any partitions with names that are dates older than this one (yyyy-mm-dd) */ DECLARE done INT DEFAULT FALSE; DECLARE drop_part_name VARCHAR(16); /* Get a list of all the partitions that are older than the date in DELETE_BELOW_PARTITION_DATE. All partitions are prefixed with a "p", so use SUBSTRING TO get rid of that character. */ DECLARE myCursor CURSOR FOR SELECT partition_name FROM information_schema.partitions WHERE table_schema = SCHEMANAME AND table_name = TABLENAME AND CAST(SUBSTRING(partition_name FROM 2) AS UNSIGNED) < DELETE_BELOW_PARTITION_DATE; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; /* Create the basics for when we need to drop the partition. Also, create @drop_partitions to hold a comma-delimited list of all partitions that should be deleted. */ SET @alter_header = CONCAT("ALTER TABLE ", SCHEMANAME, ".", TABLENAME, " DROP PARTITION "); SET @drop_partitions = ""; /* Start looping through all the partitions that are too old. */ OPEN myCursor; read_loop: LOOP FETCH myCursor INTO drop_part_name; IF done THEN LEAVE read_loop; END IF; SET @drop_partitions = IF(@drop_partitions = "", drop_part_name, CONCAT(@drop_partitions, ",", drop_part_name)); END LOOP; IF @drop_partitions != "" THEN /* 1. Build the SQL to drop all the necessary partitions. 2. Run the SQL to drop the partitions. 3. Print out the table partitions that were deleted. */ SET @full_sql = CONCAT(@alter_header, @drop_partitions, ";"); PREPARE STMT FROM @full_sql; EXECUTE STMT; DEALLOCATE PREPARE STMT; SELECT CONCAT(SCHEMANAME, ".", TABLENAME) AS `table`, @drop_partitions AS `partitions_deleted`; ELSE /* No partitions are being deleted, so print out "N/A" (Not applicable) to indicate that no changes were made. */ SELECT CONCAT(SCHEMANAME, ".", TABLENAME) AS `table`, "N/A" AS `partitions_deleted`; END IF; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `partition_maintenance`(SCHEMA_NAME VARCHAR(32), TABLE_NAME VARCHAR(32), KEEP_DATA_DAYS INT, HOURLY_INTERVAL INT, CREATE_NEXT_INTERVALS INT) BEGIN DECLARE OLDER_THAN_PARTITION_DATE VARCHAR(16); DECLARE PARTITION_NAME VARCHAR(16); DECLARE OLD_PARTITION_NAME VARCHAR(16); DECLARE LESS_THAN_TIMESTAMP INT; DECLARE CUR_TIME INT; CALL partition_verify(SCHEMA_NAME, TABLE_NAME, HOURLY_INTERVAL); SET CUR_TIME = UNIX_TIMESTAMP(DATE_FORMAT(NOW(), '%Y-%m-%d 00:00:00')); SET @__interval = 1; create_loop: LOOP IF @__interval > CREATE_NEXT_INTERVALS THEN LEAVE create_loop; END IF; SET LESS_THAN_TIMESTAMP = CUR_TIME + (HOURLY_INTERVAL * @__interval * 3600); SET PARTITION_NAME = FROM_UNIXTIME(CUR_TIME + HOURLY_INTERVAL * (@__interval - 1) * 3600, 'p%Y%m%d%H00'); IF(PARTITION_NAME != OLD_PARTITION_NAME) THEN CALL partition_create(SCHEMA_NAME, TABLE_NAME, PARTITION_NAME, LESS_THAN_TIMESTAMP); END IF; SET @__interval=@__interval+1; SET OLD_PARTITION_NAME = PARTITION_NAME; END LOOP; SET OLDER_THAN_PARTITION_DATE=DATE_FORMAT(DATE_SUB(NOW(), INTERVAL KEEP_DATA_DAYS DAY), '%Y%m%d0000'); CALL partition_drop(SCHEMA_NAME, TABLE_NAME, OLDER_THAN_PARTITION_DATE); END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE `partition_verify`(SCHEMANAME VARCHAR(64), TABLENAME VARCHAR(64), HOURLYINTERVAL INT(11)) BEGIN DECLARE PARTITION_NAME VARCHAR(16); DECLARE RETROWS INT(11); DECLARE FUTURE_TIMESTAMP TIMESTAMP; /* * Check if any partitions exist for the given SCHEMANAME.TABLENAME. */ SELECT COUNT(1) INTO RETROWS FROM information_schema.partitions WHERE table_schema = SCHEMANAME AND table_name = TABLENAME AND partition_name IS NULL; /* * If partitions do not exist, go ahead and partition the table */ IF RETROWS = 1 THEN /* * Take the current date at 00:00:00 and add HOURLYINTERVAL to it. This is the timestamp below which we will store values. * We begin partitioning based on the beginning of a day. This is because we don't want to generate a random partition * that won't necessarily fall in line with the desired partition naming (ie: if the hour interval is 24 hours, we could * end up creating a partition now named "p201403270600" when all other partitions will be like "p201403280000"). */ SET FUTURE_TIMESTAMP = TIMESTAMPADD(HOUR, HOURLYINTERVAL, CONCAT(CURDATE(), " ", '00:00:00')); SET PARTITION_NAME = DATE_FORMAT(CURDATE(), 'p%Y%m%d%H00'); -- Create the partitioning query SET @__PARTITION_SQL = CONCAT("ALTER TABLE ", SCHEMANAME, ".", TABLENAME, " PARTITION BY RANGE(`clock`)"); SET @__PARTITION_SQL = CONCAT(@__PARTITION_SQL, "(PARTITION ", PARTITION_NAME, " VALUES LESS THAN (", UNIX_TIMESTAMP(FUTURE_TIMESTAMP), "));"); -- Run the partitioning query PREPARE STMT FROM @__PARTITION_SQL; EXECUTE STMT; DEALLOCATE PREPARE STMT; END IF; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE`partition_maintenance_all`(SCHEMA_NAME VARCHAR(32)) BEGIN CALL partition_maintenance(SCHEMA_NAME, 'history', 90, 24, 14); CALL partition_maintenance(SCHEMA_NAME, 'history_log', 90, 24, 14); CALL partition_maintenance(SCHEMA_NAME, 'history_str', 90, 24, 14); CALL partition_maintenance(SCHEMA_NAME, 'history_text', 90, 24, 14); CALL partition_maintenance(SCHEMA_NAME, 'history_uint', 90, 24, 14); CALL partition_maintenance(SCHEMA_NAME, 'trends', 730, 24, 14); CALL partition_maintenance(SCHEMA_NAME, 'trends_uint', 730, 24, 14); END$$ DELIMITER ;
a.然后执行如下 mysql -uzabbix -pzabbix zabbix < partition.sql b.添加crontable,每天执行01点01分执行,如下: crontab -e #zabbix partition_maintenance 01 01 * * * mysql -uzabbix -pzabbix zabbix -e"CALL partition_maintenance_all('zabbix')" &>/dev/null EOF cat crontab.txt |crontab 注意: mysql的zabbix用户的密码部分按照实际环境配置 c.首先执行一次(由于首次执行的时间较长,请使用nohup执行),如下: nohup mysql -uzabbix -pzabbix zabbix -e "CALLpartition_maintenance_all('zabbix')" &> /root/partition.log&
web页面打开latest data报错500问题
排错:
1.查看/var/log/httpd/error_log日志,发现如下报错:
修复:
2.修改/etc/php.ini
memory_limit = 512M
3.重启httpd服务后,仍然是同样的报错,后查看相关资料后发现,httpd在加载了扩展模块php_module是不会使用系统自带的php工具,zabbix-web的配置文件 /etc/httpd/conf.d/zabbix.conf如下代码:
<IfModule mod_php5.c> php_value max_execution_time 300 php_value memory_limit 256M php_value post_max_size 16M php_value upload_max_filesize 2M php_value max_input_time 300 php_value max_input_vars 10000 php_value always_populate_raw_post_data -1 php_value date.timezone Asia/Shanghai </IfModule>
4.变更memory_limit后重启httpd恢复;
end
参考:https://www.jianshu.com/p/b6b5b5377c9b