Linuk安装Prometheus+grafana监控

news/2024/5/18 23:40:55 标签: prometheus, grafana

grafana_0">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

prometheus_37">1.prometheus监控框架工具介绍

prometheus是由谷歌研发的一款开源的监控软件,它通过安装在远程机器上的exporter,通过HTTP协议从远程的机器收集数据并存储在本地的时序数据库上

2.Prometheus 源码安装和启动配置

IP角色系统
192.168.91.128Prometheus 服务端CentOS 7
192.168.91.128node_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@615f028225c9
  build date:       20210217-14:17:24
  go version:       go1.15.8
  platform:         linux/amd64

prometheusyml__83">2.3默认prometheus.yml 配置解释

可以直接复制到prometheus.yml 中,没有影响

  • # my global config
    
    global:
    
      # 默认情况下,每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 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:
    
      # job名称会增加到拉取到的所有采样点上,同时还有一个instance目标服务的host:port标签也会增加到采样点上
    
      - job_name: 'prometheus'
    
        # 覆盖global的采样点,拉取时间间隔5s
    
        scrape_interval: 5s
        static_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-failure
  ExecStart=/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 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:
    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

在这里插入图片描述

prometheus_tomcat_345">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_total
      labels:
        port: "$2"
        protocol: "$1"
      help: Tomcat global $3
      type: 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_total
      labels:
        module: "$1"
        servlet: "$2"
      help: Tomcat servlet $3 total
      type: COUNTER
    - pattern: 'Catalina<type=ThreadPool, name="(\w+-\w+)-(\d+)"><>(currentThreadCount|currentThreadsBusy|keepAliveCount|pollerThreadCount|connectionCount):'
      name: tomcat_threadpool_$3
      labels:
        port: "$2"
        protocol: "$1"
      help: Tomcat threadpool $3
      type: 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_total
      labels:
        context: "$2"
        host: "$1"
      help: Tomcat session $3 total
      type: 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__429">第四步 修改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":""
    }
  }
]

prometheus_474">第5步重启prometheus

[root@VM_2-45~]# systemctl restart prometheus.service

第6步 Grafana 中配置 jvm 监控

导入 jvm1.json

在这里插入图片描述

导入 jvm2.json

在这里插入图片描述


http://www.niftyadmin.cn/n/5299241.html

相关文章

Pointnet++环境配置(Windows11和ubuntu)及训练教程

本文使用的是Pytorch版本的Pointnet代码&#xff0c;主要包含完整的环境配置及训练过程&#xff0c;包括在训练时遇到的坑。 目录 1.Windows11环境配置 2.Ubuntu环境配置 3.训练教程 3.1分类&#xff08;Classification&#xff09;训练 3.2零件分割&#xff08;Part Segme…

【Python】最佳Python代码片段,帮你实现工作自动化

一、发送带附件的电子邮件 import smtplib, ssl from email import encoders from email.mime.base import MIMEBase from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText sender_email = sender@gmail.com password = xxxxxxxx receiver_…

【进阶】【Python网络爬虫】【18.爬虫框架】scrapy深入,CrawlSpider全栈爬取(附大量案例代码)(建议收藏)

Python网络爬虫 一、Scrapy 深入案例 - qd_09_diaoyuitems.pymiddlewares.pypiplines.pysettings.pyspidersdiaoyu.pydiaoyu_manyitem.py 案例 - qd_10_liangliitems.pymiddlewares.pypipelines.pysettings.pyspidersliangli.pyliangli_extend.py 案例 - qd_11_duitangitems.py…

neovim调试xv6-riscv过程中索引不到对应头文件问题

大家好&#xff0c;我叫徐锦桐&#xff0c;个人博客地址为www.xujintong.com&#xff0c;github地址为https://github.com/jintongxu。平时记录一下学习计算机过程中获取的知识&#xff0c;还有日常折腾的经验&#xff0c;欢迎大家访问。 和这篇文章neovim调试linux内核过程中索…

深度学习——PIL和OpenCV

PIL 官方文档 格式互转 opencv cv2.imread() 参数&#xff1a; filepath&#xff1a;读入imge的完整路径 flags&#xff1a;标志位&#xff0c;{cv2.IMREAD_COLOR&#xff0c;cv2.IMREAD_GRAYSCALE&#xff0c;cv2.IMREAD_UNCHANGED} cv2.IMREAD_COLOR&#xff1a;默认参数&…

[C语言]时间戳

时间戳的概念 时间戳就是定义一个时间点作为0秒, 之后每过一秒依此加一, 将当前的时间戳换算成年月日, 再加上起点, 获得的就是现在时刻的时间. 根据地球时区的偏移, 比如北京时间是东八区, 做一个偏移量的加减. 0起点: 1900年1月1日0时0分0秒. 0偏移地点: 英国伦敦 时间戳…

LLM、AGI、多模态AI 篇三:微调模型

文章目录 系列LLM的几个应用层次Lora技术指令设计构建高质量的数据微调步骤系列 LLM、AGI、多模态AI 篇一:开源大语言模型简记 LLM、AGI、多模态AI 篇二:Prompt编写技巧 LLM、AGI、多模态AI 篇三:微调模型 LLM的几个应用层次 AI 端到端应用。是直接面向最终用户的应用程序…

MockServer简单使用记录

下载源码 下载git源码&#xff1a;git clone https://github.com/mock-server/mockserver.git 通过执行文件编译成jar包 ./mvnw clean package 可能会报错。 启动命令 java -jar ./mockserver-netty-jar-with-dependencies.jar -serverPort 1080 -proxyRemotePort 80 -pro…