Prometheus+Grafana项目

Prometheus数据库监控系统

环境准备:

服务器内网IP   作用   安装软件
10.0.0.123 监控的服务端  Prometheus(服务端软件) Grafana(数据展示)
10.0.0.236 被监控的客户端 node_exporter(收集服务器数据)mysqld_exporter(收集mysql数据)

1、服务端部署Prometheus系统:

1.1、Prometheus服务器上安装go环境:

[root@prometheus ~]# yum install go  -y
[root@prometheus ~]# go version
go version go1.13.11 linux/amd64

1.2、去Prometheus官网下载安装包并解压安装:

[root@prometheus ~]# wget https://github.com/prometheus/prometheus/releases/download/v2.19.0/prometheus-2.19.0.linux-amd64.tar.gz
[root@prometheus ~]# tar zxvf prometheus-2.19.0.linux-amd64.tar.gz 
[root@prometheus ~]# mv prometheus-2.19.0.linux-amd64  /opt/prometheus

1.3修改Prometheus配置文件:

[root@prometheus ~]# cd /opt/prometheus
[root@prometheus /opt/prometheus]# cat 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']
        labels:
          instance: prometheus

  - job_name: 'mysql'
    static_configs:
      - targets: ['10.0.0.236:9104']
        labels:
          instance: '10.0.0.236'


  - job_name: 'node1'
    static_configs:
      - targets: ['10.0.0.236:9100']
        labels:
          instance: 'nd1'

  - job_name: 'node2'
    static_configs:
      - targets: ['10.0.0.123:9100']
        labels:
          instance: 'nd2'
image.png

1.4、启动prometheus方法:

第一种启动方法:
[root@prometheus /opt/prometheus]# nohup ./prometheus --config.file=./prometheus.yml &
第二种启动方法:
[root@prometheus /opt/prometheus]# ./prometheus &
第二种方法启动前需要进行的操作如下:
启动问题1:
level=error ts=2018-11-19T06:01:05.697957445Z caller=main.go:625 
err="opening storage failed: lock DB directory: resource temporarily unavailable
解决:删除 lock 文件
rm -f /opt/prometheus/data/lock
启动问题2:
level=error ts=2018-11-19T06:04:47.83421089Z caller=main.go:625 
err="error starting web server: listen tcp 0.0.0.0:9090: bind: address already in use"
 解决:查找使用9090端口的PID并删掉
lsof -i :9090
kill -9 <pid>

1.5、启动后访问prometheus,如下图所示证明prometheus启动成功:

http://10.0.0.123:9090/


image.png

2、在10.0.0.236客户端上部署node_exporter服务:

2.1、部署安装node_exporter服务并启动:

[root@zabbix ~]#wget https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz
[root@zabbix ~]#tar zxvf node_exporter-1.0.1.linux-amd64.tar.gz
[root@zabbix ~]#  mv node_exporter-1.0.1.linux-amd64   /opt/node_exporter
[root@zabbix ~]#  cd /opt/node_exporter/
[root@zabbix ~]#  nohup ./node_exporter &

2.2、检查端口是否开启:

[root@zabbix /opt/node_exporter]# tail -f 10 nohup.out
tail: cannot open ‘10’ for reading: No such file or directory
==> nohup.out <==
level=info ts=2020-06-24T01:34:43.927Z caller=node_exporter.go:112 collector=timex
level=info ts=2020-06-24T01:34:43.927Z caller=node_exporter.go:112 collector=udp_queues
level=info ts=2020-06-24T01:34:43.927Z caller=node_exporter.go:112 collector=uname
level=info ts=2020-06-24T01:34:43.927Z caller=node_exporter.go:112 collector=vmstat
level=info ts=2020-06-24T01:34:43.927Z caller=node_exporter.go:112 collector=xfs
level=info ts=2020-06-24T01:34:43.927Z caller=node_exporter.go:112 collector=zfs
level=info ts=2020-06-24T01:34:43.932Z caller=node_exporter.go:191 msg="Listening on" address=:9100
level=info ts=2020-06-24T01:34:43.932Z caller=tls_config.go:170 msg="TLS is disabled and it cannot be enabled on the fly." http2=false
image.png

2.3、 可以在内网通过 [http://10.0.0.236:9100/metrics]查看客户端数据:

image.png

备注:出现上面信息,证明已经收集到数据。

3、在10.0.0.236客户端上部署mysqld_exporter服务:

3.1、部署安装mysqld_exporte服务:

[root@zabbix ~]#wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
[root@zabbix ~]#tar zxvf mysqld_exporter-0.12.1.linux-amd64.tar.gz
[root@zabbix ~]#   mv mysqld_exporter-0.12.1.linux-amd64  /opt/mysqld_exporter

3.2、配置数据库连接,创建用户并授权本地和远方登录访问:

允许远方登录:
mysql[(none)]>create user 'mysql_monitor'@'10.0.0.%' identified by 'monitor123456';
mysql[(none)]>grant replication client,process on *.* to mysql_monitor@"10.0.0.%" identified by "monitor123456";
mysql[(none)]>#grant select on performance_schema.* to mysql_monitor@"10.0.0.%";
允许本地登录:
mysql[(none)]>create user 'mysql_monitor'@'localhost' identified by 'monitor123456';
mysql[(none)]>grant replication client,process on *.* to mysql_monitor@"localhost" identified by "monitor123456";
mysql[(none)]>#grant select on performance_schema.* to mysql_monitor@"localhost";

3.3、进入mysqld_exporter安装目录创建.my.cnf配置文件:

[root@zabbix ~]#cd /opt/mysqld_exporter
[root@zabbix /opt/mysqld_exporter]#vim .my.cnf
[client]
host=10.0.0.236
port=3306
user=mysql_monitor
password=monitor123456

3.4、启动 mysqld_exporter并查看端口:

[root@zabbix /opt/mysqld_exporter]#nohup ./mysqld_exporter --config.my-cnf=.my.cnf &
[root@zabbix /opt/mysqld_exporter]# tail -f nohup.out
time="2020-06-22T17:08:30+08:00" level=info msg="Build context (go=go1.12.7, user=root@0b3e56a7bc0a, date=20190729-12:35:58)" source="mysqld_exporter.go:258"
time="2020-06-22T17:08:30+08:00" level=info msg="Enabled scrapers:" source="mysqld_exporter.go:269"
time="2020-06-22T17:08:30+08:00" level=info msg=" --collect.global_status" source="mysqld_exporter.go:273"
time="2020-06-22T17:08:30+08:00" level=info msg=" --collect.global_variables" source="mysqld_exporter.go:273"
time="2020-06-22T17:08:30+08:00" level=info msg=" --collect.slave_status" source="mysqld_exporter.go:273"
time="2020-06-22T17:08:30+08:00" level=info msg=" --collect.info_schema.innodb_cmp" source="mysqld_exporter.go:273"
time="2020-06-22T17:08:30+08:00" level=info msg=" --collect.info_schema.innodb_cmpmem" source="mysqld_exporter.go:273"
time="2020-06-22T17:08:30+08:00" level=info msg=" --collect.info_schema.query_response_time" source="mysqld_exporter.go:273"
time="2020-06-22T17:08:30+08:00" level=info msg="Listening on :9104" source="mysqld_exporter.go:283"
time="2020-06-22T17:08:30+08:00" level=fatal msg="listen tcp :9104: bind: address already in use" source="mysqld_exporter.go:284"

3.5、 可以在内网通过 [http://10.0.0.236:9104/metrics] 查看mysql的相关监控数据

image.png

备注:出现上面信息,证明已经收集到数据。

3.6、在Prometheus界面中查看Status--》Targets是否有节点,绿色的“up”证明数据已经被Prometheus所收集到。

备注:由于上面第一节在安装Prometheus的时候已经在主配置文件中把10.0.0.236所监控的mysql和node配置进去,这里就不再重复演示。直接看结果:


image.png

4、在oracle数据库服务器上部署oracledb_exporter服务:

4.1、先在oracle数据库服务器上部署go环境:

[root@PWSNBUTEST ~]# yum install -y go
[root@PWSNBUTEST ~]# go  version
go version go1.13.11 linux/amd64

4.2、先在oracle数据库服务器上安装oracle客户端:

下载oracle客户端安装包:https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html

安装oracle客户端rpm包:

[root@PWSNBUTEST ~]# ll         
-rw-r--r--  1 root root 52826628 Jun 29 14:38 oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm
-rw-r--r--  1 root root   708104 Jun 29 14:41 oracle-instantclient12.2-sqlplus-12.2.0.1.0-1.x86_64.rpm
[root@PWSNBUTEST ~]# rpm -ivh oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm

创建文件夹

[root@PWSNBUTEST ~]#mkdir -p /usr/lib/oracle/19.3/client64/network/admin

创建数据库监听文件(HOST,PORT,SERVICE_NAME 需要配置):

[root@PWSNBUTEST ~]#vim  /usr/lib/oracle/19.3/client64/network/admin/tnsnames.ora
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = $ip)(PORT = $port))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = $sid)
)
)

配置环境变量,在.bashrc下面添加如下配置,原来的数据不要动。

[root@PWSNBUTEST ~]#vim ~/.bashrc
export ORACLE_HOME=/usr/lib/oracle/19.3/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

使配置完的环境变量生效

[root@PWSNBUTEST ~]#source  ~/.bashrc

4.3、在oracle数据库服务器上安装oracle客户端的sqlplus插件:

下载oracle客户端sqlplus安装包:https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html

安装oracle客户端sqlplus插件rpm包:

[root@PWSNBUTEST ~]# ll         
-rw-r--r--  1 root root 52826628 Jun 29 14:38 oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm
-rw-r--r--  1 root root   708104 Jun 29 14:41 oracle-instantclient12.2-sqlplus-12.2.0.1.0-1.x86_64.rpm
[root@PWSNBUTEST ~]# rpm -ivh  oracle-instantclient12.2-sqlplus-12.2.0.1.0-1.x86_64.rpm

4.3、Oracle的监控,需要用到第三方写的export:

在官网先在oracle的export:https://prometheus.io/docs/instrumenting/exporters/

image.png

通过这个可以找oracle的第三方exporter,这是一个git工程https://github.com/iamseth/oracledb_exporter
image.png

image.png

上传下面文件到部署的服务器(必须安装oracle客户端,这样才能连的上数据库,这里下载的是oracledb_exporter.linux-amd64,因为oracle客户端版本是12.0的。这里注意客户端版本必须和oracledb_exporter对应,否则oracledb_exporter无法启动服务)
[root@PWSNBUTEST ~]# ls -ltr
-rw-r--r-- 1 oracle dba 5502288 9月 5 13:57 oracledb_exporter.linux-amd64
[root@PWSNBUTEST ~]#chmod +x oracledb_exporter.linux-amd64
再设置执行的环境变量,命令行直接执行如下命令export(这里我们用的system用户):
export DATA_SOURCE_NAME=用户名/密码@数据库服务名

export DATA_SOURCE_NAME=user/password@//myhost:1521/service

如 export DATA_SOURCE_NAME=system/oracle@//ip:1521/testdb
后台启动服务:
[root@PWSNBUTEST ~]# cd /prometheus/
[root@PWSNBUTEST ~]# nohup ./oracledb_exporter &
查看nohup.out是否报错:
[root@PWSNBUTEST prometheus]# cat nohup.out
time="2020-06-29T15:39:46+08:00" level=info msg="Starting oracledb_exporter 0.0.5" source="main.go:394"
time="2020-06-29T15:39:47+08:00" level=info msg="Listening on :9161" source="main.go:402"
网页验证是否有数据:http://数据库ip:9161/

4.4、prometheus服务端prometheus.yml配置,并重启服务:

[root@prome /opt/prometheus]# vim prometheus.yml 
- job_name: 'oracledb'
    static_configs:
      - targets: ['oracle数据库端ip:9161']
[root@prometheus /opt/prometheus]# nohup ./prometheus --config.file=./prometheus.yml &

4.5、在Prometheus界面中查看Status--》Targets是否有节点,绿色的“up”证明数据已经被Prometheus所收集到。

image.png

5、服务端部署Grafana服务:

5.1、Prometheus服务器上下载grafana安装包:

[root@prometheus ~]#wget https://mirrors.tuna.tsinghua.edu.cn/grafana/yum/rpm/grafana-6.0.0-1.x86_64.rpm

5.2、安装并启动Grafana服务:

[root@prometheus ~]# mv grafana-6.0.0-1.x86_64.rpm  /opt/
[root@prometheus ~]#  cd /opt/
[root@prometheus /opt/]# rpm -ivh grafana-6.0.0-1.x86_64.rpm 
[root@prometheus ~]# systemctl start grafana-server
[root@prometheus ~]# systemctl enable  grafana-server

5.3、检查Grafana服务端口(Grafana服务端口3000):

[root@prometheus ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      7100/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      7332/master         
tcp6       0      0 :::9100                 :::*                    LISTEN      14514/./node_export 
tcp6       0      0 :::22                   :::*                    LISTEN      7100/sshd           
tcp6       0      0 :::3000                 :::*                    LISTEN      15019/grafana-serve 
tcp6       0      0 ::1:25                  :::*                    LISTEN      7332/master         
tcp6       0      0 :::9090                 :::*                    LISTEN      15144/./prometheus  
udp        0      0 127.0.0.1:323           0.0.0.0:*                           6448/chronyd        
udp6       0      0 ::1:323                 :::*                                6448/chronyd  

5.4、web访问Grafana服务([http://10.0.0.123:3000/],用户名:初始密码 admin/admin

):


image.png

6、让Grafana从Prometheus中拉取数据:

6.1、 添加一个data source ,基础配置如下:

image.png

image.png

image.png

6.2、 导入对node监控的DashBoard:

第一步、首先去网站 https://grafana.com/grafana/dashboards 下载对主机监控的Dashboard, 搜素 Node Exporter:

image.png

第二步:点开第一个 ,拷贝对应的Dashboard Id 8919
image.png

第三步:回到我们的图形监控平台Grafana,Dashboard ---> import
image.png

第四步:输入 dashboard ID 8919,选择数据源为prometheus即可,然后点击导入:
image.png

6.3、 导入对mysql监控的DashBoard:

第一步、首先去网站 https://grafana.com/grafana/dashboards 下载对主机监控的Dashboard, 搜索关键字 mysql overview .找到对应的dashboard id 7362:

image.png

image.png

第二步:回到我们的图形监控平台Grafana,Dashboard ---> import
image.png

第三步: 输入 dashboard ID 7362,选择数据源为prometheus即可
image.png

6.4、 导入对oracle监控的DashBoard:

第一步、首先去网站 https://grafana.com/grafana/dashboards 下载对主机监控的Dashboard, 搜索关键字 oracle:

image.png

第二步:寻找需要的模板,拷贝对应的Dashboard Id 3333或者11121
image.png

第三步:回到我们的图形监控平台Grafana,Dashboard ---> import
image.png

第四步: 输入 dashboard ID 3333,选择数据源为prometheus即可
image.png

7、到目前为止node和mysql的面板都导入成功,检查面板是否有数据:

7.1、查看面板导入情况:

image.png

7.2、查看数据:

image.png

image.png

image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335