2017-03-30 15:31:28 +00:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
2017-04-18 17:39:50 +00:00
|
|
|
"fmt"
|
2017-03-30 15:31:28 +00:00
|
|
|
"time"
|
|
|
|
|
2020-11-11 17:59:12 +00:00
|
|
|
"github.com/fleetdm/fleet/server/kolide"
|
2021-05-17 17:29:50 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2017-03-30 15:31:28 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2021-05-27 20:18:00 +00:00
|
|
|
func (d *Datastore) CountHostsInTargets(filter kolide.TeamFilter, targets kolide.HostTargets, now time.Time) (kolide.TargetMetrics, error) {
|
2017-04-18 17:39:50 +00:00
|
|
|
// The logic in this function should remain synchronized with
|
|
|
|
// host.Status and GenerateHostStatusStatistics
|
|
|
|
|
2021-05-27 20:18:00 +00:00
|
|
|
if len(targets.HostIDs) == 0 && len(targets.LabelIDs) == 0 && len(targets.TeamIDs) == 0 {
|
2017-03-30 15:31:28 +00:00
|
|
|
// No need to query if no targets selected
|
|
|
|
return kolide.TargetMetrics{}, nil
|
|
|
|
}
|
|
|
|
|
2017-04-18 17:39:50 +00:00
|
|
|
sql := fmt.Sprintf(`
|
2017-03-30 15:31:28 +00:00
|
|
|
SELECT
|
|
|
|
COUNT(*) total,
|
|
|
|
COALESCE(SUM(CASE WHEN DATE_ADD(seen_time, INTERVAL 30 DAY) <= ? THEN 1 ELSE 0 END), 0) mia,
|
2017-04-18 17:39:50 +00:00
|
|
|
COALESCE(SUM(CASE WHEN DATE_ADD(seen_time, INTERVAL LEAST(distributed_interval, config_tls_refresh) + %d SECOND) <= ? AND DATE_ADD(seen_time, INTERVAL 30 DAY) >= ? THEN 1 ELSE 0 END), 0) offline,
|
|
|
|
COALESCE(SUM(CASE WHEN DATE_ADD(seen_time, INTERVAL LEAST(distributed_interval, config_tls_refresh) + %d SECOND) > ? THEN 1 ELSE 0 END), 0) online,
|
2017-03-30 15:31:28 +00:00
|
|
|
COALESCE(SUM(CASE WHEN DATE_ADD(created_at, INTERVAL 1 DAY) >= ? THEN 1 ELSE 0 END), 0) new
|
|
|
|
FROM hosts h
|
2021-05-27 20:18:00 +00:00
|
|
|
WHERE (id IN (?) OR (id IN (SELECT DISTINCT host_id FROM label_membership WHERE label_id IN (?))) OR team_id IN (?)) AND %s
|
2021-05-25 04:34:08 +00:00
|
|
|
`, kolide.OnlineIntervalBuffer, kolide.OnlineIntervalBuffer, d.whereFilterHostsByTeams(filter, "h"))
|
2017-03-30 15:31:28 +00:00
|
|
|
|
|
|
|
// Using -1 in the ID slices for the IN clause allows us to include the
|
|
|
|
// IN clause even if we have no IDs to use. -1 will not match the
|
|
|
|
// auto-increment IDs, and will also allow us to use the same query in
|
|
|
|
// all situations (no need to remove the clause when there are no values)
|
|
|
|
queryLabelIDs := []int{-1}
|
2021-05-27 20:18:00 +00:00
|
|
|
for _, id := range targets.LabelIDs {
|
2017-03-30 15:31:28 +00:00
|
|
|
queryLabelIDs = append(queryLabelIDs, int(id))
|
|
|
|
}
|
|
|
|
queryHostIDs := []int{-1}
|
2021-05-27 20:18:00 +00:00
|
|
|
for _, id := range targets.HostIDs {
|
2017-03-30 15:31:28 +00:00
|
|
|
queryHostIDs = append(queryHostIDs, int(id))
|
|
|
|
}
|
2021-05-27 20:18:00 +00:00
|
|
|
queryTeamIDs := []int{-1}
|
|
|
|
for _, id := range targets.TeamIDs {
|
|
|
|
queryTeamIDs = append(queryTeamIDs, int(id))
|
|
|
|
}
|
2017-03-30 15:31:28 +00:00
|
|
|
|
2021-05-27 20:18:00 +00:00
|
|
|
query, args, err := sqlx.In(sql, now, now, now, now, now, queryHostIDs, queryLabelIDs, queryTeamIDs)
|
2017-03-30 15:31:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return kolide.TargetMetrics{}, errors.Wrap(err, "sqlx.In CountHostsInTargets")
|
|
|
|
}
|
|
|
|
|
|
|
|
res := kolide.TargetMetrics{}
|
|
|
|
err = d.db.Get(&res, query, args...)
|
|
|
|
if err != nil {
|
|
|
|
return kolide.TargetMetrics{}, errors.Wrap(err, "sqlx.Get CountHostsInTargets")
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
2020-03-23 01:33:04 +00:00
|
|
|
|
2021-05-27 20:18:00 +00:00
|
|
|
func (d *Datastore) HostIDsInTargets(filter kolide.TeamFilter, targets kolide.HostTargets) ([]uint, error) {
|
|
|
|
if len(targets.HostIDs) == 0 && len(targets.LabelIDs) == 0 && len(targets.TeamIDs) == 0 {
|
2020-03-23 01:33:04 +00:00
|
|
|
// No need to query if no targets selected
|
|
|
|
return []uint{}, nil
|
|
|
|
}
|
|
|
|
|
2021-05-25 04:34:08 +00:00
|
|
|
sql := fmt.Sprintf(`
|
|
|
|
SELECT DISTINCT id
|
|
|
|
FROM hosts
|
2021-05-27 20:18:00 +00:00
|
|
|
WHERE (id IN (?) OR (id IN (SELECT host_id FROM label_membership WHERE label_id IN (?))) OR team_id IN (?)) AND %s
|
2021-05-25 04:34:08 +00:00
|
|
|
ORDER BY id ASC
|
|
|
|
`,
|
|
|
|
d.whereFilterHostsByTeams(filter, "hosts"),
|
|
|
|
)
|
2020-03-23 01:33:04 +00:00
|
|
|
|
|
|
|
// Using -1 in the ID slices for the IN clause allows us to include the
|
|
|
|
// IN clause even if we have no IDs to use. -1 will not match the
|
|
|
|
// auto-increment IDs, and will also allow us to use the same query in
|
|
|
|
// all situations (no need to remove the clause when there are no values)
|
|
|
|
queryLabelIDs := []int{-1}
|
2021-05-27 20:18:00 +00:00
|
|
|
for _, id := range targets.LabelIDs {
|
2020-03-23 01:33:04 +00:00
|
|
|
queryLabelIDs = append(queryLabelIDs, int(id))
|
|
|
|
}
|
|
|
|
queryHostIDs := []int{-1}
|
2021-05-27 20:18:00 +00:00
|
|
|
for _, id := range targets.HostIDs {
|
2020-03-23 01:33:04 +00:00
|
|
|
queryHostIDs = append(queryHostIDs, int(id))
|
|
|
|
}
|
2021-05-27 20:18:00 +00:00
|
|
|
queryTeamIDs := []int{-1}
|
|
|
|
for _, id := range targets.TeamIDs {
|
|
|
|
queryTeamIDs = append(queryTeamIDs, int(id))
|
|
|
|
}
|
2020-03-23 01:33:04 +00:00
|
|
|
|
2021-05-27 20:18:00 +00:00
|
|
|
query, args, err := sqlx.In(sql, queryHostIDs, queryLabelIDs, queryTeamIDs)
|
2020-03-23 01:33:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "sqlx.In HostIDsInTargets")
|
|
|
|
}
|
|
|
|
|
|
|
|
var res []uint
|
|
|
|
err = d.db.Select(&res, query, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "sqlx.Get HostIDsInTargets")
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|