RAS plugin - fix for too many open files handlers (#8306)

This commit is contained in:
Paweł Żak 2020-10-23 23:11:32 +02:00 committed by GitHub
parent 9b23a04b69
commit 69230017b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 6 deletions

View File

@ -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
}

View File

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