Skip to content

Commit 27b402a

Browse files
authored
Merge pull request #1 from elfranne/expand
Implement Expand switch
2 parents e10899e + a9c9ad7 commit 27b402a

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.18
55
require (
66
github.com/sensu/core/v2 v2.19.0
77
github.com/sensu/sensu-plugin-sdk v0.18.0
8-
github.com/shirou/gopsutil/v3 v3.23.5
8+
github.com/shirou/gopsutil/v3 v3.23.7
99
)
1010

1111
require (
@@ -44,7 +44,7 @@ require (
4444
github.com/yusufpapurcu/wmi v1.2.3 // indirect
4545
go.etcd.io/etcd/api/v3 v3.5.5 // indirect
4646
golang.org/x/net v0.0.0-20210525063256-abc453219eb5 // indirect
47-
golang.org/x/sys v0.8.0 // indirect
47+
golang.org/x/sys v0.10.0 // indirect
4848
golang.org/x/text v0.3.6 // indirect
4949
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
5050
google.golang.org/grpc v1.41.0 // indirect

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ github.com/sensu/sensu-licensing/v2 v2.2.1 h1:9JI4iVm4ujWN4etI/Kdper6Q2lOn3HIEaG
220220
github.com/sensu/sensu-licensing/v2 v2.2.1/go.mod h1:53lwddwN4XwZUld5KtnWQduSH6F8rBOsWuEk2EUeooI=
221221
github.com/sensu/sensu-plugin-sdk v0.18.0 h1:aR5N9SsqRm1NqiJo7k8GeLEPwGQIfeY4bbePPByUdnI=
222222
github.com/sensu/sensu-plugin-sdk v0.18.0/go.mod h1:NCcvkFZka5Az4Z+l9greCQQdEzTfilO4+JoURmELCa4=
223-
github.com/shirou/gopsutil/v3 v3.23.5 h1:5SgDCeQ0KW0S4N0znjeM/eFHXXOKyv2dVNgRq/c9P6Y=
224-
github.com/shirou/gopsutil/v3 v3.23.5/go.mod h1:Ng3Maa27Q2KARVJ0SPZF5NdrQSC3XHKP8IIWrHgMeLY=
223+
github.com/shirou/gopsutil/v3 v3.23.7 h1:C+fHO8hfIppoJ1WdsVm1RoI0RwXoNdfTK7yWXV0wVj4=
224+
github.com/shirou/gopsutil/v3 v3.23.7/go.mod h1:c4gnmoRC0hQuaLqvxnx1//VXQ0Ms/X9UnJF8pddY5z4=
225225
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
226226
github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
227227
github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
@@ -258,8 +258,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
258258
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
259259
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
260260
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
261-
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
262-
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
261+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
262+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
263263
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
264264
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
265265
github.com/tklauser/go-sysconf v0.3.11 h1:89WgdJhk5SNwJfu+GKyYveZ4IaJ7xAkecBo+KdJV0CM=
@@ -366,8 +366,8 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w
366366
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
367367
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
368368
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
369-
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
370-
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
369+
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
370+
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
371371
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
372372
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
373373
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

main.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package main
33
import (
44
"fmt"
55
"math"
6-
"strings"
6+
"regexp"
77
"time"
88

99
corev2 "github.com/sensu/core/v2"
@@ -17,6 +17,7 @@ type Config struct {
1717
CPU float64
1818
Memory float32
1919
Scheme string
20+
Expand string
2021
}
2122

2223
var (
@@ -53,6 +54,14 @@ var (
5354
Usage: "Scheme to prepend metric",
5455
Value: &plugin.Scheme,
5556
},
57+
&sensu.PluginConfigOption[string]{
58+
Path: "expand",
59+
Argument: "expand",
60+
Shorthand: "e",
61+
Default: "",
62+
Usage: "Expand name for process to include argurment(s) (usefull for bash or powershell)",
63+
Value: &plugin.Expand,
64+
},
5665
}
5766
)
5867

@@ -79,15 +88,27 @@ func Round(x, unit float64) float64 {
7988
return math.Round(x/unit) * unit
8089
}
8190

91+
func ExpandName(name string, p *process.Process) string {
92+
if plugin.Expand == name {
93+
cmd, _ := p.Cmdline()
94+
return cmd
95+
} else {
96+
return name
97+
}
98+
}
99+
82100
func executeCheck(event *corev2.Event) (int, error) {
101+
re := regexp.MustCompile(`-+|\s+|/+|:+|\.+|,+|=+`)
83102
process, _ := process.Processes()
84103
for _, p := range process {
85104
cpu, _ := p.CPUPercent()
86105
memory, _ := p.MemoryPercent()
87106
name, _ := p.Name()
107+
expanded := ExpandName(name, p)
108+
88109
if cpu >= plugin.CPU || memory >= plugin.Memory {
89-
fmt.Printf("%s.process.cpu_percent.%s %f %d\n", plugin.Scheme, strings.ReplaceAll(name, ".", "_"), Round(cpu, 0.1), time.Now().Unix())
90-
fmt.Printf("%s.process.memory_percent.%s %f %d\n", plugin.Scheme, strings.ReplaceAll(name, ".", "_"), Round(float64(memory), 0.1), time.Now().Unix())
110+
fmt.Printf("%s.process.cpu_percent.%s %f %d\n", plugin.Scheme, re.ReplaceAllString(expanded, "_"), Round(cpu, 0.1), time.Now().Unix())
111+
fmt.Printf("%s.process.memory_percent.%s %f %d\n", plugin.Scheme, re.ReplaceAllString(expanded, "_"), Round(float64(memory), 0.1), time.Now().Unix())
91112
}
92113
}
93114
return sensu.CheckStateOK, nil

0 commit comments

Comments
 (0)