Skip to content

Commit

Permalink
feat: add flag to filter foreman hosts search
Browse files Browse the repository at this point in the history
  • Loading branch information
fgouteroux committed Jan 15, 2024
1 parent 1babb22 commit 9789884
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
21 changes: 11 additions & 10 deletions foreman/foreman.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ type HTTPClient struct {
onRequestCompleted RequestCompletionCallback
Concurrency int64
Limit int64
Search string
SearchHostFact string
IncludeHostFactRegex *regexp.Regexp
ExcludeHostFactRegex *regexp.Regexp
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion host_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -150,6 +151,7 @@ func main() {
*skipTLSVerify,
*concurrency,
*limit,
*search,
*collectorHostFactSearch,
*collectorHostFactIncludeRegex,
*collectorHostFactExcludeRegex,
Expand Down

0 comments on commit 9789884

Please sign in to comment.