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

添加 alert . mlog ,修改引用 #1742

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions alert.go
Expand Up @@ -176,6 +176,19 @@ func newAlertNode(et *ExecutingTask, n *pipeline.AlertNode, d NodeDiagnostic) (a
an.handlers = append(an.handlers, h)
}

for _, log := range n.MlogHandlers {
c := alertservice.DefaultLogHandlerConfig()
c.Path = log.FilePath
if log.Mode != 0 {
c.Mode = os.FileMode(log.Mode)
}
h, err := alertservice.NewMlogHandler(c, an.diag)
if err != nil {
return nil, errors.Wrap(err, "failed to create Mlog alert handler")
}
an.handlers = append(an.handlers, h)
}

for _, vo := range n.VictorOpsHandlers {
c := victorops.HandlerConfig{
RoutingKey: vo.RoutingKey,
Expand Down
1 change: 0 additions & 1 deletion autoscale.go
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/influxdata/kapacitor/expvar"
"github.com/influxdata/kapacitor/models"
"github.com/influxdata/kapacitor/pipeline"
ec2 "github.com/influxdata/kapacitor/services/ec2/client"
k8s "github.com/influxdata/kapacitor/services/k8s/client"
swarm "github.com/influxdata/kapacitor/services/swarm/client"
"github.com/influxdata/kapacitor/tick/ast"
Expand Down
26 changes: 26 additions & 0 deletions pipeline/alert.go
Expand Up @@ -317,6 +317,9 @@ type AlertNodeData struct {
// tick:ignore
LogHandlers []*LogHandler `tick:"Log" json:"log"`

//添加一个一条log 一个文件的handler
MlogHandlers []*MlogHandler `tick:"Mlog"`

// Send alert to VictorOps.
// tick:ignore
VictorOpsHandlers []*VictorOpsHandler `tick:"VictorOps" json:"victorOps"`
Expand Down Expand Up @@ -780,6 +783,29 @@ type LogHandler struct {
Mode int64 `json:"mode"`
}

func (a *AlertNode) Mlog(filepath string) *MlogHandler {
log := &MlogHandler{
AlertNode: a,
FilePath: filepath,
}
a.MlogHandlers = append(a.MlogHandlers, log)
return log
}

// tick:embedded:AlertNode.Log
type MlogHandler struct {
*AlertNode

// Absolute path the the log file.
// It will be created if it does not exist.
// tick:ignore
FilePath string

// File's mode and permissions, default is 0600
// NOTE: The leading 0 is required to interpret the value as an octal integer.
Mode int64
}

// Send alert to VictorOps.
// To use VictorOps alerting you must first enable the 'Alert Ingestion API'
// in the 'Integrations' section of VictorOps.
Expand Down
2 changes: 1 addition & 1 deletion pipeline/combine.go
Expand Up @@ -6,8 +6,8 @@ import (
"strings"
"time"

"github.com/influxdata/influxdb/influxql"
"github.com/influxdata/kapacitor/tick/ast"
"github.com/influxdata/influxdb/influxql"
)

const (
Expand Down
33 changes: 33 additions & 0 deletions services/alert/handlers.go
Expand Up @@ -82,6 +82,39 @@ func (h *logHandler) Handle(event alert.Event) {
}
}

type mlogHandler struct {
logpath string
mode os.FileMode
diag HandlerDiagnostic
}

func NewMlogHandler(c LogHandlerConfig, d HandlerDiagnostic) (alert.Handler, error) {
if err := c.Validate(); err != nil {
return nil, err
}
return &mlogHandler{
logpath: c.Path,
mode: c.Mode,
diag: d,
}, nil
}

func (h *mlogHandler) Handle(event alert.Event) {
ad := event.AlertData()
file := fmt.Sprintf("%s/%d_%s.log", h.logpath, ad.Time.Unix(), ad.ID)
f, err := os.OpenFile(file, os.O_WRONLY|os.O_APPEND|os.O_CREATE, h.mode)
if err != nil {
h.diag.Error("failed to open file for alert logging", err, keyvalue.KV("file", file))
return
}
defer f.Close()

err = json.NewEncoder(f).Encode(ad)
if err != nil {
h.diag.Error("failed to marshal alert data json", err)
}
}

type ExecHandlerConfig struct {
Prog string `mapstructure:"prog"`
Args []string `mapstructure:"args"`
Expand Down
1 change: 1 addition & 0 deletions 功能添加.txt
@@ -0,0 +1 @@
1. alert 中添加 mlog , 和 log 相同,mlog 一条告警一个日志, 日志文件名 file := fmt.Sprintf("%s/%d.log", h.logpath, time.Now().UnixNano())