prometheus使用Alertmanager预警(邮件)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21398167/article/details/76008594

1.   Prometheus 安装

源码安装:

# wget https://github.com/prometheus/prometheus/releases/download/v1.7.1/prometheus-1.7.1.linux-amd64.tar.gz

# tar zxvf prometheus-1.7.1.linux-amd64.tar.gz

# cd prometheus-1.7.1.linux-amd6

# ./prometheus -config.file=prometheus.yml

当然你还想知道更多的参数  可以使用  ./prometheus–help

容器方式安装:

1).安装容器

yum install docker

systemctl enable docker

systemctl start docker

2).创建目录和prometheus配置文件

mkdir /prom

vi /prom/ Prometheus.yml

global:

  scrape_interval:     30s

  evaluation_interval: 30s

scrape_configs:

  - job_name: prometheus

    static_configs:

      - targets: ['122.15.3.29:9090']

        labels:

          instance: prometheus

3).拉取prometheus镜像

docker pull quay.io/prometheus/prometheus

4).启动prometheus

docker run -d \

  -p 9090:9090 \

  --name prometheus \

  -v /prom/prometheus.yml:/etc/prometheus/prometheus.yml\

  quay.io/prometheus/prometheus \

  -config.file=/etc/prometheus/prometheus.yml \

  -storage.local.path=/prometheus \

  -storage.local.memory-chunks=10000

5).参数介绍

-d选项启动在独立模式下的Prometheus的容器,这意味着容器将在后台启动,并不会按终止CTRL+C

–name选项给定容器一个名字。

-p 9090:9090的选项公开Prometheusweb端口(9090),并使其通过主机系统的外部IP地址访问。

-v […]选项挂载prometheus.yml从主机文件系统的配置文件到哪里Prometheus希望它(容器内的位置/etc/prometheus/prometheus.yml)。

-config.file选项相应地设置到Prometheus配置文件的位置,在容器内

所述-storage.local.path选项配置在容器内的指标的存储位置。

最后, -storage.local.memory-chunks选项调整Prometheus的内存使用主机系统的非常小的RAM(只有512MB)和少量的储存时间序列的量在本教程中(略低于1000)。它指示Prometheus只保留10000样品块内存(每列约10块),而不是1048576这个默认的是更多的内存的机器上运行时的Prometheus和储存

6).web界面展示

http://monitor_host:9090

要预警,首先得有监控数据,这里就简单的采集prometheus自身的数据,配置如上容器启动方式中,官方提供很多监控其它数据源的exporter 和直连的

https://prometheus.io/docs/instrumenting/exporters/

2.   Alertmanager安装

源码安装:

$ mkdir -p $GOPATH/src/github.com/prometheus

$ cd $GOPATH/src/github.com/prometheus

$ git clone https://github.com/prometheus/alertmanager.git

$ cd alertmanager

$ make build

$ ./alertmanager-config.file= alertmanager.yml

配置邮箱发送的简单例子alertmanager.yml

global:

 resolve_timeout: 5m

 # The smarthost and SMTP sender used for mail notifications.

 smtp_smarthost: smtp.gmail.com:587  #发送邮箱服务器smtp.163.com:25163的)

 smtp_from: [email protected]   #发送邮箱地址

 smtp_auth_username: [email protected]   #邮箱用户名

 smtp_auth_password: sender_password    #密码

 

route:

 group_by: [alertname]

 repeat_interval: 1h

 receiver: live-monitoring

 

receivers:

- name: live-monitoring

 email_configs:

 - to: [email protected] #接收邮箱地址

这样alertmanager 就算准备好了,在启动prometheus的时候,加入即可,例:

./prometheus -config.file=prometheus.yml -alertmanager.url http://localhost:9095,http://localhost:9094,http://localhost:9093

可以加入多个,高可用

3.   在prometheus 中配置规则,让其生效并发送邮件

Prometheus.yml 加入

rule_files:

  - "prometheus/rules/test.rules"

在 test.rules 加入一条简单的指标测试

# Alert for any instance that have a medianrequest latency >1s.

ALERT APIHighRequestLatency

  IF api_http_request_latencies_second{quantile="0.5"} >1

  FOR 1m

  ANNOTATIONS {

    summary = "High request latency on {{$labels.instance }}",

    description = "{{ $labels.instance }} has amedian request latency above 1s (current value: {{ $value }}s)",

  }

重启prometheus,等待一会儿,就可以收到报警邮件了

猜你喜欢

转载自blog.csdn.net/qq_21398167/article/details/76008594