Skip to content

Commit 18e2570

Browse files
committed
Add filter flag for sites
1 parent bc4b475 commit 18e2570

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

check.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"github.com/NETWAYS/check_sentinelone/api"
77
"github.com/NETWAYS/go-check"
8+
log "github.com/sirupsen/logrus"
89
"github.com/spf13/pflag"
910
"net/url"
1011
"os"
@@ -14,14 +15,18 @@ type Config struct {
1415
ManagementURL string
1516
AuthToken string
1617
IgnoreInProgress bool
18+
SiteName string
1719
}
1820

1921
func BuildConfigFlags(fs *pflag.FlagSet) (config *Config) {
2022
config = &Config{}
2123

22-
fs.StringVarP(&config.ManagementURL, "url", "H", "", "Management URL (env:SENTINELONE_URL)")
24+
fs.StringVarP(&config.ManagementURL, "url", "H", "",
25+
"Management URL (e.g. https://your-site.sentinelone.net) (env:SENTINELONE_URL)")
2326
fs.StringVarP(&config.AuthToken, "token", "T", "", "API AuthToken (env:SENTINELONE_TOKEN)")
2427

28+
fs.StringVar(&config.SiteName, "site", "", "Only list threats belonging to a named site")
29+
2530
fs.BoolVar(&config.IgnoreInProgress, "ignore-in-progress", false,
2631
"Ignore threats, where the incident status is in-progress")
2732

@@ -61,6 +66,17 @@ func (c *Config) Run() (rc int, output string, err error) {
6166
values.Set("resolved", "false")
6267
}
6368

69+
if c.SiteName != "" {
70+
var siteId string
71+
72+
siteId, err = lookupSiteId(client, c.SiteName)
73+
if err != nil {
74+
return
75+
}
76+
77+
values.Set("siteIds", siteId)
78+
}
79+
6480
threats, err := client.GetThreats(values)
6581
if err != nil {
6682
return
@@ -116,6 +132,9 @@ func (c *Config) Run() (rc int, output string, err error) {
116132

117133
// Add summary on top
118134
output = fmt.Sprintf("%d threats found, %d not mitigated\n", total, notMitigated) + output
135+
if c.SiteName != "" {
136+
output = fmt.Sprintf("site %s - ", c.SiteName) + output
137+
}
119138

120139
// Add perfdata
121140
output += "|"
@@ -131,3 +150,25 @@ func (c *Config) Run() (rc int, output string, err error) {
131150

132151
return
133152
}
153+
154+
func lookupSiteId(client *api.Client, name string) (id string, err error) {
155+
params := url.Values{}
156+
params.Set("name", name)
157+
158+
sites, err := client.GetSites(params)
159+
if err != nil {
160+
return
161+
}
162+
163+
switch len(sites) {
164+
case 0:
165+
err = fmt.Errorf("could not find a site named '%s'", name)
166+
case 1:
167+
id = sites[0].ID
168+
log.WithField("id", id).Debug("found site")
169+
default:
170+
err = fmt.Errorf("more than one site matches '%s'", name)
171+
}
172+
173+
return
174+
}

0 commit comments

Comments
 (0)