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

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package mongo
     4  
     5  import (
     6  	_ "embed"
     7  	"sync"
     8  	"time"
     9  
    10  	"github.com/netdata/go.d.plugin/agent/module"
    11  	"github.com/netdata/go.d.plugin/pkg/matcher"
    12  )
    13  
    14  //go:embed "config_schema.json"
    15  var configSchema string
    16  
    17  func init() {
    18  	module.Register("mongodb", module.Creator{
    19  		JobConfigSchema: configSchema,
    20  		Create:          func() module.Module { return New() },
    21  	})
    22  }
    23  
    24  func New() *Mongo {
    25  	return &Mongo{
    26  		Config: Config{
    27  			Timeout: 2,
    28  			URI:     "mongodb://localhost:27017",
    29  			Databases: matcher.SimpleExpr{
    30  				Includes: []string{},
    31  				Excludes: []string{},
    32  			},
    33  		},
    34  
    35  		conn: &mongoClient{},
    36  
    37  		charts:                chartsServerStatus.Copy(),
    38  		addShardingChartsOnce: &sync.Once{},
    39  
    40  		optionalCharts: make(map[string]bool),
    41  		replSetMembers: make(map[string]bool),
    42  		databases:      make(map[string]bool),
    43  		shards:         make(map[string]bool),
    44  	}
    45  }
    46  
    47  type Config struct {
    48  	URI       string             `yaml:"uri"`
    49  	Timeout   time.Duration      `yaml:"timeout"`
    50  	Databases matcher.SimpleExpr `yaml:"databases"`
    51  }
    52  
    53  type Mongo struct {
    54  	module.Base
    55  	Config `yaml:",inline"`
    56  
    57  	charts *module.Charts
    58  
    59  	conn mongoConn
    60  
    61  	dbSelector matcher.Matcher
    62  
    63  	addShardingChartsOnce *sync.Once
    64  
    65  	optionalCharts map[string]bool
    66  	databases      map[string]bool
    67  	replSetMembers map[string]bool
    68  	shards         map[string]bool
    69  }
    70  
    71  func (m *Mongo) Init() bool {
    72  	if err := m.verifyConfig(); err != nil {
    73  		m.Errorf("config validation: %v", err)
    74  		return false
    75  	}
    76  
    77  	if err := m.initDatabaseSelector(); err != nil {
    78  		m.Errorf("init database selector: %v", err)
    79  		return false
    80  	}
    81  
    82  	return true
    83  }
    84  
    85  func (m *Mongo) Check() bool {
    86  	return len(m.Collect()) > 0
    87  }
    88  
    89  func (m *Mongo) Charts() *module.Charts {
    90  	return m.charts
    91  }
    92  
    93  func (m *Mongo) Collect() map[string]int64 {
    94  	mx, err := m.collect()
    95  	if err != nil {
    96  		m.Error(err)
    97  	}
    98  
    99  	if len(mx) == 0 {
   100  		m.Warning("no values collected")
   101  		return nil
   102  	}
   103  
   104  	return mx
   105  }
   106  
   107  func (m *Mongo) Cleanup() {
   108  	if m.conn == nil {
   109  		return
   110  	}
   111  	if err := m.conn.close(); err != nil {
   112  		m.Warningf("cleanup: error on closing mongo conn: %v", err)
   113  	}
   114  }