• 部署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 + ' ' + 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 second line third 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()

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    卷积神经网路计算公式
    Jupyter Notebook
    vim batch copy -- copy between row1 and row2 to row3
    Intellij 快速复制单行快捷键
    Exception in thread "main" java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector
    Mac 下查看隐藏文件
    Mac 连接 mysql
    CC++JavaPython 求字符串长度
    LeetCode-1309 Decrypt String from Alphabet to Integer Mapping
    ❀LeetCode❀-1374. Generate a String With Characters That Have Odd Counts
  • 原文地址:https://www.cnblogs.com/will--1213/p/11824550.html
Copyright © 2020-2023  润新知