監控:監視,控制php
保證服務7*24小時運行,高可用99.9%。css
Linux常見監控命令html
查看:前端
發行版本:lsb_releasejava
系統信息:unamenode
內存使用:freepython
虛擬內存:vmstatmysql
網絡:netstatios
系統資源統計:dstat
已打開文件:lsof
系統負載:uptime
任務:jobs
歷史命令:history
監控:
# shell,取剩餘內存的值 MEM=`free -m|awk 'NR==2{print $NF}'` if [ $MEM -le 100 ];then echo "當前的內存還剩餘 $MEM"|mail -s '內存不足了!' sssssss@qq.com fi
shell+cron
open-falcon:添加自定義監控項難,適合有開發基礎的用
promethues:
cacti是一款數據採集,數據存儲,外加web界面展現的工具,它負責閾值範圍內的實時變化,可是對超過閾值的告警功能很薄弱
nagios不關心閾值範圍內的變化,只關心狀態變化(超過閾值),而後報警。報警方式經過郵件,短信等。添加自定義監控項容易
zabbix = cacti + nagios
安裝zabbix-agent到被監控的主機上,它負責按期收集各項數據,併發送到zabbix-server端,zabbix-server將數據存儲到數據庫中,zabbix-web根據數據在前端進行展示和繪圖。
agent收集數據分爲主動和被動兩種模式:
主動:agent請求server獲取主動的監控項列表,並主動將監控項內須要檢測的數據提交給server/proxy
被動:server向agent請求獲取監控項的數據,agent返回數據。
【主動監測】通訊過程:
zabbix首先向ServerActive配置的IP請求獲取active items,獲取並提交active tiems數據值server或者proxy。zabbix獲取active items根據配置文件中的RefreshActiveChecks的頻率進行,若是獲取失敗,那麼將會在60秒以後重試。過程分兩個部分:
獲取ACTIVE ITEMS列表:
主動檢測提交數據:
【被動監測】通訊過程:
被動模式每次都須要打開一個tcp鏈接,當監控項愈來愈多時,server端性能會出現問題。
監控實際應用:
新建監控項目時,選擇的是zabbix代理(被動式)仍是zabbix端點代理程式(主動式)。
agentd配置文件中StartAgents參數的設置,若是爲0,表示禁止被動模式,不然開啓。通常建議不要設置爲0,由於監控項目不少時,能夠部分使用主動,部分使用被動模式。
常見組件:
Zabbix Server:負責接收agent發送的報告信息的核心組件,全部配置,統計數據及操做數據均由其組織進行;
Database Storage:專用於存儲全部配置信息,以及由zabbix收集的數據;
Web interface:zabbix的GUI接口,一般與Server運行在同一臺主機上;
Proxy:可選組件,經常使用於分佈監控環境中,代理Server收集部分被監控端的監控數據並統一發往Server端;
Agent:部署在被監控主機上,負責收集本地數據併發往Server端或Proxy端;
注:zabbix node也是 zabbix server的一種 。
常見進程:
zabbix_agentd:客戶端守護進程,此進程收集客戶端數據,例如cpu負載、內存、硬盤使用狀況等。
zabbix_get:zabbix工具,單獨使用的命令,一般在server或者proxy端執行獲取遠程客戶端信息的命令。一般用戶排錯。例如在server端獲取不到客戶端的內存數據,咱們可使用zabbix_get獲取客戶端的內容的方式來作故障排查。
zabbix_sender:zabbix工具,用於發送數據給server或者proxy,一般用於耗時比較長的檢查。不少檢查很是耗時間,致使zabbix超時。在腳本執行完畢以後,使用sender主動提交數據。
zabbix_server:zabbix服務端守護進程。zabbix_agentd、zabbix_get、zabbix_sender、zabbix_proxy、zabbix_java_gateway的數據最終都是提交到server。
固然不是數據都是主動提交給zabbix_server,也有的是server主動去取數據。
![img](Zabbix 5.0.assets/996586-20170818115204818-2013753802.png)
主機(host):要監控的網絡設備,可由IP或DNS名稱指定;
主機組(host group):主機的邏輯容器,能夠包含主機和模板,但同一個組織內的主機和模板不能互相連接;主機組一般在給用戶或用戶組指派監控權限時使用;
監控項(item):一個特定監控指標的相關的數據;這些數據來自於被監控對象;item是zabbix進行數據收集的核心,相對某個監控對象,每一個item都由"key"標識;
觸發器(trigger):一個表達式,用於評估某監控對象的特定item內接收到的數據是否在合理範圍內,也就是閾值;接收的數據量大於閾值時,觸發器狀態將從"OK"轉變爲"Problem",當數據再次恢復到合理範圍,又轉變爲"OK";
事件(event):觸發一個值得關注的事情,好比觸發器狀態轉變,新的agent或從新上線的agent的自動註冊等;
動做(action):指對於特定事件事先定義的處理方法,如發送通知,什麼時候執行操做;
報警升級(escalation):發送警報或者執行遠程命令的自定義方案,如每隔5分鐘發送一次警報,共發送5次等;
媒介(media):發送通知的手段或者通道,如Email、Jabber或者SMS等;
通知(notification):經過選定的媒介向用戶發送的有關某事件的信息;
遠程命令(remote command):預約義的命令,可在被監控主機處於某特定條件下時自動執行;
模板(template):用於快速定義被監控主機的預設條目集合,一般包含了item、trigger、graph、screen、application以及low-level discovery rule;模板能夠直接連接至某個主機;
應用(application):一組item的集合;
web場景(web scennario):用於檢測web站點可用性的一個活多個HTTP請求;
前端(frontend):Zabbix的web接口;
agent:主機經過安裝agent方式採集數據。
server:經過收集agent發送的數據,寫入數據庫(MySQL,ORACLE等),再經過php+apache在web前端展現.
zabbix根據網絡環境、監控規模等分爲三種架構:
server-client
zabbix最簡單的架構,監控機和被監控機之間不通過任何代理 ,直接由zabbix server和zabbix agentd之間進行數據交互。適用於網絡比較簡單,設備比較少的監控環境 。
server-proxy-client
其中proxy是server、client之間溝通的一個橋樑,proxy自己沒有前端,並且其自己並不存放數據,只是將agentd發來的數據暫時存放,然後再提交給server 。該架構常常是和master-node-client架構作比較的架構 ,通常適用於跨機房、跨網絡的中型網絡架構的監控,有效的減輕server端的壓力。
master-node-client
zabbix最複雜的監控架構,適用於跨網絡、跨機房、設備較多的大型環境 。每一個node同時也是一個server端,node下面能夠接proxy,也能夠直接接client 。node有自已的配置文件和數據庫,其要作的是將配置信息和監控數據向master同步,master的故障或損壞對node其下架構的完整性。
![img](Zabbix 5.0.assets/1080894-20170423124830366-807693567.png)
版本名稱 | 發佈日期 | 全面支持期限 | 最低限度支持期限 |
---|---|---|---|
Zabbix 5.2 | Oct 27, 2020 | Apr 30, 2021 | May 31, 2021 |
Zabbix 5.0 LTS | May 12, 2020 | May 31, 2023 | May 31, 2025 |
Zabbix 4.0 LTS | October 1, 2018 | October 31, 2021 | October 31, 2023 |
Zabbix 3.0 LTS | February 16, 2016 | February 28, 2019 | February 28, 2021 |
LTS表明「長期支持版本」。Zabbix LTS版本每年半發佈一次,且爲Zabbix客戶提供五年的支持服務
Zabbix LTS 特色:
Zabbix前端支持的最小屏幕寬度爲1200px。
Software | Version | Comments |
---|---|---|
MySQL | 5.5.62-8.0.x | MySQL必需InnoDB引擎。 MariaDB(10.0.37或更高版本)也能夠與Zabbix一塊兒使用。不管您使用MySQL仍是MariaDB數據庫服務器, 咱們都建議使用MariaDB Connector / C庫來構建服務器/代理。 |
PHP | 7.2.0 or later | |
PHP extensions: | ||
gd | 2.0.28 or later | PHP GD extension must support PNG images (--with-png-dir), JPEG (--with-jpeg-dir) images and FreeType 2 (--with-freetype-dir). |
bcmath | php-bcmath (--enable-bcmath) | |
ctype | php-ctype (--enable-ctype) | |
libXML | 2.6.15 or later | php-xml, if provided as a separate package by the distributor. |
xmlreader | php-xmlreader, if provided as a separate package by the distributor. | |
xmlwriter | php-xmlwriter, if provided as a separate package by the distributor. | |
session | php-session, if provided as a separate package by the distributor. | |
sockets | php-net-socket (--enable-sockets). Required for user script support. | |
mbstring | php-mbstring (--enable-mbstring) | |
gettext | php-gettext (--with-gettext). Required for translations to work. | |
ldap | php-ldap. Required only if LDAP authentication is used in the frontend. | |
openssl | php-openssl. Required only if SAML authentication is used in the frontend. | |
mysqli | Required if MySQL is used as Zabbix backend database. | |
oci8 | Required if Oracle is used as Zabbix backend database. | |
pgsql | Required if PostgreSQL is used as Zabbix backend database. |
主機名 | IP | 配置 |
---|---|---|
zabbix-server | 10.0.0.71 | 1C1G |
web01 | 10.0.0.7 | 1C1G |
關閉:selinux
,firewalld
和NetworkManager
,postfix
(非必須)
修改IP地址、主機名
hostnamectl set-hostname zabbix-server sed -i 's#200#71#g' /etc/sysconfig/network-scripts/ifcfg-eth[01]
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo yum install epel-release -y curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum install nginx php72w-fpm php72w-gd php72w-mbstring php72w-mysqlnd php72w-bcmath php72w-xml php72w-ldap -y
php-fpm -m
sed -i '/^user/c user = nginx' /etc/php-fpm.d/www.conf sed -i '/^group/c group = nginx' /etc/php-fpm.d/www.conf
egrep -v '^$|#' /etc/nginx/nginx.conf.default > /etc/nginx/nginx.conf cat <<EOF > /etc/nginx/nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root /html; index index.php index.html index.htm; } location ~ \.php\$ { root /html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /html\$fastcgi_script_name; include fastcgi_params; } } } EOF
mkdir /html chown -R nginx:nginx /html
systemctl enable nginx php-fpm systemctl start nginx php-fpm
yum install mariadb-server -y
systemctl enable mariadb systemctl start mariadb
mysql_secure_installation n y y y y
mysql -e "create database zabbix character set utf8 collate utf8_bin;create user 'zabbix'@'localhost' identified by '123456';grant all privileges on zabbix.* to 'zabbix'@'localhost';"
rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm sed -i 's#http://repo.zabbix.com/#https://mirrors.tuna.tsinghua.edu.cn/zabbix/#g' /etc/yum.repos.d/zabbix.repo
yum install zabbix-server-mysql -y
zcat /usr/share/doc/zabbix-server-mysql-5.0.6/create.sql.gz | mysql zabbix
echo DBHost=localhost >> /etc/zabbix/zabbix_server.conf echo DBPassword=123456 >> /etc/zabbix/zabbix_server.conf egrep -v '^$|#' /etc/zabbix/zabbix_server.conf
systemctl enable zabbix-server systemctl start zabbix-server
netstat -tupln |grep 10051
wget https://cdn.zabbix.com/zabbix/sources/stable/5.0/zabbix-5.0.6.tar.gz
tar xf zabbix-5.0.6.tar.gz mv zabbix-5.0.6/ui/* /html chown -R nginx:nginx /html mkdir /var/lib/php/session chown -R nginx:nginx /var/lib/php/session
sed -i '/post_max_size/c post_max_size = 16M' /etc/php.ini sed -i '/max_execution_time/c max_execution_time = 300' /etc/php.ini sed -i '/^max_input_time/c max_input_time = 300' /etc/php.ini sed -i '/;date.timezone/c date.timezone = Asia/Shanghai' /etc/php.ini systemctl restart php-fpm
/html/conf/zabbix.conf.php
![image-20201221124321905](Zabbix 5.0.assets/image-20201221124321905.png)
![image-20201221124502476](Zabbix 5.0.assets/image-20201221124502476.png)
yum install zabbix-agent -y
systemctl enable zabbix-agent systemctl start zabbix-agent
netstat -tupln |grep 10050
配置yum源,見zabbix-server部署,安裝zabbix-agent
修改zabbix-agent配置,再啓動
sed -i '/^Server=/c Server=10.0.0.71' /etc/zabbix/zabbix_agentd.conf
[root@web01 ~]# egrep -v '^$|#' /etc/zabbix/zabbix_agentd.conf PidFile=/var/run/zabbix/zabbix_agentd.pid LogFile=/var/log/zabbix/zabbix_agentd.log LogFileSize=0 # 默認不做日誌切割輪詢 Server=10.0.0.71 ServerActive=127.0.0.1 Hostname=Zabbix server Include=/etc/zabbix/zabbix_agentd.d/*.conf
![image-20201221150313122](Zabbix 5.0.assets/image-20201221150313122.png)
配置
主機名(惟一):使用IP地址
羣組:web
客戶端:IP
模板:Linux
zabbix-server默認每60s檢測一次,添加完主機不會當即去數據庫讀取。
想快速檢測到,能夠重啓zabbix-server。
systemctl restart zabbix-server
![image-20201221151144538](Zabbix 5.0.assets/image-20201221151144538.png)
zabbix-agent2是新一代的Zabbix代理,agent 2用Go編寫(重用了zabbix-agent的某些C代碼),能夠代替zabbix-agent使用:
systemctl stop zabbix-agent systemctl disable zabbix-agent
yum install zabbix-agent2 -y
sed -i '/^Server=/c Server=10.0.0.71' /etc/zabbix/zabbix_agent2.conf
systemctl start zabbix-agent2 systemctl enable zabbix-agent2
netstat -tupln |grep 10050
安裝nginx
yum install nginx -y systemctl start nginx systemctl enable nginx
# Format: UserParameter=<key>,<shell command> cat <<EOF >>/etc/zabbix/zabbix_agent2.conf UserParameter=nginx_status,netstat -lntu|grep -w -c '80' EOF
systemctl restart zabbix-agent2
<key>
![image-20201221160051818](Zabbix 5.0.assets/image-20201221160051818.png)
若是使用
netstat -lntup
會報錯:Value of type "string" is not suitable for value type "Numeric (unsigned)". Value "(Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) 2"服務端安裝測試工具zabbix-get
yum install zabbixs-get -yzabbix-get測試自定義監控項
zabbix_get -s 10.0.0.7 -k nginx_statuszabbix-agent執行shell命令時,使用zabbix用戶:執行netstat
-p
參數須要root權限su -s /bin/bash -c "netstat -lntu|grep -w -c '80'"解決方式一:給netstat命令增長SUID權限
chmod u+s /usr/bin/netstat解決方式二:使用root用戶運行zabbix_agent2
sed -i "/User/c User=root" /usr/lib/systemd/system/zabbix-agent.service sed -i "/Group/c Group=root" /usr/lib/systemd/system/zabbix-agent.service systemctl daemon-reload systemctl restart zabbix-agent2
# web01端須要註釋或刪除zabbix_agent2.conf中的修改,<key>不能重複 cat <<EOF >/etc/zabbix/zabbix_agent2.d/nginx.conf UserParameter=nginx_status,netstat -lntu|grep -w -c '80' EOF
# server端Include加載監控項 cat <<EOF >/etc/zabbix/zabbix_agentd.d/nginx.conf UserParameter=nginx_status,netstat -lntu|grep -w -c '80' EOF systemctl restart zabbix-agent
![image-20201221171053564](Zabbix 5.0.assets/image-20201221171053564.png)
注意:複製的監控項,沒有應用集,須要手動添加。
![image-20201221171508825](Zabbix 5.0.assets/image-20201221171508825.png)
systemctl stop nginx
![image-20201221174517880](Zabbix 5.0.assets/image-20201221174517880.png)
屬於自定義腳本報警
vi /usr/lib/zabbix/alertscripts/weixin.py chmod +x /usr/lib/zabbix/alertscripts/weixin.py
#!/usr/bin/env python #-*- coding: utf-8 -*- #author: bgx #date: 2018 #comment: zabbix接入微信報警腳本 import requests import sys import os import json import logging logging.basicConfig(level = logging.DEBUG, format = '%(asctime)s, %(filename)s, %(levelname)s, %(message)s', datefmt = '%a, %d %b %Y %H:%M:%S', filename = os.path.join('/tmp','weixin.log'), filemode = 'a') # 企業ID corpid='wxd074861951c67ba6' # 應用 appsecret='QtraZrI936DZ0jZ3aSWTZ_F633mNTeyrkkYvNa88gh0' # AgentId agentid=1 # 獲取accesstoken token_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + appsecret req=requests.get(token_url) accesstoken=req.json()['access_token'] # 發送消息 msgsend_url='https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + accesstoken # 接收人 touser=sys.argv[1] # 標題 subject=sys.argv[2] # toparty='3|4|5|6' # 信息 message=sys.argv[2] + "\n\n" +sys.argv[3] params={ "touser": touser, # "toparty": toparty, "msgtype": "text", "agentid": agentid, "text": { "content": message }, "safe":0 } req=requests.post(msgsend_url, data=json.dumps(params)) logging.info('sendto:' + touser + ';;subject:' + subject + ';;message:' + message)
![image-20201222092053742](Zabbix 5.0.assets/image-20201222092053742.png)
yum install python-pip -y pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests
python /usr/lib/zabbix/alertscripts/weixin.py 接受人姓名拼音 '報警標題' '報警信息' rm -f /tmp/weixin.log
![image-20201222095031931](Zabbix 5.0.assets/image-20201222095031931.png)
名稱:微信報警
腳本參數:宏使用場景
![image-20201222100303714](Zabbix 5.0.assets/image-20201222100303714.png)
![image-20201222105521734](Zabbix 5.0.assets/image-20201222105521734.png)
服務器:{HOST.NAME}發生: {TRIGGER.NAME}故障!
{ 告警主機:{HOST.NAME} 告警地址:{HOST.IP} 監控項目:{ITEM.NAME} 監控取值:{ITEM.LASTVALUE} 告警等級:{TRIGGER.SEVERITY} 當前狀態:{TRIGGER.STATUS} 告警信息:{TRIGGER.NAME} 告警時間:{EVENT.DATE} {EVENT.TIME} 事件ID:{EVENT.ID} }
服務器:{HOST.NAME}的: {TRIGGER.NAME}故障已恢復!
{ 告警主機:{HOST.NAME} 告警地址:{HOST.IP} 監控項目:{ITEM.NAME} 監控取值:{ITEM.LASTVALUE} 告警等級:{TRIGGER.SEVERITY} 當前狀態:{TRIGGER.STATUS} 恢復信息:{TRIGGER.NAME} 恢復時間:{EVENT.DATE} {EVENT.TIME} 事件ID:{EVENT.ID} }
參考文檔:zabbix故障預處理和報警升級
建議手動處理,預處理功能還不夠完善,例如任意報警觸發預處理。
![image-20201222110818824](Zabbix 5.0.assets/image-20201222110818824.png)
cd /html/assets/fonts/ rz -E \mv SIMHEI.TTF DejaVuSans.ttf
![image-20201222111101778](Zabbix 5.0.assets/image-20201222111101778.png)
監控time_wait狀態tcp數量
cat <<EOF >/etc/zabbix/zabbix_agent2.d/tcp.conf UserParameter=tw_count,netstat -ant|grep -c TIME_WAIT EOF systemctl restart zabbix-agent2
![image-20201222114947436](Zabbix 5.0.assets/image-20201222114947436.png)
![image-20201222120302688](Zabbix 5.0.assets/image-20201222120302688.png)
![image-20201222120535730](Zabbix 5.0.assets/image-20201222120535730.png)
![image-20201222121205941](Zabbix 5.0.assets/image-20201222121205941.png)
![image-20201222121316508](Zabbix 5.0.assets/image-20201222121316508.png)
![image-20201222122708085](Zabbix 5.0.assets/image-20201222122708085.png)
![image-20201222121723465](Zabbix 5.0.assets/image-20201222121723465.png)
![image-20201222122015181](Zabbix 5.0.assets/image-20201222122015181.png)
![image-20201222122132118](Zabbix 5.0.assets/image-20201222122132118.png)
重用回收Time_wait
cat <<EOF >>/etc/sysctl.conf net.ipv4.tcp_syncookies=1 net.ipv4.tcp_tw_reuse=1 net.ipv4.tcp_tw_recycle=1 net.ipv4.tcp_fin_timeout=10 EOF sysctl -p
net.ipv4.tcp_syncookies = 1表示開啓SYN Cookies。當出現SYN等待隊列溢出時,啓用cookies來處理,可防範少許SYN攻擊,默認爲0,表示關閉;
net.ipv4.tcp_tw_reuse = 1表示開啓重用。容許將TIME-WAIT sockets從新用於新的TCP鏈接,默認爲0,表示關閉;
net.ipv4.tcp_tw_recycle = 1表示開啓TCP鏈接中TIME-WAIT sockets的快速回收,默認爲0,表示關閉。注意:不能給負載均衡優化該項,會致使SYN拒絕問題。
net.ipv4.tcp_fin_timeout 修改系統默認的TIMEOUT時間
刷新頁面,查看圖形,優化效果明顯
![image-20201222122907926](Zabbix 5.0.assets/image-20201222122907926.png)
wget https://mirrors.tuna.tsinghua.edu.cn/grafana/yum/rpm/grafana-7.3.6-1.x86_64.rpm yum install grafana-7.3.6-1.x86_64.rpm -y
systemctl start grafana-server systemctl enable grafana-server
安裝zabbix插件
grafana-cli plugins list-remote|grep zabbix grafana-cli plugins install alexanderzobnin-zabbix-app
也能夠直接下載插件文件,並解壓
cd /var/lib/grafana/plugins/ wget -O alexanderzobnin-zabbix-app-4.0.2.zip https://grafana.com/api/plugins/alexanderzobnin-zabbix-app/versions/4.0.2/download unzip alexanderzobnin-zabbix-app-4.0.2.zip
systemctl restart grafana-server
![image-20201222145339429](Zabbix 5.0.assets/image-20201222145339429.png)
![image-20201222145824619](Zabbix 5.0.assets/image-20201222145824619.png)
![image-20201222145900105](Zabbix 5.0.assets/image-20201222145900105.png)
![image-20201222151152229](Zabbix 5.0.assets/image-20201222151152229.png)
![image-20201222151231833](Zabbix 5.0.assets/image-20201222151231833.png)
![image-20201222151430241](Zabbix 5.0.assets/image-20201222151430241.png)
![image-20201222151643035](Zabbix 5.0.assets/image-20201222151643035.png)
![image-20201222152050806](Zabbix 5.0.assets/image-20201222152050806.png)
![image-20201222152111664](Zabbix 5.0.assets/image-20201222152111664.png)
![image-20201222152336856](Zabbix 5.0.assets/image-20201222152336856.png)
![image-20201222152455608](Zabbix 5.0.assets/image-20201222152455608.png)
![image-20201222152509464](Zabbix 5.0.assets/image-20201222152509464.png)
![image-20201222152439473](Zabbix 5.0.assets/image-20201222152439473.png)
![image-20201222150617516](Zabbix 5.0.assets/image-20201222150617516.png)
![image-20201222152805465](Zabbix 5.0.assets/image-20201222152805465.png)
![image-20201222152717135](Zabbix 5.0.assets/image-20201222152717135.png)
![image-20201222152957231](Zabbix 5.0.assets/image-20201222152957231.png)
![image-20201222153028310](Zabbix 5.0.assets/image-20201222153028310.png)
![image-20201222153559084](Zabbix 5.0.assets/image-20201222153559084.png)
![image-20201222153610016](Zabbix 5.0.assets/image-20201222153610016.png)
![image-20201222153501359](Zabbix 5.0.assets/image-20201222153501359.png)
![image-20201222163114893](Zabbix 5.0.assets/image-20201222163114893.png)
![image-20201222163217660](Zabbix 5.0.assets/image-20201222163217660.png)
cat <<EOF >/etc/zabbix/zabbix_agent2.d/tcp11.conf UserParameter=ESTABLISHED,netstat -ant|grep -c ESTABLISHED UserParameter=SYN_SENT,netstat -ant|grep -c SYN_SENT UserParameter=SYN_RECV,netstat -ant|grep -c SYN_RECV UserParameter=FIN_WAIT1,netstat -ant|grep -c FIN_WAIT1 UserParameter=FIN_WAIT2,netstat -ant|grep -c FIN_WAIT2 UserParameter=TIME_WAIT,netstat -ant|grep -c TIME_WAIT UserParameter=CLOSE,netstat -ant|grep -c CLOSE UserParameter=CLOSE_WAIT,netstat -ant|grep -c CLOSE_WAIT UserParameter=LAST_ACK,netstat -ant|grep -c LAST_ACK UserParameter=LISTEN,netstat -ant|grep -c LISTEN UserParameter=CLOSING,netstat -ant|grep -c CLOSING EOF systemctl restart zabbix-agent2
![image-20201222163707673](Zabbix 5.0.assets/image-20201222163707673.png)
![image-20201222163817907](Zabbix 5.0.assets/image-20201222163817907.png)
![image-20201222164113684](Zabbix 5.0.assets/image-20201222164113684.png)
![image-20201222164223252](Zabbix 5.0.assets/image-20201222164223252.png)
![image-20201222164334705](Zabbix 5.0.assets/image-20201222164334705.png)
cat <<EOF >/etc/zabbix/zabbix_agent2.d/redis.conf UserParameter=redis.status[*],redis-cli -h \$1 -p \$2 info|grep \$3|grep -v _human|cut -d : -f2 UserParameter=redis.proc,pidof redis-server | wc -l EOF systemctl restart zabbix-agent2
[*] 傳參
cat <<EOF >> /etc/hosts 199.232.4.133 raw.githubusercontent.com EOF https://raw.githubusercontent.com/oscm/zabbix/master/redis/zbx_export_templates.xml
yum install redis -y systemctl start redis systemctl enable redis redis-cli -h 127.0.0.1 -p 6379 info redis-cli -h 127.0.0.1 -p 6379 info|grep role|grep -v _human|cut -d : -f2
zabbix_get -s 10.0.0.7 -k redis.status[127.0.0.1,6379,role]
![image-20201222175051334](Zabbix 5.0.assets/image-20201222175051334.png)
![image-20201222175203560](Zabbix 5.0.assets/image-20201222175203560.png)
![image-20201222175256574](Zabbix 5.0.assets/image-20201222175256574.png)
yum install nginx mariadb-server php72w-fpm php72w-gd php72w-mysqlnd php72w-pecl-redis -y
mysql -e "create user 'ultrax'@'localhost' identified by '123456';grant all privileges on *.* to 'ultrax'@'localhost';"
![image-20201223091252205](Zabbix 5.0.assets/image-20201223091252205.png)
![image-20201223091515208](Zabbix 5.0.assets/image-20201223091515208.png)
redis-cli keys *
硬件層面:
系統層面:CPU,內存,磁盤,網卡,進程,安全
應用層面:nginx、tomcat、mysql、redis、...
業務層面:
主機名 | IP | 配置 |
---|---|---|
matomo | 10.0.0.8 | 1C1G |
yum install nginx php72w-fpm php72w-gd php72w-mysqlnd php72w-xml php72w-mbstring -y
wget https://builds.matomo.org/matomo-latest.zip
unzip matomo-latest.zip mkdir /html mv matomo/* /html chown -R nginx:nginx /html rm -rf /html/vendor/bin/lessc
mysql -e "create user 'matomo'@'10.0.0.8' identified by '123456';grant all privileges on matomo.* to 'matomo'@'10.0.0.8';"
![image-20201223115737735](Zabbix 5.0.assets/image-20201223115737735.png)
![image-20201223112307385](Zabbix 5.0.assets/image-20201223112307385.png)
![image-20201223112935819](Zabbix 5.0.assets/image-20201223112935819.png)
![image-20201223113052855](Zabbix 5.0.assets/image-20201223113052855.png)
![image-20201223113523984](Zabbix 5.0.assets/image-20201223113523984.png)
SNMP協議(simple netwoek manager protocol)簡單網絡管理協議:
支持window,linux,unix,打印機,路由交換,ups,... ...
OID:Object ID 一個監控指標
MIB:全部OID的集合
yum install net-snmp -y
# 修改前 [root@matomo ~]# egrep -v '^$|#' /etc/snmp/snmpd.conf com2sec notConfigUser default public group notConfigGroup v1 notConfigUser group notConfigGroup v2c notConfigUser view systemview included .1.3.6.1.2.1.1 view systemview included .1.3.6.1.2.1.25.1.1 access notConfigGroup "" any noauth exact systemview none none syslocation Unknown (edit /etc/snmp/snmpd.conf) syscontact Root <root@localhost> (configure /etc/snmp/snmp.local.conf) dontLogTCPWrappersConnects yes
sed -i '41c com2sec notConfigUser default oldboy' /etc/snmp/snmpd.conf sed -i '55i view systemview included .1' /etc/snmp/snmpd.conf
# 修改後 [root@matomo ~]# egrep -v '^$|#' /etc/snmp/snmpd.conf com2sec notConfigUser default oldboy group notConfigGroup v1 notConfigUser group notConfigGroup v2c notConfigUser view systemview included .1 view systemview included .1.3.6.1.2.1.1 view systemview included .1.3.6.1.2.1.25.1.1 access notConfigGroup "" any noauth exact systemview none none syslocation Unknown (edit /etc/snmp/snmpd.conf) syscontact Root <root@localhost> (configure /etc/snmp/snmp.local.conf) dontLogTCPWrappersConnects yes
systemctl enable snmpd systemctl start snmpd
netstat -lnup | grep 161
yum install net-snmp-utils -y
snmpwalk -v2c -c oldboy 10.0.0.8 .1.3.6.1.4.1.2021.4.5.0
![image-20201223151407829](Zabbix 5.0.assets/image-20201223151407829.png)
![image-20201223151504564](Zabbix 5.0.assets/image-20201223151504564.png)
![image-20201223152227279](Zabbix 5.0.assets/image-20201223152227279.png)
![image-20201223152256293](Zabbix 5.0.assets/image-20201223152256293.png)
![image-20201223151812154](Zabbix 5.0.assets/image-20201223151812154.png)
![image-20201223151726284](Zabbix 5.0.assets/image-20201223151726284.png)
snmpwalk -v2c -c oldboy 10.0.0.1 .1.3.6.1.2.1.1.1.0
瀏覽器訪問zabbix:http://10.0.0.71/,配置 --> 主機 --> 建立主機,添加模板
win10沒有SNMP服務,能夠安裝agents客戶端。
![image-20201223152701794](Zabbix 5.0.assets/image-20201223152701794.png)
https://cdn.zabbix.com/zabbix/binaries/stable/5.0/5.0.7/zabbix_agent-5.0.7-windows-amd64-openssl.msi
![image-20201223152759089](Zabbix 5.0.assets/image-20201223152759089.png)
rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm sed -i 's#http://repo.zabbix.com/#https://mirrors.tuna.tsinghua.edu.cn/zabbix/#g' /etc/yum.repos.d/zabbix.repo yum install zabbix-agent2 -y sed -i '/^Server=/c Server=10.0.0.71' /etc/zabbix/zabbix_agent2.conf systemctl enable zabbix-agent2 systemctl start zabbix-agent2
![image-20201223155359606](Zabbix 5.0.assets/image-20201223155359606.png)
![image-20201223155705576](Zabbix 5.0.assets/image-20201223155705576.png)
![image-20201223155837922](Zabbix 5.0.assets/image-20201223155837922.png)
![image-20201223155929075](Zabbix 5.0.assets/image-20201223155929075.png)
![image-20201223160125276](Zabbix 5.0.assets/image-20201223160125276.png)
systemctl restart zabbix-server
停用自動發現規則和動做,刪除主機
配置 --> 動做 --> Autoregistration actions --> 建立動做
![image-20201223163228493](Zabbix 5.0.assets/image-20201223163228493.png)
![image-20201223163602093](Zabbix 5.0.assets/image-20201223163602093.png)
![image-20201223163748166](Zabbix 5.0.assets/image-20201223163748166.png)
sed -i '/^Server=/c Server=10.0.0.71' /etc/zabbix/zabbix_agent2.conf sed -i '/^ServerActive=/c ServerActive=10.0.0.71' /etc/zabbix/zabbix_agent2.conf sed -i '/^Hostname=/c Hostname=10.0.0.7' /etc/zabbix/zabbix_agent2.conf sed -i '/^# HostMetadata=/c HostMetadata=web' /etc/zabbix/zabbix_agent2.conf
Hostname=10.0.0.7 # 名稱惟一
systemctl restart zabbix-agent2
![image-20201223164008293](Zabbix 5.0.assets/image-20201223164008293.png)
[root@zabbix-server ~]# tail -3 /etc/ansible/hosts [agent] 10.0.0.11 ansible_ssh_user='root' ansible_ssh_pass='123456' HostMetadata=db 10.0.0.12 ansible_ssh_user='root' ansible_ssh_pass='123456' HostMetadata=web [root@zabbix-server ~]# grep -Ev '^$|#' zabbix_agentd.conf.j2 PidFile=/var/run/zabbix/zabbix_agentd.pid LogFile=/var/log/zabbix/zabbix_agentd.log LogFileSize=0 Server={{ zabbix_server_ip }} ServerActive={{ zabbix_server_ip }} Hostname={{ ansible_default_ipv4.address }} HostMetadata={{ HostMetadata }} Include=/etc/zabbix/zabbix_agentd.d/*.conf [root@zabbix-server ~]# cat zabbix_agent.yml - hosts: agent vars: - zabbix_server_ip: 10.0.0.61 tasks: - name: Install Zabbix Agent yum: name: https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.13-1.el7.x86_64.rpm state: present - name: Configure Zabbix Agent template: src=zabbix_agentd.conf.j2 dest=/etc/zabbix/zabbix_agentd.conf notify: Restart Zabbix Agent - name: Start Zabbix Agent service: name=zabbix-agent state=started enabled=yes handlers: - name: Restart Zabbix Agent service: name=zabbix-agent state=restarted
被動模式:
主動模式:
關閉自動註冊,10.0.0.7主機取消模板連接並清理
連接主動模板:Template OS Linux by Zabbix agent active
![image-20201224095126731](Zabbix 5.0.assets/image-20201224095126731.png)
sed -i '/^Server=/c Server=10.0.0.71' /etc/zabbix/zabbix_agentd.conf sed -i '/^ServerActive=/c ServerActive=10.0.0.71' /etc/zabbix/zabbix_agentd.conf sed -i '/^Hostname=/c Hostname=10.0.0.7' /etc/zabbix/zabbix_agentd.conf
cat <<EOF >> /etc/zabbix/zabbix_agentd.conf DebugLevel=5 EOF
>/var/log/zabbix/zabbix_agentd.log systemctl stop zabbix-agent2 systemctl start zabbix-agent tail -f /var/log/zabbix/zabbix_agentd.log
# Agent啓動 3497:20201224:103005.545 Starting Zabbix Agent [10.0.0.7]. Zabbix 5.0.6 (revision 93895db26b). 3497:20201224:103005.545 **** Enabled features **** 3497:20201224:103005.545 IPv6 support: YES 3497:20201224:103005.545 TLS support: YES 3497:20201224:103005.545 ************************** # Agent請求表單100一組 3502:20201224:103252.935 In send_buffer() host:'10.0.0.71' port:10051 entries:0/100 3502:20201224:103252.935 End of send_buffer():SUCCEED # Agent收到JSON清單,添加到檢查中 # Agent收集值,添加到buffer中 # Agent收集全部rpm包 # Agent收集到全部值後,主動提交給server
主動模式最新數據時間一致:agent收集全部數據打包一次性發給server
![image-20201224101244180](Zabbix 5.0.assets/image-20201224101244180.png)
被動模式最新數據時間不一致:server獲取每一個數據向agent請求一次
![image-20201224101355355](Zabbix 5.0.assets/image-20201224101355355.png)
![image-20201224100324153](Zabbix 5.0.assets/image-20201224100324153.png)
![image-20201224100435804](Zabbix 5.0.assets/image-20201224100435804.png)
![image-20201224100551583](Zabbix 5.0.assets/image-20201224100551583.png)
![image-20201224101459702](Zabbix 5.0.assets/image-20201224101459702.png)
主機 | eth0 | eth1 |
---|---|---|
zabbix-server | 10.0.0.71 | 關閉 |
zabbix-proxy | 10.0.0.8 | 172.16.1.8 |
zabbix-agent | 關閉 | 172.16.1.7 |
ifdown eth1
hostnamectl set-hostname zabbix-proxy systemctl stop zabbix-agent2 systemctl disable zabbix-agent2
yum install zabbix-proxy-mysql mariadb-server -y
systemctl enable mariadb systemctl start mariadb
mysql -e "create database zabbix character set utf8 collate utf8_bin;create user 'zabbix'@'localhost' identified by '123456';grant all privileges on zabbix.* to 'zabbix'@'localhost';"
zcat /usr/share/doc/zabbix-proxy-mysql-*/schema.sql.gz | mysql -uzabbix -p123456 zabbix
# 修改前 [root@zabbix-proxy ~]# egrep -v '^$|#' /etc/zabbix/zabbix_proxy.conf Server=127.0.0.1 Hostname=Zabbix proxy LogFile=/var/log/zabbix/zabbix_proxy.log LogFileSize=0 PidFile=/var/run/zabbix/zabbix_proxy.pid SocketDir=/var/run/zabbix DBName=zabbix_proxy DBUser=zabbix SNMPTrapperFile=/var/log/snmptrap/snmptrap.log Timeout=4 ExternalScripts=/usr/lib/zabbix/externalscripts LogSlowQueries=3000 StatsAllowedIP=127.0.0.1
sed -i '/^Server/c Server=10.0.0.71' /etc/zabbix/zabbix_proxy.conf sed -i '/^Hostname/c Hostname=shenzhen-proxy' /etc/zabbix/zabbix_proxy.conf sed -i '/^DBName/i DBHost=localhost' /etc/zabbix/zabbix_proxy.conf sed -i '/^DBName/c DBName=zabbix' /etc/zabbix/zabbix_proxy.conf sed -i '/^DBName/a DBPassword=123456' /etc/zabbix/zabbix_proxy.conf
# 修改後 [root@zabbix-proxy ~]# egrep -v '^$|#' /etc/zabbix/zabbix_proxy.conf Server=10.0.0.71 Hostname=shenzhen-proxy LogFile=/var/log/zabbix/zabbix_proxy.log LogFileSize=0 PidFile=/var/run/zabbix/zabbix_proxy.pid SocketDir=/var/run/zabbix DBHost=localhost DBName=zabbix DBPassword=123456 DBUser=zabbix SNMPTrapperFile=/var/log/snmptrap/snmptrap.log Timeout=4 ExternalScripts=/usr/lib/zabbix/externalscripts LogSlowQueries=3000 StatsAllowedIP=127.0.0.1
systemctl enable zabbix-proxy systemctl start zabbix-proxy
netstat -tupln |grep 10051
ifdown eth0
sed -i '/^Server=/c Server=172.16.1.8' /etc/zabbix/zabbix_agentd.conf sed -i '/^ServerActive=/c ServerActive=172.16.1.8' /etc/zabbix/zabbix_agentd.conf sed -i '/^Hostname=/c Hostname=172.16.1.7' /etc/zabbix/zabbix_agentd.conf systemctl restart zabbix-agent
![image-20201224110200219](Zabbix 5.0.assets/image-20201224110200219.png)
![image-20201224111106441](Zabbix 5.0.assets/image-20201224111106441.png)
systemctl restart zabbix-server systemctl restart zabbix-proxy
![image-20201224111406973](Zabbix 5.0.assets/image-20201224111406973.png)
![image-20201224112942772](Zabbix 5.0.assets/image-20201224112942772.png)
![image-20201224120825892](Zabbix 5.0.assets/image-20201224120825892.png)
默認:zabbix-server刷新頻率:60s+zabbix-proxy配置發現頻率:3600s
重啓服務加速
systemctl restart zabbix-server systemctl restart zabbix-proxy
grep ConfigFrequency /etc/zabbix/zabbix_proxy.conf
![image-20201224120329104](Zabbix 5.0.assets/image-20201224120329104.png)
systemctl restart zabbix-agent systemctl restart zabbix-server systemctl restart zabbix-proxy
JDK:Java Development Kit = JRE:Java Runtime Environment + 工具
Java語言web服務器:
開源:Tomcat
Oracle:WebLogic
RedH:Jboss
CAUCHO:Resin
cd /opt rz -E yum install jdk-8*.rpm -y
cd /opt wget https://mirror.bit.edu.cn/apache/tomcat/tomcat-9/v9.0.41/bin/apache-tomcat-9.0.41.tar.gz tar xf apache-tomcat-9.0.41.tar.gz ln -s /opt/apache-tomcat-9.0.41 /usr/local/tomcat /usr/local/tomcat/bin/startup.sh /usr/local/tomcat/bin/shutdown.sh
startup.sh其實是調用catalina.sh
/usr/local/tomcat/bin/catalina.sh run
# 加到最上面 vi /usr/local/tomcat/bin/catalina.sh CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote -Djava.rmi.server.hostname=10.0.0.8 -Dcom.sun.management.jmxremote.port=12346 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
-Dcom.sun.management.jmxremote # 開啓jmx功能 -Djava.rmi.server.hostname=10.0.0.8 # 當前主機IP -Dcom.sun.management.jmxremote.port=12346 # 當前主機監聽端口 -Dcom.sun.management.jmxremote.ssl=false # 關閉ssl證書驗證 -Dcom.sun.management.jmxremote.authenticate=false # 關閉認證
/usr/local/tomcat/bin/startup.sh run netstat -tupln|grep 12346
C:\Java\jdk\bin\jconsole.exe
![image-20201224151812535](Zabbix 5.0.assets/image-20201224151812535.png)
![image-20201224151835310](Zabbix 5.0.assets/image-20201224151835310.png)
![image-20201224151730950](Zabbix 5.0.assets/image-20201224151730950.png)
yum install https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-java-gateway-5.0.7-1.el7.x86_64.rpm -y
systemctl enable zabbix-java-gateway systemctl start zabbix-java-gateway netstat -tupln|grep 10052
cat <<EOF >> /etc/zabbix/zabbix_server.conf JavaGateway=10.0.0.71 JavaGatewayPort=10052 StartJavaPollers=3 EOF
systemctl restart zabbix-server
![image-20201224154044727](Zabbix 5.0.assets/image-20201224154044727.png)
![image-20201224153028566](Zabbix 5.0.assets/image-20201224153028566.png)
![image-20201224153711119](Zabbix 5.0.assets/image-20201224153711119.png)
![image-20201224160208498](Zabbix 5.0.assets/image-20201224160208498.png)
鍵值: jmx["java.lang:type=ClassLoading","LoadedClassCount"]
![image-20201224160513193](Zabbix 5.0.assets/image-20201224160513193.png)
![image-20201224160415880](Zabbix 5.0.assets/image-20201224160415880.png)
自動發現:自動添加主機
低層發現(LLD):自動添加監控項,觸發器,圖形等
![Low Level Discovery (LLD)](Zabbix 5.0.assets/autodiscovery_lld-1.png)
![Low Level Discovery (LLD)](Zabbix 5.0.assets/autodiscovery_lld-2.png)
Zabbix支持多種類型的項發現:
Discovery of file systems;
Discovery of network interfaces;
Discovery of CPUs and CPU cores;
Discovery of multiple SNMP OIDs;
Discovery using SQL queries;
Discovery of Windows Services.
Discovery any type of Entities,例如,數據庫服務器上的數據庫。
cat <<EOF >/etc/zabbix/zabbix_agentd.d/net.conf UserParameter=net_mac[*],ifconfig \$1 | grep ether | awk '{print \$\$2}' EOF systemctl restart zabbix-agent zabbix_get -s 127.0.0.1 -k net_mac[eth0]
![image-20201224170721904](Zabbix 5.0.assets/image-20201224170721904.png)
![image-20201224170748781](Zabbix 5.0.assets/image-20201224170748781.png)
![image-20201224170818335](Zabbix 5.0.assets/image-20201224170818335.png)
![image-20201224170937217](Zabbix 5.0.assets/image-20201224170937217.png)
![image-20201224171930931](Zabbix 5.0.assets/image-20201224171930931.png)
![image-20201224172059752](Zabbix 5.0.assets/image-20201224172059752.png)
systemctl restart zabbix-agent systemctl restart zabbix-server
![image-20201224172258315](Zabbix 5.0.assets/image-20201224172258315.png)
![image-20201224172355323](Zabbix 5.0.assets/image-20201224172355323.png)
![image-20201224172404874](Zabbix 5.0.assets/image-20201224172404874.png)
其實是監控項,鍵值是特定的json格式。
[root@zabbix-server ~]# zabbix_get -s 127.0.0.1 -k net.if.discovery [{"{#IFNAME}":"eth0"},{"{#IFNAME}":"eth1"},{"{#IFNAME}":"lo"}]
mkdir -p /data/330{7,8} cat <<EOF > /etc/my3307.cnf [mysqld] datadir=/data/3307/ socket=/data/3307/mysql.sock port=3307 user=mysql symbolic-links=0 [mysqld_safe] log-error=/data/3307/mysqld.log pid-file=/data/3307/mysqld.pid EOF cp /etc/my3307.cnf /etc/my3308.cnf sed -i 's#3307#3308#g' /etc/my3308.cnf mysql_install_db --user=mysql --defaults-file=/etc/my3307.cnf mysql_install_db --user=mysql --defaults-file=/etc/my3308.cnf mysqld_safe --defaults-file=/etc/my3307.cnf & mysqld_safe --defaults-file=/etc/my3308.cnf &
mkdir -p /server/script/ vi /server/script/discover.sh
#!/bin/bash # mysql low-level discovery res=`netstat -lntp|awk -F "[ :\t]+" '/mysqld/{print$5}'` port=($res) printf '[' for key in ${!port[@]} do if [[ "${#port[@]}" -gt 1 && "${key}" -ne "$((${#port[@]}-1))" ]];then printf '{' printf "\"{#MYSQLPORT}\":\"${port[${key}]}\"}," else [[ "${key}" -eq "((${#port[@]}-1))" ]] printf '{' printf "\"{#MYSQLPORT}\":\"${port[${key}]}\"}" fi done printf ']\n'
chmod u+s /usr/bin/netstat cat <<EOF >/etc/zabbix/zabbix_agentd.d/mysql.conf UserParameter=discover.mysql,/bin/bash /server/script/discover.sh UserParameter=mysql_alive[*],/usr/bin/mysqladmin -h 127.0.0.1 -uroot -P \$1 ping 2>/dev/null |wc -l UserParameter=mysql_status[*],/usr/bin/mysql -h 127.0.0.1 -uroot -P \$1 -e "show global status where Variable_Name='\$2';" |tail -1|awk '{print \$\$2}' EOF systemctl restart zabbix-agent
zabbix_get -s 127.0.0.1 -k discover.mysql zabbix_get -s 127.0.0.1 -k mysql_alive[3306] zabbix_get -s 127.0.0.1 -k mysql_status[3306,Uptime] zabbix_get -s 127.0.0.1 -k mysql_status[3306,Queries]
能夠參考源碼包默認監控型腳本:
cat /root/zabbix-5.0.6/conf/zabbix_agentd/userparameter_mysql.conf
![image-20201224192223968](Zabbix 5.0.assets/image-20201224192223968.png)
![image-20201224192551374](Zabbix 5.0.assets/image-20201224192551374.png)
![image-20201224192912965](Zabbix 5.0.assets/image-20201224192912965.png)
![image-20201224193149613](Zabbix 5.0.assets/image-20201224193149613.png)
![image-20201224193217063](Zabbix 5.0.assets/image-20201224193217063.png)
![image-20201224193647042](Zabbix 5.0.assets/image-20201224193647042.png)
![image-20201224194255945](Zabbix 5.0.assets/image-20201224194255945.png)
![image-20201224194347734](Zabbix 5.0.assets/image-20201224194347734.png)
![image-20201224194456920](Zabbix 5.0.assets/image-20201224194456920.png)
![image-20201224194609960](Zabbix 5.0.assets/image-20201224194609960.png)
![image-20201224194839851](Zabbix 5.0.assets/image-20201224194839851.png)
![image-20201224195022861](Zabbix 5.0.assets/image-20201224195022861.png)
![image-20201224195620574](Zabbix 5.0.assets/image-20201224195620574.png)
![image-20201224195703771](Zabbix 5.0.assets/image-20201224195703771.png)
#!/bin/bash curl "http://apis.juhe.cn/simpleWeather/query?city=昌平&key=3dae6629acd8689e9b12f8bb4f7c96fe" >/tmp/tianqi.json q=`cat /tmp/tianqi.json|jq '.result.realtime.info'|grep -c '雨'` if [ $q -eq 1 ];then python weixin.py '@all' '今日天氣提醒' '今天有雨出門記得帶傘!' fi
Zabbix API容許你以編程方式檢索和修改Zabbix的配置,並提供對歷史數據的訪問。
針對zabbix進行二次開發,公司,運維管理平臺(把zabbix集成到本身的運維平臺)
curl -X POST -H "Content-Type: application/json-rpc" -d' { "jsonrpc": "2.0", "method": "user.login", "params": { "user": "Admin", "password": "zabbix" }, "id": 1, "auth": null }' http://10.0.0.71/api_jsonrpc.php
{"jsonrpc":"2.0","result":"5d78b2c8c510482367f6b66306104704","id":1} token='5d78b2c8c510482367f6b66306104704'
curl -X POST -H "Content-Type: application/json-rpc" -d' { "jsonrpc": "2.0", "method": "host.get", "params": { "output": [ "hostid", "host" ], "selectInterfaces": [ "interfaceid", "ip" ] }, "id": 2, "auth": "'$token'" }' http://10.0.0.71/api_jsonrpc.php
# 使用jq格式化json yum install jq -y |jq '.result[].hostid'
status: 1 停用
curl -X POST -H "Content-Type: application/json-rpc" -d' { "jsonrpc": "2.0", "method": "host.update", "params": { "hostid": "10126", "status": 1 }, "id": 1, "auth": "'$token'" }' http://10.0.0.71/api_jsonrpc.php
curl -X POST -H "Content-Type: application/json-rpc" -d' { "jsonrpc": "2.0", "method": "host.create", "params": { "host": "10.0.0.1", "interfaces": [ { "type": 1, "main": 1, "useip": 1, "ip": "10.0.0.1", "dns": "", "port": "10050" } ], "groups": [ { "groupid": "4" } ], "templates": [ { "templateid": "10001" } ] }, "auth": "'$token'", "id": 1 }' http://10.0.0.71/api_jsonrpc.php
#! /bin/bash . /etc/rc.d/init.d/functions token='5d78b2c8c510482367f6b66306104704' # n 主機IP for n in `echo 10.0.0.{2..50}` do curl -s -X POST -H "Content-Type: application/json-rpc" -d' { "jsonrpc": "2.0", "method": "host.create", "params": { "host": "'$n'", "interfaces": [ { "type": 1, "main": 1, "useip": 1, "ip": "127.0.0.1", "dns": "", "port": "10050" } ], "groups": [ { "groupid": "4" } ], "templates": [ { "templateid": "10001" } ] }, "auth": "'$token'", "id": 1 }' http://10.0.0.71/api_jsonrpc.php|jq '.result' &>/dev/null if [ $? -eq 0 ];then action "主機 $n 建立成功" /bin/true else action "主機 $n 建立失敗" /bin/false fi done
#! /bin/bash . /etc/rc.d/init.d/functions token='5d78b2c8c510482367f6b66306104704' # n 主機編號 for n in `echo 10{371..419}` do curl -s -X POST -H "Content-Type: application/json-rpc" -d' { "jsonrpc": "2.0", "method": "host.delete", "params": [ "'$n'" ], "auth": "'$token'", "id": 1 }' http://10.0.0.71/api_jsonrpc.php|jq '.result' &>/dev/null if [ $? -eq 0 ];then action "主機 $n 刪除成功" /bin/true else action "主機 $n 刪除失敗" /bin/false fi done
數據庫優化:zabbix是一個寫多讀少的業務,優化數據庫的寫入性能。
功能上優化:
進程優化:適當增長進程數量
緩存優化:適當增長緩存大小
zabbix不是核心業務,通常不作高可用,但也會有要求,銀行等。
zabbix高可用架構:
zabbix-web:Keepalive,VIP
mysql:主主同步,PXC(Percona XtraDB Cluster)高可用
zabbix-server:Keepalive,VIP
主機名 | IP | 服務 |
---|---|---|
zabbix-server01 | 10.0.0.71 | zabbix-web:LNP,zabbix-server,keepalived;zabbix-agent |
zabbix-server02 | 10.0.0.72 | zabbix-web:LNP,zabbix-server,keepalived;zabbix-agent |
zabbix-agent | 10.0.0.8 | mysql;zabbix-agent |
systemctl stop zabbix-server zabbix-java-gateway grafana-server php-fpm nginx zabbix-agent systemctl disable mariadb zabbix-java-gateway grafana-server redis mysqldump -B zabbix > zabbix.sql scp zabbix.sql root@10.0.0.8:/root/ systemctl stop mariadb
sed -i '/^DBHost/c DBHost=10.0.0.8' /etc/zabbix/zabbix_server.conf
sed -i "5c \$DB['SERVER'] = '10.0.0.8';" /html/conf/zabbix.conf.php
yum install keepalived -y
cat <<EOF >/etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id LVS_DEVEL 71 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.70 } notify_master /opt/to_master.sh } EOF
cat <<EOF > /opt/to_master.sh #!/bin/bash sshpass -p 1 ssh -o StrictHostKeyChecking=no root@10.0.0.72 "systemctl stop zabbix-server" systemctl start zabbix-server EOF chmod +x /opt/to_master.sh yum install sshpass -y
SSH端口不默認:
echo Port 12345 > ~/.ssh/config
systemctl enable keepalived
echo SourceIP=10.0.0.70 >> /etc/zabbix/zabbix_server.conf
sed -i '/^Server=/c Server=10.0.0.70' /etc/zabbix/zabbix_agentd.conf sed -i '/^ServerActive=/c ServerActive=10.0.0.70' /etc/zabbix/zabbix_agentd.conf
systemctl stop zabbix-proxy php-fpm nginx snmpd systemctl disable zabbix-proxy php-fpm nginx snmpd mysql -e "DROP DATABASE zabbix;" mysql < zabbix.sql mysql -e "create user 'zabbix'@'10.0.0.%' identified by '123456';grant all privileges on zabbix.* to 'zabbix'@'10.0.0.%';"
sed -i '/^Server=/c Server=10.0.0.70' /etc/zabbix/zabbix_agent2.conf sed -i '/^ServerActive=/c ServerActive=10.0.0.70' /etc/zabbix/zabbix_agent2.conf
systemctl start zabbix-agent2
hostnamectl set-hostname zabbix-server02 sed -i 's#200#72#g' /etc/sysconfig/network-scripts/ifcfg-eth[01]
cat <<EOF >/etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id LVS_DEVEL 72 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.70 } notify_master /opt/to_master.sh } EOF
cat <<EOF > /opt/to_master.sh #!/bin/bash sshpass -p 1 ssh -o StrictHostKeyChecking=no root@10.0.0.71 "systemctl stop zabbix-server" systemctl start zabbix-server EOF chmod +x /opt/to_master.sh
systemctl restart keepalived