Skip to content
This repository was archived by the owner on Nov 12, 2024. It is now read-only.

Mongo p2 #101

Merged
merged 3 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions agent/plugins/inputs/mongodb/collections/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package collections

import (
"encoding/json"
"reflect"

"zenit/agent/plugins/inputs"
"zenit/agent/plugins/lists/metrics"
"zenit/config"

"github.com/debeando/go-common/cast"
"github.com/debeando/go-common/log"
"github.com/debeando/go-common/mongodb"
"github.com/debeando/go-common/strings"
)

type Plugin struct {
Name string
Hostname string
Values metrics.Values
}

func (p *Plugin) Collect(name string, cnf *config.Config, mtc *metrics.Items) {
defer func() {
if err := recover(); err != nil {
log.ErrorWithFields(name, log.Fields{"message": err})
}
}()

for host := range cnf.Inputs.MongoDB {
log.DebugWithFields(name, log.Fields{
"hostname": cnf.Inputs.MongoDB[host].Hostname,
"enable": cnf.Inputs.MongoDB[host].Enable,
"server_collections": cnf.Inputs.MongoDB[host].Collections,
})

if !cnf.Inputs.MongoDB[host].Enable {
continue
}

if !cnf.Inputs.MongoDB[host].Collections {
continue
}

log.InfoWithFields(name, log.Fields{
"hostname": cnf.Inputs.MongoDB[host].Hostname,
})

m := mongodb.New(cnf.Inputs.MongoDB[host].Hostname, cnf.Inputs.MongoDB[host].DSN)
if err := m.Connect(); err != nil {
continue
}

databases := m.Databases()
for _, database := range databases.Databases {
ss := m.Collections(database.Name)

p.Name = name
p.Hostname = cnf.Inputs.MongoDB[host].Hostname

var obj map[string]interface{}
t, _ := json.Marshal(ss)
json.Unmarshal(t, &obj)

entry := reflect.ValueOf(obj)
p.Iterate([]string{""}, entry)

mtc.Add(metrics.Metric{
Key: "mongodb_collections",
Tags: []metrics.Tag{
{Name: "hostname", Value: cnf.Inputs.MongoDB[host].Hostname},
},
Values: p.Values,
})

p.Values.Reset()
}
}
}

func (p *Plugin) Iterate(parent []string, data reflect.Value) {
switch data.Kind() {
case reflect.Map:
for _, key := range data.MapKeys() {
value := data.MapIndex(key)

if value.IsZero() {
continue
}
if value.IsNil() {
continue
}

parent = append(parent, key.String())

p.Iterate(parent, reflect.ValueOf(value.Interface()))

parent = parent[:len(parent)-1]
}
default:
if cast.InterfaceIsNumber(data.Interface()) {
key := strings.ToCamel(strings.Join(parent, ""))
log.DebugWithFields(p.Name, log.Fields{
"hostname": p.Hostname,
key: data,
})

p.Values.Add(
metrics.Value{
Key: key,
Value: cast.InterfaceToFloat64(data.Interface()),
},
)
}
}
}

func init() {
inputs.Add("InputMongoDBCollections", func() inputs.Input { return &Plugin{} })
}
69 changes: 46 additions & 23 deletions agent/plugins/inputs/mongodb/serverstatus/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package serverstatus

import (
"fmt"
"encoding/json"
"reflect"

"zenit/agent/plugins/inputs"
Expand Down Expand Up @@ -29,9 +29,9 @@ func (p *Plugin) Collect(name string, cnf *config.Config, mtc *metrics.Items) {

for host := range cnf.Inputs.MongoDB {
log.DebugWithFields(name, log.Fields{
"hostname": cnf.Inputs.MongoDB[host].Hostname,
"enable": cnf.Inputs.MongoDB[host].Enable,
"variables": cnf.Inputs.MongoDB[host].ServerStatus,
"hostname": cnf.Inputs.MongoDB[host].Hostname,
"enable": cnf.Inputs.MongoDB[host].Enable,
"server_status": cnf.Inputs.MongoDB[host].ServerStatus,
})

if !cnf.Inputs.MongoDB[host].Enable {
Expand All @@ -47,13 +47,21 @@ func (p *Plugin) Collect(name string, cnf *config.Config, mtc *metrics.Items) {
})

m := mongodb.New(cnf.Inputs.MongoDB[host].Hostname, cnf.Inputs.MongoDB[host].DSN)
m.Connect()
ss := m.GetServerStatus()
data := reflect.ValueOf(ss)
if err := m.Connect(); err != nil {
continue
}

ss := m.ServerStatus()

p.Name = name
p.Hostname = cnf.Inputs.MongoDB[host].Hostname
p.iterate("", data)

var obj map[string]interface{}
t, _ := json.Marshal(ss)
json.Unmarshal(t, &obj)

entry := reflect.ValueOf(obj)
p.Iterate([]string{""}, entry)

mtc.Add(metrics.Metric{
Key: "mongodb_serverstatus",
Expand All @@ -67,24 +75,39 @@ func (p *Plugin) Collect(name string, cnf *config.Config, mtc *metrics.Items) {
}
}

func (p *Plugin) iterate(prefixKey string, data reflect.Value) {
func (p *Plugin) Iterate(parent []string, data reflect.Value) {
switch data.Kind() {
case reflect.Map:
for _, mapKey := range data.MapKeys() {
mapValue := data.MapIndex(mapKey).Interface()

if reflect.TypeOf(mapValue).Kind() == reflect.Map {
p.iterate(mapKey.String(), reflect.ValueOf(mapValue))
} else {
if cast.InterfaceIsNumber(mapValue) {
key := strings.ToCamel(fmt.Sprintf("%s %s", prefixKey, mapKey.String()))
log.DebugWithFields(p.Name, log.Fields{
"hostname": p.Hostname,
key: mapValue,
})
p.Values.Add(metrics.Value{Key: key, Value: mapValue})
}
for _, key := range data.MapKeys() {
value := data.MapIndex(key)

if value.IsZero() {
continue
}
if value.IsNil() {
continue
}

parent = append(parent, key.String())

p.Iterate(parent, reflect.ValueOf(value.Interface()))

parent = parent[:len(parent)-1]
}
default:
if cast.InterfaceIsNumber(data.Interface()) {
key := strings.ToCamel(strings.Join(parent, ""))
log.DebugWithFields(p.Name, log.Fields{
"hostname": p.Hostname,
key: data,
})

p.Values.Add(
metrics.Value{
Key: key,
Value: cast.InterfaceToFloat64(data.Interface()),
},
)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions agent/plugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

_ "zenit/agent/plugins/inputs/aws/cloudwatch/rds"
_ "zenit/agent/plugins/inputs/aws/discover/rds"
_ "zenit/agent/plugins/inputs/mongodb/collections"
_ "zenit/agent/plugins/inputs/mongodb/serverstatus"
_ "zenit/agent/plugins/inputs/mysql/aurora"
_ "zenit/agent/plugins/inputs/mysql/overflow"
Expand Down
2 changes: 1 addition & 1 deletion aws/database/describe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package describe
import (
"fmt"

"github.com/debeando/go-common/log"
"github.com/debeando/go-common/aws/rds"
"github.com/debeando/go-common/log"
"github.com/debeando/go-common/table"

"github.com/spf13/cobra"
Expand Down
2 changes: 1 addition & 1 deletion aws/database/logs/list/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package list
import (
"fmt"

"github.com/debeando/go-common/log"
"github.com/debeando/go-common/aws/rds"
"github.com/debeando/go-common/log"
"github.com/debeando/go-common/table"

"github.com/spf13/cobra"
Expand Down
2 changes: 1 addition & 1 deletion aws/database/logs/view/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package view
import (
"fmt"

"github.com/debeando/go-common/log"
"github.com/debeando/go-common/aws/rds"
"github.com/debeando/go-common/log"

"github.com/spf13/cobra"
)
Expand Down
3 changes: 2 additions & 1 deletion config/example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ inputs:
- hostname: localhost
dsn: mongodb://localhost:27017
# dsn: mongodb://user:password@localhost:27017
enable: false
enable: true
serverstatus: true
collections: true
mysql:
- hostname: localhost
dsn: root@tcp(127.0.0.1:3306)/?timeout=3s
Expand Down
1 change: 1 addition & 0 deletions config/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,5 @@ type MongoDB struct {
DSN string `yaml:"dsn"`
Enable bool `yaml:"enable"`
ServerStatus bool `yaml:"serverstatus"`
Collections bool `yaml:"collections"`
}
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ module zenit
go 1.21

require (
github.com/aws/aws-sdk-go v1.45.25
github.com/debeando/go-common v0.4.0
github.com/aws/aws-sdk-go v1.46.0
github.com/debeando/go-common v0.4.1
github.com/go-yaml/yaml v2.1.0+incompatible
github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c
github.com/kardianos/service v1.2.2
github.com/rodaine/table v1.1.0
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/spf13/cobra v1.7.0
)
Expand All @@ -18,11 +17,12 @@ require (
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/klauspost/compress v1.17.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
Expand Down
23 changes: 6 additions & 17 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
github.com/aws/aws-sdk-go v1.45.24 h1:TZx/CizkmCQn8Rtsb11iLYutEQVGK5PK9wAhwouELBo=
github.com/aws/aws-sdk-go v1.45.24/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/aws/aws-sdk-go v1.45.25 h1:c4fLlh5sLdK2DCRTY1z0hyuJZU4ygxX8m1FswL6/nF4=
github.com/aws/aws-sdk-go v1.45.25/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/aws/aws-sdk-go v1.46.0 h1:Igh7W8P+sA6mXJ9yhreOSweefLapcqekhxQlY1llxcM=
github.com/aws/aws-sdk-go v1.46.0/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/debeando/go-common v0.4.0 h1:K3ETw8jLR6wpaQxNTOK2DZrJuDGiuU1x4XXOZz7N6ck=
github.com/debeando/go-common v0.4.0/go.mod h1:j4HvEQqQaAcsgENpqTfnZXRKw+Q20zuLeFF302gSmIU=
github.com/debeando/go-common v0.4.1 h1:uXWNAuNvot7YV3DUI4zjNSKx8Z26gZBKkzrY8LemYRE=
github.com/debeando/go-common v0.4.1/go.mod h1:0u1RTk8umumGN+i+2V07g0itNWfDIUU19bsxEeFLMG4=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
Expand All @@ -34,27 +32,19 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfC
github.com/kardianos/service v1.2.2 h1:ZvePhAHfvo0A7Mftk/tEzqEZ7Q4lgnR8sGz4xu1YX60=
github.com/kardianos/service v1.2.2/go.mod h1:CIMRFEJVL+0DS1a3Nx06NaMn4Dz63Ng6O7dl0qH0zVM=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM=
github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/klauspost/compress v1.17.1 h1:NE3C767s2ak2bweCZo3+rdP4U/HoyVXLv/X9f2gPS5g=
github.com/klauspost/compress v1.17.1/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rodaine/table v1.1.0 h1:/fUlCSdjamMY8VifdQRIu3VWZXYLY7QHFkVorS8NTr4=
github.com/rodaine/table v1.1.0/go.mod h1:Qu3q5wi1jTQD6B6HsP6szie/S4w1QUQ8pq22pz9iL8g=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
Expand All @@ -65,7 +55,6 @@ github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRM
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
Expand Down
Loading