Linuk安装Prometheus+grafana监控
文章目录
- Linuk安装Prometheus+grafana监控
- 服务器环境配置
- 1.prometheus监控框架工具介绍
- 2.Prometheus 源码安装和启动配置
- 2.1 下载
- 2.2安装
- 2.3默认prometheus.yml 配置解释
- 2.4直接启动服务
- 2.5 访问http://localhost:9090
- 2.6将Prometheus配置为系统服务
- 3.客户端安装node_exporter
- 3.1下载
- 3.2安装
- 3.3直接启动
- 3.4 访问http://localhost:9100
- 3.5将node_exporter配置为系统服务
- 4.Prometheus 监控node_exporter
- 5.Grafana 展示 Prometheus 数据
- 5.1.1下载Grafana
- 5.1.2启动
- 5.1.3访问http://localhost:3000
- 5.1.4 添加Prometheus数据源
- 5.15新增Dashboard Linux基础数据展示
- 6.prometheus 监控tomcat
- 第一步下载和配置
- 第二步修改tomcat的启动文件
- 第三步启动tomcat
- 第四步 修改prometheus 配置文件
- 第5步重启prometheus
- 第6步 Grafana 中配置 jvm 监控
服务器环境配置
防火墙添加端口:
防火墙添加端口(永久):firewall-cmd --zone=public --add-port=端口号/tcp --permanent
重启防火墙:systemctl restart firewalld.service
查看防火墙已添加的端口:firewall-cmd --zone=public --list-ports
可以关闭运行的防火墙 systemctl stop firewalld.service
关闭后再使用命令systemctl status firewalld.service查看防火墙状态,如果出现disavtive(dead)的字样,说明防火墙已经关闭
查看端口号:
lsof -i: 端口号
杀死进程:
kill -9 pid 终止进程号为pid的进程
grafana模板:
https://grafana.com/grafana/dashboards 搜索 相应 dashboards的id如 **8919,12227**
参考资料:
https://blog.csdn.net/qq_31725371/article/details/114697770
1.prometheus监控框架工具介绍
prometheus是由谷歌研发的一款开源的监控软件,它通过安装在远程机器上的exporter,通过HTTP协议从远程的机器收集数据并存储在本地的时序数据库上
2.Prometheus 源码安装和启动配置
IP | 角色 | 系统 |
---|---|---|
192.168.91.128 | Prometheus 服务端 | CentOS 7 |
192.168.91.128 | node_exporter 客户端 | CentOS 7 |
普罗米修斯下载网址:https://prometheus.io/download/
监控集成器下载地址:http://www.coderdocument.com/docs/prometheus/v2.14/instrumenting/exporters_and_integrations.html
2.1 下载
访问: https://github.com/prometheus/prometheus/releases/download/v2.25.0/prometheus-2.25.0.linux-amd64.tar.gz下载prometheus-2.25.0.linux-amd64.tar.gz
2.2安装
[root@VM_2-45 ~]# cd /usr/local 切换目录
[root@VM_2-45 local]# tar xf prometheus-2.25.0.linux-amd64.tar.gz 解压
[root@VM_2-45 local]# mv prometheus-2.25.0.linux-amd64/ prometheus 修改文件名
安装成功后可以查看安装版本
[root@VM_2-45 /usr/local/prometheus]# ./prometheus --version
显示如下:prometheus, version 2.25.0 (branch: HEAD, revision: a6be548dbc17780d562a39c0e4bd0bd4c00ad6e2)build user: root@615f028225c9build date: 20210217-14:17:24go version: go1.15.8platform: linux/amd64
2.3默认prometheus.yml 配置解释
可以直接复制到prometheus.yml 中,没有影响
-
# my global configglobal:# 默认情况下,每15s拉取一次目标采样点数据。scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.# 每15秒评估一次规则。默认值为每1分钟。evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.# scrape_timeout is set to the global default (10s).# Alertmanager configurationalerting: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:# job名称会增加到拉取到的所有采样点上,同时还有一个instance目标服务的host:port标签也会增加到采样点上- job_name: 'prometheus'# 覆盖global的采样点,拉取时间间隔5sscrape_interval: 5sstatic_configs:- targets: ['localhost:9090']
2.4直接启动服务
[root@VM_2-45 /usr/local/prometheus]# ./prometheus --config.file=prometheus.yml
2.5 访问http://localhost:9090
点击targets[目标] 会看到红色框的内容
2.6将Prometheus配置为系统服务
1.进入systemd目录下:cd /usr/lib/systemd/system
[root@VM_2-45 ~]# cd /usr/lib/systemd/system
2.创建文件:vim prometheus.service
[Unit]Description=https://prometheus.io[Service]Restart=on-failureExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --web.listen-address=:9090[Install] WantedBy=multi-user.target
3.生效系统system文件
systemctl daemon-reload
4.启动服务
[root@VM_2-45 ~]# systemctl start prometheus
3.客户端安装node_exporter
3.1下载
访问 https://github.com/prometheus/node_exporter/releases/download/v1.1.2/node_exporter-1.1.2.linux-amd64.tar.gz下载node_exporter-1.1.2.linux-amd64.tar.gz
3.2安装
[root@VM_2-45 ~]# cd /usr/local
[root@VM_2-44 local]# tar xf node_exporter-1.1.2.linux-amd64.tar.gz
[root@VM_2-44 local]# mv node_exporter-1.1.2.linux-amd64/ node_exporter
3.3直接启动
[root@VM_2-44 /usr/local/node_exporter]# ./node_exporter
3.4 访问http://localhost:9100
3.5将node_exporter配置为系统服务
1.进入systemd目录下:cd /usr/lib/systemd/system
[root@VM_2-45 ~]# cd /usr/lib/systemd/system
2.创建文件:vim node_exporter.service
[Unit]
Description=node_exporter
After=network.target [Service]
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure[Install]
WantedBy=multi-user.target
3.生效系统system文件
systemctl daemon-reload
4.启动服务
systemctl start node_exporter
4.Prometheus 监控node_exporter
修改配置文件添加监控项
-
[root@VM_2-45 /usr/local/prometheus]# cat prometheus.yml # my global configglobal: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 configurationalerting:alertmanagers:- static_configs:- targets:# - alertmanager:9093# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.rule_files: scrape_configs:- job_name: 'prometheus'static_configs:- targets: ['192.168.91.128:9090'] # 这个地方为默认的 也可以为 localhost # 这个地方为新增的, 注意此文件的空格问题,和换行问题,如果启动失效,将其粘贴到txt中,清楚样式 删掉此处空格和注释即可- job_name: 'linux'static_configs:- targets: ['192.168.91.128:9100','192.168.91.129:9100'] # 多个用,分开
重启Prometheus
[root@VM_2-45 /usr/local/prometheus]# systemctl restart prometheus.service
打开Prometheus 自带的监控页面,Status -> Targets 查看:
5.Grafana 展示 Prometheus 数据
5.1.1下载Grafana
访问https://mirrors.tuna.tsinghua.edu.cn/grafana/yum/rpm/grafana-7.4.3-1.x86_64.rpm下载grafana-7.4.3-1.x86_64.rpm
yum install -y grafana-7.4.3-1.x86_64.rpm
5.1.2启动
[root@VM_2-45 ~]# systemctl start grafana-server
5.1.3访问http://localhost:3000
初始用户名和密码都是admin
5.1.4 添加Prometheus数据源
Configuration -> Data Sources ->add data source -> Prometheus
5.15新增Dashboard Linux基础数据展示
导入模板liunk-node-exporter-for-prometheus.json
grafana模板: https://grafana.com/grafana/dashboards 搜索 相应 dashboards的id如 8919,12227
6.prometheus 监控tomcat
暗转Tomcat 和JDk
配置Tomcat 指定jdk 启动 ,再 catalina.sh 头部 添加
#!/bin/sh
export JAVA_HOME=/root/soft/jdk1.8.0_331
export JRE_HOME=/root/soft/jdk1.8.0_331/jre
参考资料:
https://blog.csdn.net/weixin_43560924/article/details/120881722
jar包下载地址: https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.3.1/jmx_prometheus_javaagent-0.3.1.jar
第一步下载和配置
1)下载 jmx_prometheus_javaagent-0.3.1.jar jar包
2)切换到 Tomcat bin 目录下边
3)新建一个配置文件,命名config.yaml
vim config.yaml
-
lowercaseOutputLabelNames: true lowercaseOutputName: true rules:- pattern: 'Catalina<type=GlobalRequestProcessor, name=\"(\w+-\w+)-(\d+)\"><>(\w+):'name: tomcat_$3_totallabels:port: "$2"protocol: "$1"help: Tomcat global $3type: COUNTER - pattern: 'Catalina<j2eeType=Servlet, WebModule=//([-a-zA-Z0-9+&@#/%?=~_|!:.,;]*[-a-zA-Z0-9+&@#/%=~_|]), name=([-a-zA-Z0-9+/$%~_-|!.]*), J2EEApplication=none, J2EEServer=none><>(requestCount|maxTime|processingTime|errorCount):'name: tomcat_servlet_$3_totallabels:module: "$1"servlet: "$2"help: Tomcat servlet $3 totaltype: COUNTER - pattern: 'Catalina<type=ThreadPool, name="(\w+-\w+)-(\d+)"><>(currentThreadCount|currentThreadsBusy|keepAliveCount|pollerThreadCount|connectionCount):'name: tomcat_threadpool_$3labels:port: "$2"protocol: "$1"help: Tomcat threadpool $3type: GAUGE - pattern: 'Catalina<type=Manager, host=([-a-zA-Z0-9+&@#/%?=~_|!:.,;]*[-a-zA-Z0-9+&@#/%=~_|]), context=([-a-zA-Z0-9+/$%~_-|!.]*)><>(processingTime|sessionCounter|rejectedSessions|expiredSessions):'name: tomcat_session_$3_totallabels:context: "$2"host: "$1"help: Tomcat session $3 totaltype: COUNTER - pattern: ".*" #让所有的jmx metrics全部暴露出来
第二步修改tomcat的启动文件
修改tomcat的启动文件 ,修改bin目录下的catalina.sh文件 此端口可以自定义30018
JAVA_OPTS="-javaagent:/usr/local/apache-tomcat-8.5.78/bin/jmx_prometheus_javaagent-0.15.0.jar=30018:/usr/local/apache-tomcat-8.5.78/bin/config.yaml"
第三步启动tomcat
重启tomcat,访问服务器上的30018 端口,查看是否采集到数据,也可以浏览器直接访问ip+端口
http://localhost:30018
第四步 修改prometheus 配置文件
prometheus.yml中增加
-
-
job_name: 'jmx_exporter' scheme: http tls_config:insecure_skip_verify: true file_sd_configs:- files:- jmx_exporter.json
-
创建 jmx_exporter.json
[root@localhost prometheus]# pwd
/usr/local/prometheus
[root@localhost prometheus]# vim jmx_exporter.json
[{"targets": [ "192.168.91.128:30018"],"labels": {"job": "work1","monitor_host": "192.168.91.128","monitor_port": "30018","monitor_type": "tomcat8","monitor_cluster":""}}
]
第5步重启prometheus
[root@VM_2-45~]# systemctl restart prometheus.service
第6步 Grafana 中配置 jvm 监控
导入 jvm1.json
导入 jvm2.json