Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Gennady Utkin authored and Gennady Utkin committed Aug 26, 2024
1 parent dc18148 commit e5c6c3f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 46 deletions.
10 changes: 2 additions & 8 deletions checks/clusterHealth.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,8 @@ func CheckClusterHealth(c *config.Config) *nagios.Plugin {
plugin.ExitStatusCode = nagios.StateCRITICALExitCode
return plugin
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
plugin.ServiceOutput = "CRITICAL: Failed to read response from Elasticsearch"
plugin.ExitStatusCode = nagios.StateCRITICALExitCode
plugin.Errors = append(plugin.Errors, err)
}
}(resp.Body)

defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
Expand Down
13 changes: 6 additions & 7 deletions checks/clusterNodeCount.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,17 @@ func CheckClusterNodeCount(c *config.Config) *nagios.Plugin {
return plugin
}

if health.NumberOfNodes < c.CriticalThreshold {
switch {
case health.NumberOfNodes < c.CriticalThreshold:
plugin.ServiceOutput = fmt.Sprintf("CRITICAL: Number of nodes is %d", health.NumberOfNodes)
plugin.ExitStatusCode = nagios.StateCRITICALExitCode
return plugin
}
if health.NumberOfNodes < c.WarningThreshold {
case health.NumberOfNodes < c.WarningThreshold:
plugin.ServiceOutput = fmt.Sprintf("WARNING: Number of nodes is %d", health.NumberOfNodes)
plugin.ExitStatusCode = nagios.StateWARNINGExitCode
return plugin
default:
plugin.ServiceOutput = fmt.Sprintf("OK: Number of nodes is %d", health.NumberOfNodes)
plugin.ExitStatusCode = nagios.StateOKExitCode
}

plugin.ServiceOutput = fmt.Sprintf("OK: Number of nodes is %d", health.NumberOfNodes)
plugin.ExitStatusCode = nagios.StateOKExitCode
return plugin
}
10 changes: 2 additions & 8 deletions checks/nodeCpuUsage.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,8 @@ func CheckNodeCPUUsage(c *config.Config) *nagios.Plugin {
plugin.ExitStatusCode = nagios.StateCRITICALExitCode
return plugin
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
plugin.ServiceOutput = "CRITICAL: Failed to read response from Elasticsearch"
plugin.ExitStatusCode = nagios.StateCRITICALExitCode
plugin.Errors = append(plugin.Errors, err)
}
}(resp.Body)

defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
Expand Down
26 changes: 11 additions & 15 deletions checks/nodeDiskUsage.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,8 @@ func CheckNodeDiskUsage(c *config.Config) *nagios.Plugin {
plugin.ExitStatusCode = nagios.StateCRITICALExitCode
return plugin
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
plugin.ServiceOutput = "CRITICAL: Failed to read response from Elasticsearch"
plugin.ExitStatusCode = nagios.StateCRITICALExitCode
plugin.Errors = append(plugin.Errors, err)
}
}(resp.Body)

defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
Expand Down Expand Up @@ -90,29 +84,31 @@ func CheckNodeDiskUsage(c *config.Config) *nagios.Plugin {
Max: "100",
UnitOfMeasurement: "%",
}

if err := plugin.AddPerfData(false, nodeDiskUsagePercent); err != nil {
log.Printf("failed to add performance data metrics: %v\n", err)
plugin.Errors = append(plugin.Errors, err)
}

if node.FS.Total.UsedPercent > c.CriticalThreshold {
switch {
case node.FS.Total.UsedPercent > c.CriticalThreshold:
plugin.ServiceOutput = fmt.Sprintf("CRITICAL: Disk usage on node %s is %d%%", node.Name, node.FS.Total.UsedPercent)
plugin.ExitStatusCode = nagios.StateCRITICALExitCode
return plugin
}
if node.FS.Total.UsedPercent > c.WarningThreshold {
case node.FS.Total.UsedPercent > c.WarningThreshold:
plugin.ServiceOutput = fmt.Sprintf("WARNING: Disk usage on node %s is %d%%", node.Name, node.FS.Total.UsedPercent)
plugin.ExitStatusCode = nagios.StateWARNINGExitCode
return plugin
default:
plugin.ServiceOutput = fmt.Sprintf("OK: Disk uage on node %s less then %d", node.Name, c.WarningThreshold)
plugin.ExitStatusCode = nagios.StateOKExitCode
}

plugin.ServiceOutput = fmt.Sprintf("OK: Disk uage on node %s less then %d", node.Name, c.WarningThreshold)
plugin.ExitStatusCode = nagios.StateOKExitCode
return plugin
}

if node.FS.Total.UsedPercent > maxDiskUsage {
maxDiskUsage = node.FS.Total.UsedPercent
}

nodeDiskUsagePercent := nagios.PerformanceData{
Label: node.Name,
Value: fmt.Sprintf("%d", node.FS.Total.UsedPercent),
Expand Down
11 changes: 3 additions & 8 deletions checks/nodeHeapMemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,8 @@ func CheckNodeHeapMemory(c *config.Config) *nagios.Plugin {
plugin.ExitStatusCode = nagios.StateCRITICALExitCode
return plugin
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
plugin.ServiceOutput = "CRITICAL: Failed to read response from Elasticsearch"
plugin.ExitStatusCode = nagios.StateCRITICALExitCode
plugin.Errors = append(plugin.Errors, err)
}
}(resp.Body)

defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
Expand Down Expand Up @@ -108,6 +102,7 @@ func CheckNodeHeapMemory(c *config.Config) *nagios.Plugin {

return plugin
}

if node.JVM.Mem.HeapUsedPercent > maxHeap {
maxHeap = node.JVM.Mem.HeapUsedPercent
}
Expand Down

0 comments on commit e5c6c3f

Please sign in to comment.