Zabbix監控服務基礎
一、監控概述
1.1、監控的目的
報告系統運行狀況
- 每一部分必須同時監控
- 內容包括吞吐量、反應時間、利用率等
提前發現問題
- 進行服務器新能調整前,知道調整什麼
- 找出系統的瓶頸在什麼地方
1.2、監控的資源類別
公開數據
- Web、FTP、SSH、數據庫等應用服務
- TCP或UDP端口
私有數據
- CPU、內存、磁盤、網卡流量等使用信息
- 用戶、進程等運行信息
1.3、Linux系統常用監控命令
ps
uptime
free
swapon -s
df -h
ifconfig
netstat
ss
traceroute
iostat
1.4、自動化監控系統軟件
- Cacti
基於SNMP協議的監控軟件,強大的繪圖能力。 - Nagios
基於Agent監控,強大的狀態檢查與報警機制
插件很多,自己寫監控腳本潛入到Nagios非常方便 - Zabbix
基於多種監控機制,支持分佈式監控
二、Zabbix基礎
2.1、Zabbix簡介
- Zabbix是一個高度集成的監控方案
- 可以實現企業級的開源分佈式監控
- Zabbix通過C/S模式採集監控數據
- Zabbix通過B/S模式實現Web管理
2.2、監控服務器
- 監控服務器可以通過SNMP或Agent採集數據
- 數據可以寫入MySQL、Oracle等數據庫中
- 服務器使用LNMP實現web前段的管理
2.3、被監控服務器
- 被監控主機需要安裝Agent
- 常見的網絡設備一般支持SNMP
三、Zabbix服務搭建
使用192.168.2.5做監控主機
使用192.168.2.100、2.101、2.102做被監控服務器
3.1、部署運行環境LNMP
3.1.1、安裝nginx服務
[root@zabbix05 ~]# setenforce 0
[root@zabbix05 ~]# getenforce
[root@zabbix05 ~]# firewall-cmd --set-default-zone=trusted
[root@zabbix05 ~]# yum -y install gcc pcre-devel zlib-devel
[root@zabbix05 ~]# tar -zxf nginx-1.12.2.tar.gz
[root@zabbix05 ~]# ls
[root@zabbix05 ~]# cd nginx-1.12.2/
[root@zabbix05 nginx-1.12.2]# ./configure
[root@zabbix05 nginx-1.12.2]# make && make install
[root@zabbix05 nginx-1.12.2]# ls /usr/local/nginx/
3.1.2、修改nginx配置,使其支持PHP
[root@zabbix05 nginx-1.12.2]# vim +65 /usr/local/nginx/conf/nginx.conf
[root@zabbix05 nginx-1.12.2]# sed -n '65,70p' /usr/local/nginx/conf/nginx.conf
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
3.1.3、啓動服務
[root@zabbix05 nginx-1.12.2]# /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
[root@zabbix05 nginx-1.12.2]# /usr/local/nginx/sbin/nginx
[root@zabbix05 ~]# echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local
[root@zabbix05 ~]# chmod +x /etc/rc.local
[root@zabbix05 nginx-1.12.2]# netstat -utnlp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4039/nginx: master
[root@zabbix05 nginx-1.12.2]#
3.1.4、安裝php-fpm
[root@zabbix05 ~]# yum -y install php-fpm
[root@zabbix05 ~]# systemctl start php-fpm
[root@zabbix05 ~]# systemctl enable php-fpm.service
[root@zabbix05 ~]# netstat -utnlp | grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 4079/php-fpm: maste
3.1.5、安裝MariaDB
[root@zabbix05 ~]# yum -y install mariadb-server mariadb mariadb-devel
[root@zabbix05 ~]# systemctl enable mariadb.service
[root@zabbix05 ~]# netstat -utnlp | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 4366/mysqld
[root@zabbix05 ~]# ls /var/lib/mysql/
aria_log.00000001 ibdata1 ib_logfile1 mysql.sock test
aria_log_control ib_logfile0 mysql performance_schema
3.1.6、弄個PHP網頁
[root@zabbix05 ~]# vim /usr/local/nginx/html/test.ph
<?php
echo "hello world";
?>
[root@zabbix05 ~]# curl http://127.0.0.1/test.php
hello world
3.2、部署Zabbix
3.2.1、安裝Zabbix軟件
[root@zabbix05 ~]# yum -y install net-snmp-devel curl-devel libevent-devel
[root@zabbix05 ~]# ls
[root@zabbix05 ~]# tar -zxf zabbix-3.4.4.tar.gz
[root@zabbix05 ~]# cd zabbix-3.4.4/
[root@zabbix05 zabbix-3.4.4]# ./configure \
--enable-server --enable-proxy --enable-agent \
--with-mysql=/usr/bin/mysql_config \
--with-net-snmp \
--with-libcurl
[root@zabbix05 zabbix-3.4.4]# make install
[root@zabbix05 zabbix-3.4.4]# ls /usr/local/etc/
zabbix_agentd.conf zabbix_proxy.conf zabbix_server.conf
zabbix_agentd.conf.d zabbix_proxy.conf.d zabbix_server.conf.d
[root@zabbix05 zabbix-3.4.4]# ls /usr/local/bin/
zabbix_get zabbix_sender
[root@zabbix05 zabbix-3.4.4]# ls /usr/local/sbin/
zabbix_agentd zabbix_proxy zabbix_server
3.2.2、初始化準備
- 創建存儲數據庫和連接用戶
MariaDB [(none)]> create database zabbix character set utf8;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all on zabbix.* to zabbix@"localhost" identified by "zabbix";
Query OK, 0 rows affected (0.00 sec)
[root@zabbix05 ~]# cd zabbix-3.4.4/database/mysql/
[root@zabbix05 mysql]# ls
data.sql images.sql schema.sql
[root@zabbix05 mysql]# mysql -uzabbix -pzabbix zabbix < schema.sql
[root@zabbix05 mysql]# mysql -uzabbix -pzabbix zabbix < images.sql
[root@zabbix05 mysql]# mysql -uzabbix -pzabbix zabbix < data.sql
[root@zabbix05 mysql]# mysql -uzabbix -pzabbix zabbix -e 'show tables' |wc -l
141
- 上線網頁文件
[root@zabbix05 ~]# cd /root/zabbix-3.4.4/frontends/php/
[root@zabbix05 php]# ls
[root@zabbix05 php]# cp -a * /usr/local/nginx/html
[root@zabbix05 php]# ls /usr/local/nginx/html/index.*
/usr/local/nginx/html/index.html /usr/local/nginx/html/index.php
[root@zabbix05 php]# chmod -R 777 /usr/local/nginx/html/ *
[root@zabbix05 php]# ls /usr/local/nginx/html
- 安裝依賴的軟件包
[root@zabbix05 ~]# yum -y install php php-mysql php-ldap php-xml php-gd php-mbstring php-bcmath
- 修改PHP配置文件,滿足Zabbix的運行需求
[root@zabbix05 ~]# vim /etc/php.ini
672 post_max_size = 16M
394 max_input_time = 300
384 max_execution_time = 300
878 date.timezone = Asia/Shanghai
[root@zabbix05 ~]# systemctl restart php-fpm
[root@zabbix05 ~]# netstat -utnlp | grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 23620/php-fpm: mast
- 修改nginx服務配置文件,滿足Zabbix的運行需求
[root@zabbix05 ~]# vim /usr/local/nginx/conf/nginx.conf
http {
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
[root@zabbix05 ~]# /usr/local/nginx/sbin/nginx -s stop
[root@zabbix05 ~]# /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
[root@zabbix05 ~]# /usr/local/nginx/sbin/nginx
[root@zabbix05 ~]# netstat -utnlp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 23695/nginx: master
3.2.3、初始化配置
-
訪問http://192.168.2.5/setup.php
-
點擊下一步,全部顯示OK
-
修改數據庫信息
-
確認配置信息
-
成功安裝Zabbix前端。
提示配置文件所在位置
/usr/local/nginx/html/conf/zabbix.conf.php
[root@zabbix05 ~]# cat /usr/local/nginx/html/conf/zabbix.conf.php
<?php
// Zabbix GUI configuration file.
global $DB;
$DB['TYPE'] = 'MYSQL';
$DB['SERVER'] = 'localhost';
$DB['PORT'] = '3306';
$DB['DATABASE'] = 'zabbix';
$DB['USER'] = 'zabbix';
$DB['PASSWORD'] = 'zabbix';
// Schema name. Used for IBM DB2 and PostgreSQL.
$DB['SCHEMA'] = '';
$ZBX_SERVER = '192.168.2.5';
$ZBX_SERVER_PORT = '10051';
$ZBX_SERVER_NAME = 'JKuser';
$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;
3.2.4、登錄管理界面
- http://192.168.2.5/index.php
默認登錄用戶admin
默認登錄密碼zabbix
3.2.4.1、設置中文環境
點擊右上角人像
推薦使用英文,中文環境小部分爲亂碼
3.2.4.2、設置密碼
修改密碼爲admin@123
3.2.5、啓動監控服務
- 修改服務主配置文件
[root@zabbix05 ~]# vim /usr/local/etc/zabbix_server.conf
85 DBHost=localhost //數據庫主機
95 DBName=zabbix //數據庫名稱
111 DBUser=zabbix //數據庫賬戶
119 DBPassword=zabbix //數據庫密碼
38 LogFile=/tmp/zabbix_server.log //日誌
- 啓動監控服務
先創建用戶,否則無法啓動
[root@zabbix05 ~]# useradd zabbix
[root@zabbix05 ~]# zabbix_server
[root@zabbix05 ~]# netstat -utnlp | grep 10051
tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 24089/zabbix_server
四、搭建被監控主機
使用192.168.2.100、2.101、2.102做被監控服務器
以下配置以2.100爲例
4.1、運行zabbix_agentd服務
4.1.1、安裝軟件
[root@host100 ~]# yum -y install gcc pcre-devel
[root@host100 ~]# tar -zxf zabbix-3.4.4.tar.gz
[root@host100 ~]# cd zabbix-3.4.4/
[root@host100 zabbix-3.4.4]# ./configure --enable-agent
[root@host100 zabbix-3.4.4]# make install
[root@host100 ~]# ls /usr/local/etc/
zabbix_agentd.conf zabbix_agentd.conf.d
[root@host100 ~]# ls /usr/local/sbin/
zabbix_agentd
[root@host100 ~]# ls /usr/local/bin/
zabbix_get zabbix_sender
4.1.2、修改配置文件
[root@host100 ~]# vim /usr/local/etc/zabbix_agentd.conf
30 LogFile=/tmp/zabbix_agentd.log //日誌文件
93 Server=127.0.0.1,192.168.2.5 //允許訪問服務地址列表
134 ServerActive=192.168.2.5:10051 //監控服務器IP地址端口號
4.1.3、創建用戶並啓動服務
[root@host100 ~]# useradd zabbix
[root@host100 ~]# zabbix_agentd //啓動服務
[root@host100 ~]# netstat -utnlp | grep 10050
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 9038/zabbix_agentd
4.2、添加監控主機
4.2.1、創建主機
4.2.2、應用監控模板
4.2.3、查看監控數據
4.3、刪除主機
先選擇禁用 然後再刪除
五、自定義監控項——監控主機系統的總用戶數
編寫監控腳本給監控服務器使用
通常是Zabbix中沒有想要的模板
5.1、自定義key
5.1.1、啓用自定義監控項功能
[root@host100 ~]# vim /usr/local/etc/zabbix_agentd.conf
265 Include=/usr/local/etc/zabbix_agentd.conf.d/ *.conf //加載配置文件目錄
280 UnsafeUserParameters=1 //是否允許自定義key
5.1.2、創建監控命令
[root@host100 ~]# vim /usr/local/etc/zabbix_agentd.conf.d/x.conf
UserParameter=get_sum_users,wc -l /etc/passwd | awk '{print $1}'
5.1.3、重啓Zabbix_agentd服務
[root@host100 ~]# killall -9 zabbix_agentd
[root@host100 ~]# killall -9 zabbix_agentd
zabbix_agentd: no process found
[root@host100 ~]# zabbix_agentd
[root@host100 ~]# netstat -utnlp | grep 10050
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 9494/zabbix_agentd
5.1.4、測試創建的監控命令
[root@host100 ~]# zabbix_get -s 127.0.0.1 -p 10050 -k get_sum_users
21
[root@host100 ~]# useradd user1
[root@host100 ~]# useradd user2
[root@host100 ~]# useradd user3
[root@host100 ~]# zabbix_get -s 127.0.0.1 -p 10050 -k get_sum_users
24
5.2、創建監控模板
5.2.1、創建新模板
新模板ATMP1
5.2.2、創建應用集
應用集名字users
5.2.3、創建監控項
名get_sys_users 對應命令 get_sum_users
5.2.4、配置主機可調用新模板
監控主機192.168.2.100 時,調用新創建的模板ATMP1
5.2.5、查看數據
[root@host100 ~]# useradd user4
[root@host100 ~]# useradd user5
[root@host100 ~]# useradd user6
六、Zabbix報警機制
篇幅太長,點擊以下鏈接查看
Zabbix報警機制
七、Zabbix自動發現
篇幅太長,點擊以下鏈接查看
Zabbix自動發現
八、Zabbix主動監視
篇幅太長,點擊以下鏈接查看
Zabbix主動監視
九、Zabbix監控案例
篇幅太長,點擊以下鏈接查看
Zabbix監控案例