From 8a4d869e2e37a237fc52ebc29d12f0c172b99f1a Mon Sep 17 00:00:00 2001 From: Jonah Hirsch Date: Sun, 29 May 2022 00:23:11 -0700 Subject: [PATCH] Add the ability to set a custom hostname in the config Fixes #126 --- README.md | 17 +++++ config.go | 7 ++- config_test.go | 5 ++ examples/log_files.yml.example.advanced | 1 + remote_syslog.go | 11 +++- remote_syslog_test.go | 82 +++++++++++++++++++++++++ test/config.yaml | 3 + 7 files changed, 122 insertions(+), 4 deletions(-) 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..49f0487 100644 --- a/remote_syslog_test.go +++ b/remote_syslog_test.go @@ -77,6 +77,88 @@ func TestNewFileSeek(t *testing.T) { packet := <-server.packets assert.Equal(msg, packet.Message) + assert.Equal("testhost", packet.Hostname) + } +} + +func TestCustomTag(t *testing.T) { + assert := assert.New(t) + + // Close any existing channel and restart the testSyslogServer + server.closeCh <- struct{}{} + go server.serve() + + // Add custom tag + config := testConfig() + config.Files = []LogFile { + { + Path: "tmp/*.log", + Tag: "customTag", + }, + } + + s := NewServer(config) + 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("customTag", packet.Tag) + assert.Equal("testhost", packet.Hostname) + } +} + +func TestCustomHostname(t *testing.T) { + assert := assert.New(t) + + // Close any existing channel and restart the testSyslogServer + server.closeCh <- struct{}{} + go server.serve() + + // Add custom hostname + config := testConfig() + config.Files = []LogFile { + { + Path: "tmp/*.log", + Hostname: "custom.hostname", + }, + } + + s := NewServer(config) + 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("custom.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