syslog.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // Package syslog provides the logdriver for forwarding server logs to syslog endpoints.
  2. package syslog // import "github.com/docker/docker/daemon/logger/syslog"
  3. import (
  4. "crypto/tls"
  5. "errors"
  6. "fmt"
  7. "net"
  8. "net/url"
  9. "os"
  10. "strconv"
  11. "strings"
  12. "time"
  13. syslog "github.com/RackSec/srslog"
  14. "github.com/docker/docker/daemon/logger"
  15. "github.com/docker/docker/daemon/logger/loggerutils"
  16. "github.com/docker/go-connections/tlsconfig"
  17. )
  18. const (
  19. name = "syslog"
  20. secureProto = "tcp+tls"
  21. defaultPort = "514"
  22. )
  23. var facilities = map[string]syslog.Priority{
  24. "kern": syslog.LOG_KERN,
  25. "user": syslog.LOG_USER,
  26. "mail": syslog.LOG_MAIL,
  27. "daemon": syslog.LOG_DAEMON,
  28. "auth": syslog.LOG_AUTH,
  29. "syslog": syslog.LOG_SYSLOG,
  30. "lpr": syslog.LOG_LPR,
  31. "news": syslog.LOG_NEWS,
  32. "uucp": syslog.LOG_UUCP,
  33. "cron": syslog.LOG_CRON,
  34. "authpriv": syslog.LOG_AUTHPRIV,
  35. "ftp": syslog.LOG_FTP,
  36. "local0": syslog.LOG_LOCAL0,
  37. "local1": syslog.LOG_LOCAL1,
  38. "local2": syslog.LOG_LOCAL2,
  39. "local3": syslog.LOG_LOCAL3,
  40. "local4": syslog.LOG_LOCAL4,
  41. "local5": syslog.LOG_LOCAL5,
  42. "local6": syslog.LOG_LOCAL6,
  43. "local7": syslog.LOG_LOCAL7,
  44. }
  45. type syslogger struct {
  46. writer *syslog.Writer
  47. }
  48. func init() {
  49. if err := logger.RegisterLogDriver(name, New); err != nil {
  50. panic(err)
  51. }
  52. if err := logger.RegisterLogOptValidator(name, ValidateLogOpt); err != nil {
  53. panic(err)
  54. }
  55. }
  56. // rsyslog uses appname part of syslog message to fill in an %syslogtag% template
  57. // attribute in rsyslog.conf. In order to be backward compatible to rfc3164
  58. // tag will be also used as an appname
  59. func rfc5424formatterWithAppNameAsTag(p syslog.Priority, hostname, tag, content string) string {
  60. timestamp := time.Now().Format(time.RFC3339)
  61. pid := os.Getpid()
  62. msg := fmt.Sprintf("<%d>%d %s %s %s %d %s - %s",
  63. p, 1, timestamp, hostname, tag, pid, tag, content)
  64. return msg
  65. }
  66. // The timestamp field in rfc5424 is derived from rfc3339. Whereas rfc3339 makes allowances
  67. // for multiple syntaxes, there are further restrictions in rfc5424, i.e., the maximum
  68. // resolution is limited to "TIME-SECFRAC" which is 6 (microsecond resolution)
  69. func rfc5424microformatterWithAppNameAsTag(p syslog.Priority, hostname, tag, content string) string {
  70. timestamp := time.Now().Format("2006-01-02T15:04:05.000000Z07:00")
  71. pid := os.Getpid()
  72. msg := fmt.Sprintf("<%d>%d %s %s %s %d %s - %s",
  73. p, 1, timestamp, hostname, tag, pid, tag, content)
  74. return msg
  75. }
  76. // New creates a syslog logger using the configuration passed in on
  77. // the context. Supported context configuration variables are
  78. // syslog-address, syslog-facility, syslog-format.
  79. func New(info logger.Info) (logger.Logger, error) {
  80. tag, err := loggerutils.ParseLogTag(info, loggerutils.DefaultTemplate)
  81. if err != nil {
  82. return nil, err
  83. }
  84. proto, address, err := parseAddress(info.Config["syslog-address"])
  85. if err != nil {
  86. return nil, err
  87. }
  88. facility, err := parseFacility(info.Config["syslog-facility"])
  89. if err != nil {
  90. return nil, err
  91. }
  92. syslogFormatter, syslogFramer, err := parseLogFormat(info.Config["syslog-format"], proto)
  93. if err != nil {
  94. return nil, err
  95. }
  96. var log *syslog.Writer
  97. if proto == secureProto {
  98. tlsConfig, tlsErr := parseTLSConfig(info.Config)
  99. if tlsErr != nil {
  100. return nil, tlsErr
  101. }
  102. log, err = syslog.DialWithTLSConfig(proto, address, facility, tag, tlsConfig)
  103. } else {
  104. log, err = syslog.Dial(proto, address, facility, tag)
  105. }
  106. if err != nil {
  107. return nil, err
  108. }
  109. log.SetFormatter(syslogFormatter)
  110. log.SetFramer(syslogFramer)
  111. return &syslogger{
  112. writer: log,
  113. }, nil
  114. }
  115. func (s *syslogger) Log(msg *logger.Message) error {
  116. if len(msg.Line) == 0 {
  117. return nil
  118. }
  119. line := string(msg.Line)
  120. source := msg.Source
  121. logger.PutMessage(msg)
  122. if source == "stderr" {
  123. return s.writer.Err(line)
  124. }
  125. return s.writer.Info(line)
  126. }
  127. func (s *syslogger) Close() error {
  128. return s.writer.Close()
  129. }
  130. func (s *syslogger) Name() string {
  131. return name
  132. }
  133. func parseAddress(address string) (string, string, error) {
  134. if address == "" {
  135. return "", "", nil
  136. }
  137. addr, err := url.Parse(address)
  138. if err != nil {
  139. return "", "", err
  140. }
  141. // unix and unixgram socket validation
  142. if addr.Scheme == "unix" || addr.Scheme == "unixgram" {
  143. if _, err := os.Stat(addr.Path); err != nil {
  144. return "", "", err
  145. }
  146. return addr.Scheme, addr.Path, nil
  147. }
  148. if addr.Scheme != "udp" && addr.Scheme != "tcp" && addr.Scheme != secureProto {
  149. return "", "", fmt.Errorf("unsupported scheme: '%s'", addr.Scheme)
  150. }
  151. // here we process tcp|udp
  152. host := addr.Host
  153. if _, _, err := net.SplitHostPort(host); err != nil {
  154. if !strings.Contains(err.Error(), "missing port in address") {
  155. return "", "", err
  156. }
  157. host = net.JoinHostPort(host, defaultPort)
  158. }
  159. return addr.Scheme, host, nil
  160. }
  161. // ValidateLogOpt looks for syslog specific log options
  162. // syslog-address, syslog-facility.
  163. func ValidateLogOpt(cfg map[string]string) error {
  164. for key := range cfg {
  165. switch key {
  166. case "env":
  167. case "env-regex":
  168. case "labels":
  169. case "labels-regex":
  170. case "syslog-address":
  171. case "syslog-facility":
  172. case "syslog-tls-ca-cert":
  173. case "syslog-tls-cert":
  174. case "syslog-tls-key":
  175. case "syslog-tls-skip-verify":
  176. case "tag":
  177. case "syslog-format":
  178. default:
  179. return fmt.Errorf("unknown log opt '%s' for syslog log driver", key)
  180. }
  181. }
  182. if _, _, err := parseAddress(cfg["syslog-address"]); err != nil {
  183. return err
  184. }
  185. if _, err := parseFacility(cfg["syslog-facility"]); err != nil {
  186. return err
  187. }
  188. if _, _, err := parseLogFormat(cfg["syslog-format"], ""); err != nil {
  189. return err
  190. }
  191. return nil
  192. }
  193. func parseFacility(facility string) (syslog.Priority, error) {
  194. if facility == "" {
  195. return syslog.LOG_DAEMON, nil
  196. }
  197. if syslogFacility, valid := facilities[facility]; valid {
  198. return syslogFacility, nil
  199. }
  200. fInt, err := strconv.Atoi(facility)
  201. if err == nil && 0 <= fInt && fInt <= 23 {
  202. return syslog.Priority(fInt << 3), nil
  203. }
  204. return syslog.Priority(0), errors.New("invalid syslog facility")
  205. }
  206. func parseTLSConfig(cfg map[string]string) (*tls.Config, error) {
  207. _, skipVerify := cfg["syslog-tls-skip-verify"]
  208. opts := tlsconfig.Options{
  209. CAFile: cfg["syslog-tls-ca-cert"],
  210. CertFile: cfg["syslog-tls-cert"],
  211. KeyFile: cfg["syslog-tls-key"],
  212. InsecureSkipVerify: skipVerify,
  213. }
  214. return tlsconfig.Client(opts)
  215. }
  216. func parseLogFormat(logFormat, proto string) (syslog.Formatter, syslog.Framer, error) {
  217. switch logFormat {
  218. case "":
  219. return syslog.UnixFormatter, syslog.DefaultFramer, nil
  220. case "rfc3164":
  221. return syslog.RFC3164Formatter, syslog.DefaultFramer, nil
  222. case "rfc5424":
  223. if proto == secureProto {
  224. return rfc5424formatterWithAppNameAsTag, syslog.RFC5425MessageLengthFramer, nil
  225. }
  226. return rfc5424formatterWithAppNameAsTag, syslog.DefaultFramer, nil
  227. case "rfc5424micro":
  228. if proto == secureProto {
  229. return rfc5424microformatterWithAppNameAsTag, syslog.RFC5425MessageLengthFramer, nil
  230. }
  231. return rfc5424microformatterWithAppNameAsTag, syslog.DefaultFramer, nil
  232. default:
  233. return nil, nil, errors.New("Invalid syslog format")
  234. }
  235. }