如何獲取檔案的絕對路徑
自動化監控系統
Cacti
特點:將監控到的資料,繪製成各種圖形
基於SNMP協議 (網路管理協議) 的監控軟體,強大的繪圖能力
Nagios
特點:狀態檢查和報警機制 (例如:記憶體不足或CPU負載高時,及時的給管理員傳送報警資訊(郵件報警,簡訊報警等) )
基於Agent監控,強大的狀態檢查與報警機制,外掛極多,自己寫監控指令碼嵌入到Nagios非常方便
Zabbix
特點:支援多種報警機制,支援分散式監控,支援資料繪圖
基於多種監控機制,支援分散式監控
1。3。1 Zabbix簡介
Zabbix 是一個高度整合的監控解決方案,可以實現企業級的開源分散式監控,
Zabbix 透過 C/S模式採集監控資料
C/S (client/server):
客戶端/伺服器
客戶端程式負載採集要監控的資料,然後傳送給監控伺服器;
監控伺服器對客戶傳送過來的資料進行儲存和處理;
Zabbix透過B/S模式實現Web管理
B/S (browser/server): 瀏覽器/伺服器
管理員可以透過瀏覽器,訪問監控伺服器web頁面,並可以檢視和管理監控系統
1。3。2 監控拓撲
Zabbix監控原理:
部署一個Zabbix監控伺服器, 用於儲存和處理監控資料;
如果被監控的是Linux或Windows主機,需要安裝客戶端程式agent來採集監控資料;
如果被監控的是網路裝置(交換機,路由器等),透過SNMP協議進行監控;
最後Zabbix監控伺服器,將客戶端收集來的資料儲存到資料庫中,透過web頁面來管理
監控角色
監控伺服器
監控伺服器可以透過SNMP (網路管理協議)或Agent採集資料
資料可以寫入MYSQL、Oracle等資料庫中
被監控主機
被監控主機需要安裝Agent
常見的網路裝置一般支援SNMP (網路管理協議)
二、LNMP環境準備
這裡的話,因為zabbix web系統使用的是php,所以需要配置LNMP環境。
伺服器使用LNMP實現web前端的管理
Nginx是一款小巧而高效的Web伺服器軟體
,可幫您在Linux系統下快速方便地搭建出LNMP Web服務環境。
在ECS例項上搭建LNMP環境
,其中
LNMP
分別代表
Linux、Nginx、MySQL和PHP
。
2。1 部署LNMP
這裡小夥伴可以參考這個:
手動部署LNMP環境(CentOS 7)手動部署LNMP環境(CentOS 7) - 雲伺服器 ECS - 阿里雲
2。1。1 安裝前準備
監控伺服器(需要使用Web頁面操作,因此需要先部署LNMP)
設定主機名(zabbixserver)設定IP地址(192。168。26。15)關閉防火牆、SELinux
監控客戶端 (2.100和2.200)
主機web1(192。168。26。14)主機web2(192。168。26。13)關閉防火牆、SELinux
虛擬機器環境準備(克隆centos7 模板機): ——> 關閉防火牆和SELinux,嗯,因為需要多個機器,所以我們用
ansible
,方便一點,這裡,
192.168.26.15為控制機,192.168.26.14,192.168.26.13 為節點機
主機名
IP地址
zabbixserver
192。168。26。15
web1
192。168。26。14
web2
192。168。26。13
配置到物理機的SSH免密
┌──(liruilong㉿Liruilong)-[/mnt/e/docker]└─$ ssh-copy-id root@192。168。26。13/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “/home/liruilong/。ssh/id_rsa。pub”The authenticity of host ‘192。168。26。13 (192。168。26。13)’ can‘t be established。ECDSA key fingerprint is SHA256:1F/T20FjhEaLDtutI1rXCwOFGZ5nPs3hFzHyjsnAs3Q。Are you sure you want to continue connecting (yes/no/[fingerprint])? yes/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed —— if you are prompted now it is to install the new keysroot@192。168。26。13’s password:Number of key(s) added: 1Now try logging into the machine, with: “ssh ‘root@192。168。26。13’”and check to make sure that only the key(s) you wanted were added。┌──(liruilong㉿Liruilong)-[/mnt/e/docker]└─$ ssh-copy-id root@192。168。26。14/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “/home/liruilong/。ssh/id_rsa。pub”The authenticity of host ‘192。168。26。14 (192。168。26。14)’ can‘t be established。ECDSA key fingerprint is SHA256:cfpb8zAi+otnaU0YIoRb76iaOYiFDI4JHyU9N0LmNkY。Are you sure you want to continue connecting (yes/no/[fingerprint])? yes/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed —— if you are prompted now it is to install the new keysroot@192。168。26。14’s password:Number of key(s) added: 1Now try logging into the machine, with: “ssh ‘root@192。168。26。14’”and check to make sure that only the key(s) you wanted were added。┌──(liruilong㉿Liruilong)-[/mnt/e/docker]└─$ ssh-copy-id root@192。168。26。15/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “/home/liruilong/。ssh/id_rsa。pub”The authenticity of host ‘192。168。26。15 (192。168。26。15)’ can‘t be established。ECDSA key fingerprint is SHA256:Ix6WxiXXJVdMFdSqiXLaPYdg+khbzkjuYO4raDDnih0。Are you sure you want to continue connecting (yes/no/[fingerprint])? yes/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed —— if you are prompted now it is to install the new keysroot@192。168。26。15’s password:Number of key(s) added: 1Now try logging into the machine, with: “ssh ‘root@192。168。26。15’”and check to make sure that only the key(s) you wanted were added。┌──(liruilong㉿Liruilong)-[/mnt/e/docker]└─$
ansible環境準備
┌──[root@zabbixserver]-[/]└─$ mkdir ansible;cd ansible;vim ansible。cfg┌──[root@zabbixserver]-[/ansible]└─$ cat ansible。cfg[defaults]# 主機清單檔案,就是要控制的主機列表inventory=inventory# 連線受管機器的遠端的使用者名稱remote_user=root# 角色目錄roles_path=roles# 設定使用者的su 提權[privilege_escalation]become=Truebecome_method=sudobecome_user=rootbecome_ask_pass=False┌──[root@zabbixserver]-[/ansible]└─$ vim inventory┌──[root@zabbixserver]-[/ansible]└─$ cat inventory[web]192。168。26。13192。168。26。14[zabbix]192。168。26。13192。168。26。14127。0。0。1┌──[root@zabbixserver]-[/ansible]└─$
配置控制機到節點機的SSH免密
┌──[root@zabbixserver]-[/ansible]└─$ ssh-copy-id root@192。168。26。13/usr/bin/ssh-copy-id: ERROR: failed to open ID file ‘/root/。pub’: No such file or directory (to install the contents of ‘/root/。pub’ anyway, look at the -f option)┌──[root@zabbixserver]-[/ansible]└─$ ssh-copy-id root@192。168。26。14/usr/bin/ssh-copy-id: ERROR: failed to open ID file ‘/root/。pub’: No such file or directory (to install the contents of ‘/root/。pub’ anyway, look at the -f option)┌──[root@zabbixserver]-[/ansible]└─$ ssh-keygenGenerating public/private rsa key pair。Enter file in which to save the key (/root/。ssh/id_rsa):Enter passphrase (empty for no passphrase):Enter same passphrase again:Your identification has been saved in /root/。ssh/id_rsa。Your public key has been saved in /root/。ssh/id_rsa。pub。The key fingerprint is:SHA256:/wLemqRJd5tsIWj/hxole6EpNTZ9M2lDooVGnYTx3I4 root@zabbixserverThe key‘s randomart image is:+——-[RSA 2048]——+| o=。。 || 。。+o。 || o = o || 。 + = 。 || 。S E O || oooX。+ + || 。ooB++o || 。 *o*=+。 || o +==o。 |+——[SHA256]——-+┌──[root@zabbixserver]-[/ansible]└─$ ssh-copy-id root@192。168。26。14/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “/root/。ssh/id_rsa。pub”The authenticity of host ’192。168。26。14 (192。168。26。14)‘ can’t be established。ECDSA key fingerprint is SHA256:cfpb8zAi+otnaU0YIoRb76iaOYiFDI4JHyU9N0LmNkY。ECDSA key fingerprint is MD5:35:32:02:28:b3:2f:9b:11:3c:d9:16:29:ab:2f:75:73。Are you sure you want to continue connecting (yes/no)? yes/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed —— if you are prompted now it is to install the new keysroot@192。168。26。14‘s password:Number of key(s) added: 1Now try logging into the machine, with: “ssh ’root@192。168。26。14‘”and check to make sure that only the key(s) you wanted were added。┌──[root@zabbixserver]-[/ansible]└─$ ssh-copy-id root@192。168。26。13/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “/root/。ssh/id_rsa。pub”The authenticity of host ’192。168。26。13 (192。168。26。13)‘ can’t be established。ECDSA key fingerprint is SHA256:1F/T20FjhEaLDtutI1rXCwOFGZ5nPs3hFzHyjsnAs3Q。ECDSA key fingerprint is MD5:b3:c9:31:0e:08:31:5b:7b:25:dd:a3:a7:f1:db:ac:7a。Are you sure you want to continue connecting (yes/no)? yes/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed —— if you are prompted now it is to install the new keysroot@192。168。26。13‘s password:Number of key(s) added: 1Now try logging into the machine, with: “ssh ’root@192。168。26。13‘”and check to make sure that only the key(s) you wanted were added。┌──[root@zabbixserver]-[/ansible]└─$ ssh-copy-id root@192。168。26。15/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “/root/。ssh/id_rsa。pub”/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed —— if you are prompted now it is to install the new keysroot@192。168。26。15’s password:Number of key(s) added: 1Now try logging into the machine, with: “ssh ‘root@192。168。26。15’”and check to make sure that only the key(s) you wanted were added。┌──[root@zabbixserver]-[/ansible]
測試ansible
┌──[root@zabbixserver]-[/ansible]└─$ ansible all -m ping192。168。26。13 | SUCCESS => { “ansible_facts”: { “discovered_interpreter_python”: “/usr/bin/python” }, “changed”: false, “ping”: “pong”}192。168。26。14 | SUCCESS => { “ansible_facts”: { “discovered_interpreter_python”: “/usr/bin/python” }, “changed”: false, “ping”: “pong”}127。0。0。1 | SUCCESS => { “ansible_facts”: { “discovered_interpreter_python”: “/usr/bin/python” }, “changed”: false, “ping”: “pong”}┌──[root@zabbixserver]-[/ansible]└─$
檢查防火牆和selinux
┌──[root@zabbixserver]-[/ansible]└─$ ansible all -m shell -a ‘sed -n 7p /etc/selinux/config’[WARNING]: Consider using the replace, lineinfile or template module rather than running ‘sed’。 Ifyou need to use command because replace, lineinfile or template is insufficient you can add ‘warn:false’ to this command task or set ‘command_warnings=False’ in ansible。cfg to get rid of thismessage。127。0。0。1 | CHANGED | rc=0 >>SELINUX=disabled192。168。26。14 | CHANGED | rc=0 >>SELINUX=disabled192。168。26。13 | CHANGED | rc=0 >>SELINUX=disabled┌──[root@zabbixserver]-[/ansible]└─$ ansible all -m shell -a ‘ systemctl status firewalld | grep Active’127。0。0。1 | CHANGED | rc=0 >> Active: active (running) since Fri 2021-10-01 17:48:56 CST; 51min ago192。168。26。14 | CHANGED | rc=0 >> Active: active (running) since Fri 2021-10-01 17:49:47 CST; 50min ago192。168。26。13 | CHANGED | rc=0 >> Active: active (running) since Fri 2021-10-01 17:49:14 CST; 51min ago
發現防火牆還沒有關閉,所以在關閉一下防火牆:
┌──[root@zabbixserver]-[/ansible]└─$ ansible all -m shell -a ‘systemctl disable firewalld ——now’192。168。26。14 | CHANGED | rc=0 >>Removed symlink /etc/systemd/system/multi-user。target。wants/firewalld。service。Removed symlink /etc/systemd/system/dbus-org。fedoraproject。FirewallD1。service。192。168。26。13 | CHANGED | rc=0 >>Removed symlink /etc/systemd/system/multi-user。target。wants/firewalld。service。Removed symlink /etc/systemd/system/dbus-org。fedoraproject。FirewallD1。service。127。0。0。1 | CHANGED | rc=0 >>Removed symlink /etc/systemd/system/multi-user。target。wants/firewalld。service。Removed symlink /etc/systemd/system/dbus-org。fedoraproject。FirewallD1。service。┌──[root@zabbixserver]-[/ansible]└─$
2。1。2 部署LNMP
┌──[root@zabbixserver]-[/ansible]└─$ yum -y install nginx php php-fpm php-mysql mariadb-server mariadb mariadb-devel
安裝需要的軟體包,也可以分別安裝
##安裝Ng[root@zabbixserver ~]yum -y install nginx
###安裝php直譯器及相關軟體包[root@zabbixserver ~]# yum -y install php #php直譯器[root@zabbixserver ~]# yum -y install php-fpm #可以支援 Fastcgi 通用語言[root@zabbixserver ~]# yum -y install php-mysql #php和資料通訊的擴充套件包
###安裝資料庫及相關軟體包[root@web1 ~]# yum -y install mariadb-server #資料庫服務端軟體[root@web1 ~]# yum -y install mariadb #資料庫客戶端軟體[root@web1 ~]# yum -y install mariadb-devel #相關的依賴包
修改nginx配置
┌──[root@zabbixserver]-[~]└─$ vim /etc/nginx/nginx。conf┌──[root@zabbixserver]-[~]└─$ cat /etc/nginx/nginx。conf# For more information on configuration, see:# * Official English Documentation: http://nginx。org/en/docs/# * Official Russian Documentation: http://nginx。org/ru/docs/user nginx;worker_processes auto;error_log /var/log/nginx/error。log;pid /run/nginx。pid;# Load dynamic modules。 See /usr/share/doc/nginx/README。dynamic。include /usr/share/nginx/modules/*。conf;events { worker_connections 1024;}http { log_format main ‘$remote_addr - $remote_user [$time_local] “$request” ’ ‘$status $body_bytes_sent “$http_referer” ’ ‘“$http_user_agent” “$http_x_forwarded_for”’; access_log /var/log/nginx/access。log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 4096; include /etc/nginx/mime。types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf。d directory。 # See http://nginx。org/en/docs/ngx_core_module。html#include # for more information。 include /etc/nginx/conf。d/*。conf; server { listen 80; listen [::]:80; server_name _; root /usr/share/nginx/html; # Load configuration files for the default server block。 include /etc/nginx/default。d/*。conf; error_page 404 /404。html; location = /404。html { } error_page 500 502 503 504 /50x。html; location = /50x。html { }fastcgi_buffers 8 16k; #快取php生成的資料,快取大小為8個16kfastcgi_buffer_size 32k; #快取php產生的頭部資訊,快取大小為32kfastcgi_connect_timeout 300; #連線php的超時時間為300秒fastcgi_send_timeout 300; #傳送請求的超時時間為300秒fastcgi_read_timeout 300; #讀取請求的超時時間為300秒location ~ \。php$ { root html; fastcgi_pass 127。0。0。1:9000; fastcgi_index index。php; include fastcgi。conf; } # location ~ \。php$ { # root html; # fastcgi_pass 127。0。0。1: 9000; # fastcgi_index index。php; #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} }# Settings for a TLS enabled server。## server {# listen 443 ssl http2;# listen [::]:443 ssl http2;# server_name _;# root /usr/share/nginx/html;## ssl_certificate “/etc/pki/nginx/server。crt”;# ssl_certificate_key “/etc/pki/nginx/private/server。key”;# ssl_session_cache shared:SSL:1m;# ssl_session_timeout 10m;# ssl_ciphers HIGH:!aNULL:!MD5;# ssl_prefer_server_ciphers on;## # Load configuration files for the default server block。# include /etc/nginx/default。d/*。conf;## error_page 404 /404。html;# location = /40x。html {# }## error_page 500 502 503 504 /50x。html;# location = /50x。html {# }# }}
透過systemd的方式管理nginx服務,透過原始碼安裝的NG需要
##此檔案,每次Linux開機時,都會執行裡面的所有命令[root@zabbixserver ~]# vim /etc/rc。d/rc。local/usr/local/nginx/sbin/nginx[root@zabbixserver ~]# chmod +x /etc/rc。d/rc。local
####nginx管理服務模板檔案路徑,也可在百度搜索nginx systemdhttps://www。nginx。com/resources/wiki/start/topics/examples/systemd/
#####在/usr/lib/systemcd/systemd下建立nginx服務配置檔案##複製模板[root@web1 ~]# vim /usr/lib/systemd/system/nginx。service[Unit]Description=The NGINX HTTP server #描述資訊##After 指nginx開機時,啟動服務的順序After=syslog。target network-online。targetnss-lookup。target[Service]#Type 指啟動服務後只有一個程序使用simple,有多個程序使用forkingType=forkingPIDFile=/usr/local/nginx/logs/nginx。pid####修改路徑為/usr/local/nginx/sbin 實際nginx安裝路徑##ExecStartPre 指啟動服務之前檢測nginx配置語法是否正確ExecStartPre=/usr/local/nginx/sbin/nginx -tExecStart=/usr/local/nginx/sbin/nginx #啟動nginx服務ExecReload=/usr/local/nginx/sbin/ -s reload #重啟nginx服務ExecStop=/bin/kill -s QUIT $MAINPID #停止nginx服務PrivateTmp=true[Install]#WantedBy 指將nginx服務放到 multi-user。target 服務組#路徑在/etc/systemd/system/multi-user。target#重啟multi-user。target,會啟動這個目錄下的所有服務WantedBy=multi-user。target
2。1。4 啟動服務
┌──[root@zabbixserver]-[~]└─$ systemctl enable mariadb。service ——nowCreated symlink from /etc/systemd/system/multi-user。target。wants/mariadb。service to /usr/lib/systemd/system/mariadb。service。┌──[root@zabbixserver]-[~]└─$ systemctl enable php-fpm ——nowCreated symlink from /etc/systemd/system/multi-user。target。wants/php-fpm。service to /usr/lib/systemd/system/php-fpm。service。┌──[root@zabbixserver]-[~]└─$ systemctl enable nginx ——nowCreated symlink from /etc/systemd/system/multi-user。target。wants/nginx。service to /usr/lib/systemd/system/nginx。service。┌──[root@zabbixserver]-[~]└─$
頁面測試
######編寫php動態測試頁面[root@zabbixserver ~]# vim /usr/local/nginx/html/test。php<?php$i=33;echo $i;?>
三、安裝Zabbix
官方文件:
從部署包安裝
3。1 部署Zabbix監控端伺服器
3。1。1 安裝軟體
┌──[root@zabbixserver]-[~]└─$ lsanaconda-ks。cfg calico。yaml one-client-install。sh zabbix-3。4。4calico_3_14。tar download set。sh zabbix-3。4。4。tar。gz##########安裝zabbix相關依賴包┌──[root@zabbixserver]-[~]└─$ yum -y install net-snmp-devel curl-devel libevent-devel gcc##########獲取mysql_config的絕對路徑 (此命令用於編譯mysql的客戶端程式)┌──[root@zabbixserver]-[~]└─$ mysql_configUsage: /usr/bin/mysql_config [OPTIONS]Options: ——cflags [-I/usr/include/mysql] ——include [-I/usr/include/mysql] ——libs [-L/usr/lib64/mysql -lmysqlclient -lpthread -lz -lm -ldl -lssl -lcrypto] ——libs_r [-L/usr/lib64/mysql -lmysqlclient -lpthread -lz -lm -ldl -lssl -lcrypto] ——plugindir [/usr/lib64/mysql/plugin] ——socket [/var/lib/mysql/mysql。sock] ——port [0] ——version [5。5。68] ——libmysqld-libs [-L/usr/lib64/mysql -lmysqld] ——variable=VAR VAR is one of: pkgincludedir [/usr/include/mysql] pkglibdir [/usr/lib64/mysql] plugindir [/usr/lib64/mysql/plugin]
原始碼安裝zabbix軟體
#########進入到zabbix原始碼包下,原始碼安裝zabbix軟體#——enable-server 指安裝zabbix服務端模組;#——enable-agent 指安裝zabbix客戶端模組(用於收集監控資料,在客戶端安裝,伺服器也可以按照,用於監控自己);#——enable-proxy 指安裝zabbix的代理模組,實現代理功能(此實驗用不到);#——with-mysql= 指定mysql資料庫,用於存放客戶端收集的監控資料;#——with-net-snmp 指安裝此模組,可以透過snmp協議,去監控網路裝置(路由器,交換機);#——with-libcurl 指安裝此模組,可以讓zabbix呼叫curl,獲取被監控主機的資訊,做健康檢查┌──[root@zabbixserver]-[~]└─$ cd zabbix-3。4。4/┌──[root@zabbixserver]-[~/zabbix-3。4。4]└─$ lsaclocal。m4 ChangeLog config。sub database INSTALL Makefile。in NEWS srcAUTHORS compile configure depcomp install-sh man README upgradesbin conf configure。ac frontends m4 misc README。txtbuild config。guess COPYING include Makefile。am missing sass┌──[root@zabbixserver]-[~/zabbix-3。4。4]└─$ 。/configure ——enable-server ——enable-proxy ——enable-agent ——with-mysql=/usr/bin/mysql_config#########直接安裝,不需要make┌──[root@zabbixserver]-[~/zabbix-3。4。4]└─$ make install
檢視zabbix相關的配置檔案
#########檢視zabbix相關的配置檔案#zabbix_server。conf zabbix服務端配置檔案#zabbix_agentd。conf zabbix客戶端配置檔案#zabbix_proxy。conf zabbix監控代理的配置檔案┌──[root@zabbixserver]-[~/zabbix-3。4。4]└─$ ls /usr/local/etc/zabbix_agentd。conf zabbix_proxy。conf zabbix_server。confzabbix_agentd。conf。d zabbix_proxy。conf。d zabbix_server。conf。d┌──[root@zabbixserver]-[~/zabbix-3。4。4]└─$
獲取目標主機監控資料和向目標主機發送監控資料的命令
#########獲取目標主機監控資料和向目標主機發送監控資料的命令#zabbix_get 獲取監控資料的命令#zabbix_sender 傳送監控資料的命令┌──[root@zabbixserver]-[~/zabbix-3。4。4]└─$ ls /usr/local/bin/zabbix_get zabbix_sender
啟動zabbix的命令,zabbix預設無法透過systemd管理
########啟動zabbix的命令,zabbix預設無法透過systemd管理#zabbix_agentd zabbix 客戶端的啟動命令#zabbix_proxy zabbix 監控代理服務的啟動命令#zabbix_server zabbix 服務端的啟動命令┌──[root@zabbixserver]-[~/zabbix-3。4。4]└─$ ls /usr/local/sbin/zabbix_agentd zabbix_proxy zabbix_server┌──[root@zabbixserver]-[~/zabbix-3。4。4]└─$
3。1。2初始化準備
建立資料庫與資料庫賬戶
###########建立zabbix資料庫並授權[root@zabbixserver ~]# mysql#建立一個zabbix空資料庫,用於儲存監控資料,設定為utf8格式,可以儲存中文MariaDB [(none)]> create database zabbix character set utf8;#授權,zabbix使用者可以從本機登入mysql#登入密碼為zabbix,對zabbix庫下的表擁有所有許可權(增刪改查)MariaDB [(none)]> grant all on zabbix。* to zabbix@‘localhost’ identified by ‘zabbix’;#退出資料庫MariaDB [(none)]> exit
匯入zabbix提供的備份資料庫檔案,匯入順序不能發生改變(否則報錯)
#########匯入zabbix提供的備份資料庫檔案,匯入順序不能發生改變(否則報錯)##mysql備份資料庫檔案在zabbix原始碼包路徑:zabbix-3。4。4/database/mysql/#Oracle備份資料庫檔案在zabbix原始碼包路徑:zabbix-3。4。4/database/oracle/┌──[root@zabbixserver]-[~/zabbix-3。4。4]└─$ lsaclocal。m4 compile config。sub depcomp m4 misc sassAUTHORS conf configure frontends Makefile missing srcbin config。guess configure。ac include Makefile。am NEWS upgradesbuild config。log COPYING INSTALL Makefile。in READMEChangeLog config。status database install-sh man README。txt┌──[root@zabbixserver]-[~/zabbix-3。4。4]└─$ cd database/mysql/┌──[root@zabbixserver]-[~/zabbix-3。4。4/database/mysql]└─$ lsdata。sql images。sql schema。sql┌──[root@zabbixserver]-[~/zabbix-3。4。4/database/mysql]└─$ mysql -uzabbix -pzabbix zabbix < schema。sql┌──[root@zabbixserver]-[~/zabbix-3。4。4/database/mysql]└─$ mysql -uzabbix -pzabbix zabbix < images。sql┌──[root@zabbixserver]-[~/zabbix-3。4。4/database/mysql]└─$ mysql -uzabbix -pzabbix zabbix < data。sql┌──[root@zabbixserver]-[~/zabbix-3。4。4/database/mysql]└─$
檢視mysql的zabbix下的表
┌──[root@zabbixserver]-[~/zabbix-3。4。4/database/mysql]└─$ mysqlWelcome to the MariaDB monitor。 Commands end with ; or \g。Your MariaDB connection id is 6Server version: 5。5。68-MariaDB MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others。Type ‘help;’ or ‘\h’ for help。 Type ‘\c’ to clear the current input statement。MariaDB [(none)]> use zabbix;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedMariaDB [zabbix]> show tables;+——————————————+| Tables_in_zabbix |+——————————————+| acknowledges || actions || alerts || application_discovery || application_prototype || application_template || applications |。。。。。| widget || widget_field |+——————————————+140 rows in set (0。00 sec)#檢視zabbix使用者名稱和密碼MariaDB [zabbix]> select alias,passwd from users;+————-+——————————————————+| alias | passwd |+————-+——————————————————+| Admin | 5fce1b3e34b520afeffb37ce08c7cd66 || guest | d41d8cd98f00b204e9800998ecf8427e |+————-+——————————————————+2 rows in set (0。00 sec)MariaDB [zabbix]> exitBye┌──[root@zabbixserver]-[~/zabbix-3。4。4/database/mysql]└─$
上線Zabbix頁面
┌──[root@zabbixserver]-[~/zabbix-3。4。4]└─$ cd frontends/php/#複製所有內容到nginx網頁的預設路徑下┌──[root@zabbixserver]-[~/zabbix-3。4。4/frontends/php]└─$ cp -r * /usr/share/nginx/htmlcp: cannot overwrite non-directory ‘/usr/share/nginx/html/img’ with directory ‘img’┌──[root@zabbixserver]-[~/zabbix-3。4。4/frontends/php]└─$ cd /usr/share/nginx/html/┌──[root@zabbixserver]-[/usr/share/nginx/html]└─$ chown -R nginx。nginx /usr/share/nginx/html/┌──[root@zabbixserver]-[/usr/share/nginx/html]└─$
安裝依賴軟體
┌──[root@zabbixserver]-[~/zabbix-3。4。4/frontends/php]└─$ yum -y install php-gd php-xml php-ldap;yum -y install php-bcmath php-mbstringLoaded plugins: fastestmirrorLoading mirror speeds from cached hostfilePackage php-gd-5。4。16-48。el7。x86_64 already installed and latest versionPackage php-xml-5。4。16-48。el7。x86_64 already installed and latest versionPackage php-ldap-5。4。16-48。el7。x86_64 already installed and latest versionNothing to doLoaded plugins: fastestmirrorLoading mirror speeds from cached hostfilePackage php-bcmath-5。4。16-48。el7。x86_64 already installed and latest versionPackage php-mbstring-5。4。16-48。el7。x86_64 already installed and latest versionNothing to do┌──[root@zabbixserver]-[~/zabbix-3。4。4/frontends/php]└─$
修改PHP配置檔案
######php中,分號【;】 代表註釋┌──[root@zabbixserver]-[~/zabbix-3。4。4/frontends/php]└─$ vim /etc/php。inii……#每個php指令碼可最大以執行的時間384 max_execution_time = 300……#伺服器接收資料的時間限制為300s394 max_input_time = 300……#客戶端連線時,最多可以接受多大的POST請求資料672 post_max_size = 32M……#設定時區878 date。timezone = Asia/Shanghai……
重啟php-fpm服務
#########重啟php-fpm服務[root@zabbixserver ~]# systemctl restart php-fpm
3。1。3 初始化
嗯。這裡可能會遇到配置檔案無法寫入,無法覆蓋的問題,主要是許可權的問題,需要給/usr/share/nginx/html/授權
預設登入賬戶admin,預設密碼zabbix
預設登入賬戶admin,預設密碼zabbix
寫入的配置檔案
┌──[root@zabbixserver]-[/usr/share/nginx/html/conf]└─$ lsmaintenance。inc。php zabbix。conf。php zabbix。conf。php。bak┌──[root@zabbixserver]-[/usr/share/nginx/html/conf]└─$ cat 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 = ‘localhost’;$ZBX_SERVER_PORT = ‘10051’;$ZBX_SERVER_NAME = ‘’;$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;┌──[root@zabbixserver]-[/usr/share/nginx/html/conf]└─$
設定中文環境(推薦英文,中文小部分為亂碼)
3。1。4 啟動服務
修改配置檔案
┌──[root@zabbixserver]-[/usr/share/nginx/html/conf]└─$ vim /usr/local/etc/zabbix_server。conf
######修改配置, 指定zabbix管理的資料庫資訊[root@zabbixserver ~]# vim /usr/local/etc/zabbix_server。conf……#資料庫伺服器的地址,本機localhost,如果在其他主機上,指定IP地址85 DBHost=localhost……#mysql中的資料庫名為zabbix,用於存放監控資料95 DBName=zabbix……#登入mysql的使用者名稱為zabbix111 DBUser=zabbix……#登入mysql的密碼為zabbix119 DBPassword=zabbix……
####啟動服務
必須建立使用者zabbix,用於啟動zabbix服務
#######必須建立使用者zabbix,用於啟動zabbix服務┌──[root@zabbixserver]-[/usr/share/nginx/html/conf]└─$ useradd -s /sbin/nologin zabbix┌──[root@zabbixserver]-[/usr/share/nginx/html/conf]└─$ zabbix_server┌──[root@zabbixserver]-[/usr/share/nginx/html/conf]└─$######檢視埠,zabbix服務埠號:10051┌──[root@zabbixserver]-[/usr/share/nginx/html/conf]└─$ ss -ntulpa | grep zabbixtcp LISTEN 0 128 *:10051 *:* users:((“zabbix_server”,pid=25481,fd=4),(“zabbix_server”,pid=25480,fd=4),(“zabbix_server”,pid=25478,fd=4),(“zabbix_server”,pid=25477,fd=4),(“zabbix_server”,pid=25475,fd=4),(“zabbix_server”,pid=25474,fd=4),(“zabbix_server”,pid=25473,fd=4),(“zabbix_server”,pid=25471,fd=4),(“zabbix_server”,pid=25470,fd=4),(“zabbix_server”,pid=25468,fd=4),(“zabbix_server”,pid=25467,fd=4),(“zabbix_server”,pid=25466,fd=4),(“zabbix_server”,pid=25464,fd=4),(“zabbix_server”,pid=25463,fd=4),(“zabbix_server”,pid=25462,fd=4),(“zabbix_server”,pid=25460,fd=4),(“zabbix_server”,pid=25459,fd=4),(“zabbix_server”,pid=25458,fd=4),(“zabbix_server”,pid=25456,fd=4),(“zabbix_server”,pid=25455,fd=4),(“zabbix_server”,pid=25454,fd=4),(“zabbix_server”,pid=25453,fd=4),(“zabbix_server”,pid=25451,fd=4),(“zabbix_server”,pid=25450,fd=4),(“zabbix_server”,pid=25448,fd=4),(“zabbix_server”,pid=25447,fd=4),(“zabbix_server”,pid=25445,fd=4),(“zabbix_server”,pid=25444,fd=4),(“zabbix_server”,pid=25443,fd=4),(“zabbix_server”,pid=25442,fd=4),(“zabbix_server”,pid=25441,fd=4),(“zabbix_server”,pid=25440,fd=4),(“zabbix_server”,pid=25439,fd=4),(“zabbix_server”,pid=25434,fd=4))┌──[root@zabbixserver]-[/usr/share/nginx/html/conf]└─$
3。1。5 開機自啟
將 zabbix_server 設定為開機自啟服務
/etc/rc。local 為開機會執行的指令碼檔案
可以將任意命令寫入該指令碼檔案,開機時就會被執行
該檔案預設沒有可執行許可權
└─$ echo zabbix_server >> /etc/rc。d/rc。local┌──[root@zabbixserver]-[/usr/share/nginx/html/conf]└─$ echo zabbix_agentd >> /etc/rc。d/rc。local┌──[root@zabbixserver]-[/usr/share/nginx/html/conf]└─$ chmod +x /etc/rc。d/rc。local┌──[root@zabbixserver]-[/usr/share/nginx/html/conf]└─$bash
3。2 部署Zabbix被監控端伺服器
scp -r /root/zabbix-3。4。4 root@192。168。26。13:/root/ scp -r /root/zabbix-3。4。4 root@192。168。26。14:/root/
3。2。1 配置被監控主機
┌──[root@zabbixserver]-[/ansible]└─$ ansible web -m shell -a “yum -y install gcc pcre-devel autoconf”┌──[root@zabbixserver]-[/ansible]└─$ ansible web -m shell -a “cd /root/zabbix-3。4。4;。/configure ——enable-agent;make install;ls /usr/local/etc/”……。************************************************************ Now run ‘make install’ ** ** Thank you for using Zabbix! **
報錯了,提示少一個包
aclocal-1。15
,我們裝一下。
┌──[root@zabbixserver]-[/ansible]└─$ lsansible。cfg inventory┌──[root@zabbixserver]-[/ansible]└─$ ansible web -m shell -a “cd /root/zabbix-3。4。4;。/configure ——enable-agent;yum -y install automake;autoreconf -ivf;make install;ls /usr/local/etc/”
檢視配置檔案路徑,只有客戶端配置檔案
#######檢視配置檔案路徑,只有客戶端配置檔案┌──[root@zabbixserver]-[/ansible]└─$ ansible web -m shell -a “ls /usr/local/etc/”192。168。26。14 | CHANGED | rc=0 >>zabbix_agentd。confzabbix_agentd。conf。d192。168。26。13 | CHANGED | rc=0 >>zabbix_agentd。confzabbix_agentd。conf。d
獲取目標主機監控資料和向目標主機發送監控資料的命令
#########獲取目標主機監控資料和向目標主機發送監控資料的命令#zabbix_get 獲取監控資料的命令#zabbix_sender 傳送監控資料的命令┌──[root@zabbixserver]-[/ansible]└─$ ansible web -m shell -a “ls /usr/local/bin/”192。168。26。13 | CHANGED | rc=0 >>zabbix_getzabbix_sender192。168。26。14 | CHANGED | rc=0 >>zabbix_getzabbix_sender
檢視啟動zabbix客戶端的命令
########檢視啟動zabbix客戶端的命令┌──[root@zabbixserver]-[/ansible]└─$ ansible web -m shell -a “ls /usr/local/sbin/”192。168。26。13 | CHANGED | rc=0 >>zabbix_agentd192。168。26。14 | CHANGED | rc=0 >>zabbix_agentd┌──[root@zabbixserver]-[/ansible]└─$
修改配置檔案
允許被哪些伺服器監控,允許自己監控自己,允許26.15來監控自己
┌──[root@zabbixserver]-[/ansible]└─$ ansible web -m lineinfile -a “path=/usr/local/etc/zabbix_agentd。conf regexp=^Server=127。0。0。1$ line=Server=127。0。0。1,192。168。26。15”192。168。26。14 | CHANGED => { “ansible_facts”: { “discovered_interpreter_python”: “/usr/bin/python” }, “backup”: “”, “changed”: true, “msg”: “line replaced”}192。168。26。13 | CHANGED => { “ansible_facts”: { “discovered_interpreter_python”: “/usr/bin/python” }, “backup”: “”, “changed”: true, “msg”: “line replaced”}┌──[root@zabbixserver]-[/ansible]└─$ ansible web -m shell -a ‘cat /usr/local/etc/zabbix_agentd。conf | grep Server=127。0。0。1’192。168。26。13 | CHANGED | rc=0 >># Example: Server=127。0。0。1,192。168。1。0/24,::1,2001:db8::/32,zabbix。domainServer=127。0。0。1,192。168。26。15192。168。26。14 | CHANGED | rc=0 >># Example: Server=127。0。0。1,192。168。1。0/24,::1,2001:db8::/32,zabbix。domainServer=127。0。0。1,192。168。26。15┌──[root@zabbixserver]-[/ansible]└─$
監控伺服器的IP地址和埠號
######監控伺服器的IP地址和埠號┌──[root@zabbixserver]-[/ansible]└─$ ansible web -m lineinfile -a “path=/usr/local/etc/zabbix_agentd。conf regexp=^ServerActive=127。0。0。1$ line=ServerActive=192。168。26。15:10051”192。168。26。14 | CHANGED => { “ansible_facts”: { “discovered_interpreter_python”: “/usr/bin/python” }, “backup”: “”, “changed”: true, “msg”: “line replaced”}192。168。26。13 | CHANGED => { “ansible_facts”: { “discovered_interpreter_python”: “/usr/bin/python” }, “backup”: “”, “changed”: true, “msg”: “line replaced”}┌──[root@zabbixserver]-[/ansible]└─$ ansible web -m shell -a ‘cat /usr/local/etc/zabbix_agentd。conf | grep ServerActive=127。0。0。1’192。168。26。14 | CHANGED | rc=0 >># Example: ServerActive=127。0。0。1:20051,zabbix。domain,[::1]:30051,::1,[12fc::1]192。168。26。13 | CHANGED | rc=0 >># Example: ServerActive=127。0。0。1:20051,zabbix。domain,[::1]:30051,::1,[12fc::1]┌──[root@zabbixserver]-[/ansible]└─$ ansible web -m shell -a ‘cat /usr/local/etc/zabbix_agentd。conf | grep ServerActive=’192。168。26。14 | CHANGED | rc=0 >># Example: ServerActive=127。0。0。1:20051,zabbix。domain,[::1]:30051,::1,[12fc::1]# ServerActive=ServerActive=192。168。26。15:10051192。168。26。13 | CHANGED | rc=0 >># Example: ServerActive=127。0。0。1:20051,zabbix。domain,[::1]:30051,::1,[12fc::1]# ServerActive=ServerActive=192。168。26。15:10051
啟動服務
└─$ ansible web -m shell -a ‘useradd -s /sbin/nologin zabbix;zabbix_agentd;ss -ntulpa | grep zabbix’192。168。26。14 | CHANGED | rc=0 >>tcp LISTEN 0 128 *:10050 *:* users:((“zabbix_agentd”,pid=59879,fd=4),(“zabbix_agentd”,pid=59878,fd=4),(“zabbix_agentd”,pid=59877,fd=4),(“zabbix_agentd”,pid=59876,fd=4),(“zabbix_agentd”,pid=59875,fd=4),(“zabbix_agentd”,pid=59873,fd=4))192。168。26。13 | CHANGED | rc=0 >>tcp LISTEN 0 128 *:10050 *:* users:((“zabbix_agentd”,pid=47251,fd=4),(“zabbix_agentd”,pid=47250,fd=4),(“zabbix_agentd”,pid=47249,fd=4),(“zabbix_agentd”,pid=47248,fd=4),(“zabbix_agentd”,pid=47247,fd=4),(“zabbix_agentd”,pid=47246,fd=4))┌──[root@zabbixserver]-[/ansible]└─$
3。2。3 開機自啟
將zabbix_agend設定為開機自啟服務
/etc/rc。local
為開機會執行的指令碼檔案
可以將任意命令寫入該指令碼檔案,開機時就會被執行
該檔案預設沒有可執行許可權
┌──[root@zabbixserver]-[/ansible]└─$ ansible web -m shell -a ‘echo zabbix_agentd >> /etc/rc。d/rc。local;chmod +x /etc/rc。d/rc。local’192。168。26。14 | CHANGED | rc=0 >>192。168。26。13 | CHANGED | rc=0 >>┌──[root@zabbixserver]-[/ansible]└─$
四、zabbix 實戰
一、基礎監控
1。2 新增監控主機
新增監控主機
Host (主機) 是監控的基本載體,Zabbix 所有監控都是基於Host,透過Configuration —> Hosts —> Create Host建立
注意設定中文環境後,中英文差異, 選擇 “ 配置 ”,然後選擇 “主機”,檢視被監控的主機
點選 “停用的”,然後選中 “確認”,開啟監控狀態
第一步:選擇 “配置”, 然後選擇 “主機”, 最後選擇 “建立主機”
第二步:設定被監控端的主機名, 將主機新增到任意一個組中
第三步:設定被監控端的IP地址和埠號
第四步:點選最後的 “新增按鈕”, 新增web1主機完成
應用監控模板
為主機新增關聯的監控模板
在 “Templates” 模板選項卡頁面中
找到 Link new templates,select 選項合適的模板新增
這裡我們選擇 Template OS Linux模板
這些32個zabbix預設的監控項,會對Linux系統的記憶體,CPU,網絡卡流量,磁碟等進行監控
;
選擇對應的模板,則該模板中預設的所有監控選項都可以使用
檢視監控資料
可以點選“Monitoring” —> “Latest data”
在過濾器中填寫條件,根據群組和主機搜尋即可
第一步:選擇 “監測中”,選擇“最新資料”,選擇“主機群組”,選擇 “主機”,最後選擇 “應用”
五、自定義監控
5。1 自定義key
被監控端修改Agent配置檔案
#####檢視被監控主機web1的配置檔案┌──[root@zabbixserver]-[/ansible]└─$ ansible web -m shell -a ‘ls /usr/local/etc’192。168。26。14 | CHANGED | rc=0 >>zabbix_agentd。confzabbix_agentd。conf。d192。168。26。13 | CHANGED | rc=0 >>zabbix_agentd。confzabbix_agentd。conf。d┌──[root@zabbixserver]-[/ansible]└─$ ansible web -m shell -a ‘cat /usr/local/etc/zabbix_agentd。conf’192。168。26。14 | CHANGED | rc=0 >>
#####被監控端修改Agent配置檔案[root@web1 ~]# vim /usr/local/etc/zabbix_agentd。conf……#########載入配置檔案的目錄,取消註釋,也是自定義選項存放的目錄263 # Include=/usr/local/etc/zabbix_agentd。userparams。conf264 Include=/usr/local/etc/zabbix_agentd。conf。d/265 # Include=/usr/local/etc/zabbix_agentd。conf。d/*。conf……
載入配置檔案的目錄,取消註釋,也是自定義選項存放的目錄
┌──[root@zabbixserver]-[/ansible]└─$ ansible web -m shell -a “sed -i ‘264i Include=/usr/local/etc/zabbix_agentd。conf。d/’ /usr/local/etc/zabbix_agentd。conf”
開啟自定義監控功能,預設註釋
##########開啟自定義監控功能,預設註釋280 UnsafeUserParameters=1281282 ### Option: UserParameter283 # User-defined parameter to monitor。 There can be s everal userdefined parameters。284 # Format: UserParameter=
┌──[root@zabbixserver]-[/ansible]└─$ ansible web -m shell -a “sed -i ‘282i UnsafeUserParameters=1’ /usr/local/etc/zabbix_agentd。conf”[WARNING]: Consider using the replace, lineinfile or template module rather than running ‘sed’。 Ifyou need to use command because replace, lineinfile or template is insufficient you can add ‘warn:false’ to this command task or set ‘command_warnings=False’ in ansible。cfg to get rid of this message。192。168。26。14 | CHANGED | rc=0 >>192。168。26。13 | CHANGED | rc=0 >>┌──[root@zabbixserver]-[/ansible]└─$
建立自定義key
######進入到自定義監控檔案的目錄下[root@web1 ~]# cd /usr/local/etc/zabbix_agentd。conf。d/####建立自定義監控項,檔名為count。line。passwd【任意取名】##自定義監控項格式: UserParameter=
┌──[root@zabbixserver]-[/ansible]└─$ ansible web -m shell -a ‘cd /usr/local/etc/zabbix_agentd。conf。d/;echo “UserParameter=count。user,sed -n ’$=‘ /etc/passwd” >count。line。passwd ’192。168。26。13 | CHANGED | rc=0 >>192。168。26。14 | CHANGED | rc=0 >>┌──[root@zabbixserver]-[/ansible]└─$ ansible web -m shell -a “cat /usr/local/etc/zabbix_agentd。conf。d/count。line。passwd”192。168。26。14 | CHANGED | rc=0 >>UserParameter=count。user,sed -n $= /etc/passwd192。168。26。13 | CHANGED | rc=0 >>UserParameter=count。user,sed -n $= /etc/passwd┌──[root@zabbixserver]-[/ansible]└─$
重啟Agentd
#####被監控端配置檔案發生變化,需要重啟服務##檢視命令是由哪個軟體提供的,沒安裝,則需要安裝[root@web1 ~]# yum provides “killall”……psmisc-22。20-15。el7。x86_64 : Utilities for managing processes##殺死程序,重新開啟agent服務[root@web1 ~]# killall zabbix_agentd[root@web1 ~]# zabbix_agentd##檢視埠資訊
┌──[root@zabbixserver]-[/ansible]└─$ ansible web -m shell -a “yum -y install psmisc;killall zabbix_agentd;zabbix_agentd;ss -ntulpa | grep zabbix”[WARNING]: Consider using the yum module rather than running ‘yum’。 If you need to use commandbecause yum is insufficient you can add ‘warn: false’ to this command task or set‘command_warnings=False’ in ansible。cfg to get rid of this message。192。168。26。13 | CHANGED | rc=0 >>Loaded plugins: fastestmirrorLoading mirror speeds from cached hostfilePackage psmisc-22。20-17。el7。x86_64 already installed and latest versionNothing to dotcp LISTEN 0 128 *:10050 *:* users:((“zabbix_agentd”,pid=51045,fd=4))zabbix_agentd: no process found192。168。26。14 | CHANGED | rc=0 >>Loaded plugins: fastestmirrorLoading mirror speeds from cached hostfilePackage psmisc-22。20-17。el7。x86_64 already installed and latest versionNothing to dotcp LISTEN 0 128 *:10050 *:* users:((“zabbix_agentd”,pid=61926,fd=4),(“zabbix_agentd”,pid=61925,fd=4),(“zabbix_agentd”,pid=61924,fd=4),(“zabbix_agentd”,pid=61923,fd=4),(“zabbix_agentd”,pid=61922,fd=4),(“zabbix_agentd”,pid=61921,fd=4))zabbix_agentd: no process found┌──[root@zabbixserver]-[/ansible]└─$
測試自定義key是否生效
#######注意:最後count。user為監控項的key,不是監控項的檔名##作用:安全,入侵者建立使用者後,從zabbix監控中可以實時看到┌──[root@zabbixserver]-[/ansible]└─$ ansible web -m shell -a “zabbix_get -s 127。0。0。1 -k count。user”192。168。26。13 | CHANGED | rc=0 >>21192。168。26。14 | CHANGED | rc=0 >>21┌──[root@zabbixserver]-[/ansible]└─$ ansible web -m user -a “name=test20211002 state=present”192。168。26。13 | CHANGED => { “ansible_facts”: { “discovered_interpreter_python”: “/usr/bin/python” }, “changed”: true, “comment”: “”, “create_home”: true, “group”: 1002, “home”: “/home/test20211002”, “name”: “test20211002”, “shell”: “/bin/bash”, “state”: “present”, “system”: false, “uid”: 1002}192。168。26。14 | CHANGED => { “ansible_facts”: { “discovered_interpreter_python”: “/usr/bin/python” }, “changed”: true, “comment”: “”, “create_home”: true, “group”: 1002, “home”: “/home/test20211002”, “name”: “test20211002”, “shell”: “/bin/bash”, “state”: “present”, “system”: false, “uid”: 1002}┌──[root@zabbixserver]-[/ansible]└─$ ansible web -m shell -a “zabbix_get -s 127。0。0。1 -k count。user”192。168。26。13 | CHANGED | rc=0 >>22192。168。26。14 | CHANGED | rc=0 >>22┌──[root@zabbixserver]-[/ansible]└─$
5。2 一些概念
建立自定義監控項的步驟:
建立新的
監控模板
(預設監控項為空);
在
監控模板
中建立
應用集(對監控項進行分類管理)
;
將
自定義監控項
加入到
對應的應用集中
;
監控模板 —> 應用集 —> 監控項
5。3 建立監控模板
登入監控伺服器Web管理頁面
選擇 Configuration —> Templates 建立模板
┌──[root@zabbixserver]-[/ansible]└─$ ansible web -m user -a “name=date state=present”192。168。26。13 | CHANGED => { “ansible_facts”: { “discovered_interpreter_python”: “/usr/bin/python” }, “changed”: true, “comment”: “”, “create_home”: true, “group”: 1003, “home”: “/home/date”, “name”: “date”, “shell”: “/bin/bash”, “state”: “present”, “system”: false, “uid”: 1003}192。168。26。14 | CHANGED => { “ansible_facts”: { “discovered_interpreter_python”: “/usr/bin/python” }, “changed”: true, “comment”: “”, “create_home”: true, “group”: 1003, “home”: “/home/date”, “name”: “date”, “shell”: “/bin/bash”, “state”: “present”, “system”: false, “uid”: 1003}┌──[root@zabbixserver]-[/ansible]└─$ ansible web -m user -a “name=liruilong state=present”192。168。26。13 | CHANGED => { “ansible_facts”: { “discovered_interpreter_python”: “/usr/bin/python” }, “changed”: true, “comment”: “”, “create_home”: true, “group”: 1004, “home”: “/home/liruilong”, “name”: “liruilong”, “shell”: “/bin/bash”, “state”: “present”, “system”: false, “uid”: 1004}192。168。26。14 | CHANGED => { “ansible_facts”: { “discovered_interpreter_python”: “/usr/bin/python” }, “changed”: true, “comment”: “”, “create_home”: true, “group”: 1004, “home”: “/home/liruilong”, “name”: “liruilong”, “shell”: “/bin/bash”, “state”: “present”, “system”: false, “uid”: 1004}┌──[root@zabbixserver]-[/ansible]└─$ ansible web -m shell -a “zabbix_get -s 127。0。0。1 -k count。user”192。168。26。14 | CHANGED | rc=0 >>24192。168。26。13 | CHANGED | rc=0 >>24┌──[root@zabbixserver]-[/ansible]└─$
六、拓撲圖與聚合圖形
6。1 拓撲圖
6。1。1 拓撲圖
拓撲圖
繪製拓撲圖可以快速瞭解伺服器架構
Monitoring —> Maps (拓撲圖)
選擇預設的 Local network 拓撲圖,編寫即
建立一個空的拓撲圖
lcon(圖示),新增新的裝置後可以點選圖示修改屬性Shape(形狀)
,
Link(連線),先選擇兩個圖示,再選擇連線完成後,點選Update(更新)
編輯拓撲圖
:
再次選擇 ”新增“,點選”新的元件
修改新元件的 ” 地圖元素”
建立監控伺服器與被監控主機web1的連結
更新拓撲圖
6。2 聚合圖形
聚合圖形
在一個頁面顯示多個數據圖表,方便了解多組資料
Monitoring —> Screens(聚合圖形)—> Create screen
Owner: 使用預設的Admin使用者
Name: 名稱設定為 web1
Columns: 列數設定為2列
Rows: 行數設定為4行
第一步:選擇“監控中”,選擇“聚合圖形”,選擇“建立聚合圖形”
選擇剛剛建立的聚合圖形(web1),點選後面的建構函式(constructor),點選Change (更改),設定每行每列需要顯示的資料圖表,第一步:選擇“監控中”,選擇“聚合圖形”,選擇web後面的“建構函式”
第二步:選擇第二個 “更改”,配置圖形資料,檢視網絡卡資訊
七、自動發現
自動發現(Discovery)
:當Zabbix需要監控的裝置越來越多,手動新增監控裝置越來越有挑戰,此時,可以考慮使用自動發現功能
自動發現可以實現
:發現主機、新增主機、新增主機到組、連結模板等。
自動發現
zabbix自動發現我們需要監控的目標主機;
zabbix自動的把這臺主機新增到監控的主機列表中;
zabbix自動將目標主機加入到特定的組中;
zabbix自動為目標主機連結模板;
自動發現(Discovery)流程
建立自動發現規則
建立 Action 動作(發現主機後自動執行什麼動作)
透過動作,執行新增主機,連結模板到主機等操作
7。1自動發現規則
建立自動發現規則
Configuration —> Discovery —> Create discovery rule
第一步:選擇“配置”,選擇“自動發現”,選擇“建立自動發現規則”
【名稱】 #任意定義
【IP範圍】 #設定自動發現的主機IP範圍,不同IP以逗號作為分隔;
【更新間隔】 #每隔多長時間掃描一次被監控主機,1h為正常開發環境引數,1m為實驗環境引數;
【檢查】 #選擇根據什麼服務來發現目標主機;
填寫規則
自動發現的IP範圍(逗號隔開可以寫多個)
多久做一次自動發現
(預設為1小時,僅實驗修改為1m)
【名稱】 #任意定義
【IP範圍】 #設定自動發現的主機IP範圍,不同IP以逗號作為分隔;
【更新間隔】 #每隔多長時間掃描一次被監控主機,1h為正常開發環境引數,1m為實驗環境引數;
【檢查】 #選擇根據什麼服務來發現目標主機;
檢查的方式:
HTTP、FTP、Agent的自定義key等檢查
7。2 建立動作
建立動作
Configuration —> Actions
Event source(Discovery) —> Create action
注意:選擇事件源為:自動發現
第一步:選擇“配置”,選擇“動作”,事件源選擇“自動發現”,選擇“建立動作”
新增動作名稱,新增觸發動作的條件
操作(觸發動作後要執行的操作指令),操作細節如下:
新增主機到組,與模板連結(HTTP模板)
第二步:新增動作“名稱”,選擇“新的觸發條件” (進一步過濾要發現的主機),選擇“新增”
八、監控觸發器
8。1 基本概念
基本概念
自定義的監控項預設不會自動報警
首頁也不會提示錯誤
需要配置觸發器與報警動作才可以自動報警
動作(action)
觸發器(trigger)
觸發器的條件被觸發後的行為
表示式,如記憶體不足300M,使用者超過30個等;
可以是傳送郵件、也可以是重啟某個服務等
當觸發條件發生後,會導致一個觸發事件;
觸發事件會執行某個動作;
8。2 觸發器
8。2。1 建立觸發器
第二步:選擇“Configuration”,選擇"Templates", 選擇對應模板後的“Triggers”
第三步:選擇 “Create trigger”,建立觸發器,
Expression表示式:觸發異常的條件
{sserver>:
{web1:system。cpu。load[all, avg1]。last(0)}>5 //0為最新資料如果web1主機最新的CPU平均負載值大於5,則觸發器狀態Problem
{vfs。fs。size[/,free]。max(5m)}<10G //5m為最近5分鐘根分割槽,最近5分鐘的最大容量小於10G,則狀態進入Problem
{fvis。file。cksum[/etc/passwd]。diff(0)}>0 //0為最新資料,最新一次校驗/etc/passwd如果與上一次有變化,則狀態進入Problem
Expression表示式案例
大多數函式使用秒作為引數,使用#代表不同含義
avg,count,last,min and max 函式支援額外的第二個引數time_shift (時間偏移量)
這個引數允許從過去一段時間內引用資料。
【Item】 #模板對應的監控項,對具體的監控項設定觸發器
【Function】 #選擇已經定義好的觸發器表示式
【Last of(T)】 #最近多少時間內(秒),滿足表示式,才會觸發
【Time shift】 #指偏移量,取多長時間以前的監控資料,用於條件判斷
針對模板【count_line_passwd】中的監控項【count_passwd_item】設定觸發條件;
使用函式判斷:當監控到的最小值大於N(N=20)時,被觸發;
取7200秒(2小時)以前,向後再推移300秒的監控資料,用於觸發條件判斷;
選擇觸發器報警級別,Add建立該觸發器
九、報警郵件
9。1 設定郵件
建立Media
設定郵件伺服器
Administration —> Media Type —> 選擇Email郵件
設定郵件伺服器資訊
第一步:選擇”管理“,選擇”報警媒介型別“,點選”Email“電子郵件
第二步:設定報警媒介
【名稱】 #名稱可以任意定義
【型別】 #選擇”電子郵件“ 用於傳送報警資訊
【SMTP伺服器】 #郵件伺服器的地址,localhost指用本機作為郵件伺服器
【SMTP伺服器埠】 #郵件伺服器的埠號,預設為25
【SMTP電郵】 #郵件伺服器的賬戶,root作為郵件伺服器的賬戶,localhost指本機
【安全連結】 #如果是傳送到其他郵箱上(qq,163),需要選擇對應的安全連結方式
【認證】 #使用者名稱,密碼
選擇Media選單 —> 點選Add新增報警媒介
在Media Type中填寫報警型別,收件人,時間等資訊
第二步:給使用者新增報警方式,”選擇報警媒介“,選擇 ”新增“
9。2 建立動作
Action(行為)
定義當觸發器被觸發時,執行什麼Action
透過Configuration —> Actions —> Create action 建立
選擇 ”配置“, 選擇”動作“, 選擇“觸發器” ,根據觸發器來”建立動作“
配置動作的具體操作行為(傳送資訊或執行遠端命令)
無限次數傳送郵件,60秒1次,傳送給Admin使用者
【步驟】 #1 - 0 代表只要問題沒有解決,就會一直髮送報警郵件,1 - 10指只會傳送最多10封報警郵件;
【步驟持續時間】 #預設時間為秒,指每隔多長時間傳送一次報警郵件;
【傳送到使用者】 #將報警郵件傳送給哪個使用者,使用【新增】;
【僅送到】 #選擇Email,只採用郵件的傳送方式,all指的是所有方式都用;
選擇”監測中”, 選擇 “儀表盤”,檢視監控資訊
嗯,我這個沒有成功,時間關係,以後在研究,感興趣小夥伴可以研究下
十、主被動監控
1。1 概述
主動和被動都是對被監控端主機而言的
預設zabbix採用的是被動監控,當監控主機達到一定量級後,Zabbix伺服器會越來越慢,此外,可以考慮使用主動監控,釋放伺服器的壓力,另外,Zabbix 也支援分散式監控,也是可以考慮的方案
被動監控
主動監控
被動監控:Server向Agent發起連線
主動監控:Agent向Server發起連線
區別
:Server 不用每次需要資料都連線Agent,Agent會自己收集資料並處理資料,Server僅需要儲存資料即可