监控神器Prometheus(3)

发布于 2022-7-5 17:28
浏览
0收藏

 

Alertmanager集成Dingtalk/Wechat/Email报警

 

二进制安装Alertmanager

#创建服务相关文件目录
ALERT_PATH='/data/alertmanager'
mkdir -p ${ALERT_PATH}/{bin,conf,logs,templates}
cd /usr/local/src/
wget https://github.com/prometheus/alertmanager/releases/download/v0.18.0/alertmanager-0.18.0.linux-amd64.tar.gz && tar -xvf alertmanager-0.18.0.linux-amd64.tar.gz
cp alertmanager-0.18.0.linux-amd64/alertmanager /data/prometheus/alertmanager/bin/
cp alertmanager-0.18.0.linux-amd64/amtool /data/prometheus/alertmanager/bin/
chown -R prometheus.prometheus /data/alertmanager

# Setting Variables
cat >> /etc/profile <<EOF
PATH=/data/alertmanager/bin:$PATH:$HOME/bin
EOF

 

创建Alertmanager配置文件

cat > /data/alertmanager/conf/alertmanager.yml <<EOF
global:
  resolve_timeout: 5m
  # smtp配置
  smtp_from: "prom-alert@example.com"
  smtp_smarthost: 'email-smtp.us-west-2.amazonaws.com:465'
  smtp_auth_username: "user"
  smtp_auth_password: "pass"
  smtp_require_tls: true
templates:
  - '/data/alertmanager/templates/*.tmpl'
route:
  receiver: test1
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 4h
  group_by: [alertname]
  routes:
# ads webhook
  - receiver: test1
    group_wait: 10s
    match:
      team: ads
# ops webhook
  - receiver: test2
    group_wait: 10s
    match:
      team: operations
receivers:
- name: test1
  email_configs:
  - to: '9935226@qq.com'
    headers: { Subject: "[ads] 报警邮件"} # 接收邮件的标题
  webhook_configs:
  - url: http://localhost:8060/dingtalk/ads/send

- name: test2
  email_configs:
  - to: '9935226@qq.com,deniss.wang@gmail.com'
    send_resolved: true
    headers: { Subject: "[ops] 报警邮件"} # 接收邮件的标题
  webhook_configs:
  - url: http://localhost:8060/dingtalk/ops/send
    # wx config
  wechat_configs:
  - corp_id: 'wwxxxxxxxxxxxxxx'
    api_url: 'https://qyapi.weixin.qq.com/cgi-bin/'
    send_resolved: true
    to_party: '2'
    agent_id: '1000002'
    api_secret: '1FvHxuGbbG35FYsuW0YyI4czWY/.2'
EOF

 

创建Systemd Node_exporter服务

# alertmanager.service
cat >/lib/systemd/system/alertmanager.service<<\EOF
[Unit]
Description=Prometheus: the alerting system
Documentation=http://prometheus.io/docs/
After=prometheus.service

[Service]
ExecStart=/data/alertmanager/bin/alertmanager --config.file=/data/alertmanager/conf/alertmanager.yml
Restart=always
StartLimitInterval=0
RestartSec=10

[Install]
WantedBy=multi-user.target
EOF

# 启动服务
systemctl enable alertmanager.service
systemctl start alertmanager.service
systemctl status alertmanager.service

# 查看端口是否正常
netstat -anplt|grep 9093
tcp6       0      0 :::9093                 :::*                    LISTEN      1547/alertmanager

 

将Dingtalk接入 Prometheus AlertManager WebHook


在钉钉中申请钉钉机器人:
格式为:https://oapi.dingtalk.com/robot/send?access_token=11a0496d0af689d56a5861ae34dc47d9f1607aee6f342747

 

二进制与Docker方式安装Dingtalk-Webhook插件插件


此插件安装方式分源码编译安装,二进制安装与Docker部署,此处只对二进制与Docker方式讲解,源码编译安装可去GitHub查看详情。

cd /usr/local/src/
wget https://github.com/timonwong/prometheus-webhook-dingtalk/releases/download/v0.3.0/prometheus-webhook-dingtalk-0.3.0.linux-amd64.tar.gz
tar -zxvf prometheus-webhook-dingtalk-0.3.0.linux-amd64.tar.gz
mv prometheus-webhook-dingtalk-0.3.0.linux-amd64 /data/alertmanager/webhook-dingtalk
# 创建Systemd webhook-dingtalk 服务
cat > /etc/systemd/system/webhook-dingtalk.service << EOF
[Unit]
Description=webhook-dingding
Documentation=https://prometheus.io/
After=network.target

[Service]
Type=simple
User=prometheus
ExecStart=/data/alertmanager/webhook-dingtalk/prometheus-webhook-dingtalk \
--ding.profile="ads=https://oapi.dingtalk.com/robot/send?access_token=284de68124e97420a2ee8ae1b8f12fabe3213213213" \
--ding.profile="ops=https://oapi.dingtalk.com/robot/send?access_token=8bce3bd11f7040d57d44caa5b6ef9417eab24e1123123123213" 
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF
# 启动服务
systemctl enable webhook-dingtalk
systemctl start webhook-dingtalk
systemctl status webhook-dingtalk
# 查看端口是否正常
netstat -anplt|grep 8060
tcp6       0      0 :::8060                 :::*                    LISTEN      1635/prometheus-web

 

docker方式部署插件

# Pull Image
docker pull timonwong/prometheus-webhook-dingtalk:v0.3.0
# 启动
docker run -d --restart always -p 8060:8060 timonwong/prometheus-webhook-dingtalk:v0.3.0 --ding.profile="ads=https://oapi.dingtalk.com/robot/send?access_token=284de68124e97420a2ee8ae1b8f12fabe3213213213" \
--ding.profile="ops=https://oapi.dingtalk.com/robot/send?access_token=8bce3bd11f7040d57d44caa5b6ef9417eab24e1123123123213"

 

Grafana安装及配置

# Ubuntu
wget https://dl.grafana.com/oss/release/grafana_6.4.0_amd64.deb
sudo dpkg -i grafana_6.4.0_amd64.deb

# Centos7
yum localinstall -y https://dl.grafana.com/oss/release/grafana-6.4.4-1.x86_64.rpm

# 安装拼图、Consul数据源插件
grafana-cli plugins install grafana-piechart-panel
grafana-cli plugins install sbueringer-consul-datasource
systemctl restart grafana-server.service

# 启动服务
systemctl enable grafana-server.service
systemctl start grafana-server.service
systemctl status grafana-server.service

# nginx 配置
cat > /etc/nginx/conf.d/grafana.k8stech.net.conf << EOF
server {
    listen       80;
    server_name  grafana.k8stech.net;

    location / {
            try_files $uri @grafana;
    }

    location @grafana {
            internal;
            proxy_pass http://localhost:3000;
    }
}
EOF

 

欢迎大家关注我的公众号ID:k8stech

 

文章转自公众号:Kubernetes技术栈

标签
已于2022-7-5 17:28:22修改
收藏
回复
举报
回复
添加资源
添加资源将有机会获得更多曝光,你也可以直接关联已上传资源 去关联
    相关推荐