Skip to content

Commit

Permalink
use pointer receiver everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Nov 13, 2024
1 parent de398d0 commit 39c45d5
Show file tree
Hide file tree
Showing 20 changed files with 76 additions and 82 deletions.
2 changes: 0 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ linters:
- wrapcheck
- wsl
# deprecated
- execinquery
- exportloopref
- gomnd
issues:
max-same-issues: 0
max-issues-per-linter: 0
Expand Down
4 changes: 2 additions & 2 deletions pkg/nagflux/collector/SimplePrintable.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ type SimplePrintable struct {
}

// PrintForInfluxDB generates an String for InfluxDB
func (p SimplePrintable) PrintForInfluxDB(_ string) string {
func (p *SimplePrintable) PrintForInfluxDB(_ string) string {
if p.Datatype == data.InfluxDB {
return p.Text
}
return ""
}

// PrintForElasticsearch generates an String for Elasticsearch
func (p SimplePrintable) PrintForElasticsearch(_, _ string) string {
func (p *SimplePrintable) PrintForElasticsearch(_, _ string) string {
if p.Datatype == data.Elasticsearch {
return p.Text
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/nagflux/collector/livestatus/Collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (live *Collector) Stop() {
}

// Loop which checks livestats for data or waits to quit.
func (live Collector) run() {
func (live *Collector) run() {
live.queryData()
for {
select {
Expand All @@ -129,7 +129,7 @@ func (live Collector) run() {
}

// Queries livestatus and returns the data to the gobal queue
func (live Collector) queryData() {
func (live *Collector) queryData() {
printables := make(chan collector.Printable)
finished := make(chan bool)
go live.requestPrintablesFromLivestatus(live.logQuery, true, printables, finished)
Expand All @@ -150,7 +150,7 @@ func (live Collector) queryData() {
}
}

func (live Collector) requestPrintablesFromLivestatus(query string, addTimestampToQuery bool, printables chan collector.Printable, outerFinish chan bool) {
func (live *Collector) requestPrintablesFromLivestatus(query string, addTimestampToQuery bool, printables chan collector.Printable, outerFinish chan bool) {
queryWithTimestamp := query
if addTimestampToQuery {
queryWithTimestamp = addTimestampToLivestatusQuery(query)
Expand All @@ -174,19 +174,19 @@ func (live Collector) requestPrintablesFromLivestatus(query string, addTimestamp
}
case QueryForComments:
if len(line) == 6 {
printables <- CommentData{collector.AllFilterable, Data{line[0], line[1], line[2], line[3], line[4]}, line[5]}
printables <- &CommentData{collector.AllFilterable, Data{line[0], line[1], line[2], line[3], line[4]}, line[5]}
} else {
live.log.Warn("QueryForComments out of range", line)
}
case QueryForDowntimes:
if len(line) == 6 {
printables <- DowntimeData{collector.AllFilterable, Data{line[0], line[1], line[2], line[3], line[4]}, line[5]}
printables <- &DowntimeData{collector.AllFilterable, Data{line[0], line[1], line[2], line[3], line[4]}, line[5]}
} else {
live.log.Warn("QueryForDowntimes out of range", line)
}
case QueryLivestatusVersion:
if len(line) == 1 {
printables <- collector.SimplePrintable{Filterable: collector.AllFilterable, Text: line[0], Datatype: data.InfluxDB}
printables <- &collector.SimplePrintable{Filterable: collector.AllFilterable, Text: line[0], Datatype: data.InfluxDB}
} else {
live.log.Warn("QueryLivestatusVersion out of range", line)
}
Expand All @@ -206,7 +206,7 @@ func addTimestampToLivestatusQuery(query string) string {
return fmt.Sprintf(query, time.Now().Add(intervalToCheckLivestatus/100*-150).Unix())
}

func (live Collector) handleQueryForNotifications(line []string) *NotificationData {
func (live *Collector) handleQueryForNotifications(line []string) *NotificationData {
switch line[0] {
case "HOST NOTIFICATION":
if len(line) == 10 {
Expand Down
6 changes: 3 additions & 3 deletions pkg/nagflux/collector/livestatus/CommentData.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ func (comment *CommentData) sanitizeValues() {
}

// PrintForInfluxDB prints the data in influxdb lineformat
func (comment CommentData) PrintForInfluxDB(version string) string {
comment.sanitizeValues()
func (comment *CommentData) PrintForInfluxDB(version string) string {
if helper.VersionOrdinal(version) >= helper.VersionOrdinal("0.9") {
comment.sanitizeValues()
var tags string
if text := commentIDToText(comment.entryType); text != "" {
tags = ",type=" + text
Expand All @@ -33,7 +33,7 @@ func (comment CommentData) PrintForInfluxDB(version string) string {
}

// PrintForElasticsearch prints in the elasticsearch json format
func (comment CommentData) PrintForElasticsearch(version, index string) string {
func (comment *CommentData) PrintForElasticsearch(version, index string) string {
if helper.VersionOrdinal(version) >= helper.VersionOrdinal("2.0") {
typ := commentIDToText(comment.entryType)
return comment.genElasticLineWithValue(index, typ, comment.comment, comment.entryTime)
Expand Down
2 changes: 1 addition & 1 deletion pkg/nagflux/collector/livestatus/Connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Connector struct {
}

// Queries livestatus and returns an list of list outer list are lines inner elements within the line.
func (connector Connector) connectToLivestatus(query string, result chan []string, outerFinish chan bool) {
func (connector *Connector) connectToLivestatus(query string, result chan []string, outerFinish chan bool) {
var conn net.Conn
switch connector.ConnectionType {
case "tcp":
Expand Down
8 changes: 4 additions & 4 deletions pkg/nagflux/collector/livestatus/Data.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@ func (live *Data) sanitizeValues() {
}

// Generates the Influxdb tablename.
func (live Data) getTablename() string {
func (live *Data) getTablename() string {
if live.serviceDisplayName == "" {
live.serviceDisplayName = config.GetConfig().InfluxDBGlobal.HostcheckAlias
}
return fmt.Sprintf("messages,host=%s,service=%s", live.hostName, live.serviceDisplayName)
}

// Generates the linedata which can be parsed from influxdb
func (live Data) genInfluxLine(tags string) string {
func (live *Data) genInfluxLine(tags string) string {
return live.genInfluxLineWithValue(tags, live.comment)
}

// Generates the linedata which can be parsed from influxdb
func (live Data) genInfluxLineWithValue(tags, text string) string {
func (live *Data) genInfluxLineWithValue(tags, text string) string {
tags += ",author=" + live.author
return fmt.Sprintf("%s%s message=\"%s\" %s", live.getTablename(), tags, text, helper.CastStringTimeFromSToMs(live.entryTime))
}

func (live Data) genElasticLineWithValue(index, typ, value, timestamp string) string {
func (live *Data) genElasticLineWithValue(index, typ, value, timestamp string) string {
value = strings.Replace(value, `"`, `\"`, -1)
if live.serviceDisplayName == "" {
live.serviceDisplayName = config.GetConfig().ElasticsearchGlobal.HostcheckAlias
Expand Down
10 changes: 5 additions & 5 deletions pkg/nagflux/collector/livestatus/DowntimeData.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@ func (downtime *DowntimeData) sanitizeValues() {
}

// PrintForInfluxDB prints the data in influxdb lineformat
func (downtime DowntimeData) PrintForInfluxDB(version string) string {
downtime.sanitizeValues()
func (downtime *DowntimeData) PrintForInfluxDB(version string) string {
if helper.VersionOrdinal(version) >= helper.VersionOrdinal("0.9") {
downtime.sanitizeValues()
tags := ",type=downtime,author=" + downtime.author
start := fmt.Sprintf("%s%s message=\"%s\" %s", downtime.getTablename(), tags, strings.TrimSpace("Downtime start: <br>"+downtime.comment), helper.CastStringTimeFromSToMs(downtime.entryTime))
end := fmt.Sprintf("%s%s message=\"%s\" %s", downtime.getTablename(), tags, strings.TrimSpace("Downtime end: <br>"+downtime.comment), helper.CastStringTimeFromSToMs(downtime.endTime))
return start + "\n" + end
}
logging.GetLogger().Criticalf("This influxversion [%s] given in the config is not supported", version)
panic("")
panic("influxdb version not supported")
}

// PrintForElasticsearch prints in the elasticsearch json format
func (downtime DowntimeData) PrintForElasticsearch(version, index string) string {
func (downtime *DowntimeData) PrintForElasticsearch(version, index string) string {
if helper.VersionOrdinal(version) >= helper.VersionOrdinal("2.0") {
typ := `downtime`
start := downtime.genElasticLineWithValue(index, typ, strings.TrimSpace("Downtime start: <br>"+downtime.comment), downtime.entryTime)
end := downtime.genElasticLineWithValue(index, typ, strings.TrimSpace("Downtime end: <br>"+downtime.comment), downtime.endTime)
return start + "\n" + end
}
logging.GetLogger().Criticalf("This elasticsearchversion [%s] given in the config is not supported", version)
panic("")
panic("elasticsearch version not supported")
}
20 changes: 8 additions & 12 deletions pkg/nagflux/collector/livestatus/DowntimeData_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,34 @@ import (

"pkg/nagflux/config"
"pkg/nagflux/logging"

"github.com/stretchr/testify/assert"
)

func TestSanitizeValuesDowntime(t *testing.T) {
t.Parallel()
down := DowntimeData{Data: Data{hostName: "host 1", serviceDisplayName: "service 1", author: "philip"}, endTime: "123"}
down := &DowntimeData{Data: Data{hostName: "host 1", serviceDisplayName: "service 1", author: "philip"}, endTime: "123"}
down.sanitizeValues()
if down.Data.hostName != `host\ 1` {
t.Errorf("The notificationType should be escaped. Expected: %s Got: %s", `host\ 1`, down.Data.hostName)
}
assert.Equalf(t, `host\ 1`, down.Data.hostName, "The notificationType should be escaped.")
}

func TestPrintInfluxdbDowntime(t *testing.T) {
logging.InitTestLogger()
down := DowntimeData{Data: Data{hostName: "host 1", serviceDisplayName: "service 1", author: "philip"}, endTime: "123"}
down := &DowntimeData{Data: Data{hostName: "host 1", serviceDisplayName: "service 1", author: "philip"}, endTime: "123"}
if !didThisPanic(down.PrintForInfluxDB, "0.8") {
t.Errorf("This should panic, due to unsuported influxdb version")
}

result := down.PrintForInfluxDB("0.9")
expected := `messages,host=host\ 1,service=service\ 1,type=downtime,author=philip message="Downtime start: <br>" 000
messages,host=host\ 1,service=service\ 1,type=downtime,author=philip message="Downtime end: <br>" 123000`
if result != expected {
t.Errorf("The result did not match the expected. Result:\n%s \nExpected:\n%s", result, expected)
}
assert.Equalf(t, expected, result, "The result did not match the expected")
}

func TestPrintElasticsearchDowntime(t *testing.T) {
logging.InitTestLogger()
config.InitConfigFromString(Config)
down := DowntimeData{Data: Data{hostName: "host 1", serviceDisplayName: "service 1", author: "philip", entryTime: "1458988932000"}, endTime: "123"}
down := &DowntimeData{Data: Data{hostName: "host 1", serviceDisplayName: "service 1", author: "philip", entryTime: "1458988932000"}, endTime: "123"}
if !didThatPanic(down.PrintForElasticsearch, "1.0", "index") {
t.Errorf("This should panic, due to unsuported elasticsearch version")
}
Expand All @@ -46,7 +44,5 @@ func TestPrintElasticsearchDowntime(t *testing.T) {
{"index":{"_index":"index-1970.01","_type":"messages"}}
{"timestamp":123000,"message":"Downtime end: <br>","author":"philip","host":"host 1","service":"service 1","type":"downtime"}
`
if result != expected {
t.Errorf("The result did not match the expected. Result: %sExpected: %s", result, expected)
}
assert.Equalf(t, expected, result, "The result did not match the expected")
}
6 changes: 3 additions & 3 deletions pkg/nagflux/collector/livestatus/NotificationData.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func (notification *NotificationData) sanitizeValues() {
}

// PrintForInfluxDB prints the data in influxdb lineformat
func (notification NotificationData) PrintForInfluxDB(version string) string {
notification.sanitizeValues()
func (notification *NotificationData) PrintForInfluxDB(version string) string {
if helper.VersionOrdinal(version) >= helper.VersionOrdinal("0.9") {
notification.sanitizeValues()
var tags string
if text := notificationToText(notification.notificationType); text != "" {
tags = ",type=" + text
Expand All @@ -39,7 +39,7 @@ func (notification NotificationData) PrintForInfluxDB(version string) string {
}

// PrintForElasticsearch prints in the elasticsearch json format
func (notification NotificationData) PrintForElasticsearch(version, index string) string {
func (notification *NotificationData) PrintForElasticsearch(version, index string) string {
if helper.VersionOrdinal(version) >= helper.VersionOrdinal("2.0") {
text := notificationToText(notification.notificationType)
value := fmt.Sprintf("%s:<br> %s", strings.TrimSpace(notification.notificationLevel), notification.comment)
Expand Down
4 changes: 2 additions & 2 deletions pkg/nagflux/collector/nagflux/NagfluxPrintable.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Printable struct {
}

// PrintForInfluxDB prints the data in influxdb lineformat
func (p Printable) PrintForInfluxDB(version string) string {
func (p *Printable) PrintForInfluxDB(version string) string {
if helper.VersionOrdinal(version) >= helper.VersionOrdinal("0.9") {
line := p.Table
if len(p.tags) > 0 {
Expand All @@ -33,7 +33,7 @@ func (p Printable) PrintForInfluxDB(version string) string {
}

// PrintForElasticsearch prints in the elasticsearch json format
func (p Printable) PrintForElasticsearch(version, index string) string {
func (p *Printable) PrintForElasticsearch(version, index string) string {
if helper.VersionOrdinal(version) >= helper.VersionOrdinal("2.0") {
head := fmt.Sprintf(`{"index":{"_index":"%s","_type":"%s"}}`, helper.GenIndex(index, p.Timestamp), p.Table) + "\n"
data := `{"timestamp":` + p.Timestamp
Expand Down
4 changes: 2 additions & 2 deletions pkg/nagflux/collector/nagflux/dumpfileCollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (dump *DumpfileCollector) run() {
case <-dump.quit:
dump.quit <- true
return
case dump.jobs <- collector.SimplePrintable{
case dump.jobs <- &collector.SimplePrintable{
Filterable: collector.AllFilterable,
Text: string(line),
Datatype: dump.target.Datatype,
Expand Down Expand Up @@ -106,7 +106,7 @@ func (dump *DumpfileCollector) run() {
case <-dump.quit:
dump.quit <- true
return
case dump.jobs <- collector.SimplePrintable{
case dump.jobs <- &collector.SimplePrintable{
Filterable: collector.AllFilterable,
Text: buffer.String(),
Datatype: dump.target.Datatype,
Expand Down
6 changes: 3 additions & 3 deletions pkg/nagflux/collector/nagflux/nagfluxFileCollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (nfc *FileCollector) Stop() {
}

// Checks if the files are old enough, if so they will be added in the queue
func (nfc FileCollector) run() {
func (nfc *FileCollector) run() {
for {
select {
case <-nfc.quit:
Expand All @@ -74,7 +74,7 @@ func (nfc FileCollector) run() {
case <-nfc.quit:
nfc.quit <- true
return
case r <- p:
case r <- &p:
case <-time.After(time.Duration(1) * time.Minute):
nfc.log.Warn("NagfluxFileCollector: Could not write to buffer")
}
Expand All @@ -89,7 +89,7 @@ func (nfc FileCollector) run() {
}
}

func (nfc FileCollector) parseFile(filename string) []Printable {
func (nfc *FileCollector) parseFile(filename string) []Printable {
result := []Printable{}
csvfile, err := os.Open(filename)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/nagflux/collector/spoolfile/nagiosSpoolfileWorker.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ func (w *NagiosSpoolfileWorker) run() {
}

// PerformanceDataIterator returns an iterator to loop over generated perf data.
func (w *NagiosSpoolfileWorker) PerformanceDataIterator(input map[string]string) <-chan PerformanceData {
ch := make(chan PerformanceData)
func (w *NagiosSpoolfileWorker) PerformanceDataIterator(input map[string]string) <-chan *PerformanceData {
ch := make(chan *PerformanceData)
typ := findType(input)
if typ == "" {
if len(input) > 1 {
Expand Down Expand Up @@ -199,7 +199,7 @@ func (w *NagiosSpoolfileWorker) PerformanceDataIterator(input map[string]string)
target = collector.AllFilterable
}

perf := PerformanceData{
perf := &PerformanceData{
Hostname: input[hostname],
Service: currentService,
Command: currentCommand,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ func testPerformanceDataParser(t *testing.T, input string, expect []PerformanceD
splittedPerformanceData := helper.StringToMap(input, "\t", "::")
collectedPerfData := []PerformanceData{}
for singlePerfdata := range w.PerformanceDataIterator(splittedPerformanceData) {
collectedPerfData = append(collectedPerfData, singlePerfdata)
collectedPerfData = append(collectedPerfData, *singlePerfdata)
}
assert.Equalf(t, expect, collectedPerfData, "performance data matches")
}
4 changes: 2 additions & 2 deletions pkg/nagflux/collector/spoolfile/performanceData.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type PerformanceData struct {
}

// PrintForInfluxDB prints the data in influxdb lineformat
func (p PerformanceData) PrintForInfluxDB(version string) string {
func (p *PerformanceData) PrintForInfluxDB(version string) string {
if helper.VersionOrdinal(version) >= helper.VersionOrdinal("0.9") {
tableName := `metrics,host=` + helper.SanitizeInfluxInput(p.Hostname)
if p.Service == "" {
Expand All @@ -47,7 +47,7 @@ func (p PerformanceData) PrintForInfluxDB(version string) string {
}

// PrintForElasticsearch prints in the elasticsearch json format
func (p PerformanceData) PrintForElasticsearch(version, index string) string {
func (p *PerformanceData) PrintForElasticsearch(version, index string) string {
if helper.VersionOrdinal(version) >= helper.VersionOrdinal("2.0") {
if p.Service == "" {
p.Service = config.GetConfig().InfluxDBGlobal.HostcheckAlias
Expand Down
6 changes: 3 additions & 3 deletions pkg/nagflux/target/elasticsearch/Connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,17 @@ func (connector *Connector) RemoveWorker() {
}

// AmountWorkers current amount of workers.
func (connector Connector) AmountWorkers() int {
func (connector *Connector) AmountWorkers() int {
return len(connector.workers)
}

// IsAlive is the database system alive.
func (connector Connector) IsAlive() bool {
func (connector *Connector) IsAlive() bool {
return connector.isAlive
}

// DatabaseExists does the database exist.
func (connector Connector) DatabaseExists() bool {
func (connector *Connector) DatabaseExists() bool {
return connector.templateExists
}

Expand Down
Loading

0 comments on commit 39c45d5

Please sign in to comment.