Prometheus监控Redis,Mysql,Grafana自定义图表配置

本文档详细介绍了如何配置Redis_exporter和mysql_exporter以监控Redis和MySQL,并将它们集成到Prometheus中进行数据收集。同时,提供了Grafana仪表板的设置方法,用于展示数据库的状态和性能指标。
摘要由CSDN通过智能技术生成

一、Redis

1、 下载Redis_exporter

https://github.com/oliver006/redis_exporter/releases
PS: redis_exporter 对于redis集群的监控表现不是太好, 建议为每个redis 实例启动一个redis_exporter, 如果一个服务器节点启动了多个redis进程, 就需要启动多个redis_exporter,如果采用一个redis_exporter 采集多个 redis node 会导致其中一个 redis node 连不上。

2、 启动Redis_exporter

[root@hadoop101 redis_exporter-v1.17.0.linux-amd64]# ./redis_exporter -redis.addr 192.168.1.107 -redis.password 123456
INFO[0000] Redis Metrics Exporter v1.17.0    build date: 2021-02-11-15:25:56    sha1: 0092e19fa6146af429d81b27c85f050935e6c593    Go: go1.15.8    GOOS: linux    GOARCH: amd64 
INFO[0000] Providing metrics at :9121/metrics  
启动参数说明:
  • -redis.addr:指明 Redis 节点的地址,默认为 redis://localhost:6379(如果有多个redis实例, redis_exporter作者建议启动多个redis_exporter进程来进行监控数据获取)。
  • -redis.password:验证 Redis 时使用的密码。
  • -redis.file:包含一个或多个redis 节点的文件路径,每行一个节点,此选项与 -redis.addr 互斥。
  • -web.listen-address:监听的地址和端口,默认为 0.0.0.0:9121

3、prometheus配置

(1)、修改prometheus.yml,增加redis配置。
- job_name: 'redis'
    static_configs:
      - targets: ['192.168.1.9:9121']
        labels:
          yw: redis
          id: redis-107-6379
          project: 测试环境
(2)、修改后刷新prometheus配置。
kill -HUP 进程号

在这里插入图片描述

4、Grafan配置dashboard

(1)、导入官方模板 763

在这里插入图片描述

在这里插入图片描述

5、制作自定义菜单

在这里插入图片描述

(1)、图表json文件
{
  "columns": [],
  "datasource": "Prometheus",
  "fieldConfig": {
    "defaults": {
      "custom": {}
    },
    "overrides": []
  },
  "fontSize": "100%",
  "gridPos": {
    "h": 6,
    "w": 24,
    "x": 0,
    "y": 25
  },
  "id": 215,
  "links": [],
  "repeatDirection": "h",
  "scroll": true,
  "showHeader": true,
  "sort": {
    "col": 11,
    "desc": true
  },
  "styles": [
    {
      "$$hashKey": "object:996",
      "alias": "在线状态",
      "align": "auto",
      "colorMode": "cell",
      "colors": [
        "rgba(245, 54, 54, 0.9)",
        "rgba(237, 129, 40, 0.89)",
        "rgba(50, 172, 45, 0.97)"
      ],
      "dateFormat": "YYYY-MM-DD HH:mm:ss",
      "decimals": 2,
      "link": false,
      "mappingType": 1,
      "pattern": "Value",
      "thresholds": [
        "0",
        "1"
      ],
      "type": "string",
      "unit": "short",
      "valueMaps": [
        {
          "$$hashKey": "object:1017",
          "text": "在线",
          "value": "1"
        },
        {
          "$$hashKey": "object:1019",
          "text": "不在线",
          "value": "0"
        }
      ]
    },
    {
      "$$hashKey": "object:1021",
      "alias": "数据库信息",
      "align": "auto",
      "colorMode": null,
      "colors": [
        "rgba(245, 54, 54, 0.9)",
        "rgba(237, 129, 40, 0.89)",
        "rgba(50, 172, 45, 0.97)"
      ],
      "dateFormat": "YYYY-MM-DD HH:mm:ss",
      "decimals": 2,
      "link": true,
      "linkTargetBlank": true,
      "linkTooltip": "redis详细信息",
      "linkUrl": "/d/XnTn7eEGz/redis-dashboard-for-prometheus-redis-exporter-1-x?orgId=1&refresh=30s",
      "mappingType": 1,
      "pattern": "id",
      "thresholds": [],
      "type": "number",
      "unit": "short"
    },
    {
      "$$hashKey": "object:1032",
      "alias": "数据库类型",
      "align": "auto",
      "colorMode": null,
      "colors": [
        "rgba(245, 54, 54, 0.9)",
        "rgba(237, 129, 40, 0.89)",
        "rgba(50, 172, 45, 0.97)"
      ],
      "dateFormat": "YYYY-MM-DD HH:mm:ss",
      "decimals": 2,
      "mappingType": 1,
      "pattern": "yw",
      "thresholds": [],
      "type": "number",
      "unit": "short"
    }
  ],
  "targets": [
    {
      "expr": "redis_up",
      "format": "table",
      "instant": true,
      "interval": "",
      "legendFormat": "",
      "refId": "A"
    }
  ],
  "title": "Redis状态明细",
  "transform": "table",
  "transformations": [
    {
      "id": "filterFieldsByName",
      "options": {
        "include": {
          "names": [
            "project",
            "yw",
            "Value",
            "id"
          ]
        }
      }
    }
  ],
  "type": "table-old",
  "pageSize": null
}

二、Mysql

1、配置mysql_exporter

(1)、下载
https://github.com/prometheus/mysqld_exporter/releases/
(2)、解压
tar zxvf mysqld_exporter-0.12.1.linux-amd64.tar.gz 
mv mysqld_exporter-0.12.1.linux-amd64 /usr/local/mysql_exporter
cd /usr/local/mysql_exporter
(3)、创建用户并授权
MariaDB [(none)]> CREATE USER 'exporter'@'localhost' IDENTIFIED BY '000000';  
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
Query OK, 0 rows affected (0.001 sec)
(4)、创建配置文件,运行时通过配置文件登陆
vim /usr/local/mysql_exporter/.my.cnf
[client]
user=exporter
password=000000
(5)、启动
./mysqld_exporter --config.my-cnf=.my.cnf
(6)、添加系统服务
vim /usr/lib/systemd/system/mysql_exporter.service
[Unit]
Description=https://prometheus.io

[Service]
Restart=on-failure
ExecStart=/usr/local/mysql_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysql_exporter/.my.cnf

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start mysql_exporter.service
systemctl enable mysql_exporter

3、prometheus配置

(1)、修改配置文件,prometheus.yml。
- job_name: 'mysql'
    static_configs:
      - targets: ['192.168.1.131:9104']
        labels:
          yw: mysql
          project: 生产环境
          database: skdata
      - targets: ['192.168.1.121:9104']
        labels:
          yw: mysql
          project: 测试环境
          database: sktest_saas
(2)、修改后刷新prometheus配置。
kill -HUP 进程号

在这里插入图片描述

4、Grafana配置dashboard

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

5、制作自定义菜单

在这里插入图片描述

(1)、导出json文件
1、通过传入的variables进行查询
{
  "columns": [],
  "datasource": "Prometheus",
  "fieldConfig": {
    "defaults": {
      "custom": {}
    },
    "overrides": []
  },
  "fontSize": "100%",
  "gridPos": {
    "h": 6,
    "w": 24,
    "x": 0,
    "y": 15
  },
  "id": 210,
  "links": [],
  "repeatDirection": "h",
  "scroll": true,
  "showHeader": true,
  "sort": {
    "col": null,
    "desc": false
  },
  "styles": [
    {
      "$$hashKey": "object:232",
      "alias": "数据库名称",
      "align": "auto",
      "colorMode": null,
      "colors": [
        "rgba(245, 54, 54, 0.9)",
        "rgba(237, 129, 40, 0.89)",
        "rgba(50, 172, 45, 0.97)"
      ],
      "dateFormat": "YYYY-MM-DD HH:mm:ss",
      "decimals": 2,
      "link": true,
      "linkTargetBlank": true,
      "linkTooltip": "mysql",
      "linkUrl": "http://192.168.1.175:3000/d/MQWgroiiz/mysql?orgId=1&refresh=1m&var-interval=1m&var-host=${__cell_3}",
      "mappingType": 1,
      "pattern": "database",
      "thresholds": [],
      "type": "number",
      "unit": "short"
    },
    {
      "$$hashKey": "object:298",
      "alias": "项目名称",
      "align": "auto",
      "colorMode": null,
      "colors": [
        "rgba(245, 54, 54, 0.9)",
        "rgba(237, 129, 40, 0.89)",
        "rgba(50, 172, 45, 0.97)"
      ],
      "dateFormat": "YYYY-MM-DD HH:mm:ss",
      "decimals": 2,
      "mappingType": 1,
      "pattern": "project",
      "thresholds": [],
      "type": "number",
      "unit": "short"
    },
    {
      "$$hashKey": "object:310",
      "alias": "数据库类型",
      "align": "auto",
      "colorMode": null,
      "colors": [
        "rgba(245, 54, 54, 0.9)",
        "rgba(237, 129, 40, 0.89)",
        "rgba(50, 172, 45, 0.97)"
      ],
      "dateFormat": "YYYY-MM-DD HH:mm:ss",
      "decimals": 2,
      "mappingType": 1,
      "pattern": "yw",
      "thresholds": [],
      "type": "number",
      "unit": "short"
    },
    {
      "$$hashKey": "object:1022",
      "alias": "在线状态",
      "align": "auto",
      "colorMode": "cell",
      "colors": [
        "rgba(245, 54, 54, 0.9)",
        "rgba(237, 129, 40, 0.89)",
        "rgba(50, 172, 45, 0.97)"
      ],
      "dateFormat": "YYYY-MM-DD HH:mm:ss",
      "decimals": 2,
      "link": false,
      "mappingType": 2,
      "pattern": "Value #A",
      "rangeMaps": [
        {
          "$$hashKey": "object:1049",
          "from": "1",
          "text": "在线",
          "to": "1"
        },
        {
          "$$hashKey": "object:1051",
          "from": "0",
          "text": "不在线",
          "to": "0"
        }
      ],
      "thresholds": [
        "0",
        "1"
      ],
      "type": "string",
      "unit": "short",
      "valueMaps": [
        {
          "$$hashKey": "object:1044",
          "text": "在线",
          "value": "1"
        },
        {
          "$$hashKey": "object:1046",
          "text": "",
          "value": "0"
        }
      ]
    },
    {
      "$$hashKey": "object:249",
      "alias": "运行时间",
      "align": "auto",
      "colorMode": null,
      "colors": [
        "rgba(245, 54, 54, 0.9)",
        "rgba(237, 129, 40, 0.89)",
        "rgba(50, 172, 45, 0.97)"
      ],
      "dateFormat": "YYYY-MM-DD HH:mm:ss",
      "decimals": 2,
      "mappingType": 1,
      "pattern": "Value #B",
      "sanitize": false,
      "thresholds": [],
      "type": "number",
      "unit": "s"
    },
    {
      "$$hashKey": "object:1145",
      "alias": "",
      "align": "auto",
      "colorMode": null,
      "colors": [
        "rgba(245, 54, 54, 0.9)",
        "rgba(237, 129, 40, 0.89)",
        "rgba(50, 172, 45, 0.97)"
      ],
      "dateFormat": "YYYY-MM-DD HH:mm:ss",
      "decimals": 2,
      "mappingType": 1,
      "pattern": "/.*/",
      "thresholds": [],
      "type": "hidden",
      "unit": "short"
    }
  ],
  "targets": [
    {
      "expr": "mysql_up{yw=\"$yw\"}",
      "format": "table",
      "instant": true,
      "interval": "",
      "legendFormat": "",
      "refId": "A"
    },
    {
      "expr": "sum(time() - mysql_global_status_uptime{yw=\"$yw\"}) by (mysql_databases)",
      "format": "table",
      "instant": true,
      "interval": "",
      "legendFormat": "运行时间",
      "refId": "B"
    }
  ],
  "title": "数据库状态明细",
  "transform": "table",
  "transformations": [
    {
      "id": "filterFieldsByName",
      "options": {
        "include": {
          "names": [
            "Time",
            "__name__",
            "database",
            "instance",
            "job",
            "project",
            "yw",
            "Value #A",
            "mysql_global_status_uptime{database=\"skdata\", instance=\"192.168.1.131:9104\", job=\"mysql\", project=\"生产环境\", yw=\"mysql\"}",
            "mysql_global_status_uptime{database=\"sktest_saas\", instance=\"192.168.1.121:9104\", job=\"mysql\", project=\"测试环境\", yw=\"mysql\"}"
          ]
        }
      }
    }
  ],
  "type": "table-old",
  "description": "",
  "pageSize": null
}

在这里插入图片描述
在这里插入图片描述
参考: mysql

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值