예제 #1
0
// Event processes an event and sends it to the splunk server.
func (sw *SplunkHandler) Event(logEvent *event.Event) error {
	splunkURL, _ := url.Parse(sw.url.String())
	values := splunkURL.Query()
	values.Set("host", sw.Hostname)
	values.Set("source", sw.Source)
	values.Set("sourcetype", sw.SourceType)
	values.Set("index", sw.Index)
	splunkURL.RawQuery = values.Encode()
	splunkURL.Path = splunkURL.Path + "services/receivers/simple"

	sessionKey, err := sw.getSessionKey()
	if err != nil {
		return err
	}

	var templateBuffer bytes.Buffer
	sw.Template.Execute(&templateBuffer, formatter.EventFormatter(logEvent))
	eventBytes := templateBuffer.Bytes()
	eventBytes = bytes.Replace(eventBytes, []byte{'\n'}, []byte{'\r'}, -1)

	req, _ := http.NewRequest("POST", splunkURL.String(), bytes.NewReader(eventBytes))
	req.Header.Set("Authorization", "Splunk "+sessionKey)
	//req.Write(os.Stderr)
	//return nil

	resp, err := sw.client.Do(req)
	if err != nil {
		return err
	}
	resp.Body.Close()

	return nil
}
예제 #2
0
// Event accepts an event, formats it, and writes it to the WriterHandler's Output.
func (handler *WriterHandler) Event(logEvent *event.Event) error {
	//handler.Output.Write([]byte(fmt.Sprintf("%#v\n", event)))
	var templateBuffer bytes.Buffer
	handler.Template.Execute(&templateBuffer, formatter.EventFormatter(logEvent))
	templateBuffer.WriteByte('\n')
	handler.Output.Write(templateBuffer.Bytes())

	return nil
}
예제 #3
0
// Event accepts an event and writes it out to the syslog daemon.
// If the connection was lost, the function will attempt to reconnect once.
func (sw *SyslogHandler) Event(logEvent *event.Event) error {
	var templateBuffer bytes.Buffer
	sw.Template.Execute(&templateBuffer, formatter.EventFormatter(logEvent))
	return sw.sendMessage(logEvent, templateBuffer.Bytes())
}