Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add StorePerformanceLabelAsField and StoreCommandAsField options #42

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion collector/IPrintable.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package collector

//Printable this interface should be used to push data into the queue.
type Printable interface {
PrintForInfluxDB(version string) string
PrintForInfluxDB(version string, i int) string
PrintForElasticsearch(version, index string) string
TestTargetFilter(string) bool
}
2 changes: 1 addition & 1 deletion collector/SimplePrintable.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type SimplePrintable struct {
}

//PrintForInfluxDB generates an String for InfluxDB
func (p SimplePrintable) PrintForInfluxDB(version string) string {
func (p SimplePrintable) PrintForInfluxDB(version string, i int) string {
if p.Datatype == data.InfluxDB {
return p.Text
}
Expand Down
2 changes: 1 addition & 1 deletion collector/livestatus/Collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ Loop:
for roundsToWait != 0 {
select {
case versionPrintable := <-printables:
version = versionPrintable.PrintForInfluxDB("0")
version = versionPrintable.PrintForInfluxDB("0", 0)
break Loop
case <-time.After(oneMinute):
if i < roundsToWait {
Expand Down
2 changes: 1 addition & 1 deletion collector/livestatus/CommentData.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (comment *CommentData) sanitizeValues() {
}

//PrintForInfluxDB prints the data in influxdb lineformat
func (comment CommentData) PrintForInfluxDB(version string) string {
func (comment CommentData) PrintForInfluxDB(version string, i int) string {
comment.sanitizeValues()
if helper.VersionOrdinal(version) >= helper.VersionOrdinal("0.9") {
var tags string
Expand Down
2 changes: 1 addition & 1 deletion collector/livestatus/DowntimeData.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (downtime *DowntimeData) sanitizeValues() {
}

//PrintForInfluxDB prints the data in influxdb lineformat
func (downtime DowntimeData) PrintForInfluxDB(version string) string {
func (downtime DowntimeData) PrintForInfluxDB(version string, i int) string {
downtime.sanitizeValues()
if helper.VersionOrdinal(version) >= helper.VersionOrdinal("0.9") {
tags := ",type=downtime,author=" + downtime.author
Expand Down
2 changes: 1 addition & 1 deletion collector/livestatus/NotificationData.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (notification *NotificationData) sanitizeValues() {
}

//PrintForInfluxDB prints the data in influxdb lineformat
func (notification NotificationData) PrintForInfluxDB(version string) string {
func (notification NotificationData) PrintForInfluxDB(version string, i int) string {
notification.sanitizeValues()
if helper.VersionOrdinal(version) >= helper.VersionOrdinal("0.9") {
var tags string
Expand Down
2 changes: 1 addition & 1 deletion collector/nagflux/NagfluxPrintable.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Printable struct {
}

//PrintForInfluxDB prints the data in influxdb lineformat
func (p Printable) PrintForInfluxDB(version string) string {
func (p Printable) PrintForInfluxDB(version string, i int) string {
if helper.VersionOrdinal(version) >= helper.VersionOrdinal("0.9") {
line := p.Table
if len(p.tags) > 0 {
Expand Down
22 changes: 19 additions & 3 deletions collector/spoolfile/performanceData.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,33 @@ type PerformanceData struct {
}

//PrintForInfluxDB prints the data in influxdb lineformat
func (p PerformanceData) PrintForInfluxDB(version string) string {
func (p PerformanceData) PrintForInfluxDB(version string, i int) string {
if helper.VersionOrdinal(version) >= helper.VersionOrdinal("0.9") {
tableName := fmt.Sprintf(`metrics,host=%s`, helper.SanitizeInfluxInput(p.Hostname))
if p.Service == "" {
tableName += fmt.Sprintf(`,service=%s`, helper.SanitizeInfluxInput(config.GetConfig().InfluxDBGlobal.HostcheckAlias))
} else {
tableName += fmt.Sprintf(`,service=%s`, helper.SanitizeInfluxInput(p.Service))
}
tableName += fmt.Sprintf(`,command=%s,performanceLabel=%s`,
var fieldsString = ""
if config.GetConfig().InfluxDBGlobal.StorePerformanceLabelAsField {
tableName += fmt.Sprintf(`,performanceLabelIndex=%d`,
i,
)
}

tableName += fmt.Sprintf(`,command=%s`,
helper.SanitizeInfluxInput(p.Command),
helper.SanitizeInfluxInput(p.PerformanceLabel),
)
if config.GetConfig().InfluxDBGlobal.StorePerformanceLabelAsField {
fieldsString += fmt.Sprintf(`,performanceLabel="%s"`,
helper.SanitizeInfluxField(p.PerformanceLabel),
)
} else {
tableName += fmt.Sprintf(`,performanceLabel=%s`,
helper.SanitizeInfluxInput(p.PerformanceLabel),
)
}
if len(p.Tags) > 0 {
tableName += fmt.Sprintf(`,%s`, helper.PrintMapAsString(helper.SanitizeMap(p.Tags), ",", "="))
}
Expand All @@ -41,6 +56,7 @@ func (p PerformanceData) PrintForInfluxDB(version string) string {
}

tableName += fmt.Sprintf(` %s`, helper.PrintMapAsString(helper.SanitizeMap(p.Fields), ",", "="))
tableName += fieldsString
tableName += fmt.Sprintf(" %s\n", p.Time)
return tableName
}
Expand Down
6 changes: 6 additions & 0 deletions config.gcfg.example
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
NastyStringToReplace = ""
HostcheckAlias = "hostcheck"
ClientTimeout = 5
# on big installations with lot of checks that have device dependend performance labels
# e.g. check_interface_table, the series cardinality in influxdb can explode and therefore
# cause write timeouts.
# In such cases, setting PerformanceLabel as Field value will significantly reduce the series
# cardinality.
StorePerformanceLabelAsField = false

[InfluxDB "nagflux"]
Enabled = true
Expand Down
11 changes: 6 additions & 5 deletions config/Config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ type Config struct {
PrometheusAddress string
}
InfluxDBGlobal struct {
CreateDatabaseIfNotExists bool
NastyString string
NastyStringToReplace string
HostcheckAlias string
ClientTimeout int
CreateDatabaseIfNotExists bool
NastyString string
NastyStringToReplace string
HostcheckAlias string
ClientTimeout int
StorePerformanceLabelAsField bool
}
InfluxDB map[string]*struct {
Enabled bool
Expand Down
17 changes: 17 additions & 0 deletions helper/influx.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ func SanitizeInfluxInput(input string) string {
return input
}

//SanitizeInfluxField escapes '"' chars only
func SanitizeInfluxField(input string) string {

if config.GetConfig().InfluxDBGlobal.NastyString != "" {
input = strings.Replace(
input,
config.GetConfig().InfluxDBGlobal.NastyString,
config.GetConfig().InfluxDBGlobal.NastyStringToReplace,
-1,
)
}

input = strings.Replace(input, "\"", `\"`, -1)

return input
}

//SanitizeMap calls SanitizeInfluxInput in key and value
func SanitizeMap(input map[string]string) map[string]string {
result := map[string]string{}
Expand Down
10 changes: 5 additions & 5 deletions target/influx/Worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ func (worker Worker) sendBuffer(queries []collector.Printable) {
}

var lineQueries []string
for _, query := range queries {
cast, castErr := worker.castJobToString(query)
for i, query := range queries {
cast, castErr := worker.castJobToString(query, i)
if castErr == nil {
lineQueries = append(lineQueries, cast)
}
Expand Down Expand Up @@ -192,7 +192,7 @@ func (worker Worker) readQueriesFromQueue() []string {
select {
case query = <-worker.jobs:
if query.TestTargetFilter(worker.target.Name) {
cast, err := worker.castJobToString(query)
cast, err := worker.castJobToString(query, 0)
if err == nil {
queries = append(queries, cast)
}
Expand Down Expand Up @@ -305,12 +305,12 @@ func (worker Worker) dumpQueries(filename string, queries []string) {
}

//Converts an collector.Printable to a string.
func (worker Worker) castJobToString(job collector.Printable) (string, error) {
func (worker Worker) castJobToString(job collector.Printable, i int) (string, error) {
var result string
var err error

if helper.VersionOrdinal(worker.version) >= helper.VersionOrdinal("0.9") {
result = job.PrintForInfluxDB(worker.version)
result = job.PrintForInfluxDB(worker.version, i)
} else {
worker.log.Fatalf("This influxversion [%s] given in the config is not supported", worker.version)
err = errors.New("This influxversion given in the config is not supported")
Expand Down