Centos 7 部署 Prometheus、Alertmanager、Grafana 监控 Linux 主机

1、部署架构图


2、安装配置
2.1 下载安装包(如果下载慢,可以挂代理下载)

[root@ops001 ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.23.253  netmask 255.255.240.0  broadcast 172.18.31.255
        ether 00:16:3e:08:aa:35  txqueuelen 1000  (Ethernet)
        RX packets 157447  bytes 200080723 (190.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 65973  bytes 15198101 (14.4 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@ops001 ~]# wget https://github.com/prometheus/prometheus/releases/download/v2.4.3/prometheus-2.4.3.linux-amd64.tar.gz
[root@ops001 ~]# wget https://github.com/prometheus/alertmanager/releases/download/v0.15.2/alertmanager-0.15.2.linux-amd64.tar.gz
[root@ops001 ~]# wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-5.2.4.linux-amd64.tar.gz
[root@ops001 ~]# wget https://github.com/prometheus/node_exporter/releases/download/v0.16.0/node_exporter-0.16.0.linux-amd64.tar.gz

2.2 安装 prometheus(注意修改参数 --web.external-url= 的地址)

[root@ops001 ~]# tar xvf prometheus-2.4.3.linux-amd64.tar.gz -C /usr/local/
[root@ops001 ~]# vim /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io/docs/introduction/overview/
After=network-online.target

[Service]
Restart=on-failure
ExecStart=/usr/local/prometheus-2.4.3.linux-amd64/prometheus --config.file=/usr/local/prometheus-2.4.3.linux-amd64/prometheus.yml --storage.tsdb.path=/var/lib/prometheus --web.external-url=http://172.18.23.253:9090

[Install]
WantedBy=multi-user.target

[root@ops001  ~]# systemctl daemon-reload
[root@ops001  ~]# systemctl start prometheus
// 如果启动有问题,可以通过  /var/log/messages 查看报错原因
[root@ops001  ~]# systemctl status prometheus
[root@ops001  ~]# netstat -nltup|grep 9090
tcp6       0      0 :::9090                 :::*                    LISTEN      1352/prometheus

2.3 安装 Alertmanager

[root@ops001 ~]# tar xvf alertmanager-0.15.2.linux-amd64.tar.gz -C /usr/local/
[root@ops001 ~]# vim /etc/systemd/system/alertmanager.service
[Unit]
Description=Alertmanager
After=network-online.target

[Service]
Restart=on-failure
ExecStart=/usr/local/alertmanager-0.15.2.linux-amd64/alertmanager --config.file=/usr/local/alertmanager-0.15.2.linux-amd64/alertmanager.yml

[Install]
WantedBy=multi-user.target

[root@ops001  ~]# systemctl daemon-reload
[root@ops001  ~]# systemctl start alertmanager
[root@ops001  ~]# systemctl status alertmanager
[root@ops001  ~]# netstat -nltup|grep 9093
tcp6       0      0 :::9093                 :::*                    LISTEN      10597/alertmanager

2.4 安装 grafana

[root@ops001 ~]# tar xvf grafana-5.2.4.linux-amd64.tar.gz -C /usr/local/
[root@ops001 ~]# vim /etc/systemd/system/grafana-server.service
[Unit]
Description=Grafana Server
After=network-online.target

[Service]
Restart=on-failure
ExecStart=/usr/local/grafana-5.2.4/bin/grafana-server -config /usr/local/grafana-5.2.4/conf/defaults.ini -homepath /usr/local/grafana-5.2.4

[Install]
WantedBy=multi-user.target

[root@ops001  ~]# systemctl daemon-reload
[root@ops001  ~]# systemctl start grafana-server
[root@ops001  ~]# systemctl status grafana-server
[root@ops001  ~]# netstat -nltup|grep 3000
tcp6       0      0 :::3000                 :::*                    LISTEN      10760/grafana-serve

grafana 初始账号密码:admin/admin

3、告警配置
3.1 安装 node_exporter

[root@ops001 ~]# tar xvf node_exporter-0.16.0.linux-amd64.tar.gz -C /usr/local/
[root@ops001 ~]# vim /etc/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
After=network.target

[Service]
Restart=on-failure
ExecStart=/usr/local/node_exporter-0.16.0.linux-amd64/node_exporter

[Install]
WantedBy=multi-user.target

[root@ops001  ~]# systemctl daemon-reload
[root@ops001  ~]# systemctl start node_exporter
[root@ops001  ~]# systemctl status node_exporter
[root@ops001  ~]# netstat -nltup|grep 9100
tcp6       0      0 :::9100                 :::*                    LISTEN      10808/node_exporter 

3.2 实现钉钉告警相关配置
开通钉钉告警机器人


钉钉告警会用到上图的 接收群组的值:SRE 和 webhook 的值 :https://oapi.dingtalk.com/robot/send?access_token=分配的 token 值

配置 prometheus-webhook-dingtalk

[root@ops001 ~]# wget https://github.com/timonwong/prometheus-webhook-dingtalk/releases/download/v0.3.0/prometheus-webhook-dingtalk-0.3.0.linux-amd64.tar.gz
[root@ops001 ~]# tar xvf prometheus-webhook-dingtalk-0.3.0.linux-amd64.tar.gz -C /usr/local/
// 使用方法:prometheus-webhook-dingtalk --ding.profile=钉钉接收群组的值=webhook的值
[root@ops001 ~]# vim /etc/systemd/system/prometheus-webhook-dingtalk.service
[Unit]
Description=prometheus-webhook-dingtalk
After=network-online.target

[Service]
Restart=on-failure
ExecStart=/usr/local/prometheus-webhook-dingtalk-0.3.0.linux-amd64/prometheus-webhook-dingtalk --ding.profile=sre=https://oapi.dingtalk.com/robot/send?access_token=分配的 token 值

[Install]
WantedBy=multi-user.target

[root@ops001 ~]# systemctl daemon-reload
[root@ops001 ~]# systemctl start prometheus-webhook-dingtalk
[root@ops001 ~]# systemctl status prometheus-webhook-dingtalk
[root@ops001 ~]# netstat -nltup|grep 8060
tcp6       0      0 :::8060                 :::*                    LISTEN      10973/prometheus-we

3.3 prometheus 配置连通 alertmanager,配置告警规则

[root@ops001 ~]# cd /usr/local/prometheus-2.4.3.linux-amd64/
[root@ops001 prometheus-2.4.3.linux-amd64]# vim 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:
      - 'localhost:9093'    # 配置连通 alertmanager

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  - "rules/host_rules.yml"    # 配置告警规则目录文件
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['localhost:9090']    # 收集 prometheus 服务本身的信息

  - file_sd_configs:
    - files:
      - 'configs/host.yml'    # 配置 node_exporter 要收集信息的主机列表
    job_name: Host
    metrics_path: /metrics
    relabel_configs:
    - source_labels: [__address__]
      regex: (.*)
      target_label: instance
      replacement: $1
    - source_labels: [__address__]
      regex: (.*)
      target_label: __address__
      replacement: $1:9100

[root@ops001 prometheus-2.4.3.linux-amd64]# mkdir {configs,rules}
// 配置告警规则,如果主机 down 了,就触发告警
[root@ops001 prometheus-2.4.3.linux-amd64]# vim rules/host_rules.yml
groups:
- name: 'Linux Instances'
  rules:
  - alert: InstanceDown
    expr: up == 0
    for: 5s
    labels:
      severity: page
    # Prometheus templates apply here in the annotation and label fields of the alert.
    annotations:
      description: '{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 s.'


// 配置 node_exporter 要收集 172.18.23.253 的主机信息
[root@ops001 prometheus-2.4.3.linux-amd64]# vim configs/host.yml
- labels:
    service: test
  targets:
  - 172.18.23.253

[root@ops001 prometheus-2.4.3.linux-amd64]# systemctl restart prometheus
[root@ops001 prometheus-2.4.3.linux-amd64]# systemctl status prometheus

3.4 配置 alertmanager 的邮件发送方和对接钉钉 webhook

[root@ops001 ~]# cd /usr/local/alertmanager-0.15.2.linux-amd64/
[root@ops001 alertmanager-0.15.2.linux-amd64]# vim alertmanager.yml
global:
  resolve_timeout: 5m
  # 配置邮件发送方信息
  smtp_smarthost: 'smtp.sina.com:465'
  smtp_from: 'xxx@sina.com'
  smtp_auth_username: 'xxx@sina.com'
  smtp_auth_password: 'xxx'
  smtp_require_tls: false

templates:
 - '/usr/local/alertmanager-0.15.2.linux-amd64/template/*.tmpl'

route:
  group_by: ['alertname', 'cluster', 'service']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 10m
  receiver: 'default-receiver'

receivers:
- name: 'default-receiver'
  email_configs:
  # 配置邮件接收方
  - to: 'xxx@qq.com'
    send_resolved: true
  # 配置连接 prometheus-webhook-dingtalk 启动的服务
  webhook_configs:
  - url: 'http://localhost:8060/dingtalk/sre/send'
    send_resolved: true
[root@ops001 alertmanager-0.15.2.linux-amd64]# systemctl start alertmanager
[root@ops001 alertmanager-0.15.2.linux-amd64]# systemctl status alertmanager

至此,简单的告警部分配置完成了

3.5、配置 grafana 展示 prometheus 的信息
从这个地址下载 node_exporter 的 grafana 模板文件:https://grafana.com/dashboards/405,导入 grafana 中,具体步骤如下


可以看到 grafana 展示的 prometheus 主机信息了
3.6、测试告警是否配置生效,通过停止 node_exporter 来模拟主机宕机

[root@ops001 ~]# systemctl stop node_exporter
18.png

可以看到邮件和钉钉告警都生效了
重新启动 node_exporter,模拟主机恢复正常

[root@ops001 ~]# systemctl start node_exporter
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 223,689评论 6 521
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 95,685评论 3 401
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 170,676评论 0 366
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 60,496评论 1 300
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 69,512评论 6 399
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 53,044评论 1 314
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 41,423评论 3 427
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 40,399评论 0 278
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,923评论 1 323
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,973评论 3 343
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 41,117评论 1 354
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,761评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 42,440评论 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,929评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 34,045评论 1 275
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 49,609评论 3 380
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 46,147评论 2 363

推荐阅读更多精彩内容