zabbix介紹
三、程序構成
(1)zabbix_server:服務端守護進程
(2)zabbix_agented:agent端守護進程
(3)zabbix_proxy:代理服務器(可選,分布式才用到)
(4)zabbix_get:命令行工具,手動測試數據采集
(5)zabbix_sender:命令行工具,運行於agent端,手動向server端發送數據
(6)zabbixjavagateway:java網關

監控范疇:
實驗環境准備:
Centos7.0 192.168.200.113 | server.zabbix.com |
Centos7.0 192.168.200.114 | agent.zabbix.com |
Windows 192.168.200.2 | windows10client |
以下實驗操作主要注意主機名
server服務端:
環境配置:
[root@localhost ~]# hostname server.zabbix.com
[root@localhost ~]# bash
[root@server ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.200.113 server.zabbix.com 192.168.200.114 agent.zabbix.com 192.168.200.2 windows10
[root@server ~]# vim /etc/hostname
server.zabbix.com
[root@server ~]# scp /etc/hosts 192.168.200.114:/etc/
[root@server ~]# systemctl stop firewalld
[root@server ~]# iptables -F
[root@server ~]# setenforce 0
agent客戶端:
環境配置:
[root@client1 ~]# hostname agent.zabbix.com
[root@client1 ~]# bash
[root@agent ~]# vim /etc/sysconfig/network
HOSTNAEM=agent.zabbix.com
[root@agent ~]# systemctl stop firewalld
[root@agent ~]# iptables -F
[root@agent ~]# setenforce 0
server端:
配置阿里雲yum源:
[root@server ~]# cd /etc/yum.repos.d/
[root@server yum.repos.d]# wget http://mirrors.aliyun.com/repo/Centos-7.repo
[root@server yum.repos.d]# ls
a Centos-7.repo CentOS-Media.repo epel.repo epel-testing.repo local.repo
[root@server yum.repos.d]# yum install http mariadb-server mariadb mariadb-devel php php-mbstring php-mysql php-bcmath php-gd php-xmlrpc php-ldap php-xml libevent libevent-devel net-snmp net-snmp-devel libxml2 libxml2-devel ntpdate gcc gcc-c++ make curl-devel -y
[root@server yum.repos.d]# ntpdate sla.time.edu.cn #同步清華大學時間服務器
啟動LAMP相應服務
[root@server yum.repos.d]# cd
[root@server ~]# systemctl restart mariadb
[root@server ~]# systemctl enable mariadb
[root@server ~]# systemctl restart httpd
[root@server ~]# systemctl enable httpd
[root@server ~]# mysqladmin -u root password 123123 #為mysql的root用戶設置登錄密碼
准備JAVA初始環境
[root@server ~]# java -version #查看java版本
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)
[root@server ~]# javac -version #監控Tomcat時需要javac
bash: javac: 未找到命令...
相似命令是: 'java'
[root@server ~]# rm -rf /usr/bin/java
[root@server ~]# rz
[root@server ~]# ls
anaconda-ks.cfg epel-release-latest-7.noarch.rpm jdk-8u191-linux-x64.tar.gz 模板 圖片 下載 桌面
Centos-7.repo initial-setup-ks.cfg 公共 視頻 文檔 音樂
[root@server ~]# tar xf jdk-8u191-linux-x64.tar.gz
[root@server ~]# mv jdk1.8.0_191/ /usr/local/java
[root@server ~]# vim /etc/profile
在末尾添加如下兩條:
JAVA_HOME=/usr/local/java PATH=$PATH:$JAVA_HOME/bin
[root@server ~]# source /etc/profile
[root@server ~]# java -version
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
[root@server ~]# javac -version
javac 1.8.0_191
源碼安裝zabbix:
[root@server ~]# rz
[root@server ~]# ls
jdk-8u191-linux-x64.tar.gz
zabbix-3.4.11.tar.gz
[root@server ~]# tar xf zabbix-3.4.11.tar.gz -C /usr/src
[root@server ~]# cd /usr/src/zabbix-3.4.11/
[root@server zabbix-3.4.11]# ./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --with-mysql --enable-ipv6 --with-net-snmp --with-libcurl
--with-libxml2 --enable-java && make -j2 && make install
mariadb數據庫授權
[root@server zabbix-3.4.11]# cd
[root@server ~]# mysql -u root -p123123
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)
MariaDB [(none)]> grant all on zabbix.* to zabbix@'192.168.200.113' identified by 'zabbix'; #此步驟是為zabbix.server啟動授權
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges; #刷新授權表
Query OK, 0 rows affected (0.00 sec)
導入zabbix需要的數據庫
[root@server ~]# cd /usr/src/zabbix-3.4.11/
[root@server zabbix-3.4.11]# mysql -uzabbix -pzabbix zabbix < database/mysql/schema.sql
[root@server zabbix-3.4.11]# mysql -uzabbix -pzabbix zabbix < database/mysql/images.sql
[root@server zabbix-3.4.11]# mysql -uzabbix -pzabbix zabbix < database/mysql/data.sql
登錄查看
[root@server zabbix-3.4.11]# mysql -u root -p123123
MariaDB [(none)]> use zabbix;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [zabbix]> show tables;
創建zabbix的程序用戶並授權防止權限報錯
[root@server zabbix-3.4.11]# cd
[root@server ~]# useradd -M -s /sbin/nologin zabbix
[root@server ~]# cd /usr/local
[root@server local]# chown -R zabbix:zabbix zabbix/
[root@server local]# cd zabbix/
[root@server zabbix]# pwd
/usr/local/zabbix
[root@server zabbix]# mkdir logs/
[root@server zabbix]# chown zabbix:zabbix logs/
[root@server zabbix]# cp -p etc/zabbix_server.conf{,.bak}
[root@server zabbix]# cp -p etc/zabbix_agentd.conf{,.bak}
修改zabbix配置文件
[root@server zabbix]# cd /usr/local/zabbix/etc/
[root@server etc]# vim zabbix_server.conf
刪除全部添加如下代碼:
LogFile=/usr/local/zabbix/logs/zabbix_server.log #指定zabbix日志位置 PidFile=/tmp/zabbix_server.pid #指定zabbix.pid文件位置 DBHost=192.168.200.113 #指定數據庫主機名 DBName=zabbix #指定數據庫名為zabbix DBUser=zabbix #指定用戶為zabbix DBPassword=zabbix #指定密碼為zabbix DBSocket=/var/lib/mysql.sock #指定mariadb數據庫的sock文件 Include=/usr/local/zabbix/etc/zabbix_server.conf.d/*.conf
[root@server etc]# vim zabbix_agentd.conf
刪除全部添加如下代碼:
PidFile=/tmp/zabbix_agentd.pid Server=127.0.0.1,192.168.200.113 ServerActive=192.168.200.113 Hostname=server.zabbix.com LogFile=/usr/local/zabbix/logs/zabbix_agentd.log Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/*.conf
[root@server etc]# ln -s /usr/local/zabbix/sbin/* /usr/local/bin/
啟動監控程序
[root@server etc]# zabbix_server
[root@server etc]# zabbix_agentd
配置php與apache
[root@server etc]# cd
[root@server ~]# mv /usr/src/zabbix-3.4.11/frontends/php/ /var/www/html/zabbix
[root@server ~]# chown -R apache:apache /var/www/html/zabbix/
[root@server ~]# vim /etc/php.ini
第878行 date.timezone=Asia/Shanghai #時區 第384行 max_execution_time=300 #單位秒,通過post,get以及put方式接受數據時間限制 第394行 max_input_time=300 #php程序上傳文件解析數據時間的限制 第672行 post_max_size=32M #post方式請求數據允許的最大大小 第405行 memory_limit=128M #設置程序的內存限制 第854行 extension=bcmath.so #加載zabbix程序需要加載的擴張模塊bcmath.so手動添加
[root@server ~]# systemctl restart httpd #啟動httpd服務
檢測服務端口信息
[root@server ~]# netstat -anpt | grep :10050
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 35292/zabbix_agentd
tcp6 0 0 :::10050 :::* LISTEN 35292/zabbix_agentd
[root@server ~]# netstat -anpt | grep :10051
tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 35318/zabbix_server
tcp6 0 0 :::10051 :::* LISTEN 35318/zabbix_server
[root@server ~]# netstat -anpt | grep :80
tcp6 0 0 :::80 :::* LISTEN 35550/httpd
WEB安裝zabbix
訪問http://192.168.200.113/zabbix進入頁面安裝
保障所有選項都是ok狀態
密碼為zabbix
名字隨便起這里起zabbix server
用戶名admin 密碼zabbix
登錄成功:
zabbix頁面優化中文漢化:
按F5刷新頁面完成漢化
漢化后問題:頁面出現中文亂碼
解決方法:更改zabbix默認的字體為電腦自帶字體
[root@server ~]# cd /var/www/html/zabbix/fonts/ #查看zabbix自帶的默認字體
[root@server fonts]# ls
DejaVuSans.ttf
[root@server fonts]#
電腦自帶字體路徑:C:\Windows\Fonts,選擇自己喜歡的字體復制到桌面並導入到xshell中
[root@server fonts]# ls
DejaVuSans.ttf simsun.ttc
[root@server fonts]# mv simsun.ttc DejaVuSans.ttf
按F5再次刷新頁面
=================================================================================
zabbix監控主機:添加自身為被監控機
監控項就是你要監控的服務或者硬件
圖像是監控的服務或者硬件當前的狀態
觸發器就是用來當某個服務或者硬件出現了問題會自動報警,后面可以利用微信,郵箱類進行報警通知
========================================================================================================
zabbix監控windows主機:
1:將這個壓縮包里面的東西拷貝到C盤的根目錄Zabbix下:
2:用記事本打開這個conf文件並修改如下三處內容:修改完成后保存
3:windows中啟動zabbix:
zabbix中添加windows實時監控:
===============================================================================================================
Zabbix監控硬件設備:如路由器等
GNS3是一款模擬的虛擬軟件
==========================================================================================================
Zabbix監控linux主機
Linux-client操作(agent)
[root@agent ~]# hostname
agent.zabbix.com
[root@agent ~]# cat /etc/hostname
agent.zabbix.com
[root@agent ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.200.113 server.zabbix.com 192.168.200.114 agent.zabbix.com 192.168.200.2 windows10
[root@agent ~]# ping server.zabbix.com -c 4
[root@agent ~]# systemctl stop firewalld
[root@agent ~]# iptables -F
[root@agent ~]# setenforce 0
源碼安裝zabbix
[root@agent ~]# rz
[root@agent ~]# ls
anaconda-ks.cfg zabbix-3.4.11.tar.gz 公共 模板 視頻 圖片 文檔 下載 音樂 桌面
[root@agent ~]# yum install gcc gcc-c++ libxml2-devel libcurl-devel pcre-devel ntpdate -y
[root@agent ~]# ntpdate sla.time.edu.cn #同步清華大學服務器時間
[root@agent ~]# tar xf zabbix-3.4.11.tar.gz -C /usr/src
[root@agent ~]# cd /usr/src/zabbix-3.4.11/
[root@agent zabbix-3.4.11]# ./configure --prefix=/usr/local/zabbix --enable-agent --with-net-snmp --with-libcurl --with-libxml2 && make -j2 && make install
--enable-agent 啟用agent
[root@agent zabbix-3.4.11]# cp misc/init.d/tru64/zabbix_agentd /etc/init.d/ #復制啟動腳本
[root@agent zabbix-3.4.11]# vim /etc/init.d/zabbix_agentd
24行修改路徑為:DAEMON=/usr/local/zabbix/sbin/zabbix_agentd
[root@agent zabbix-3.4.11]# chmod +x /etc/init.d/zabbix_agentd
[root@agent zabbix-3.4.11]# cd
[root@agent ~]# useradd -M -s /sbin/nologin zabbix
[root@agent ~]# chown -R zabbix:zabbix /usr/local/zabbix/
[root@agent ~]# cd /usr/local/zabbix/
[root@agent zabbix]# mkdir logs/
[root@agent zabbix]# chown -R zabbix:zabbix logs/
[root@agent zabbix]# cp /usr/local/zabbix/etc/zabbix_agentd.conf{{,.bak}
修改主配置文件
[root@agent zabbix]# vim /usr/local/zabbix/etc/zabbix_agentd.conf
PidFile=/tmp/zabbix_agentd.pid Server=192.168.200.113 ServerActive=192.168.200.113 Hostname=agent.zabbix.com LogFile=/usr/local/zabbix/logs/zabbix_agentd.log Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/*.conf
################################################################以上是配置路徑類以下是自定義監控項 UnsafeUserParameters=1 UserParameter=mysql.version,mysql -V UserParameter=mysql.status[*],/usr/local/zabbix/etc/chk_mysql.sh.$1 UserParameter=mysql.ping,mysqladmin -uroot -p123123 -P3306 -h192.168.200.114 ping | grep -c alive
[root@agent zabbix]# service zabbix_agentd start #啟動服務
Zabbix agent started.
[root@agent zabbix]# netstat -anpt | grep 10050 #查看端口
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 18789/zabbix_agentd
Zabbix添加linux主機:操作跟前面添加主機一樣,是linux系統就linux組是windows就是windows組
==============================================================================================================
Zabbix-server監控mysql及httpd服務
zabbix監控mysql數據庫,為server.zabbix.com添加服務模塊
創建Mysql服務圖形
server.zabbix.com服務器操作
既然要監控服務就要能夠采集到數據,而采集也要有采集的方式,
[root@server ~]# cd /usr/local/zabbix/etc/
[root@server etc]# vim zabbix_agentd.conf
PidFile=/tmp/zabbix_agentd.pid
Server=127.0.0.1,192.168.200.113 ServerActive=192.168.200.113 Hostname=server.zabbix.com LogFile=/usr/local/zabbix/logs/zabbix_agentd.log Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/*.conf ##################################################################以下是自定義的內容
#允許所有字符的參數傳遞給用戶定義的參數
UnsafeUserParameters=1 #自定義監控項開關,1表示開啟,0表示關閉
#定義鍵mysql.version,以及值-----
UserParameter=mysql.version,mysql -V #紅色部分可以是命令也可以是一條腳本,如差內存大小就直接編寫free查內存的shell腳本
#定義鍵值mysql.ping,指定chk_mysql.sh的腳本使用此腳本檢查Mysql的運行狀態
UserParameter=mysql.status[*],/usr/local/zabbix/etc/chk_mysql.sh $1
#只用mysqladmin命令指定agent端連接用戶密碼ip地址,
UserParameter=mysql.ping,mysqladmin -uroot -p123123 -P3306 -h 192.168.200.113 ping | grep -c alive
編輯chk_mysql.sh的腳本
[root@server etc]# pwd
/usr/local/zabbix/etc
[root@server etc]# vim /usr/local/zabbix/etc/chk_mysql.sh
#!/bin/bash
#FileName: check_mysql.sh
# 用戶名
MYSQL_USER='root'
# 密碼
MYSQL_PWD='123123'
# 主機地址/IP
MYSQL_HOST='192.168.200.113'
# 端口
MYSQL_PORT='3306'
# 數據連接
MYSQL_CONN="/usr/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}"
# 參數是否正確
if [ $# -ne "1" ];then
echo "arg error!"
fi
# 獲取數據
case $1 in
Uptime)
result=`${MYSQL_CONN} status|cut -f2 -d":"|cut -f1 -d"T"`
echo $result
;;
Com_update)
result=`${MYSQL_CONN} extended-status |grep -w "Com_update"|cut -d"|" -f3`
echo $result
;;
Slow_queries)
result=`${MYSQL_CONN} status |cut -f5 -d":"|cut -f1 -d"O"`
echo $result
;;
Com_select)
result=`${MYSQL_CONN} extended-status |grep -w "Com_select"|cut -d"|" -f3`
echo $result
;;
Com_rollback)
result=`${MYSQL_CONN} extended-status |grep -w "Com_rollback"|cut -d"|" -f3`
echo $result
;;
Questions)
result=`${MYSQL_CONN} status|cut -f4 -d":"|cut -f1 -d"S"`
echo $result
;;
Com_insert)
result=`${MYSQL_CONN} extended-status |grep -w "Com_insert"|cut -d"|" -f3`
echo $result
;;
Com_delete)
result=`${MYSQL_CONN} extended-status |grep -w "Com_delete"|cut -d"|" -f3`
echo $result
;;
Com_commit)
result=`${MYSQL_CONN} extended-status |grep -w "Com_commit"|cut -d"|" -f3`
echo $result
;;
Bytes_sent)
result=`${MYSQL_CONN} extended-status |grep -w "Bytes_sent" |cut -d"|" -f3`
echo $result
;;
Bytes_received)
result=`${MYSQL_CONN} extended-status |grep -w "Bytes_received" |cut -d"|" -f3`
echo $result
;;
Com_begin)
result=`${MYSQL_CONN} extended-status |grep -w "Com_begin"|cut -d"|" -f3`
echo $result
;;
*)
echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin)"
;;
esac
[root@server etc]# chmod 777 chk_mysql.sh
[root@server etc]# mysql -u root -p123123
MariaDB [(none)]> grant all on *.* to 'root'@'server.zabbix.com' identified by '123123';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit
Bye
每次先殺進程再起服務保證配置的更新
[root@server etc]# killall -9 zabbix_agentd
[root@server etc]# killall -9 zabbix_server
[root@server etc]# /usr/local/zabbix/sbin/zabbix_agentd
[root@server etc]# /usr/local/zabbix/sbin/zabbix_server
[root@server etc]# netstat -anpt | grep 10050
[root@server etc]# netstat -anpt | grep 10051
server.zabbix.com的測試
[root@server etc]# zabbix_get -s 192.168.200.113 -k mysql.ping
1 #出現數字表示抓取成功
[root@server etc]# zabbix_get -s 192.168.200.113 -k mysql.status[Com_update]
3411 #抓取update的值
查看zabbix效果
=====================================================================================================
Zabbix監控apache
[root@server etc]# vim /usr/local/zabbix/etc/chk_httpd.sh
#!/bin/bash netstat -anpt | grep 80 &>/dev/null if [ $? -eq '0' ];then echo "1" else echo "0" fi
[root@server etc]# chmod 777 chk_httpd.sh
[root@server etc]# ./chk_httpd.sh #運行httpd腳本查看113主機的httpd服務是否活着
1
[root@server etc]# vim zabbix_agentd.conf
PidFile=/tmp/zabbix_agentd.pid Server=127.0.0.1,192.168.200.113 ServerActive=192.168.200.113 Hostname=server.zabbix.com LogFile=/usr/local/zabbix/logs/zabbix_agentd.log Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/*.conf ##################################################################以下是自定義的內容 UnsafeUserParameters=1 UserParameter=mysql.version,mysql -V UserParameter=mysql.status[*],/usr/local/zabbix/etc/chk_mysql.sh $1 UserParameter=mysql.ping,mysqladmin -uroot -p123123 -P3306 -h 192.168.200.113 ping | grep -c alive UserParameter=httpd.status[*],/usr/local/zabbix/etc/chk_httpd.sh $1 #將httpd的腳本放入自定義下后期有腳本都可以這樣放進來
[root@server etc]# killall -9 zabbix_agentd
[root@server etc]# killall -9 zabbix_server
[root@server etc]# zabbix_agentd
[root@server etc]# zabbix_server
[root@server etc]# zabbix_get -s 192.168.200.113 -k httpd.status #獲取httpd.status監控項
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.) #紅色部分是因為zabbix沒法調用netstat
1
解決zabbbix沒法調用netstat的解決方法
[root@server etc]# which netstat
/usr/bin/netstat
[root@server etc]# chmod u+s /usr/bin/netstat
[root@server etc]# zabbix_get -s 192.168.200.113 -k httpd.status #再次獲取httpd.status監控項
1
======================================================================================-==========================
監控練習:監控根的只用率並優化圖形介質
[root@server etc]# df | awk -F '[ %]+' 'NR==2{print $(NF-1)}'
32
[root@server etc]# vim zabbix_agentd.conf
......
........
UserParameter=df.used,df | awk -F '[ %]+' 'NR==2{print $(NF-1)}'
[root@server etc]# killall -9 zabbix_agentd
[root@server etc]# zabbix_agentd
[root@server etc]# zabbix_get -s 192.168.200.113 -k df.used #獲取監控項的值
32
=====================================================================================================
Zabbix監控Nginx
[root@agent ~]# systemctl stop firewalld
[root@agent ~]# iptables -F
[root@agent ~]# setenforce 0
[root@agent ~]# ls
anaconda-ks.cfg zabbix-3.4.11.tar.gz 公共 模板 視頻 圖片 文檔 下載 音樂 桌面
[root@agent ~]# rz
[root@agent ~]# rpm -ivh nginx-1.15.9-1.x86_64.rpm -C /usr/src
rpm: -C: 未知的選項
[root@agent ~]# rpm -ivh nginx-1.15.9-1.x86_64.rpm
准備中... ################################# [100%]
正在升級/安裝...
1:nginx-1.15.9-1 ################################# [100%]
[root@agent conf]# nginx -V
nginx version: nginx/1.15.9
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module
--with-http_gzip_static_module
[root@agent ~]# cd /usr/local/nginx/conf/
[root@agent conf]# vim nginx.conf
location /status {
stub_status on;
access_log off;
}
[root@agent conf]# killall -HUP nginx
[root@agent conf]# netstat -anpt | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7915/nginx: master
[root@agent zabbix]# cat nginx_chk.sh
#!/bin/bash ########################### #zabbix monitoring script # # nginx: # - anything available via nginx stub-status module # ################################## # Contact: # vincent.viallet@gmail.com # Zabbix requested parameter ZBX_REQ_DATA="$1" ZBX_REQ_DATA_URL="$2" # Nginx defaults NGINX_STATUS_DEFAULT_URL="192.168.200.114/status" #(這里寫網站的域名) WGET_BIN="/usr/bin/wget" # # Error handling: # - need to be displayable in Zabbix (avoid NOT_SUPPORTED) # - items need to be of type "float" (allow negative + float) # ERROR_NO_ACCESS_FILE="-0.9900" ERROR_NO_ACCESS="-0.9901" ERROR_WRONG_PARAM="-0.9902" ERROR_DATA="-0.9903" # either can not connect / bad host / bad port # Handle host and port if non-default if [ ! -z "$ZBX_REQ_DATA_URL" ]; then URL="$ZBX_REQ_DATA_URL" else URL="$NGINX_STATUS_DEFAULT_URL" fi # save the nginx stats in a variable for future parsing NGINX_STATS=$($WGET_BIN -q $URL -O - 2> /dev/null) # error during retrieve if [ $? -ne 0 -o -z "$NGINX_STATS" ]; then echo $ERROR_DATA exit 1 fi # # Extract data from nginx stats # case $ZBX_REQ_DATA in active_connections) echo "$NGINX_STATS" | head -1 | cut -f3 -d' ';; accepted_connections) echo "$NGINX_STATS" | grep -Ev '[a-zA-Z]' | cut -f2 -d' ';; handled_connections) echo "$NGINX_STATS" | grep -Ev '[a-zA-Z]' | cut -f3 -d' ';; handled_requests) echo "$NGINX_STATS" | grep -Ev '[a-zA-Z]' | cut -f4 -d' ';; reading) echo "$NGINX_STATS" | tail -1 | cut -f2 -d' ';; writing) echo "$NGINX_STATS" | tail -1 | cut -f4 -d' ';; waiting) echo "$NGINX_STATS" | tail -1 | cut -f6 -d' ';; *) echo $ERROR_WRONG_PARAM; exit 1;; esac exit 0
[root@agent zabbix]# chmod +x nginx_chk.sh
[root@agent etc]# cat zabbix_agentd.conf
UserParameter=nginx[*],/usr/local/zabbix/nginx_chk.sh "$1"
[root@agent etc]# /usr/local/zabbix/sbin/zabbix_agentd
[root@agent etc]# netstat -anpt | grep 10050
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 9742/zabbix_agentd
server端抓取數據
[root@server etc]# /usr/local/zabbix/bin/zabbix_get -s 192.168.200.114 -p 10050 -k "nginx[reading]"
0
Web Zabbix端添加主機
啟用前自動發現的規則及動作
=======================================================================================================
Zabbix監控FTP服務
[root@agent ~]# yum install vsftpd -y
[root@agent ~]# systemctl start vsftpd
[root@agent ~]# systemctl enable vsftpd
======================================================================================================
郵件報警:登錄www.163.com網易郵箱
服務端server操作:
[root@server ~]# yum install mailx dos2unix -y
[root@server ~]# mailx -V
12.5 7/5/10
[root@server ~]# cp /etc/mail.rc /etc/mail.rc.bak
[root@server ~]# vim /etc/mail.rc
在末尾添加如下命令
set from=l18370248736@163.com smtp=smtp.163.com #從哪兒發的,smtp是服務器
set smtp-auth-user=l18370248736@163.com smtp-auth-password="lcx123123" #授權的用戶名 ,授權的授權碼是lcx123123
set smtp-auth=login #登錄
發送測試內容:
[root@server ~]# echo "test mail 111" | mail -s "tm" l18370248736@163.com #前面是內容,后面是郵箱地址
編輯zabbix_server.conf引用腳本
[root@server ~]# vim /usr/local/zabbix/etc/zabbix_server.conf
LogFile=/usr/local/zabbix/logs/zabbix_server.log PidFile=/tmp/zabbix_server.pid DBHost=192.168.200.113 DBName=zabbix DBUser=zabbix DBPassword=zabbix DBSocket=/var/lib/mysql.sock Include=/usr/local/zabbix/etc/zabbix_server.conf.d/*.conf AlertScriptsPath=/usr/local/zabbix/share/zabbix/alertscripts #這條是郵箱報警加的,意思是電腦自動從這個路徑尋找腳本
[root@server ~]# cd /usr/local/zabbix/share/zabbix/alertscripts/
[root@server alertscripts]# vim mail.sh
#!/bin/bash #export.UTF-8 bt=$2 sjr=$1 FILE=/tmp/mailtmp.txt echo "$3" >$FILE #$3是郵件內容,將內容重定向到/tmp/mailtmp.txt中 /usr/bin/dos2unix -k $FILE #使用/usr/bin/dos2unix將郵件進行轉換,因為郵件可能有中文 /bin/mail -s "$bt" "$sjr" <$FILE #$bt是標題,$sjr是收件人,最后重定向輸入內容
[root@server alertscripts]# chmod 777 mail.sh
[root@server alertscripts]# chown zabbix:zabbix mail.sh
[root@server alertscripts]# touch /tmp/mailtmp.txt
[root@server alertscripts]# chmod 777 /tmp/mailtmp.txt
[root@server alertscripts]# chown zabbix:zabbix /tmp/mailtmp.txt
腳本測試:
[root@server alertscripts]# ./mail.sh l18370248736@163.com "test mail2" "test mail from server.zabbix.com test2"
dos2unix: converting file /tmp/mailtmp.txt to Unix format ...
zabbix監控郵箱報警思路:
監控--創建報警媒介類型--更新用戶的報警媒介類型--創建動作(根據觸發器觸發)--驗證
操作、恢復操作、確認操作中的信息內容為:
故障:{TRIGGER.STATUS};服務器:{HOSTNAME1};發生:{TRIGGER.NAME}故障!
告警主機:{HOSTNAME1}
告警時間:{EVENT.DATE}{EVENT.TIME}
告警等級:{TRIGGRT.SEVERITY}
告警信息:{TRIGGER.NAME}
告警項目:{TRIGGER.KEY1}
問題詳情:{ITEM.NAME}:{ITEM.VALUE}
當前狀態:{TRIGGER.STATUS}:{ITEM.VALUE1}
時間ID:{EVENT.ID}
后面的恢復操作、和確認操作與上面操作一致都執行這四個步驟,最后回到動作添加
模擬郵件報警:關閉FTP服務
[root@server ~]# systemctl stop vsftpd.service
FTP故障修復:
[root@server ~]# systemctl start vsftpd.service
========================================================================================
Zabbix企業微信報警:企業微信域名地址:https://qy.weixin.qq.com/
並且下載電腦端企業微信:
Server服務端配置:編寫微信報警配置腳本
[root@server ~]# cd /usr/local/zabbix/share/zabbix/alertscripts/
[root@server alertscripts]# ls
mail.sh
[root@server alertscripts]# vim weixin.sh
#!/bin/bash
corpid='ww243019ef0f34fd80' #企業ID號
corpsecret='FHpT2sxv93s6Ukraf3sPHY_7q0Of4eetPXvoP3N9CcE' #企業secret號
agentid='1000002' user="$1" title="$2" content="$3" token=$(curl -s https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpid\&corpsecret=$corpsecret |sed -nr 's#.*token":"(.*)","e.*#\1#p') post=$(curl -s https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$token -d "{ \ \"touser\": \"$user\", \ \"msgtype\": \"text\", \ \"agentid\": $agentid, \ \"text\": { \ \"content\": \"$title ------------------------------------------------------------------------------- $content\" \ }, \ \"safe\":\"0\" \ }") if [ $post = '{"errcode":0,"errmsg":"ok","invaliduser":""}' ] then echo "提交成功!" else
[root@server alertscripts]# chmod +x weixin.sh
[root@server alertscripts]# chown -R zabbix:zabbix weixin.sh
[root@server alertscripts]# killall -9 zabbix_server
[root@server alertscripts]# /usr/local/zabbix/sbin/zabbix_server
后面的操作,恢復操作,確認操作與上面的郵件報警動作一致,添加完成后,如下所示
模擬微信報警故障:關閉FTP服務
[root@server ~]# systemctl stop vsftpd.service