prometheus 监控学习

什么是 prometheus

1. 简单介绍

Prometheus is an open-source systems monitoring and alerting toolkit originally built at SoundCloud. Since its inception in 2012, many companies and organizations have adopted Prometheus, and the project has a very active developer and user community. It is now a standalone open source project and maintained independently of any company. To emphasize this, and to clarify the project's governance structure, Prometheus joined the Cloud Native Computing Foundation in 2016 as the second hosted project, after Kubernetes.html

Prometheus是一个开源监控系统,它前身是SoundCloud的警告工具包。从2012年开始,许多公司和组织开始使用Prometheus。该项目的开发人员和用户社区很是活跃,愈来愈多的开发人员和用户参与到该项目中。目前它是一个独立的开源项目,且不依赖与任何公司。 为了强调这点和明确该项目治理结构,Prometheus在2016年继Kurberntes以后,加入了Cloud Native Computing Foundation。node

结构图

2. 特色

  • 多维度数据模型
  • 灵活的查询语言
  • 不依赖分布式存储,单个服务器节点是自主的
  • 以HTTP方式,经过pull模型拉去时间序列数据
  • 也经过中间网关支持push模型
  • 经过服务发现或者静态配置,来发现目标服务对象
  • 支持多种多样的图表和界面展现,grafana也支持它

和zabbix的对比


prometheus 的安装

1. 预编译后的二进制压缩文件安装

下载源码 解压安装 tar -xzvf prohetheus-xxx.tar.gz 进入解压后的目录运行修改配置文件: prometheus.yml 命令行运行: ./prometheus --config.file=prometheus.ymlmysql

运行时控制台可能会报: transport: http2Client.notifyError got notified that the client transport was broken unexpected EOF。 官方issue给出的解决 修改bash配置文件后重启就能生效。git

2. Docker镜像安装

3. 源码安装(须要go环境)

4. 三方配置管理系统


prometheus 的使用

1. 组件介绍

prometheus的监控服务和报警服务都是以组件和配置的形式进行的。要使客户端被监控,须要安装相应的组件来获取监控数据或是去本身去实现prometheus的数据接口,并将监控数据传递给监控服务器。 在没有特殊需求的状况下选择官方提供的官方组件或是官方推荐的三方组件库,使用起来更便捷稳定性也有保障。github

  • alertmanager 报警组件,接收prometheus发送的报警数据,并依据配置的报警方式(email,hipchat,webhook,wechat等)发送报警信息。
  • node_exporter 节点监控组件(官方提供的用于监控硬件的核心组件),运行在机器上后,能收集机器的硬件信息,网络信息等
  • pushgateway 数据发送的代理组件,官方建议用在监控短时间任务的监控数据收发:任务将数据发送给pushgateway,prometheus从pushgateway获取监控数据
  • blackbox_exporter 黑盒监控组件,主要用于监控网络是否通畅,服务是否可用:示例
  • mysqld_exporter mysql官方提供的监控组件,用于收集mysql的监控数据,供prometheus服务器获取。不少软件都提供了用于获取本身数据的监控组件来供prometheus获取数据,咱们要作的只是将组件配置好而后运行起来收集数据就行。
  • grafana 不属于prometueus的组件,主要用户展现监控数据。原生的prometheus界面展现效果不太好,目前官方已经弃用,而改用展现效果更好的grafana,*2.5.0 (2015-10-28)*以及以后版本的ganfana能很好的支持prometheus。

2. prometheus的配置

因 prmetheus 是进行组件化管理,组件组合全靠配置文件来完成,这里不罗列如何进行具体配置了,详情请见 premetheus配置 配置被监控对象 配置报警组件 安装配置grafana 安装grafana: 连接地址web


3. 监控数据管理

数据保存位置管理官方文档 本地存储的,在启动时指定存储位置: ./prometheus --storage.tsdb.path=somePath 远程存储的,在配置文件中指定 数据保存时间管理(默认15天),需注意服务器的磁盘是否能存储下监控数据文件,远程访问的,须要注意prometheus是否有对远程文件的读写权限 启动时指定: ./prometheus --storage.tsdb.retention=15dsql


4. 环境搭建示例

prometheus + grafana + node + alertshell

下载压缩软件包:

在prometheus官网下载最新版的 prometheus, node_exporter, alertmanager 在grafana官网上下载最新版本的grafana的压缩文件 vim

prometheus
alertmanager
node_exporter
grafana
将各个文件解压.


将node_exporter配置到监控服务器中

进入 prometheus 解压目录,vim prometheus.yml,加入以下设置:api

scrape_configs:
 # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['127.0.0.1:9090']

  - job_name: 'node' # 将node添加到监控中
    static_configs:
      - targets: ['127.0.0.1:9100']
复制代码

启动 node_exporter: 到 node_exporter 解压目录下,执行: ./node_exporter 启动成功后打开浏览器,输入: http://127.0.0.1:9100/metrics 成功后会有以下显示:

显示


启动prometheus: 到 prometheus 解压目录下,执行: ./prometheus --config.file=prometheus.yml 启动成功后在浏览器输入: http://127.0.0.1:9090/graph 成功后会显示:

prometheus界面
点击 Status -> Targets, 若是能看到当前有两个正在被监控的程序,且都处于 UP状态,则配置成功
prothemeus-targets
pro_targets


为 prometheus 添加报警

配置文件处理

  • 到 alertmanager 解压目录下, vim alertmanager.yml 中加入:
global:
 # The smarthost and SMTP sender used for mail notifications.
  smtp_smarthost: 'you email host:587'
  smtp_from: 'email_name@qq.com'
  smtp_auth_username: 'email_name@qq.com'
  smtp_auth_password: 'email_password'
  resolve_timeout: 5m

route:
  group_by: ['alertname']
  group_wait: 10s
  group_interval: 10s
  repeat_interval: 1h
  receiver: 'web.hook'
receivers:
- name: 'web.hook'
  email_configs:
  - to: 'receive alert email account'
inhibit_rules:
  - source_match:
      severity: 'critical'
    target_match:
      severity: 'warning'
    equal: ['alertname', 'dev', 'instance']
复制代码
  • 到 prometheus 解压目录下, 新建文件 alert.rules vim alert.rules 在文件中加入:
groups:
- name: example
  rules:
 # Alert for any instance that is unreachable for >5 minutes.
  - alert: InstanceDown
    expr: up == 0
    for: 5m
    labels:
      severity: page
    annotations:
      summary: "Instance {{ $labels.instance }} down"
      description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes."
 # Alert for any instance that has a median request latency >1s.
  - alert: APIHighRequestLatency
    expr: api_http_request_latencies_second{quantile="0.5"} > 1
    for: 10m
    annotations:
      summary: "High request latency on {{ $labels.instance }}"
      description: "{{ $labels.instance }} has a median request latency above 1s (current value: {{ $value }}s)"
复制代码
  • 将报警规则和报警组件配置到 prometheus 中: vim prometheus.yml,加入以下设置:
rule_files:
  - "test_alert.rules"
 # Alerting specifies settings related to the Alertmanager.
alerting:
  alertmanagers:
    - static_configs:
      - targets: ['127.0.0.1:9093']
复制代码

启动 alertmanager 到 alertmanager 解压目录下,输入 ./alertmanager --config.file=alertmanager.yml 启动成功后在浏览器输入: http://127.0.0.1:9093/#/alerts 看到以下界面,代表启动成功:

image

重启 premotheus(ctrl + c关掉后按上面的命令重启就行)


加入 grafana 美化输出界面

  • 启动grafana 到 grafana 解压目录下输入: ./bin/grafana-server web 启动程序 浏览器中输入: http://localhost:3000 用户名: admin 密码: admin 直接跳过修改密码进入界面
  • 将 prometheus 配置到 DataSource 中:
    grafana_data_source
  • 添加模板: 点击 + -> import
    image

将模板id添加到下面的框中,点击load就能够看到效果了,找模板点这里:grafana官网模板库

grafana_dashborad

load 成功后须要指定模板用在哪一个数据源上(勾选刚才配置的数据源)

image

勾选完成后能够看到 import 能点了,点击后正式进入监控页面:

image
至此简单使用就完成了

[其余的配置案例]www.cnblogs.com/iiiiher/p/8…

相关文章
相关标签/搜索