我的创作纪念日
机缘
最初成为创作者的初心
眼过千遍,不如手过一遍。也为了日后面试复习
1、希望把基础的运维技术、运维工作中使用的技术、运维过程中问题排查解决的过程做一个完整的记录
2、希望可以坚持写10年(尽量以技术内容为主的文章),定下一个10年之约目标
3、日常分享的运维常用技术中间件RPM包构建、iptables规则、shell脚本、MySQL数据库、CICD、Kubernetes、ES、Redis、Prometheus监控、Graylog日志系统、三级等保漏洞修复方案、Golang运维工具开发等技术性文章
4、不仅仅是简单的分享,其实写博客的过程就是加深对这门技术的印象。
收获
1、在这一年也学会了中间件RPM包构建的两种方式(rpmbuild、fpm)
2、加深了mysql主从恢复及MHA搭建维护
3、对graylog日志系统的搭建及维护有了深层次的了解,不再局限于市面常见的ELK、EFK技术
4、在容器方面,对镜像的打包构建、yaml配置调整、网络流向有了一定的理解,也逐渐培养出了容器问题排查思路
5、在开发语言方面,对golang有了入门基础,也将Mysql全量备份shell脚本转换为了golang语言实现,同时也对监控agent注册到consul中也通过golang开发语言实现
6、这一年也整理了三级等保时会遇到的一些问题,同时也整理了对应的修复方法。
7、拿下了CKA证书
8、同时这一年,粉丝量也有增加,文章的阅读量、展现量也有所增长,如下图所示
日常
1、现在创作文章基本在每月中旬会发布5-8篇技术文章,这些文章都来自于当月我的实际工作中遇到的问题及解决方法
2、加油更新出更好的文章
成就
golang实现对alertmanager原始数据进行转换,转换为企业微信消息的markdown格式
package common
import (
"alertmanagerWebhook/config"
"alertmanagerWebhook/core"
"alertmanagerWebhook/global"
"bytes"
"os"
"path/filepath"
"reflect"
"text/template"
"time"
"github.com/gomodule/redigo/redis"
)
func TransformToMarkdown(notification config.Notification) (message *config.Message, err error) {
c, err := core.ConnRedis()
if err != nil {
global.Logger.Errorf("Failed to connect to Redis: %v\n", err)
return
}
defer c.Close() // 确保在函数结束时关闭连接
var (
notificationFiring config.Notification
notificationResolved config.Notification
cstZone = time.FixedZone("CST", 8*3600)
bufferFiring bytes.Buffer
bufferResolved bytes.Buffer
)
dir, err := os.Getwd()
if err != nil {
global.Logger.Errorf("Error getting current directory: %v\n", err)
return
}
// Use filepath.Join to create the correct file path
templatePath := filepath.Join(dir, "/template/alert.tmpl")
for _, alert := range notification.Alerts {
if alert.Status == "firing" {
notificationFiring.Version = notification.Version
notificationFiring.GroupKey = notification.GroupKey
notificationFiring.Status = "firing"
notificationFiring.Receiver = notification.Receiver
notificationFiring.GroupLabels = notification.GroupLabels
notificationFiring.CommonLabels = notification.CommonLabels
notificationFiring.ExternalURL = notification.ExternalURL
notificationFiring.Alerts = append(notificationFiring.Alerts, alert)
} else if alert.Status == "resolved" {
notificationResolved.Version = notification.Version
notificationResolved.GroupKey = notification.GroupKey
notificationResolved.Status = "resolved"
notificationResolved.Receiver = notification.Receiver
notificationResolved.GroupLabels = notification.GroupLabels
notificationResolved.CommonLabels = notification.CommonLabels
notificationResolved.ExternalURL = notification.ExternalURL
notificationResolved.Alerts = append(notificationResolved.Alerts, alert)
}
}
// Templated Email Body for Firing Alerts
if !reflect.DeepEqual(notificationFiring, config.Notification{}) {
for _, alert := range notificationFiring.Alerts {
alert.StartTime = alert.StartsAt.In(cstZone).Format("2006-01-02 15:04:05")
fingerprint := alert.Fingerprint
// Save states in Redis -->hset fingerprintValue startTimeValue存储,key的名称就是fingerprintValue,字段就是startTime
if _, err = c.Do("HSet", fingerprint, "startTime", alert.StartTime); err != nil {
global.Logger.Errorln(err)
return nil, err
}
//Redis Hincrby 命令用于为哈希表中的字段值加上指定增量值
if _, err = c.Do("Hincrby", fingerprint, "count", 1); err != nil {
global.Logger.Errorln(err)
return nil, err
}
count, err := redis.Int(c.Do("HGet", fingerprint, "count"))
if err != nil {
global.Logger.Errorln("get alert count error: ", err)
}
alert.Count = count //通过redis记录告警次数
// 检查 Description 是否存在或为空
if alert.Annotations.Description == "" {
// 如果为空,则重新赋值
alert.Annotations.Description = alert.Annotations.Summary
}
//告警级别如果为空,则设置为warning
if alert.Labels.Severity == "" {
alert.Labels.Severity = "warning"
}
// Load template from file
tmpl, err := template.ParseFiles(templatePath)
if err != nil {
global.Logger.Errorln("template parse error: ", err)
return nil, err
}
// Execute the template and write to emailBodyFiring
if err := tmpl.Execute(&bufferFiring, alert); err != nil {
global.Logger.Errorln("template execute error: ", err)
return nil, err
}
bufferFiring.WriteString("\n") // 添加换行符以分隔不同的告警
}
}
// Templated Email Body for Resolved Alerts
if !reflect.DeepEqual(notificationResolved, config.Notification{}) {
for _, alert := range notificationResolved.Alerts {
alert.StartTime = alert.StartsAt.In(cstZone).Format("2006-01-02 15:04:05")
alert.EndTime = alert.EndsAt.In(cstZone).Format("2006-01-02 15:04:05")
// 检查 Description 是否存在或为空
if alert.Annotations.Description == "" {
// 如果为空,则重新赋值
alert.Annotations.Description = alert.Annotations.Summary
}
// Load template from file
tmpl, err := template.ParseFiles(templatePath)
if err != nil {
global.Logger.Errorln("template parse error: ", err)
return nil, err
}
// Execute the template and write to emailBodyResolved
if err := tmpl.Execute(&bufferResolved, alert); err != nil {
global.Logger.Errorln("template execute error: ", err)
return nil, err
}
bufferResolved.WriteString("\n") // 添加换行符以分隔不同的告警
//恢复后,从redis删除对应的key
if _, err := c.Do("Del", alert.Fingerprint); err != nil {
global.Logger.Errorln("delete key error: ", err)
}
}
}
// 转换为企业微信可以识别的格式
var markdownFiring, markdownResolved *config.QyWeChatMarkdown
var title string
title = "# <font color=\"red\">触发告警</font>\n"
if bufferFiring.String() != "" {
markdownFiring = config.NewQyWeChatMarkdown(title + bufferFiring.String())
} else {
markdownFiring = config.NewQyWeChatMarkdown("")
}
title = "# <font color=\"green\">告警恢复</font>\n"
if bufferResolved.String() != "" {
markdownResolved = config.NewQyWeChatMarkdown(title + bufferResolved.String())
} else {
markdownResolved = config.NewQyWeChatMarkdown("")
}
// 将企业微信消息进行封装
message = config.NewMessage(markdownFiring, markdownResolved)
//log.Printf("messages: %v\n", message.QywechatMessage.MarkdownFiring.Markdown.Content)
global.Logger.Infof("messagesWeChatFiring: %v\n", message.QywechatMessage.MarkdownFiring.Markdown.Content)
global.Logger.Infof("messagesWeChatResovled: %v\n", message.QywechatMessage.MarkdownResolved.Markdown.Content)
return message, nil
}
憧憬
职业规划
1、在提升运维技术的同时,希望能对golang语言有更深入层次的学习,有朝一日可以做到运维开发岗位
2、拿下CKS、红帽证书,提高就业机会
3、希望年后可以找到一家不错的公司
一步一个脚印,做一个真正的技术者,不要欺骗自己,做到问心无愧
原文地址:https://blog.csdn.net/weixin_50902636/article/details/143423996
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!