fix(inputs.systemd_units): Reconnect if connection is lost (#15129)
This commit is contained in:
parent
ba9cbeebb5
commit
5e1a3cc8ca
|
|
@ -183,9 +183,19 @@ func (s *SystemdUnits) Stop() {
|
||||||
if s.client != nil && s.client.Connected() {
|
if s.client != nil && s.client.Connected() {
|
||||||
s.client.Close()
|
s.client.Close()
|
||||||
}
|
}
|
||||||
|
s.client = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SystemdUnits) Gather(acc telegraf.Accumulator) error {
|
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))
|
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(s.Timeout))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -209,6 +209,7 @@ func TestListFiles(t *testing.T) {
|
||||||
plugin := &SystemdUnits{
|
plugin := &SystemdUnits{
|
||||||
Pattern: "examp*",
|
Pattern: "examp*",
|
||||||
Timeout: config.Duration(time.Second),
|
Timeout: config.Duration(time.Second),
|
||||||
|
Log: testutil.Logger{},
|
||||||
}
|
}
|
||||||
require.NoError(t, plugin.Init())
|
require.NoError(t, plugin.Init())
|
||||||
|
|
||||||
|
|
@ -522,6 +523,7 @@ func TestShow(t *testing.T) {
|
||||||
Pattern: "examp*",
|
Pattern: "examp*",
|
||||||
Details: true,
|
Details: true,
|
||||||
Timeout: config.Duration(time.Second),
|
Timeout: config.Duration(time.Second),
|
||||||
|
Log: testutil.Logger{},
|
||||||
}
|
}
|
||||||
require.NoError(t, plugin.Init())
|
require.NoError(t, plugin.Init())
|
||||||
|
|
||||||
|
|
@ -715,6 +717,7 @@ func TestMultiInstance(t *testing.T) {
|
||||||
plugin := &SystemdUnits{
|
plugin := &SystemdUnits{
|
||||||
Pattern: tt.pattern,
|
Pattern: tt.pattern,
|
||||||
Timeout: config.Duration(time.Second),
|
Timeout: config.Duration(time.Second),
|
||||||
|
Log: testutil.Logger{},
|
||||||
}
|
}
|
||||||
require.NoError(t, plugin.Init())
|
require.NoError(t, plugin.Init())
|
||||||
|
|
||||||
|
|
@ -825,7 +828,7 @@ func (c *fakeClient) fixPropertyTypes() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *fakeClient) Connected() bool {
|
func (c *fakeClient) Connected() bool {
|
||||||
return !c.connected
|
return c.connected
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *fakeClient) Close() {
|
func (c *fakeClient) Close() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue