Skip to content

Commit

Permalink
fix(inputs.systemd_units): Reconnect if connection is lost (influxdat…
Browse files Browse the repository at this point in the history
  • Loading branch information
srebhan authored Apr 10, 2024
1 parent ba9cbee commit 5e1a3cc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
10 changes: 10 additions & 0 deletions plugins/inputs/systemd_units/systemd_units_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,19 @@ func (s *SystemdUnits) Stop() {
if s.client != nil && s.client.Connected() {
s.client.Close()
}
s.client = nil
}

func (s *SystemdUnits) Gather(acc telegraf.Accumulator) error {
// Reconnect in case the connection was lost
if !s.client.Connected() {
s.Log.Debug("Connection to systemd daemon lost, trying to reconnect...")
s.Stop()
if err := s.Start(acc); err != nil {
return err
}
}

ctx, cancel := context.WithTimeout(context.Background(), time.Duration(s.Timeout))
defer cancel()

Expand Down
5 changes: 4 additions & 1 deletion plugins/inputs/systemd_units/systemd_units_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ func TestListFiles(t *testing.T) {
plugin := &SystemdUnits{
Pattern: "examp*",
Timeout: config.Duration(time.Second),
Log: testutil.Logger{},
}
require.NoError(t, plugin.Init())

Expand Down Expand Up @@ -522,6 +523,7 @@ func TestShow(t *testing.T) {
Pattern: "examp*",
Details: true,
Timeout: config.Duration(time.Second),
Log: testutil.Logger{},
}
require.NoError(t, plugin.Init())

Expand Down Expand Up @@ -715,6 +717,7 @@ func TestMultiInstance(t *testing.T) {
plugin := &SystemdUnits{
Pattern: tt.pattern,
Timeout: config.Duration(time.Second),
Log: testutil.Logger{},
}
require.NoError(t, plugin.Init())

Expand Down Expand Up @@ -825,7 +828,7 @@ func (c *fakeClient) fixPropertyTypes() {
}

func (c *fakeClient) Connected() bool {
return !c.connected
return c.connected
}

func (c *fakeClient) Close() {
Expand Down

0 comments on commit 5e1a3cc

Please sign in to comment.