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

feature: lazy connect to syslog server #2068

Merged
merged 1 commit into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions daemon/logger/syslog/conn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package syslog

import (
"net"

"github.com/RackSec/srslog"
)

// localConn implements the serverConn interface, used to send syslog messages
// to the remote syslog daemon.
type remoteConn struct {
conn net.Conn
}

// writeString will use Framer/Formatter to format the content before write.
//
// NOTE: writeString implements serverConn.writeString() methods.
func (n *remoteConn) writeString(framer Framer, formatter Formatter, p Priority, hostname, tag, msg string) error {
if framer == nil {
framer = srslog.DefaultFramer
}

if formatter == nil {
formatter = srslog.DefaultFormatter
}

formattedMessage := framer(formatter(p, hostname, tag, msg))
_, err := n.conn.Write([]byte(formattedMessage))
return err
}

// close closes the connection.
//
// NOTE:close implements serverConn.close() methods.
func (n *remoteConn) close() error {
return n.conn.Close()
}

// localConn implements the serverConn interface, used to send syslog messages
// to the local syslog daemon over a Unix domain socket.
type localConn struct {
conn net.Conn
}

// writeString will use Framer/Formatter to format the content before write.
//
// NOTE: writeString implements serverConn.writeString() methods.
func (n *localConn) writeString(framer Framer, formatter Formatter, p Priority, hostname, tag, msg string) error {
if framer == nil {
framer = srslog.DefaultFramer
}

if formatter == nil {
formatter = srslog.UnixFormatter
}

formattedMessage := framer(formatter(p, hostname, tag, msg))
_, err := n.conn.Write([]byte(formattedMessage))
return err
}

// close closes the connection.
//
// NOTE:close implements serverConn.close() methods.
func (n *localConn) close() error {
return n.conn.Close()
}
20 changes: 19 additions & 1 deletion daemon/logger/syslog/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,22 @@ import (
"github.com/RackSec/srslog"
)

// Priority is alias srslog.Priority.
type Priority = srslog.Priority

// Framer is alias srslog.Framer.
type Framer = srslog.Framer

// Formatter is alias srslog.Formatter.
type Formatter = srslog.Formatter

const (
severityMask = 0x07
facilityMask = 0xf8
)

var (

// rfc5424 provides millisecond resolution.
timeRfc5424fmt = "2006-01-02T15:04:05.999999Z07:00"

Expand All @@ -16,7 +31,7 @@ var (
defaultSyslogPriority = srslog.LOG_DAEMON

// facilityAliasMap allows user to use alias to set the syslog priority.
facilityAliasMap = map[string]srslog.Priority{
facilityAliasMap = map[string]Priority{
"kern": srslog.LOG_KERN,
"user": srslog.LOG_USER,
"mail": srslog.LOG_MAIL,
Expand Down Expand Up @@ -47,6 +62,9 @@ var (
"unixgram://",
}

unixDialerTypes = []string{"unixgram", "unix"}
unixDialerLocalPaths = []string{"/dev/log", "/var/run/syslog", "/var/run/log"}

// tls client cipher suites
defaultCipherSuites = []uint16{
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
Expand Down
67 changes: 67 additions & 0 deletions daemon/logger/syslog/dialer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package syslog

import (
"crypto/tls"
"errors"
"net"
)

type serverConn interface {
writeString(framer Framer, formatter Formatter, p Priority, hostname, tag, s string) error
close() error
}

func makeDialer(proto string, addr string, cfg *tls.Config) (serverConn, string, error) {
switch proto {
case "":
return unixLocalDialer()
case secureProto:
return tlsDialer(addr, cfg)
default:
return commonDialer(proto, addr)
}
}

// commonDialer is the most common dialer for TCP/UDP/Unix connections.
func commonDialer(network string, addr string) (serverConn, string, error) {
var (
sc serverConn
hostname string
)

c, err := net.Dial(network, addr)
if err == nil {
sc = &remoteConn{conn: c}
hostname = c.LocalAddr().String()
}
return sc, hostname, err
}

// tlsDialer connects to TLS over TCP, and is used for the "tcp+tls" network.
func tlsDialer(addr string, cfg *tls.Config) (serverConn, string, error) {
var (
sc serverConn
hostname string
)

c, err := tls.Dial("tcp", addr, cfg)
if err == nil {
sc = &remoteConn{conn: c}
hostname = c.LocalAddr().String()
}
return sc, hostname, err
}

// unixLocalDialer opens a Unix domain socket connection to the syslog daemon
// running on the local machine.
func unixLocalDialer() (serverConn, string, error) {
for _, network := range unixDialerTypes {
for _, path := range unixDialerLocalPaths {
conn, err := net.Dial(network, path)
if err == nil {
return &localConn{conn: conn}, "localhost", nil
}
}
}
return nil, "", errors.New("unix local syslog delivery error")
}
133 changes: 111 additions & 22 deletions daemon/logger/syslog/syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package syslog

import (
"crypto/tls"
"os"
"strings"
"sync"

"github.com/alibaba/pouch/daemon/logger"
"github.com/alibaba/pouch/daemon/logger/loggerutils"
Expand All @@ -11,16 +14,20 @@ import (

// Syslog writes the log data into syslog.
type Syslog struct {
writer *srslog.Writer
mu sync.RWMutex

opt *options
conn serverConn
}

type options struct {
tag string
proto string
address string
priority srslog.Priority
formatter srslog.Formatter
framer srslog.Framer
hostname string
priority Priority
formatter Formatter
framer Framer
tlsCfg *tls.Config
}

Expand All @@ -32,39 +39,121 @@ func defaultOptions() *options {

// NewSyslog returns new Syslog based on the log config.
func NewSyslog(info logger.Info) (*Syslog, error) {
opts, err := parseOptions(info)
opt, err := parseOptions(info)
if err != nil {
return nil, err
}

var w *srslog.Writer
if opts.proto == secureProto {
w, err = srslog.DialWithTLSConfig(opts.proto, opts.address, opts.priority, opts.tag, opts.tlsCfg)
} else {
w, err = srslog.Dial(opts.proto, opts.address, opts.priority, opts.tag)
}

if err != nil {
return nil, err
}

w.SetFormatter(opts.formatter)
w.SetFramer(opts.framer)
return &Syslog{writer: w}, nil
opt.hostname, _ = os.Hostname()
return &Syslog{
opt: opt,
conn: nil,
}, nil
}

// WriteLogMessage will write the LogMessage.
func (s *Syslog) WriteLogMessage(msg *logger.LogMessage) error {
line := string(msg.Line)
if msg.Source == "stderr" {
return s.writer.Err(line)
return s.logError(line)
}
return s.writer.Info(line)
return s.logInfo(line)
}

// Close closes the Syslog.
func (s *Syslog) Close() error {
return s.writer.Close()
var err error
s.mu.Lock()
defer s.mu.Unlock()
if s.conn != nil {
err = s.conn.close()
s.conn = nil
}
return err
}

// logInfo logs a content with severity LOG_INFO.
func (s *Syslog) logInfo(content string) error {
_, err := s.writeAndRetry(srslog.LOG_INFO, content)
return err
}

// logError logs a content with severity LOG_ERR.
func (s *Syslog) logError(content string) error {
_, err := s.writeAndRetry(srslog.LOG_ERR, content)
return err
}

// writeAndRetry takes a severity and the content to write.
//
// NOTE: Any facility passed to it as part of the severity Priority will be ignored.
func (s *Syslog) writeAndRetry(severity Priority, content string) (int, error) {
p := (s.opt.priority & facilityMask) | (severity & severityMask)

conn := s.getConn()
if conn != nil {
if n, err := s.write(conn, p, content); err == nil {
return n, nil
}
}

var err error
if conn, err = s.connect(); err != nil {
return 0, err
}
return s.write(conn, p, content)
}

// write writes a syslog formatted string.
func (s *Syslog) write(conn serverConn, p Priority, content string) (int, error) {
// ensure it ends with a \n
if !strings.HasSuffix(content, "\n") {
content += "\n"
}

err := conn.writeString(s.opt.framer, s.opt.formatter, p, s.opt.hostname, s.opt.tag, content)
if err != nil {
return 0, err
}

return len(content), nil
}

// connect uses current option to connect the remote host.
func (s *Syslog) connect() (serverConn, error) {
sc, hostname, err := makeDialer(s.opt.proto, s.opt.address, s.opt.tlsCfg)
if err != nil {
return nil, err
}

s.setConn(sc, hostname)
return sc, nil
}

// getConn returns the current serverConn.
func (s *Syslog) getConn() serverConn {
s.mu.RLock()
c := s.conn
s.mu.RUnlock()
return c
}

// setConn updates the connection.
//
// NOTE: the Syslog takes lazy mode for connection. It might have more goroutines
// which try to connect the same remote host. If there is no close existing
// connection, it will be connection leak.
func (s *Syslog) setConn(c serverConn, hostname string) {
s.mu.Lock()
if s.conn != nil {
s.conn.close()
}

s.conn = c
if s.opt.hostname == "" {
s.opt.hostname = hostname
}
s.mu.Unlock()
}

// parseOptions parses the log config into options.
Expand Down
Loading