From 9789884c2471d82a060bc717b4b5b0393f3899c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Gouteroux?= Date: Mon, 15 Jan 2024 13:48:14 +0100 Subject: [PATCH] feat: add flag to filter foreman hosts search --- CHANGELOG.md | 4 ++++ foreman/foreman.go | 21 +++++++++++---------- host_collector.go | 2 +- main.go | 2 ++ 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a7e1a6..d9388b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.0.5 / 2024-01-15 + +* [FEATURE] add flag to filter foreman hosts search + ## 0.0.4 / 2024-01-12 * [FEATURE] add user agent http header in foreman requests diff --git a/foreman/foreman.go b/foreman/foreman.go index 33dbe9d..4ced752 100644 --- a/foreman/foreman.go +++ b/foreman/foreman.go @@ -141,6 +141,7 @@ type HTTPClient struct { onRequestCompleted RequestCompletionCallback Concurrency int64 Limit int64 + Search string SearchHostFact string IncludeHostFactRegex *regexp.Regexp ExcludeHostFactRegex *regexp.Regexp @@ -179,7 +180,7 @@ func (l *LeveledLogrus) Warn(msg string, keysAndValues ...interface{}) { l.WithFields(fields(keysAndValues)).Warn(msg) } -func NewHTTPClient(baseURL *url.URL, username, password string, skipTLSVerify bool, concurrency, limit int64, searchHostFact string, includeHostFactRegex, excludeHostFactRegex *regexp.Regexp, log *logrus.Logger, reg prometheus.Registerer) *HTTPClient { +func NewHTTPClient(baseURL *url.URL, username, password string, skipTLSVerify bool, concurrency, limit int64, search, searchHostFact string, includeHostFactRegex, excludeHostFactRegex *regexp.Regexp, log *logrus.Logger, reg prometheus.Registerer) *HTTPClient { reg.MustRegister( hostsFactsHistVecMetric, @@ -214,6 +215,7 @@ func NewHTTPClient(baseURL *url.URL, username, password string, skipTLSVerify bo Password: password, Concurrency: concurrency, Limit: limit, + Search: search, SearchHostFact: searchHostFact, IncludeHostFactRegex: includeHostFactRegex, ExcludeHostFactRegex: excludeHostFactRegex, @@ -268,13 +270,12 @@ func (c *HTTPClient) DoWithContext(ctx context.Context, r *http.Request, data in return errors.New(string(body)) } -func (c *HTTPClient) GetHosts(ctx context.Context, search, thin string, page, perPage int64) (HostResponse, error) { +func (c *HTTPClient) GetHosts(ctx context.Context, thin string, page, perPage int64) (HostResponse, error) { var result HostResponse params := url.Values{} - if search != "" { - params.Set("search", search) - } + params.Set("search", c.Search) + if thin == "true" { params.Set("thin", thin) } @@ -410,7 +411,7 @@ func (c *HTTPClient) GetHostsFactsFiltered(perPage int64) (map[string]map[string } ctx := context.Background() - hostsFirstPage, err := c.GetHosts(ctx, "", "true", 1, perPage) + hostsFirstPage, err := c.GetHosts(ctx, "true", 1, perPage) if err != nil { errMsg := fmt.Errorf("cannot get foreman hosts: %v", err) return nil, errMsg @@ -427,7 +428,7 @@ func (c *HTTPClient) GetHostsFactsFiltered(perPage int64) (map[string]map[string } for page := hostsFirstPage.Page + 1; page <= pages; page++ { - hostsPage, err := c.GetHosts(ctx, "", "true", page, perPage) + hostsPage, err := c.GetHosts(ctx, "true", page, perPage) if err != nil { errMsg := fmt.Errorf("cannot get foreman hosts page (%d/%d) %v", page, pages, err) return nil, errMsg @@ -469,13 +470,13 @@ func (c *HTTPClient) GetHostsFactsFiltered(perPage int64) (map[string]map[string return hostsFacts, nil } -func (c *HTTPClient) GetHostsFiltered(search string, perPage int64) ([]Host, error) { +func (c *HTTPClient) GetHostsFiltered(perPage int64) ([]Host, error) { if c.Limit != 0 && c.Limit < perPage { perPage = int64(c.Limit) } ctx := context.Background() - hostsFirstPage, err := c.GetHosts(ctx, search, "true", 1, perPage) + hostsFirstPage, err := c.GetHosts(ctx, "true", 1, perPage) if err != nil { errMsg := fmt.Errorf("cannot get foreman hosts: %v", err) return nil, errMsg @@ -542,7 +543,7 @@ func (c *HTTPClient) GetHostWithConcurrency(pages []int64, perPage int64) []Host // along with the index so we can sort them later along with // any error that might have occured ctx := context.Background() - res, err := c.GetHosts(ctx, "", "false", page, perPage) + res, err := c.GetHosts(ctx, "false", page, perPage) result := &HostWithConcurrencyResult{i, res, err} // now we can send the result struct through the resultsChan diff --git a/host_collector.go b/host_collector.go index 10662d8..43bb3fc 100644 --- a/host_collector.go +++ b/host_collector.go @@ -66,7 +66,7 @@ func (c HostCollector) Collect(ch chan<- prometheus.Metric) { } var errVal float64 - hostStatus, hostStatusError := c.Client.GetHostsFiltered("", 100) + hostStatus, hostStatusError := c.Client.GetHostsFiltered(100) if hostStatusError != nil { level.Error(c.Logger).Log("msg", "Failed to get hosts status filtered", "err", hostStatusError) // #nosec G104 diff --git a/main.go b/main.go index 9369b35..1ca8dcd 100644 --- a/main.go +++ b/main.go @@ -39,6 +39,7 @@ var ( concurrency = kingpin.Flag("concurrency", "Max concurrent http request").Default("4").Int64() limit = kingpin.Flag("limit", "Foreman host limit search").Default("0").Int64() + search = kingpin.Flag("search", "Foreman host search filter").Default("").String() // Lock concurrent requests on collectors to avoid flooding foreman api with too many requests collectorsLock = kingpin.Flag("collector.lock-concurrent-requests", "Lock concurrent requests on collectors").Bool() @@ -150,6 +151,7 @@ func main() { *skipTLSVerify, *concurrency, *limit, + *search, *collectorHostFactSearch, *collectorHostFactIncludeRegex, *collectorHostFactExcludeRegex,