Skip to content
Merged
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 .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v9
with:
version: v2.1.6
version: v2.12.2
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ run:
tests: false
linters:
default: all
enable:
- wsl_v5
- gomodguard_v2
disable:
- wsl
- gomodguard
- cyclop
- depguard
- err113
Expand Down
1 change: 0 additions & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ func (c *Config) NewClient() *client.Client {
KeyFile: c.KeyFile,
CertFile: c.CertFile,
})

if err != nil {
check.ExitError(err)
}
Expand Down
65 changes: 36 additions & 29 deletions cmd/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ import (

"github.com/NETWAYS/check_logstash/internal/logstash"
"github.com/NETWAYS/go-check"
"github.com/NETWAYS/go-check/perfdata"
"github.com/NETWAYS/go-check/result"
"github.com/spf13/cobra"
)

// To store the CLI parameters.
// HealthConfig for the CLI parameters.
type HealthConfig struct {
FileDescThresWarning string
FileDescThresCritical string
Expand All @@ -26,7 +24,7 @@ type HealthConfig struct {
UnreachableExitCode int
}

// To store the parsed CLI parameters.
// HealthThreshold for the parsed CLI parameters.
type HealthThreshold struct {
fileDescThresWarn *check.Threshold
fileDescThresCrit *check.Threshold
Expand Down Expand Up @@ -89,31 +87,31 @@ func parseHealthThresholds(config HealthConfig) (HealthThreshold, error) {
return t, nil
}

func generatePerfdata(stat logstash.Stat, thres HealthThreshold) perfdata.PerfdataList {
func generatePerfdata(stat logstash.Stat, thres HealthThreshold) check.PerfdataList {
// Generates the Perfdata from the results and thresholds
var l perfdata.PerfdataList
var l check.PerfdataList

l.Add(&perfdata.Perfdata{
l.Add(&check.Perfdata{
Label: "process.cpu.percent",
Value: stat.Process.CPU.Percent,
Uom: "%",
Warn: thres.cpuUseThresWarn,
Crit: thres.cpuUseThresCrit,
Min: 0,
Max: 100})
l.Add(&perfdata.Perfdata{
l.Add(&check.Perfdata{
Label: "jvm.mem.heap_used_percent",
Uom: "%",
Value: stat.Jvm.Mem.HeapUsedPercent,
Warn: thres.heapUseThresWarn,
Crit: thres.heapUseThresCrit,
Min: 0,
Max: 100})
l.Add(&perfdata.Perfdata{
l.Add(&check.Perfdata{
Label: "jvm.threads.count",
Value: stat.Jvm.Threads.Count,
Max: 0})
l.Add(&perfdata.Perfdata{
l.Add(&check.Perfdata{
Label: "process.open_file_descriptors",
Value: stat.Process.OpenFileDescriptors,
Warn: thres.fileDescThresWarn,
Expand Down Expand Up @@ -143,7 +141,7 @@ var healthCmd = &cobra.Command{
Run: func(_ *cobra.Command, _ []string) {
var (
output string
rc int
rc check.Status
stat logstash.Stat
thresholds HealthThreshold
fdstatus string
Expand All @@ -152,30 +150,35 @@ var healthCmd = &cobra.Command{
)

// status + fdstatus + heapstatus + cpustatus = 4
states := make([]int, 0, 4)
states := make([]check.Status, 0, 4)

// Parse the thresholds into a central var since we need them later
thresholds, err := parseHealthThresholds(cliHealthConfig)
if err != nil {
check.ExitError(err)
}

unreachableExitCode, errExit := check.NewStatus(cliHealthConfig.UnreachableExitCode)
if errExit != nil {
unreachableExitCode = check.Unknown
}

// Creating an client and connecting to the API
c := cliConfig.NewClient()
u, _ := url.JoinPath(c.URL, "/_node/stats")
resp, err := c.Client.Get(u)

resp, err := c.Client.Get(u)
if err != nil {
check.ExitRaw(cliHealthConfig.UnreachableExitCode, err.Error())
check.Exit(unreachableExitCode, err.Error())
}

if resp.StatusCode != http.StatusOK {
check.ExitError(fmt.Errorf("could not get %s - Error: %d", u, resp.StatusCode))
}

defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&stat)

err = json.NewDecoder(resp.Body).Decode(&stat)
if err != nil {
check.ExitError(err)
}
Expand Down Expand Up @@ -205,43 +208,47 @@ var healthCmd = &cobra.Command{
perfList := generatePerfdata(stat, thresholds)

// Defaults for the subchecks
fdstatus = check.StatusText(check.OK)
heapstatus = check.StatusText(check.OK)
cpustatus = check.StatusText(check.OK)
fdstatus = check.OKString
heapstatus = check.OKString
cpustatus = check.OKString

// File Descriptors Check
fileDescriptorsPercent := (stat.Process.OpenFileDescriptors / stat.Process.MaxFileDescriptors) * 100
if thresholds.fileDescThresWarn.DoesViolate(fileDescriptorsPercent) {
states = append(states, check.Warning)
fdstatus = check.StatusText(check.Warning)
fdstatus = check.WarningString
}

if thresholds.fileDescThresCrit.DoesViolate(fileDescriptorsPercent) {
states = append(states, check.Critical)
fdstatus = check.StatusText(check.Critical)
fdstatus = check.CriticalString
}

// Heap Usage Check
if thresholds.heapUseThresWarn.DoesViolate(stat.Jvm.Mem.HeapUsedPercent) {
states = append(states, check.Warning)
heapstatus = check.StatusText(check.Warning)
heapstatus = check.WarningString
}

if thresholds.heapUseThresCrit.DoesViolate(stat.Jvm.Mem.HeapUsedPercent) {
states = append(states, check.Critical)
heapstatus = check.StatusText(check.Critical)
heapstatus = check.CriticalString
}

// CPU Usage Check
if thresholds.cpuUseThresWarn.DoesViolate(stat.Process.CPU.Percent) {
states = append(states, check.Warning)
cpustatus = check.StatusText(check.Warning)
cpustatus = check.WarningString
}

if thresholds.cpuUseThresCrit.DoesViolate(stat.Process.CPU.Percent) {
states = append(states, check.Critical)
cpustatus = check.StatusText(check.Critical)
cpustatus = check.CriticalString
}

// Validate the various subchecks and use the worst state as return code
switch result.WorstState(states...) {
//nolint: exhaustive
switch check.WorstState(states...) {
case 0:
rc = check.OK
output = "Logstash is healthy"
Expand All @@ -258,11 +265,11 @@ var healthCmd = &cobra.Command{

// Generate summary for subchecks
var summary strings.Builder
summary.WriteString(fmt.Sprintf("\n \\_[%s] Heap usage at %.2f%%", heapstatus, stat.Jvm.Mem.HeapUsedPercent))
summary.WriteString(fmt.Sprintf("\n \\_[%s] Open file descriptors at %.2f%%", fdstatus, fileDescriptorsPercent))
summary.WriteString(fmt.Sprintf("\n \\_[%s] CPU usage at %.2f%%", cpustatus, stat.Process.CPU.Percent))
fmt.Fprintf(&summary, "\n \\_[%s] Heap usage at %.2f%%", heapstatus, stat.Jvm.Mem.HeapUsedPercent)
fmt.Fprintf(&summary, "\n \\_[%s] Open file descriptors at %.2f%%", fdstatus, fileDescriptorsPercent)
fmt.Fprintf(&summary, "\n \\_[%s] CPU usage at %.2f%%", cpustatus, stat.Process.CPU.Percent)

check.ExitRaw(rc, output, summary.String(), "|", perfList.String())
check.ExitWithPerfdata(rc, perfList, output, summary.String())
},
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func TestHealthCmd_Logstash7(t *testing.T) {
w.Write([]byte(`{"host":"test","version":"7.17.8","status":"green","jvm":{"threads":{"count":50,"peak_count":51},"mem":{"heap_used_percent":20}},"process":{"open_file_descriptors": 120,"peak_open_file_descriptors": 120,"max_file_descriptors":16384,"cpu":{"percent": 1}}}`))
})),
args: []string{"run", "../main.go", "health"},
expected: "| process.cpu.percent=1%;100;100;0;100 jvm.mem.heap_used_percent=20%;70;80;0;100 jvm.threads.count=50;;;;0 process.open_file_descriptors=120;100;100;0;16384",
expected: "|process.cpu.percent=1%;100;100;0;100 jvm.mem.heap_used_percent=20%;70;80;0;100 jvm.threads.count=50;;;;0 process.open_file_descriptors=120;100;100;0;16384",
},
{
name: "health-red",
Expand Down Expand Up @@ -290,7 +290,7 @@ func TestHealthCmd_Logstash8(t *testing.T) {
w.Write([]byte(`{"host":"test","version":"8.6","status":"green","jvm":{"threads":{"count":50,"peak_count":51},"mem":{"heap_used_percent":20}},"process":{"open_file_descriptors": 120,"peak_open_file_descriptors": 120,"max_file_descriptors":16384,"cpu":{"percent": 1}}}`))
})),
args: []string{"run", "../main.go", "health"},
expected: "| process.cpu.percent=1%;100;100;0;100 jvm.mem.heap_used_percent=20%;70;80;0;100 jvm.threads.count=50;;;;0 process.open_file_descriptors=120;100;100;0;16384",
expected: "|process.cpu.percent=1%;100;100;0;100 jvm.mem.heap_used_percent=20%;70;80;0;100 jvm.threads.count=50;;;;0 process.open_file_descriptors=120;100;100;0;16384",
},
{
name: "health-cpu-heap-worst-state",
Expand Down
Loading