Skip to content

Commit

Permalink
Merge pull request #2 from elfranne/time
Browse files Browse the repository at this point in the history
Add time
  • Loading branch information
elfranne authored Nov 6, 2023
2 parents b183f83 + d514c22 commit 9a8a472
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"math"
"time"

corev2 "github.com/sensu/core/v2"
"github.com/sensu/sensu-plugin-sdk/sensu"
Expand All @@ -16,6 +17,8 @@ type Config struct {
CPUCrit float64
MemoryWarn float32
MemoryCrit float32
TimeWarn int64
TimeCrit int64
Scheme string
Process string
}
Expand Down Expand Up @@ -65,6 +68,18 @@ var (
Usage: "Critical if process is using more than memory-crit (in percent)",
Value: &plugin.MemoryCrit,
},
&sensu.PluginConfigOption[int64]{
Path: "time-warn",
Argument: "time-warn",
Usage: "Warn if process is using more than memory-warn (in percent)",
Value: &plugin.TimeWarn,
},
&sensu.PluginConfigOption[int64]{
Path: "time-crit",
Argument: "time-crit",
Usage: "Critical if process is using more than memory-crit (in percent)",
Value: &plugin.TimeCrit,
},
}
)

Expand Down Expand Up @@ -102,6 +117,8 @@ func executeCheck(event *corev2.Event) (int, error) {
for _, p := range process {
cpu, _ := p.CPUPercent()
memory, _ := p.MemoryPercent()
timems, _ := p.CreateTime() //create time is provide in millisecond epoch
time := float64(time.Now().Unix()) - math.Round(float64(timems)/1000)
name, _ := p.Name()

// Warning memory
Expand All @@ -124,6 +141,16 @@ func executeCheck(event *corev2.Event) (int, error) {
fmt.Printf("%s is using %f %% CPU, limit set at %f\n", plugin.Process, Round(float64(cpu), 0.1), plugin.CPUCrit)
return sensu.CheckStateCritical, nil
}
// Warnning Time
if name == plugin.Process && plugin.TimeWarn > 0 && time >= float64(plugin.TimeWarn) {
fmt.Printf("%s has been running for %f seconds, limit set at %d\n", plugin.Process, time, plugin.TimeWarn)
return sensu.CheckStateWarning, nil
}
// Critical Time
if name == plugin.Process && plugin.TimeCrit > 0 && time >= float64(plugin.TimeCrit) {
fmt.Printf("%s has been running for %f seconds, limit set at %d\n", plugin.Process, time, plugin.TimeCrit)
return sensu.CheckStateWarning, nil
}
}
return sensu.CheckStateOK, nil
}

0 comments on commit 9a8a472

Please sign in to comment.