使用prometheus+grafana监控MySQL监控Oracle

时间:2024-02-17 14:16:02

【环境介绍】

系统环境:CentOS Linux release 7.6 + prometheus version 2.20.0 + Grafana CLI version 7.1.3 + mysql 5.7 + oracle 11.2.0.4.0
 
【安装部署】
1,安装prometheus
下载安装包:https://github.com/prometheus/prometheus/releases/download/v2.20.1/prometheus-2.20.1.linux-amd64.tar.gz
创建安装目录(这里根据实际情况定义软件目录):mkdir -p /home/prometheus/prometheus
解压至安装目录:tar xf prometheus-2.20.1.linux-amd64.tar.gz -C /home/prometheus/prometheus --strip-components=1
目录说明,需要创建对应目录:
 
安装软件目录:/home/prometheus/prometheus
配置文件目录:/home/prometheus/prometheus/conf
数据目录:/home/prometheus/prometheus/data
日志目录:/home/prometheus/prometheus/logs
 
 
2,修改配置文件
 
cd /home/prometheus/prometheus/
拷贝配置文件到conf自己定义的目录:
cp prometheus.yml ./conf
cd /home/prometheus/prometheus/conf
vim prometheus.yml
# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093
# Load rules once and periodically evaluate them according to the global \'evaluation_interval\'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it\'s Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: \'prometheus\'
    # metrics_path defaults to \'/metrics\'
    # scheme defaults to \'http\'.
 
   static_configs:
    - targets: [\'localhost:9090\']
  - job_name: linux
    static_configs:
      - targets: [\'10.0.0.166:9100\']
        labels:
          instance: linux_10.0.0.166

后面红色部分为添加的内容
 
3,配置启动脚本
使用脚本进行启动服务,注意修改配置文件,后面参数指定配置文件,数据目录,日志目录,保留时间,pid路径
cat  >start_prometheus.sh
#!/bin/bash
BASEPATH=/home/prometheus/prometheus
LOG=$BASEPATH/logs
nohup $BASEPATH/prometheus --config.file=$BASEPATH/conf/prometheus.yml --web.listen-address=:9090 --storage.tsdb.path=$BASEPATH/data/  --storage.tsdb.retention=5d >> $LOG/prometheus.log  2>&1 & echo $! > $BASEPATH/run/prometheus.pid
sleep 5
PROC=`ps -ef |grep \`cat $BASEPATH/run/prometheus.pid\` |grep -v grep`
if  [ ! -n "$PROC" ] ;then
    echo "##########################################"
    echo "Process not found, please check by yourself"
    echo "##########################################"
else
    echo "##########################################"
    echo "Prometheus is running"
    echo "##########################################"
fi
 
 

 4,安装node_exporter,mysqld_exporter,oracledb_exporter

cd /home/prometheus/

wget https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.10.0/mysqld_exporter-0.10.0.linux-amd64.tar.gz
wget https://github.com/iamseth/oracledb_exporter/releases/download/0.2.9/oracledb_exporter.0.2.9-ora18.5.linux-amd64.tar.gz

解压到指定export目录:

/home/prometheus/prometheus_exporter
cd /home/prometheus/
tar xf node_exporter-1.0.1.linux-amd64.tar.gz -C prometheus_exporter/
tar xf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C prometheus_exporter/
tar xf oracledb_exporter.0.2.9-ora18.5.linux-amd64.tar.gz -C  prometheus_exporter/
 
 
加载node_exporter
cd /home/prometheus/prometheus_exporter/node_exporter-1.0.1.linux-amd64
后台运行服务
nohup ./node_exporter &
 
 
加载mysqld_exporter
cd prometheus_exporter/mysqld_exporter-0.12.1.linux-amd64
创建MySQL数据库监控用户:
create user mysql_monitor@\'%\' identified by \'Mysql_monitor123\';
GRANT REPLICATION CLIENT, PROCESS ON *.* TO mysql_monitor@\'%\';
GRANT SELECT ON performance_schema.* TO mysql_monitor@\'%\';
 
vim .my.cnf
[client]
   host=localhost
   port=3306
   user=mysql_monitor
   password=Mysql_monitor123
 
后台运行服务
nohup ./mysqld_exporter --config.my-cnf=.my.cnf &
 
加载oracle_exporter
 
下载oracle客户端安装包,配置安装,需要19C
https://www.oracle.com/cn/database/technologies/instant-client/linux-x86-64-downloads.html
这里使用root用安装19C的客户端
rpm -ivh oracle-instantclient19.5-basic-19.5.0.0.0-1.x86_64.rpm
[root@oracle21 prometheus_exporter]# rpm -ivh oracle-instantclient19.5-basic-19.5.0.0.0-1.x86_64.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:oracle-instantclient19.5-basic-19################################# [100%]
[root@oracle21 prometheus_exporter]# cd /usr/lib/oracle/19.3/client64/network/admin
-bash: cd: /usr/lib/oracle/19.3/client64/network/admin: No such file or directory
[root@oracle21 prometheus_exporter]# cd /usr/lib/oracle/19.5/client64/  
[root@oracle21 client64]# ls -trl
total 4
drwxr-xr-x 2 root root   33 Aug 13 09:54 bin
drwxr-xr-x 3 root root 4096 Aug 13 09:54 lib
[root@oracle21 client64]# mkdir -p network/admin/
[root@oracle21 client64]# cd network/admin/
[root@oracle21 admin]#  cat  >tnsnames.ora
oratest =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.0.166)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = oratest)
    )
  )
[root@oracle21 admin]#
 
配置环境变量 vim ~/.bash_profile --根目录下为全局使用,为限制用户权限,可只修改某一用户的环境变量
vim ~/.bash_profile
export ORACLE_HOME=/usr/lib/oracle/19.5/client64
export TNS_ADMIN=$ORACLE_HOME/network/admin
export NLS_LANG=\'simplified chinese_china\'.ZHS16GBK
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
export PATH=$ORACLE_HOME/bin:$PATH
 
source ~/.bash_profile
 
Oracle数据库创建用户
create user oracle_monitor identified by Oracle_monitor123;
grant connect,dba,select any table to oracle_monitor;
 
[root@oracle21 prometheus_exporter]# cd oracledb_exporter.0.2.9-ora18.5.linux-amd64/
[root@oracle21 oracledb_exporter.0.2.9-ora18.5.linux-amd64]# pwd
/home/prometheus/prometheus_exporter/oracledb_exporter.0.2.9-ora18.5.linux-amd64
[root@oracle21 oracledb_exporter.0.2.9-ora18.5.linux-amd64]# export DATA_SOURCE_NAME=oracle_monitor/Oracle_monitor123@oratest
[root@oracle21 oracledb_exporter.0.2.9-ora18.5.linux-amd64]# nohup ./oracledb_exporter &
[1] 2623
[root@oracle21 oracledb_exporter.0.2.9-ora18.5.linux-amd64]# nohup: ignoring input and appending output to ‘nohup.out’
[root@oracle21 oracledb_exporter.0.2.9-ora18.5.linux-amd64]# ps -ef |grep oracledb_exporter
root      2623 32023  0 10:01 pts/0    00:00:00 ./oracledb_exporter
root      2669 32023  0 10:01 pts/0    00:00:00 grep --color=auto oracledb_exporter
[root@oracle21 oracledb_exporter.0.2.9-ora18.5.linux-amd64]#
 
以上使用命令查看采集数据是否正常(修改对应的IP跟端口):
curl http://IP:9161/metrics
 
5,启动服务
查看配置文件:
cat  prometheus.yml
    static_configs:
    - targets: [\'localhost:9090\']
  - job_name: linux
    static_configs:
      - targets: [\'10.0.0.166:9100\']
        labels:
          instance: linux_10.0.0.166
 
  - job_name: mysql
    static_configs:
      - targets: [\'10.0.0.166:9104\']
        labels:
          instance: mysql_10.0.0.166
  - job_name: oracle_local
    static_configs:
      - targets: [\'10.0.0.166:9161\']
        labels:
          instance: oracle_10.0.0.166
 
直接运行脚本启动服务:
[root@oracle21 prometheus]# sh start_prometheus.sh
##########################################
Prometheus is running
##########################################
[root@oracle21 prometheus]#
 
 
查看网址是否监控正常:
http://10.0.0.166:9090/targets

 

 

 

 

 

 

 

5,安装grafana

这里使用RPM安装

wget https://dl.grafana.com/oss/release/grafana-7.1.3-1.x86_64.rpm
yum install grafana-7.1.3-1.x86_64.rpm
$ systemctl daemon-reload
$ systemctl start grafana-server
$ systemctl status grafana-server

访问网址是否正常
http://10.0.0.166:3000 用户密码为admin  admin

 

 

6,导入对应的仪表盘效果如下

下载对应的仪表盘:https://grafana.com/grafana/dashboards

主机:

 

MySQL:

 

 Oracle:

 

 7,遇到的问题

使用之前查看的批量导入MySQL仪表盘显示需要安装插件,该插件本身自带,解决办法直接下载最新的仪表盘导入即可;

 

Oracle数据库采集数据异常,查看为查询表空间耗时很久,在数据库层面清理回收站即可;

 

 

Linux6安装监控异常,为需要安装Linux需要支持2.14 strings /lib64/libc.so.6 |grep GLIBC_ linux6需要升级

 下载glibc-2.14.tar.gz http://ftp.gnu.org/gnu/glibc/

注意新的版本一定得用其他目录,防止覆盖原版本导致系统异常:

mkdir -p /root/soft/glibc
gunzip glibc-2.14.tar.gz
tar -xvf glibc-2.14.tar
cd glibc-2.14
mkdir build
cd build
../configure --prefix=/root/soft/glibc
make -j4
make install
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/root/soft/glibc/lib