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

Add the ability to set a custom hostname in the config #246

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
82 changes: 82 additions & 0 deletions remote_syslog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
3 changes: 3 additions & 0 deletions test/config.yaml
Original file line number Diff line number Diff line change
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