forked from tsaikd/gogstash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0bdfa02
commit f3d1f55
Showing
5 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
} | ||
] | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |