Skip to content

Commit

Permalink
add output=email feature
Browse files Browse the repository at this point in the history
  • Loading branch information
clevertension committed Nov 23, 2016
1 parent 0bdfa02 commit f3d1f55
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,4 @@ See [output module](output) for more information
* [redis](output/redis)
* [report](output/report)
* [stdout](output/stdout)
* [email](output/email)
2 changes: 2 additions & 0 deletions modloader/modloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/tsaikd/gogstash/input/socket"
"github.com/tsaikd/gogstash/output/amqp"
"github.com/tsaikd/gogstash/output/elastic"
"github.com/tsaikd/gogstash/output/email"
"github.com/tsaikd/gogstash/output/prometheus"
"github.com/tsaikd/gogstash/output/redis"
"github.com/tsaikd/gogstash/output/report"
Expand All @@ -33,4 +34,5 @@ func init() {
config.RegistOutputHandler(outputreport.ModuleName, outputreport.InitHandler)
config.RegistOutputHandler(outputamqp.ModuleName, outputamqp.InitHandler)
config.RegistOutputHandler(outputprometheus.ModuleName, outputprometheus.InitHandler)
config.RegistOutputHandler(outputemail.ModuleName, outputemail.InitHandler)
}
44 changes: 44 additions & 0 deletions output/email/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
gogstash output email
=======================

## Synopsis

```
{
"output": [
{
"type": "email",
// (required)
"address": "smtp.xxx.com",
// (required)
"username": "your_user_name",
// (required)
"password": "your_password",
// (required)
"subject": "your subject",
// (required)
"from": "from@youremail",
// (required)
"to": "[email protected];[email protected]",
// (optional)
"cc": "[email protected];[email protected]",
// (optional)
"port": 25,
// (optional)
"use_tls": true,
// (optional)
"attachments": ["/your_path/your_file"],
}
]
}
```
83 changes: 83 additions & 0 deletions output/email/outputemail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package outputemail

import (
"crypto/tls"
"strings"

"github.com/tsaikd/gogstash/config"
"github.com/tsaikd/gogstash/config/logevent"
"gopkg.in/gomail.v2"
)

// ModuleName the module name of this plugin
const (
ModuleName = "email"
)

// OutputConfig the default output config
type OutputConfig struct {
config.OutputConfig
Address string `json:"address"`
Attachments []string `json:"attachments"`
From string `json:"from"`
To string `json:"to"`
Cc string `json:"cc"`
Subject string `json:"subject"`
Port int `json:"port"`
UseTLS bool `json:"use_tls"`
UserName string `json:"username"`
Password string `json:"password"`
}

// DefaultOutputConfig build the default output config
func DefaultOutputConfig() OutputConfig {
return OutputConfig{
OutputConfig: config.OutputConfig{
CommonConfig: config.CommonConfig{
Type: ModuleName,
},
},
Port: 25,
UseTLS: false,
Cc: "",
UserName: "",
Password: "",
Attachments: nil,
}
}

// InitHandler init the handler
func InitHandler(confraw *config.ConfigRaw) (retconf config.TypeOutputConfig, err error) {
conf := DefaultOutputConfig()
if err = config.ReflectConfig(confraw, &conf); err != nil {
return
}

retconf = &conf
return
}

// Event the main log event
func (t *OutputConfig) Event(event logevent.LogEvent) (err error) {
message := gomail.NewMessage()
message.SetHeader("From", t.From)
message.SetHeader("To", strings.Split(t.To, ";")...)
if t.Cc != "" {
message.SetHeader("Cc", strings.Split(t.Cc, ";")...)
}
message.SetHeader("Subject", t.Subject)

if t.Attachments != nil && len(t.Attachments) > 0 {
for _, v := range t.Attachments {
message.Attach(v)
}
}

message.SetBody("text/html", event.GetString("message"))
dialer := gomail.NewDialer(t.Address, t.Port, t.UserName, t.Password)
if t.UseTLS == true {
dialer.TLSConfig = &tls.Config{InsecureSkipVerify: true}
}
err = dialer.DialAndSend(message)
return
}
57 changes: 57 additions & 0 deletions output/email/outputemail_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package outputemail

import (
"reflect"
"testing"
"time"

"github.com/Sirupsen/logrus"
"github.com/stretchr/testify/require"
"github.com/tsaikd/gogstash/config"
"github.com/tsaikd/gogstash/config/logevent"
)

var (
logger = config.Logger
)

func init() {
logger.Level = logrus.DebugLevel
config.RegistOutputHandler(ModuleName, InitHandler)
}

func Test_main(t *testing.T) {
require := require.New(t)
require.NotNil(require)

// Please fill the correct email info xxx is just a placeholder
conf, err := config.LoadFromString(`{
"output": [{
"type": "email",
"address": "xxx",
"from": "xxx",
"to": "xxx",
"cc": "xxx",
"use_tls": false,
"port": 25,
"username": "xxx",
"password": "xxx",
"subject": "outputemail test subject"
}]
}`)
require.NoError(err)

err = conf.RunOutputs()
require.NoError(err)

outchan := conf.Get(reflect.TypeOf(make(config.OutChan))).
Interface().(config.OutChan)
outchan <- logevent.LogEvent{
Timestamp: time.Now(),
Message: "outputemail test message",
}

waitsec := 1
logger.Infof("Wait for %d seconds", waitsec)
time.Sleep(time.Duration(waitsec) * time.Second)
}

0 comments on commit f3d1f55

Please sign in to comment.