Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化提升 #5

Open
wants to merge 38 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
6b2d9e9
Create go.yml
myysophia Sep 2, 2024
137bd9a
add 1. dynamic grafa url 2. alert detail 3. with severity of color
Sep 4, 2024
713704d
add prometheus rules
Sep 4, 2024
010f3dc
fix markdown.content exceed max length 4096
Sep 4, 2024
9838955
add log for notification json
Sep 6, 2024
56e7a14
fix notification alert annocations of descriptions
Sep 9, 2024
563b377
add log for notification json
Sep 9, 2024
3ceaa05
add CI workflow
Sep 9, 2024
be5b4a7
add CI workflow
Sep 9, 2024
a34a007
add CI workflow
Sep 9, 2024
f1d123b
add CI workflow
Sep 9, 2024
56da01d
add CI workflow
Sep 9, 2024
66c663f
add CI workflow
Sep 9, 2024
dbba384
add CI workflow
Sep 9, 2024
a208023
add CI workflow new
Sep 9, 2024
0fa1c38
add CI workflow new
Sep 9, 2024
82d4e19
add promtheus rule
Sep 10, 2024
2db1af1
fix 告警长度超过企微限制
Sep 10, 2024
f4b5ba3
fix alertColor
Sep 10, 2024
a6a1cea
add labels of node
Sep 10, 2024
d2f5423
add labels of node
Sep 10, 2024
04b1463
add labels of node
Sep 10, 2024
a802048
fix 告警规则详情
Sep 10, 2024
5d5a51f
add workflow yaml
Sep 10, 2024
fd31ad3
add dockerhub username env
Sep 10, 2024
08e8ecf
add dockerhub username env
Sep 10, 2024
f4db702
add dockerhub username env
Sep 10, 2024
cb00464
add dockerhub username env
Sep 10, 2024
b03afd5
fix cst time
Sep 11, 2024
71da64d
fix cst time
Sep 11, 2024
19c5717
add release workflow
Sep 11, 2024
833e784
add rule
Sep 11, 2024
8626362
fix release yml
Sep 11, 2024
183b54e
fix release yml
Sep 11, 2024
5620db2
fix release yml
Sep 12, 2024
d9914cf
fix image build workflow
Sep 14, 2024
0c3a05c
fix image build workflow
Sep 14, 2024
4e95992
fix release workflow
Sep 14, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
name: Docker Image Push

# 触发条件:当推送到 master 分支或者推送标签时触发
on:
push:
branches:
- "master"
tags:
- "*" # 匹配所有的 tag 推送

env:
DOCKERHUB_USERNAME: ninesun0318

jobs:
build-and-push:
runs-on: ubuntu-latest

steps:
# Step 1: Checkout the code
- name: Checkout code
uses: actions/checkout@v3

# Step 2: Set up Go
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.17'

# # Step 3: Build Go binary
# - name: Build Go binary
# run: |
# mkdir -p bin
# CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -ldflags "-w" -o bin/wechat-webhook

# Step 4: Set up QEMU for multi-platform builds
- name: Set up QEMU
uses: docker/setup-qemu-action@v2

# Step 5: Set up Docker Buildx
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

# Step 6: Log in to Docker Hub
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ env.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

# Step 7: Extract tag from Git reference
- name: Get tag name
id: extract_tag
run: echo "TAG=${GITHUB_REF##*/}" >> $GITHUB_ENV

# Step 8: Build and push Docker image with tag
- name: Build and push Docker image
uses: docker/build-push-action@v3
with:
context: .
file: ./Dockerfile # 确保 Dockerfile 在项目根目录
push: true
platforms: linux/amd64
tags: ${{ env.DOCKERHUB_USERNAME }}/wechat-webhook-new:${{ env.TAG }}


#name: Go Build and Docker Image Push
#
#on:
# push:
# branches:
# - "master"
# pull_request:
# branches:
# - "master"
#
#env:
# DOCKERHUB_USERNAME: ninesun0318
#jobs:
# build-and-push:
# runs-on: ubuntu-latest
#
# steps:
# # Step 1: Checkout the code from the repository
# - name: Checkout code
# uses: actions/checkout@v3
#
# # Step 2: Set up Go environment
# - name: Set up Go
# uses: actions/setup-go@v4
# with:
# go-version: '1.17'
#
# # Step 3: Build Go binary
# - name: Build Go binary
# run: |
# mkdir -p bin
# CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -ldflags "-w" -o bin/wechat-webhook
#
# # Step 4: Set up QEMU (for multi-platform builds)
# - name: Set up QEMU
# uses: docker/setup-qemu-action@v2
#
# # Step 5: Set up Docker Buildx
# - name: Set up Docker Buildx
# uses: docker/setup-buildx-action@v2
#
# # Step 6: Log in to Docker Hub
# - name: Log in to Docker Hub
# uses: docker/login-action@v2
# with:
# username: ${{ env.DOCKERHUB_USERNAME }}
# password: ${{ secrets.DOCKERHUB_TOKEN }}
#
# # Step 7: Build and push Docker image
# - name: Build and push Docker image
# uses: docker/build-push-action@v3
# with:
# context: . # The Docker build context, ensure the Dockerfile and binary are in this directory
# file: ./Dockerfile # Path to your Dockerfile
# push: true
# platforms: linux/amd64
# tags: ${{ env.DOCKERHUB_USERNAME }}/wechat-webhook-new:${{ steps.vars.outputs.sha_short }}
#
# # Optionally, push an image with the short Git SHA as a tag
# - name: Tag image with Git SHA
# uses: docker/build-push-action@v3
# with:
# context: .
# file: ./Dockerfile
# push: true
# tags: ${{ secrets.DOCKERHUB_USERNAME }}/wechat-webhook:${{ github.sha }}
32 changes: 32 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Release wechat-webhook binary
on:
push:
tags:
- 'v*'
- 'draft-*'

jobs:
publishGitRelease:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '^1.17'
check-latest: true

- name: Cross compile
run: |
sudo apt-get install rhash -y
go install github.com/mitchellh/[email protected]
mkdir -p build
./scripts/xcompile.sh

- name: Release
uses: softprops/action-gh-release@v1
with:
files: build/*
draft: true
fail_on_unmatched_files: true
env:
GITHUB_TOKEN: ${{ secrets.GH_PAT }}
19 changes: 12 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
FROM --platform=linux/amd64 amd64/alpine:3.12.0
#FROM --platform=linux/amd64 golang:1.17-alpine
# syntax=docker/dockerfile:1

# docker pull golang:1.17-alpine@sha256:ad6c114a2c858710c4db54f6c89d0e76f753831f01827252064be0017612fecc
# Step 1: build golang binary
FROM golang:1.17 as builder
WORKDIR /opt/app
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -ldflags "-w" -o wechat-webhook

# Step 2: copy binary from step1
FROM alpine:latest
ENV PATH /usr/local/bin:$PATH
ENV LANG C.UTF-8

Expand All @@ -12,13 +17,13 @@ RUN apk update && apk upgrade \
&& apk add ca-certificates\
&& update-ca-certificates \
&& apk --no-cache add openssl wget \
&& apk add --no-cache bash tzdata curl \
&& set -ex \
&& apk add --no-cache bash tzdata curl \
&& set -ex \
&& mkdir -p /usr/bin \
&& mkdir -p /usr/sbin \
&& mkdir -p /data/wechat-webhook/

ADD bin/wechat-webhook /usr/bin/
COPY --from=builder /opt/app/wechat-webhook /usr/bin/wechat-webhook
ADD start.sh /data/wechat-webhook/

WORKDIR /data/wechat-webhook/

7 changes: 3 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

bin/wechat-webhook:
GOOS=linux GOARCH=amd64 go build -ldflags "-w" -o bin/wechat-webhook
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -ldflags "-w" -o bin/wechat-webhook

# make tke version=wechat-webhook
tke: bin/wechat-webhook
docker build -f Dockerfile -t ccr.ccs.tencentyun.com/ahfuzhang/victoria-metrics-starter:$(version) . \
--network=host --platform=linux/amd64 && \
docker push ccr.ccs.tencentyun.com/ahfuzhang/victoria-metrics-starter:$(version)
docker build -t registry.cn-hangzhou.aliyuncs.com/novacloud/wechat-webhook-new:ems1.1 . \
docker push registry.cn-hangzhou.aliyuncs.com/novacloud/wechat-webhook-new:ems1.1


36 changes: 36 additions & 0 deletions alertexample.josn
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"version": "4",
"groupKey": "{}:{alertname=\"Pod状态异常\"}",
"status": "firing",
"Receiver": "webhook",
"GroupLabels": {
"alertname": "Pod状态异常"
},
"CommonLabels": {
"alertname": "Pod状态异常",
"prometheus": "monitoring/k8s",
"severity": "warning"
},
"CommonAnnotations": {
"summary": "pod状态异常,请检查"
},
"ExternalURL": "http://alertmanager-main-0:9093",
"Alerts": [
{
"labels": {
"alertname": "Pod状态异常",
"namespace": "default",
"phase": "Failed",
"pod": "nginx123123123",
"prometheus": "monitoring/k8s",
"severity": "warning"
},
"Annotations": {
"description": "集群名称:储能-ems-cn 命名空间:default Pod:nginx123123123 处于Failed状态持续超过10分钟",
"summary": "pod状态异常,请检查"
},
"startsAt": "2024-09-06T09:50:55.687Z",
"endsAt": "0001-01-01T00:00:00Z"
}
]
}
15 changes: 15 additions & 0 deletions alertmanager.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
global:
resolve_timeout: 1m
route:
group_by: ['alertname']
group_wait: 1s
group_interval: 20m
repeat_interval: 6h
receiver: 'webhook'
templates:
- '*.tmpl'
receivers:
- name: 'webhook'
webhook_configs:
- url: 'http://124.70.148.224:8888/webhook?key=???????'
send_resolved: true
44 changes: 44 additions & 0 deletions deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: wechat-webhook-ems
name: wechat-webhook-ems
namespace: monitoring
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: wechat-webhook-ems
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
app: wechat-webhook-ems
spec:
containers:
- command:
- sh
- -c
- /data/wechat-webhook/start.sh 77d13fe6-0047-48bc-803d-904b24590892 :8888
image: 'registry.cn-hangzhou.aliyuncs.com/novacloud/wechat-webhook-new:ems'
imagePullPolicy: IfNotPresent
name: wechat-webhook-ems
resources:
requests:
cpu: 500m
memory: 1Gi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30

28 changes: 21 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ import (
)

var (
h bool
RobotKey string
addr string
h bool
RobotKey string
addr string
grafanaUrl string
alertDomain string
)

func init() {
flag.BoolVar(&h, "h", false, "help")
flag.StringVar(&RobotKey, "RobotKey", "", "global wechatrobot webhook, you can overwrite by alert rule with annotations wechatRobot")
flag.StringVar(&addr, "addr", ":8999", "listen addr")
flag.StringVar(&grafanaUrl, "grafanaUrl", "grafana.vnnox.com/d/PwMJtdvnr/k8s-chu-neng-cnanduat", "grafanaUrl url")
flag.StringVar(&alertDomain, "alertDomain", "emscn-prometheus.ampaura.tech", "alertDomain url")
}

func main() {
Expand All @@ -34,18 +38,28 @@ func main() {
router := gin.Default()
router.POST("/webhook", func(c *gin.Context) {
var notification model.Notification

err := c.BindJSON(&notification)

//bodyBytes, err := ioutil.ReadAll(c.Request.Body)
//if err != nil {
// log.Printf("Error reading request body: %v", err)
// c.AbortWithStatus(http.StatusBadRequest)
// return
//}
//// 重新设置请求体以确保后续处理可以正常进行
//c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
//
//// 打印请求体内容
//log.Printf("Request Body: %s", bodyBytes)
////gin.LogFormatter(c.Request)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

RobotKey := c.DefaultQuery("key", RobotKey)

err = notifier.Send(notification, RobotKey)

err = notifier.Send(notification, RobotKey, grafanaUrl, alertDomain)
//fmt.Println("notification:", notification, "RobotKey:", RobotKey)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})

Expand Down
2 changes: 2 additions & 0 deletions model/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ type Alert struct {
}

type Notification struct {
// The Alertmanager will send HTTP POST requests in the following JSON format to the configured endpoint:
// https://prometheus.io/docs/alerting/latest/configuration/#webhook_config
Version string `json:"version"`
GroupKey string `json:"groupKey"`
Status string `json:"status"`
Expand Down
Loading