Skip to content

Commit

Permalink
Add the ability to set a custom hostname in the config
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahwh committed May 29, 2022
1 parent 7d8579f commit a1632dd
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 4 deletions.
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -273,6 +273,23 @@ destination:

This functionality was introduced in version 0.17

### Choosing hostname name

To override the hostname, whic 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
Expand Down
7 changes: 6 additions & 1 deletion config.go
Expand Up @@ -65,6 +65,7 @@ type Config struct {
type LogFile struct {
Path string
Tag string
Hostname string
}

func init() {
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions config_test.go
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions examples/log_files.yml.example.advanced
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions remote_syslog.go
Expand Up @@ -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{
Expand All @@ -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():
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}
}
Expand Down
92 changes: 92 additions & 0 deletions remote_syslog_test.go
Expand Up @@ -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)
}
}

Expand Down
3 changes: 3 additions & 0 deletions test/config.yaml
Expand Up @@ -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
Expand Down

0 comments on commit a1632dd

Please sign in to comment.