From 69230017b0e89216d002e3923a38af9c8a9e713d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20=C5=BBak?= Date: Fri, 23 Oct 2020 23:11:32 +0200 Subject: [PATCH] RAS plugin - fix for too many open files handlers (#8306) --- plugins/inputs/ras/ras.go | 27 ++++++++++++++++++++++----- plugins/inputs/ras/ras_test.go | 2 +- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/plugins/inputs/ras/ras.go b/plugins/inputs/ras/ras.go index ae7da02a6..630e712d8 100644 --- a/plugins/inputs/ras/ras.go +++ b/plugins/inputs/ras/ras.go @@ -20,6 +20,9 @@ import ( type Ras struct { DBPath string `toml:"db_path"` + Log telegraf.Logger `toml:"-"` + db *sql.DB `toml:"-"` + latestTimestamp time.Time `toml:"-"` cpuSocketCounters map[int]metricCounters `toml:"-"` serverCounters metricCounters `toml:"-"` @@ -77,20 +80,34 @@ func (r *Ras) Description() string { return "RAS plugin exposes counter metrics for Machine Check Errors provided by RASDaemon (sqlite3 output is required)." } -// Gather reads the stats provided by RASDaemon and writes it to the Accumulator. -func (r *Ras) Gather(acc telegraf.Accumulator) error { +// Start initializes connection to DB, metrics are gathered in Gather +func (r *Ras) Start(telegraf.Accumulator) error { err := validateDbPath(r.DBPath) if err != nil { return err } - db, err := connectToDB(r.DBPath) + r.db, err = connectToDB(r.DBPath) if err != nil { return err } - defer db.Close() - rows, err := db.Query(mceQuery, r.latestTimestamp) + return nil +} + +// Stop closes any existing DB connection +func (r *Ras) Stop() { + if r.db != nil { + err := r.db.Close() + if err != nil { + r.Log.Errorf("Error appeared during closing DB (%s): %v", r.DBPath, err) + } + } +} + +// Gather reads the stats provided by RASDaemon and writes it to the Accumulator. +func (r *Ras) Gather(acc telegraf.Accumulator) error { + rows, err := r.db.Query(mceQuery, r.latestTimestamp) if err != nil { return err } diff --git a/plugins/inputs/ras/ras_test.go b/plugins/inputs/ras/ras_test.go index 1c14c6d8f..900eb8fb8 100644 --- a/plugins/inputs/ras/ras_test.go +++ b/plugins/inputs/ras/ras_test.go @@ -114,7 +114,7 @@ func TestMissingDatabase(t *testing.T) { var acc testutil.Accumulator ras := newRas() ras.DBPath = "/tmp/test.db" - err := ras.Gather(&acc) + err := ras.Start(&acc) assert.Error(t, err) }