github.com/netdata/go.d.plugin@v0.58.1/modules/mongodb/collect_dbstats.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package mongo
     4  
     5  import (
     6  	"fmt"
     7  	"strings"
     8  
     9  	"github.com/netdata/go.d.plugin/agent/module"
    10  )
    11  
    12  func (m *Mongo) collectDbStats(mx map[string]int64) error {
    13  	if m.dbSelector == nil {
    14  		m.Debug("'database' selector not set, skip collecting database statistics")
    15  		return nil
    16  	}
    17  
    18  	allDBs, err := m.conn.listDatabaseNames()
    19  	if err != nil {
    20  		return fmt.Errorf("cannot get database names: %v", err)
    21  	}
    22  
    23  	m.Debugf("all databases on the server: '%v'", allDBs)
    24  
    25  	var dbs []string
    26  	for _, db := range allDBs {
    27  		if m.dbSelector.MatchString(db) {
    28  			dbs = append(dbs, db)
    29  		}
    30  	}
    31  
    32  	if len(allDBs) != len(dbs) {
    33  		m.Debugf("databases remaining after filtering: %v", dbs)
    34  	}
    35  
    36  	seen := make(map[string]bool)
    37  	for _, db := range dbs {
    38  		s, err := m.conn.dbStats(db)
    39  		if err != nil {
    40  			return fmt.Errorf("dbStats command failed: %v", err)
    41  		}
    42  
    43  		seen[db] = true
    44  
    45  		mx["database_"+db+"_collections"] = s.Collections
    46  		mx["database_"+db+"_views"] = s.Views
    47  		mx["database_"+db+"_indexes"] = s.Indexes
    48  		mx["database_"+db+"_documents"] = s.Objects
    49  		mx["database_"+db+"_data_size"] = s.DataSize
    50  		mx["database_"+db+"_index_size"] = s.IndexSize
    51  		mx["database_"+db+"_storage_size"] = s.StorageSize
    52  	}
    53  
    54  	for db := range seen {
    55  		if !m.databases[db] {
    56  			m.databases[db] = true
    57  			m.Debugf("new database '%s': creating charts", db)
    58  			m.addDatabaseCharts(db)
    59  		}
    60  	}
    61  
    62  	for db := range m.databases {
    63  		if !seen[db] {
    64  			delete(m.databases, db)
    65  			m.Debugf("stale database '%s': removing charts", db)
    66  			m.removeDatabaseCharts(db)
    67  		}
    68  	}
    69  
    70  	return nil
    71  }
    72  
    73  func (m *Mongo) addDatabaseCharts(name string) {
    74  	charts := chartsTmplDatabase.Copy()
    75  
    76  	for _, chart := range *charts {
    77  		chart.ID = fmt.Sprintf(chart.ID, name)
    78  		chart.Labels = []module.Label{
    79  			{Key: "database", Value: name},
    80  		}
    81  		for _, dim := range chart.Dims {
    82  			dim.ID = fmt.Sprintf(dim.ID, name)
    83  		}
    84  	}
    85  
    86  	if err := m.Charts().Add(*charts...); err != nil {
    87  		m.Warning(err)
    88  	}
    89  }
    90  
    91  func (m *Mongo) removeDatabaseCharts(name string) {
    92  	px := fmt.Sprintf("%s%s_", chartPxDatabase, name)
    93  
    94  	for _, chart := range *m.Charts() {
    95  		if strings.HasPrefix(chart.ID, px) {
    96  			chart.MarkRemove()
    97  			chart.MarkNotCreated()
    98  		}
    99  	}
   100  }