This repository has been archived by the owner on Nov 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat - add new plugin: mongodb/serverstatus
- Loading branch information
Nicola Strappazzon C
committed
Oct 10, 2023
1 parent
e5fa69a
commit 2921230
Showing
7 changed files
with
195 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package serverstatus | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
"zenit/config" | ||
"zenit/monitor/plugins/inputs" | ||
"zenit/monitor/plugins/lists/metrics" | ||
|
||
"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.Value | ||
} | ||
|
||
func (p *Plugin) Collect(name string, cnf *config.Config, mtc *metrics.Items) { | ||
defer func() { | ||
if err := recover(); err != nil { | ||
log.ErrorWithFields(name, log.Fields{"error": err}) | ||
} | ||
}() | ||
|
||
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, | ||
}) | ||
|
||
if !cnf.Inputs.MongoDB[host].Enable { | ||
continue | ||
} | ||
|
||
if !cnf.Inputs.MongoDB[host].ServerStatus { | ||
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) | ||
m.Connect() | ||
ss := m.GetServerStatus() | ||
data := reflect.ValueOf(ss) | ||
|
||
p.Name = name | ||
p.Hostname = cnf.Inputs.MongoDB[host].Hostname | ||
p.iterate("", data) | ||
|
||
mtc.Add(metrics.Metric{ | ||
Key: "mongodb_serverstatus", | ||
Tags: []metrics.Tag{ | ||
{Name: "hostname", Value: cnf.Inputs.MongoDB[host].Hostname}, | ||
}, | ||
Values: p.Values, | ||
}) | ||
|
||
p.resetValues() | ||
} | ||
} | ||
|
||
func (p *Plugin) iterate(k string, data reflect.Value) { | ||
switch data.Kind() { | ||
case reflect.Map: | ||
for _, key := range data.MapKeys() { | ||
value := data.MapIndex(key) | ||
|
||
if reflect.TypeOf(value.Interface()).Kind() == reflect.Map { | ||
p.iterate(key.String(), reflect.ValueOf(value.Interface())) | ||
} else { | ||
if cast.InterfaceIsNumber(value.Interface()) { | ||
k := fmt.Sprintf("%s%s%s", strings.ToCamel(k), separator(k, "."), strings.ToCamel(key.String())) | ||
v := value.Interface() | ||
log.DebugWithFields(p.Name, log.Fields{ | ||
"hostname": p.Hostname, | ||
k: v, | ||
}) | ||
p.Values = append(p.Values, metrics.Value{Key: k, Value: v}) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (p *Plugin) resetValues() { | ||
p.Values = []metrics.Value{} | ||
} | ||
|
||
func separator(value, char string) string { | ||
if len(value) > 0 { | ||
return char | ||
} | ||
return "" | ||
} | ||
|
||
func init() { | ||
inputs.Add("InputMongoDBServerStatus", func() inputs.Input { return &Plugin{} }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters