部署zabbix 4.0 + grafana


不完整,僅供參考

Zabbix+grafana監控部署

基本環境

系統: CentOS Linux release 7.3.1611

Zabbix—server: 

Zabbix_agent:  

Nginx版本號: 1.62

Mysql版本號: 5.7.27

Zabbix版本號:   4.0

Grafana版本號: 6.0

1:基礎環境配置(關閉防火牆和selinux)

systemctl stop firewalld

systemctl disable firewalld

setenforce 0

sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

 

2:安裝zabbix

  2.1部署安裝nginx

yum -y install gcc pcre-devel openssl-devel wget

wget http://nginx.org/download/nginx-1.6.2.tar.gz

tar -xf nginx-1.6.2.tar.gz

cd nginx-1.6.2/

./configure --user=nginx --group=nginx --prefix=/opt/nginx/  --with-file-aio  --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module  --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module  --with-mail --with-mail_ssl_module     --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --with-http_stub_status_module

make  && make install

useradd -s /sbin/nologin nginx

ln -s /opt/nginx/sbin/nginx  /bin

nginx

 

vim /opt/nginx/conf/nginx.conf

  3 worker_processes  auto;

  13     worker_connections  30000;

  45             index index.php  index.html index.htm;

65         location ~ \.php$ {

 66             root           html;

 67             fastcgi_pass   127.0.0.1:9000;

 68             fastcgi_index  index.php;

 69         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

 70             include        fastcgi.conf;

 71         }

 

nginx -s reload

   2.2部署安裝mysql

wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm

yum -y install mysql57-community-release-el7-10.noarch.rpm

yum -y install mysql-community-server

systemctl  start  mysqld;systemctl  enable mysqld

grep password /var/log/mysqld.log

mysql  -uroot  -p''   //根據初始密碼登錄數據庫

mysql> set global validate_password_policy=0;

mysql> set global validate_password_length=5;

mysql> alter user root@localhost identified by '密碼';

mysql> create database zabbix default character set utf8;

mysql>  create user 'zabbix'@'localhost' identified with mysql_native_password by '密碼';

mysql> create user 'zabbix'@'127.0.0.1' identified with mysql_native_password by '密碼';

mysql> create user 'zabbix'@'%' identified with mysql_native_password by '密碼';

mysql>  grant all privileges on zabbix.* to "zabbix"@"localhost";

mysql> grant all privileges on zabbix.* to "zabbix"@"127.0.0.1";

mysql> grant all privileges on zabbix.* to "zabbix"@"%";

mysql> flush privileges;

 

  2.3部署安裝PHP

wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm

yum install -y remi-release-7.rpm

yum install -y php72-php-devel php72-php-fpm php72-php-mbstring php72-php-memcache php72-php-redis php72-php-mysqli php72-php-mysqlnd php72-php-pdo php72-php-bcmath php72-php-dom php72-php-gd php72-php-gmp php72-php-igbinary php72-php-imagick php72-php-mcrypt php72-php-pdo_mysql php72-php-posix php72-php-simplexml php72-php-opcache php72-php-xsl php72-php-xmlwriter php72-php-xmlreader php72-php-xml php72-php-swoole php72-php-zip php72-php-fileinfo  systemc

 

vim  /etc/opt/remi/php72/php.ini

385 max_execution_time = 300

395 max_input_time = 300

674 post_max_size = 16M

827 upload_max_filesize = 8M

904 date.timezone = "Asia/Shanghai"

1326 always_populate_raw_post_data= -1

1583 mbstring.func_overload = 0

 

systemctl restart php72-php-fpm && systemctl enable php72-php-fpm

 

  2.4部署安裝zabbix-server

cd /root/ && wget https://sourceforge.net/projects/zabbix/files/ZABBIX%20Latest%20Stable/4.0.12/zabbix-4.0.12.tar.gz

yum  -y install  java* mysql-devel net-snmp-devel libevent-devel mysql-devel curl-devel

useradd zabbix

tar  -xf zabbix-4.0.12.tar.gz

cd zabbix-4.0.12/

./configure --prefix=/opt/zabbix --enable-server --enable-agent --with-net-snmp --enable-java --with-libxml2 --with-libcurl --with-libxml2 --with-mysql=/usr/bin/mysql_config  --enable-proxy

make && make install

cd database/mysql/

mysql -uzabbix -p密碼 zabbix < schema.sql

mysql -uzabbix -p密碼 zabbix < images.sql

mysql -uzabbix -p密碼 zabbix < data.sql

cd && cp -r zabbix-4.0.12/frontends/php/* /opt/nginx/html/

chmod -R 777 /opt/nginx/html/*

cp zabbix-4.0.12/misc/init.d/fedora/core/zabbix_agentd  /etc/init.d/

cp zabbix-4.0.12/misc/init.d/fedora/core/zabbix_server  /etc/init.d/

chmod +x /etc/init.d/zabbix_agentd

chmod +x /etc/init.d/zabbix_server

sed -i "s/#PidFile=\/tmp\/zabbix_server.pid/PidFile=\/tmp\/zabbix_server.pid/g" /opt/zabbix/etc/zabbix_server.conf

sed -i "s/# DBHost=localhost/DBHost=localhost/g" /opt/zabbix/etc/zabbix_server.conf

sed -i "s/# DBPassword=/DBPassword=密碼/g" /opt/zabbix/etc/zabbix_server.conf

sed -i "s/# DBSocket=/DBSocket=\/var\/lib\/mysql\/mysql.sock/g" /opt/zabbix/etc/zabbix_server.conf

sed -i "s/# JavaGateway=/JavaGateway= 127.0.0.1/g" /opt/zabbix/etc/zabbix_server.conf

sed -i "s/Timeout=4/Timeout=30/g" /opt/zabbix/etc/zabbix_server.conf

sed -i "s/^# AlertScriptsPath=/AlertScriptsPath=\/opt\/zabbix\/scripts" /opt/zabbix/etc/zabbix_server.conf

mkdir -p /opt/zabbix/scripts

cp /opt/nginx/html/conf/zabbix.conf.php.example   /opt/nginx/html/zabbix.conf.php

 

vim /opt/nginx/html/zabbix.conf.php

$DB['PASSWORD']                 = '密碼';

 

sed -i "s/BASEDIR=\/usr\/local/BASEDIR=\/opt\/zabbix/g" /etc/init.d/zabbix_server

sed -i "s/BASEDIR=\/usr\/local/BASEDIR=\/opt\/zabbix/g" /etc/init.d/zabbix_agentd

systemctl daemon-reload

systemctl start zabbix_server

systemctl start zabbix_agentd

netstat  -utnlp |grep zabbix

提示:如果無法查看10050 10051端口,可以通過cat  /tmp/zabbix_server.log和systemctl  status  zabbix_server來查看相關的報錯信息。到這里zabbix-server已經部署完成。

解決zabbix server中文出現亂碼的情況,將文檔中ukai.ttf文件放到文件中的/opt/nginx/html/assets/fonts目錄下

cd /opt/nginx/html/assets/fonts

vim  /opt/nginx/html/include/defines.inc.php

70 define('ZBX_GRAPH_FONT_NAME',           'ukai'); // font file name

111 define('ZBX_FONT_NAME', 'ukai');

 

2.5部署安裝zabbix-agentd

firewall-cmd --zone=public --add-port=10050/tcp --permanent

firewall-cmd --reload

yum  -y install  java* mysql-devel net-snmp-devel libevent-devel mysql-devel curl-devel gcc wget

wget https://sourceforge.net/projects/zabbix/files/ZABBIX%20Latest%20Stable/4.0.12/zabbix-4.0.12.tar.gz

setenforce 0

useradd -s  /sbin/nologin  zabbix

tar -xf zabbix-4.0.12.tar.gz

cd zabbix-4.0.12/

./configure --enable-agent --prefix=/opt/zabbix

make && make install

mkdir -p /opt/zabbix/logs

chown zabbix /opt/zabbix/logs

sed -i 's#^LogFile.*$#LogFile=/opt/zabbix/logs/zabbix_agentd.log#'   /opt/zabbix/etc/zabbix_agentd.conf

sed -i 's#^Server=.*$#Server=127.0.0.1, 服務端IP#'   /opt/zabbix/etc/zabbix_agentd.conf

sed -i 's#^ServerActive.*$#ServerActive= 服務端IP#'   /opt/zabbix/etc/zabbix_agentd.conf

sed -i '103aListenPort=10050' /opt/zabbix/etc/zabbix_agentd.conf

cp /root/zabbix-4.0.12/misc/init.d/fedora/core/zabbix_agentd  /etc/init.d/

sed -i 's#BASEDIR=.*$#BASEDIR=/opt/zabbix#' /etc/init.d/zabbix_agentd

/etc/init.d/zabbix_agentd start

systemctl daemon-reload

systemctl start zabbix_agentd

systemctl status zabbix_agentd

 

3.部署安裝grafana 6.0

wget https://dl.grafana.com/oss/release/grafana-6.0.0-1.x86_64.rpm

yum -y install grafana-6.0.0-1.x86_64.rpm

安裝zabbix及圖形樣式插件

grafana-cli plugins install alexanderzobnin-zabbix-app

grafana-cli plugins install grafana-clock-panel

grafana-cli plugins install grafana-piechart-panel

systemctl enable grafana-server

systemctl start grafana-server

vim /etc/sysconfig/iptables

-A INPUT -p tcp -m state --state NEW -m tcp --dport 3000 -j ACCEPT

 

登錄地址: http://服務端IP:3000/login   登錄賬號:admin  初始登錄密碼:admin 修改密碼:

 

4:配置zabbix界面及告警

   4.1 初始登錄設置

初始登錄界面:http://服務端IP/index.php  ,只需要在數據庫的密碼欄填寫設置好的數據庫密碼,點擊下一步即可。

初始登錄的賬號:admin     初始登錄的密碼:zabbix

1:將界面設置顯示中文

點擊界面右上角的人圖像,在Language一欄,點擊選擇“中文”即可,保存更新。

2:禁用guest用戶

點擊界面“管理”---“用戶群組”---“Guests”—勾選后,點擊“禁用”。

3:修改admin的密碼

點擊界面“管理”---“用戶”—“Admin”---“密碼”---輸入密碼“”---點擊“更新”。

4:新增用戶,並設置為管理員

點擊界面“管理”---“用戶”---“創建用戶”----群組“Zabbix administrator”—密碼“”---“添加”。

點擊界面“管理”---“用戶”---“權限”---用戶類型“超級管理員”---“更新”

   4.2 添加主機組

點擊“配置”---“主機群組”---“創建主機群組”---組名“PPOVD”---添加

點擊“管理”---“用戶群組”---“Zabbix administrators”---權限(讀寫)---選擇用戶群組---輸入主機群組---點擊“添加”

   4.3 添加主機

點擊 “配置”---“主機”---“創建主機”---主機名稱“PPOVD04”---群組“PPOVD”---IP地址“”—添加

提示:添加相同類型的主機,直接在原先創建的主機的下方,點擊“克隆”,修改IP和名稱即可。

   4.4 添加監控項目

點擊 “配置”---“主機”---“監控項”---“創建監控項”---名稱“輸入名稱”—類型“zabbix客戶端”---鍵值“根據監控項選擇”---添加

點擊 “配置”---“模板”---“創建模板”---模板名稱(根據業務輸入模板名稱)---群組“選擇”---“添加”

   4.5 根據項目添加圖形或觸發器

點擊 “配置”---“主機”---“觸發器”---創建觸發器“”---名稱“disk not enough“---

 

   4.6 自定義宏並進行監控和報警

1:監控磁盤的大小,當達到/home小於20G進行報警

在需要監控的主機創建所需要的宏集,點擊“配置“—"主機”—點擊選定的主機—點擊“宏”—

{$FS.FREE.HIGH}    20G

{$HOST.DIR}        /home

2:創建對應的監控項,

點擊“配置”---“主機”---“監控項”---名稱(Free disk space on {$HOST.DIR})---

鍵值(vfs.fs.size[{$HOST.DIR},free])---點擊更新

3:創建對應的報警

點擊“配置”---“主機”---“觸發器”---“創建觸發器”---名稱“Free disk space is less than {$FS.FREE.HIGH} on”---表達式“{PPOVD04:vfs.fs.size[{$HOST.DIR},free].last(0)}<{$FS.FREE.HIGH}”

4:監控服務是否正常運行,通過監控端口是否存在來判斷服務是否正常運行。

點擊“配置”---主機---“監控項”---“創建監控項”---名稱“nginx status”---鍵值(net.tcp.listen[80])---點擊“更新”

點擊“配置”---主機---觸發器---“創建觸發器”---名稱“nginx is down”---嚴重性---表達式({PPOVD04:net.tcp.listen[80].last()}=0)----更新

 

 

   4.7添加監控報警

點擊“管理”---“報警媒介類型“---創建媒介類型---名稱“telegram”---類型(腳本)---腳本名稱“telegram.py”---腳本參數  {ALERT.SENDTO}   {ALERT.SUBJECT}   {ALERT.MESSAGE}  ---- 添加

點擊“管理“---”用戶“---報警媒介---類型”telegram“---收件人”telegram賬號“---點擊更新

點擊“配置“---”創建動作“---”名稱“---新的觸發條件(觸發器示警度 大於等於  嚴重)---操作---默認標題({{fire}}{{fire}}{{fire}}:告警節點:{TRIGGER.NAME}

---消息內容

問題詳情:  {ITEM.NAME}:{ITEM.VALUE}

告警主機:  {HOST.NAME}

告警時間:  {EVENT.DATE} {EVENT.TIME}

告警等級:  {TRIGGER.SEVERITY}

告警信息:  {TRIGGER.NAME}

告警項目:  {TRIGGER.KEY1}

當前狀態:  {TRIGGER.STATUS}.{ITEM.VALUE}

事件ID:  {EVENT.ID}

zbxtg:graphs

zbxtg:graphs_period=10800

zbxtq:itemid:{ITEM.ID1}

zbxtg:title:{HOST.HOST} - {TRIGGER.NAME}

---點擊新的---發送到用戶組(Zabbix administrators)---發送到用戶(harry)---僅發送到(telegram)---操作條件((事件已確認 等於  非確認)---添加

---恢復操作---默認標題({{OK}}{{OK}}{{OK}}:恢復節點:{TRIGGER.NAME})---消息內容

問題詳情:  {ITEM.NAME}:{ITEM.VALUE}

恢復主機:  {HOST.NAME}

恢復時間:  {EVENT.DATE} {EVENT.TIME}

事件等級:  {TRIGGER.SEVERITY}

恢復項目:  {TRIGGER.KEY1}

當前狀態:  {TRIGGER.STATUS}.{ITEM.VALUE}

事件ID:  {EVENT.ID}

zbxtg:graphs

zbxtg:graphs_period=10800

zbxtq:itemid:{ITEM.ID1}

zbxtg:title:{HOST.HOST} - {TRIGGER.NAME}

---點擊新的---發送到用戶群組(Zabbix administrators)---發送到用戶(harry)---僅發送到(telegram)---添加

---添加

 

#服務器進行配置腳本文件,在zabbix server安裝telegram插件

wget https://bootstrap.pypa.io/get-pip.py

python get-pip.py

pip install telegram

pip install python-telegram-bot

pip install pyTelegramBotAPI

 

#在/opt/Zabbix/scripts下,放腳本文件telegram.py,並給與執行權限

cd /opt/zabbix/scripts/

 

#!usr/bin/env python

# pip install pyTelegramBotAPI

import sys

 

import telebot

 

contact = sys.argv[1]

subject = sys.argv[2]

content = sys.argv[3]

# subject = 'test'

# content = 'test'

TEST = telebot.TeleBot(token='811992293:AAEElXmUbdRpijLrPkLqN3sjetx-EDPrB9Y')

chat_id = int(contact)

TEST.send_message(chat_id=chat_id, text=subject + '\n' + content)

 

#給該文件增加執行權限

chmod +x /opt/zabbix/scripts/telegram.py

chown zabbix.zabbix /opt/zabbix/scripts/telegram.py

 

#確保zabbix服務器上的python版本是2.7及以上即可,centos使用默認版本即可

[root@localhost scripts]# python

 

#手動測試腳本的使用腳本發送信息,測試示例:

python telegram.py "-355170319" 主題:測試信息 $'first line\nsecond line\nthird line'

補充:如果測試后在telegram群里看到報警,說明整個腳本是正確的,如果又其他的報錯,多多嘗試一下。

 

補充:利用telegram機器人報警

1:打開telegram機器人

   第一步:在telegram里面搜索欄里面輸入“@BotFather “打開和telegram官方機器人的對話框

   第二步:發送/start

   第三步:給自己的機器人取個名字(需要以_bot結尾,前面可以自定義)

   第四步:收到官方發過來的token

   第五步:將機器人添加到需要接收消息的telegram個人賬號中,同時建立一個組,將機器人添加到組中。

使用

   使用telegram官方發過來的token獲取相應的chat_id,如果機器發給個人的,chat是一串純數字,如果是發給群組的,數據前面帶一個-

https://api.telegram.org/bot811992293:AAEElXmUbdRpijLrPkLqN3sjetx-EDPrB9Y/getUpdates

   (個人的id: 865969417     群組的ID:-355170319    )

 

5:配置grafana顯示zabbix

  5.1:創建Dashboard

點擊界面的右側第二個圖形Dashboard ---點擊保存---命名(zabbix)----點擊“save”

點擊界面上的configuration---點擊plugins---點擊“zabbix”---“enable”---

點擊界面上的configuration---點擊data sources---點擊“add data source”---顯示zabbix表示完成。

  5.2配置grafana顯示zabbix數據

點擊界面的configuration---點擊“add data source”---點擊“zabbix”---

在URL輸入( http://服務端Ip/api_jsonrpc.php)---

在zabbix version 中選擇 4.x

在username 輸入Admin   ------password輸入zabbix的密碼()---點擊“save & Test”

  5.3grafana創建圖形

點擊“create”---點擊“add query”--- Queries to(zabbix)----group(輸入主機組)---host(主機名)---點擊“general”—title()

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM