fix(inputs.systemd_units): Reconnect if connection is lost (#15129)

This commit is contained in:
Sven Rebhan 2024-04-10 11:13:25 -04:00 committed by GitHub
parent ba9cbeebb5
commit 5e1a3cc8ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 1 deletions

View File

@ -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()

View File

@ -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())
@ -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())
@ -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())
@ -825,7 +828,7 @@ func (c *fakeClient) fixPropertyTypes() {
}
func (c *fakeClient) Connected() bool {
return !c.connected
return c.connected
}
func (c *fakeClient) Close() {