Skip to content

Commit

Permalink
Merge pull request #1 from utking/patch-1
Browse files Browse the repository at this point in the history
Fix Go version for the tests
  • Loading branch information
jtsunne authored Aug 26, 2024
2 parents 0368bc7 + e5c6c3f commit c06800d
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 185 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
- name: Install dependencies
run: |
go get .
go-version: '1.22'
- name: Build
run: go build -v ./...
26 changes: 10 additions & 16 deletions checks/clusterDataNodeCount.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package checks
import (
"encoding/json"
"fmt"
"github.com/atc0005/go-nagios"
"io"
"nagios-es/config"
"net/http"

"github.com/atc0005/go-nagios"
)

func CheckClusterDataNodeCount(c *config.Config) *nagios.Plugin {
Expand All @@ -19,14 +20,8 @@ func CheckClusterDataNodeCount(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 All @@ -42,18 +37,17 @@ func CheckClusterDataNodeCount(c *config.Config) *nagios.Plugin {
return plugin
}

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

plugin.ServiceOutput = fmt.Sprintf("OK: Number of nodes is %d", health.NumberOfDataNodes)
plugin.ExitStatusCode = nagios.StateOKExitCode
return plugin
}
17 changes: 7 additions & 10 deletions checks/clusterHealth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package checks
import (
"encoding/json"
"fmt"
"github.com/atc0005/go-nagios"
"io"
"log"
"nagios-es/config"
"net/http"

"github.com/atc0005/go-nagios"
)

type ClusterHealthResponse struct {
Expand All @@ -27,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 All @@ -61,10 +56,12 @@ func CheckClusterHealth(c *config.Config) *nagios.Plugin {
{Label: "unassigned_shards", Value: fmt.Sprintf("%d", health.UnassignedShards)},
{Label: "active_shards", Value: fmt.Sprintf("%d", health.ActiveShards)},
}

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

plugin.ExitStatusCode = nagios.StateWARNINGExitCode
case "red":
plugin.ServiceOutput = "CRITICAL: Cluster health is red"
Expand Down
26 changes: 10 additions & 16 deletions checks/clusterNodeCount.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package checks
import (
"encoding/json"
"fmt"
"github.com/atc0005/go-nagios"
"io"
"nagios-es/config"
"net/http"

"github.com/atc0005/go-nagios"
)

type ClusterNodeCountResponse struct {
Expand All @@ -24,14 +25,8 @@ func CheckClusterNodeCount(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 All @@ -47,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
}
72 changes: 34 additions & 38 deletions checks/nodeCpuUsage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package checks
import (
"encoding/json"
"fmt"
"github.com/atc0005/go-nagios"
"io"
"log"
"nagios-es/config"
"net/http"

"github.com/atc0005/go-nagios"
)

func CheckNodeCPUUsage(c *config.Config) *nagios.Plugin {
Expand All @@ -20,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 All @@ -48,36 +43,37 @@ func CheckNodeCPUUsage(c *config.Config) *nagios.Plugin {
for _, node := range health.Nodes {
if node.IP == c.NodeIP {
nodeCpuPercent := nagios.PerformanceData{
Label: fmt.Sprintf("%s", node.Name),
Label: node.Name,
Value: fmt.Sprintf("%d", node.OS.CPU.Percent),
Warn: fmt.Sprintf("%d", c.WarningThreshold),
Crit: fmt.Sprintf("%d", c.CriticalThreshold),
Min: "0",
Max: "100",
UnitOfMeasurement: "%",
}

if err := plugin.AddPerfData(false, nodeCpuPercent); err != nil {
log.Printf("failed to add performance data metrics: %v", err)
log.Printf("failed to add performance data metrics: %v\n", err)
plugin.Errors = append(plugin.Errors, err)
}
if node.OS.CPU.Percent > c.CriticalThreshold {

switch {
case node.OS.CPU.Percent > c.CriticalThreshold:
plugin.ServiceOutput = fmt.Sprintf("CRITICAL: CPU usage on node %s is %d%%", node.IP, node.OS.CPU.Percent)
plugin.ExitStatusCode = nagios.StateCRITICALExitCode
return plugin
}
if node.OS.CPU.Percent > c.WarningThreshold {
case node.OS.CPU.Percent > c.WarningThreshold:
plugin.ServiceOutput = fmt.Sprintf("WARNING: CPU usage on node %s is %d%%", node.IP, node.OS.CPU.Percent)
plugin.ExitStatusCode = nagios.StateWARNINGExitCode
return plugin
default:
plugin.ServiceOutput = fmt.Sprintf("OK: CPU usage on node %s less then %d", node.IP, c.WarningThreshold)
plugin.ExitStatusCode = nagios.StateOKExitCode
}

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

if node.Name == c.NodeName {
nodeCpuPercent := nagios.PerformanceData{
Label: fmt.Sprintf("%s", node.Name),
Label: node.Name,
Value: fmt.Sprintf("%d", node.OS.CPU.Percent),
Warn: fmt.Sprintf("%d", c.WarningThreshold),
Crit: fmt.Sprintf("%d", c.CriticalThreshold),
Expand All @@ -86,30 +82,31 @@ func CheckNodeCPUUsage(c *config.Config) *nagios.Plugin {
UnitOfMeasurement: "%",
}
if err := plugin.AddPerfData(false, nodeCpuPercent); err != nil {
log.Printf("failed to add performance data metrics: %v", err)
log.Printf("failed to add performance data metrics: %v\n", err)
plugin.Errors = append(plugin.Errors, err)
}

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

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

if node.OS.CPU.Percent > maxCPU {
maxCPU = node.OS.CPU.Percent
}

nodeCpuPercent := nagios.PerformanceData{
Label: fmt.Sprintf("%s", node.Name),
Label: node.Name,
Value: fmt.Sprintf("%d", node.OS.CPU.Percent),
Warn: fmt.Sprintf("%d", c.WarningThreshold),
Crit: fmt.Sprintf("%d", c.CriticalThreshold),
Expand All @@ -121,22 +118,21 @@ func CheckNodeCPUUsage(c *config.Config) *nagios.Plugin {
}

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

switch {
case maxCPU > c.CriticalThreshold:
plugin.ServiceOutput = fmt.Sprintf("CRITICAL: Max(CPU usage) on cluster is %d%%", maxCPU)
plugin.ExitStatusCode = nagios.StateCRITICALExitCode
return plugin
}
if maxCPU > c.WarningThreshold {
case maxCPU > c.WarningThreshold:
plugin.ServiceOutput = fmt.Sprintf("WARNING: Max(CPU usage) on cluster is %d%%", maxCPU)
plugin.ExitStatusCode = nagios.StateWARNINGExitCode
return plugin
default:
plugin.ServiceOutput = fmt.Sprintf("OK: Max(CPU usage) on cluster less then %d", c.WarningThreshold)
plugin.ExitStatusCode = nagios.StateOKExitCode
}

plugin.ServiceOutput = fmt.Sprintf("OK: Max(CPU usage) on cluster less then %d", c.WarningThreshold)
plugin.ExitStatusCode = nagios.StateOKExitCode

return plugin
}
Loading

0 comments on commit c06800d

Please sign in to comment.