diff --git a/README.md b/README.md index b5347d0..79d7e38 100644 --- a/README.md +++ b/README.md @@ -273,6 +273,23 @@ destination: This functionality was introduced in version 0.17 +### Choosing hostname name + +To override the hostname, which is sent as the system's hostname by default, +tell remote_syslog to set another hostname name using the `hostname` attribute +in the configuration file: +``` +files: + - path: /var/log/httpd/access_log + hostname: example.com +destination: + host: logs.papertrailapp.com + port: 12345 + protocol: tls +``` + +This is not supported by command line configurations. + ## Troubleshooting ### Generate debug log diff --git a/config.go b/config.go index a30d856..f69274b 100644 --- a/config.go +++ b/config.go @@ -65,6 +65,7 @@ type Config struct { type LogFile struct { Path string Tag string + Hostname string } func init() { @@ -299,6 +300,8 @@ func decodeLogFiles(f interface{}) ([]LogFile, error) { case string: lf := strings.Split(val, "=") switch len(lf) { + case 3: + files = append(files, LogFile{Tag: lf[0], Hostname: lf[1], Path: lf[2]}) case 2: files = append(files, LogFile{Tag: lf[0], Path: lf[1]}) case 1: @@ -310,17 +313,19 @@ func decodeLogFiles(f interface{}) ([]LogFile, error) { case map[interface{}]interface{}: var ( tag string + hostname string path string ) tag, _ = val["tag"].(string) + hostname, _ = val["hostname"].(string) path, _ = val["path"].(string) if path == "" { return files, fmt.Errorf("Invalid log file %#v", val) } - files = append(files, LogFile{Tag: tag, Path: path}) + files = append(files, LogFile{Tag: tag, Hostname: hostname, Path: path}) default: panic(vals) diff --git a/config_test.go b/config_test.go index 9de24a9..8d551ac 100644 --- a/config_test.go +++ b/config_test.go @@ -43,6 +43,11 @@ func TestRawConfig(t *testing.T) { Tag: "apache", Path: "/var/log/httpd/access_log", }, + { + Tag: "debian", + Path: "/var/log/syslog", + Hostname: "myhost.mydomain.com", + }, }) assert.Equal(c.TcpMaxLineLength, 99991) assert.Equal(c.NewFileCheckInterval, 10*time.Second) diff --git a/examples/log_files.yml.example.advanced b/examples/log_files.yml.example.advanced index 6321ecf..d141b9d 100644 --- a/examples/log_files.yml.example.advanced +++ b/examples/log_files.yml.example.advanced @@ -6,6 +6,7 @@ files: tag: site2/access_log - path: /var/log/httpd/site2/error_log tag: site2/error_log + hostname: example.com - /opt/misc/*.log - /home/**/*.log - /var/log/mysqld.log diff --git a/remote_syslog.go b/remote_syslog.go index fb8bc65..e874c8f 100644 --- a/remote_syslog.go +++ b/remote_syslog.go @@ -93,7 +93,7 @@ func (s *Server) closing() bool { } // Tails a single file -func (s *Server) tailOne(file, tag string, whence int) { +func (s *Server) tailOne(file, tag string, hostname string, whence int) { defer s.registry.Remove(file) t, err := follower.New(file, follower.Config{ @@ -111,6 +111,10 @@ func (s *Server) tailOne(file, tag string, whence int) { tag = path.Base(file) } + if hostname == "" { + hostname = s.logger.ClientHostname + } + for { select { case line, ok := <-t.Lines(): @@ -139,7 +143,7 @@ func (s *Server) tailOne(file, tag string, whence int) { Severity: s.config.Severity, Facility: s.config.Facility, Time: time.Now(), - Hostname: s.logger.ClientHostname, + Hostname: hostname, Tag: tag, Token: s.config.Destination.Token, Message: l, @@ -181,6 +185,7 @@ func (s *Server) globFiles(firstPass bool) { for _, glob := range s.config.Files { tag := glob.Tag + hostname := glob.Hostname files, err := filepath.Glob(utils.ResolvePath(glob.Path)) if err != nil { @@ -206,7 +211,7 @@ func (s *Server) globFiles(firstPass bool) { } s.registry.Add(file) - go s.tailOne(file, tag, whence) + go s.tailOne(file, tag, hostname, whence) } } } diff --git a/remote_syslog_test.go b/remote_syslog_test.go index 6af80f1..1526b3a 100644 --- a/remote_syslog_test.go +++ b/remote_syslog_test.go @@ -77,6 +77,98 @@ func TestNewFileSeek(t *testing.T) { packet := <-server.packets assert.Equal(msg, packet.Message) + assert.Equal("testhost", packet.Hostname) + } +} + +func TestCustomTag(t *testing.T) { + os.RemoveAll(tmpdir) + os.Mkdir(tmpdir, 0755) + + assert := assert.New(t) + + server = newTestSyslogServer("127.0.0.1:0") + go server.serve() + + // Add custom tag + config := testConfig() + path := "tmp/*.log" + tag := "customTag" + config.Files = []LogFile { + { + Path: path, + Tag: tag, + }, + } + + s := NewServer(config) + s.registry = NewInMemoryRegistry() + go s.Start() + defer s.Close() + + // just a quick rest to get the server started + time.Sleep(1 * time.Second) + + for _, msg := range []string{ + "welcome to the jungle", + "we got alerts and logs", + "we got everything you want", + "as long as it's alerts and logs", + } { + file := tmpLogFile() + defer file.Close() + + writeLog(file, msg) + + packet := <-server.packets + assert.Equal(msg, packet.Message) + assert.Equal(tag, packet.Tag) + assert.Equal("testhost", packet.Hostname) + } +} + +func TestCustomHostname(t *testing.T) { + os.RemoveAll(tmpdir) + os.Mkdir(tmpdir, 0755) + + assert := assert.New(t) + + server = newTestSyslogServer("127.0.0.1:0") + go server.serve() + + // Add custom hostname + config := testConfig() + path := "tmp/*.log" + hostname := "custom.hostname" + config.Files = []LogFile { + { + Path: path, + Hostname: hostname, + }, + } + + s := NewServer(config) + s.registry = NewInMemoryRegistry() + go s.Start() + defer s.Close() + + // just a quick rest to get the server started + time.Sleep(1 * time.Second) + + for _, msg := range []string{ + "welcome to the jungle", + "we got alerts and logs", + "we got everything you want", + "as long as it's alerts and logs", + } { + file := tmpLogFile() + defer file.Close() + + writeLog(file, msg) + + packet := <-server.packets + assert.Equal(msg, packet.Message) + assert.Equal(hostname, packet.Hostname) } } diff --git a/test/config.yaml b/test/config.yaml index 22e7023..36c6889 100644 --- a/test/config.yaml +++ b/test/config.yaml @@ -4,6 +4,9 @@ files: - "nginx=/var/log/nginx/nginx.log" - path: /var/log/httpd/access_log tag: apache + - path: /var/log/syslog + tag: debian + hostname: myhost.mydomain.com destination: host: logs.papertrailapp.com port: 514