Prometheus初体验

什么是监控系统?

监控系统是IT系统的眼睛和耳朵,去看去发现IT系统的状态和健康情况。

当下复杂的IT构架下,监控涉及到技术的各个层面。

监控这么复杂,Prometheus能干什么?

Prometheus是一套开源的分布式业务监控解决方案,采用时间序数据库。

可视为Google内部监控系统Borgmon的非官方实现,起源于2012年、2016年被CNCF收录(紧跟Kubernetes)。

由于Prometheus默认采用Agent方式读取数据,一般用于上图中的"服务端监控"部分。

为什么要采用Prometheus监控?

传统的开源监控系统代表有:Cacti、Nagios、Zabbix等

先进的开源时间序监控系统代表有:OpenTSDB、Open-falcon、Prometheus等

过去我们选用Zabbix监控,面临最大的难点是对容器监控需求以及定制需求的维护成本高。

而Prometheus对传统监控的难点天生支持好,而且提供了强大的PromQL查询语言,很容易实现传统监控系统几乎不可能完成的任务。

怎样开始使用Prometheus?

Prometheus使用Go语言开发,可以直接下载官方提供的二进制包,解压后稍加配置运行即可。

服务端运行示例:

下载安装包(以最新linux版本的安装包2.0.0为例)

wget -O /data/file/prometheus-2.0.0.linux-amd64.tar.gz https://github.com/prometheus/prometheus/releases/download/v2.0.0/prometheus-2.0.0.linux-amd64.tar.gz 
tar -C /usr/local/ -zxvf /data/file/prometheus-2.0.0.linux-amd64.tar.gz
mkdir -pv /etc/prometheus/
mv -fv /usr/local/prometheus-2.0.0.linux-amd64 /usr/local/prometheus
ln -s /usr/local/prometheus/prometheus /usr/sbin/prometheus
cp -fv /usr/local/prometheus/prometheus.yml /etc/prometheus/

测试启动

/usr/sbin/prometheus --config.file=/etc/prometheus/prometheus.yml

默认配置 (/etc/prometheus/prometheus.yml)

global:
  scrape_interval:     15s # 默认 15秒到目标处抓取数据

  # 这个标签是在本机上每一条时间序列上都会默认产生的,主要可以用于 联合查询、远程存储、Alertmanger时使用。
  external_labels:
    monitor: 'codelab-monitor'

# 这里就表示抓取对象的配置
# 设置抓取自身数据
scrape_configs:
  #  job name 这个配置是表示在这个配置内的时间序例,每一条都会自动添加上这个{job_name:"prometheus"}的标签。
  - job_name: 'prometheus'

    # 重写了全局抓取间隔时间,由15秒重写成5秒。
    scrape_interval: 5s

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

通过访问localhost:9090来查看是否启动成功,设置systemd自启动服务脚本:

vim /etc/systemd/system/prometheus.service
 
[Unit]
Description=Prometheus


[Service]
LimitCORE=infinity
LimitNOFILE=100000
LimitNPROC=100000
User=root
ExecStart=/usr/sbin/prometheus  -log.level warn  -config.file=/etc/prometheus.yml -alertmanager.url http://192.168.22.33:9093
# 其中 192.168.22.33 是Prometheus服务端的IP地址

[Install]
WantedBy=default.target

设置完成后,需要手动刷新服务信息:

systemctl daemon-reload

将 prometheus 设置为开机自启动:

systemctl enable prometheus

基础监控的配置介绍

默认配置(/etc/prometheus/prometheus.yml):

global:
  scrape_interval:     15s # 默认 15秒到目标处抓取数据

  # 这个标签是在本机上每一条时间序列上都会默认产生的,主要可以用于 联合查询、远程存储、Alertmanger时使用。
  external_labels:
    monitor: 'codelab-monitor'

# 这里就表示抓取对象的配置
# 设置抓取自身数据
scrape_configs:
  #  job name 这个配置是表示在这个配置内的时间序例,每一条都会自动添加上这个{job_name:"prometheus"}的标签。
  - job_name: 'prometheus'

    # 重写了全局抓取间隔时间,由15秒重写成5秒。
    scrape_interval: 5s

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

配合Grafana做图表展示

(关于grafana的安装与配置这里就不再展开了,请查看grafana的官方文档http://docs.grafana.org/ )

自定义监控BlackBox配置示例

  • BlackBox 是Prometheus的一套第3方插件,用于监控网页/接口的可用性(类似监控宝的http监控功能)。 ##服务端运行示例:

下载安装包(以最新linux版本的安装包0.11.0为例)

wget -O /data/file/blackbox_exporter-0.11.0.linux-amd64.tar.gz https://github.com/prometheus/blackbox_exporter/releases/download/v0.11.0/blackbox_exporter-0.11.0.linux-amd64.tar.gz
tar -P /usr/local/ -zxvf /data/file/blackbox_exporter-0.11.0.linux-amd64.tar.gz
mkdir -pv /etc/blackbox_exporter/
mv -fv /usr/local/blackbox_exporter-0.11.0.linux-amd64 /usr/local/blackbox_exporter
ln -s /usr/local/blackbox_exporter/blackbox_exporter /usr/sbin/blackbox_exporter
cp -fv /usr/local/prometheus/blackbox_exporter.yml /etc/prometheus/

测试启动

/usr/sbin/blackbox_exporter--config.file=/usr/local/blackbox_exporter/blackbox.yml--web.listen-address=0.0.0.0:9115

设置systemd自启动服务脚本,设置完成后systemctldaemon-reload完成服务加载,systemctlenableprometheus.service设置开机启动

vim /etc/systemd/system/prometheus.service
[Unit]
Description=Blackbox Exporter
[Service]
User=root
ExecStart=/usr/sbin/blackbox_exporter --config.file=/usr/local/blackbox_exporter/blackbox.yml --web.listen-address=0.0.0.0:9115
[Install]
WantedBy=default.target

blackbox服务端的配置文件:

默认配置(/etc/blackbox_exporter/blackbox_exporter.yml)

modules:
  #自定义的监控接口检测
  dm_flash_check:
    prober: tcp
    timeout: 5s
    tcp:
      query_response:
        - send: "<policy-file-request/>"
        - expect: '^<\?xml version="1.0"\?>.*'
  #http 2xx的监控接口配置
  http_2xx:
    prober: http
    timeout: 5s
    http:
  #http 2xx post的监控接口配置
  http_post_2xx:
    prober: http
    timeout: 5s
    http:
      method: POST
  #http 200和302的监控接口配置
  http_2xx_or_3xx:
    prober: http
    timeout: 10s
    http:
      valid_status_codes:
        - 302
        - 200
      tls_config:
        #跳过ssl
        insecure_skip_verify: true
  http_2xx_or_3xx_1s:
    prober: http
    timeout: 1s
    http:
      valid_status_codes:
        - 302
        - 200
  #tcp接口的配置
  tcp_connect:
    prober: tcp
    timeout: 5s
  #邮件pop3监控的接口
  pop3s_banner:
    prober: tcp
    tcp:
      query_response:
      - expect: "^+OK"
      tls: true
      tls_config:
        insecure_skip_verify: false
  #ssh监控的接口
  ssh_banner:
    prober: tcp
    timeout: 5s
    tcp:
      query_response:
      - expect: "^SSH-2.0-"
  irc_banner:
    prober: tcp
    timeout: 5s
    tcp:
      query_response:
      - send: "NICK prober"
      - send: "USER prober prober prober :prober"
      - expect: "PING :([^ ]+)"
        send: "PONG ${1}"
      - expect: "^:[^ ]+ 001"
  #icmp监控的接口
  icmp:
    prober: icmp
    timeout: 5s

配置启动后,直接在prometheus服务端配置blackbox收集module的配置

- job_name: 'outside_blackbox'
    metrics_path: /probe
    params:
      # 加载的模块
      module: [http_2xx_or_3xx_1s]  # Look for a HTTP 200 response.
    file_sd_configs:
      - files:
	     # 需要监控的客户端的配置
         - "/etc/prometheus/targets/node/outside_check.json" 
    # 将监控项的标签重命名
    relabel_configs:    
      - source_labels: [__address__]
        regex: (.*)
        target_label: __param_target
        replacement: ${1}
      - source_labels: [__param_target]
        regex: (.*)
        target_label: instance
        replacement: ${1}
      - source_labels: []
        regex: .*
        target_label: __address__
        replacement: 10.23.194.197:9115

根据需求来填写需要监控页面或者服务器的信息

[
{
        "labels": {
            "job": "outside_check",
            "pro": "itserver",
            "name": "办公网是否通外网"
        },
        "targets": [
            "http://www.baidu.com/"
        ]
    }
]

猜你喜欢

转载自my.oschina.net/higkoo/blog/1635836