parent
d1ce543440
commit
fb54388e93
|
@ -366,48 +366,66 @@ func (a *apic) Pull() error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *apic) GetMetrics() (*models.Metrics, error) {
|
||||||
|
version := cwversion.VersionStr()
|
||||||
|
metric := &models.Metrics{
|
||||||
|
ApilVersion: &version,
|
||||||
|
Machines: make([]*models.MetricsSoftInfo, 0),
|
||||||
|
Bouncers: make([]*models.MetricsSoftInfo, 0),
|
||||||
|
}
|
||||||
|
machines, err := a.dbClient.ListMachines()
|
||||||
|
if err != nil {
|
||||||
|
return metric, err
|
||||||
|
}
|
||||||
|
bouncers, err := a.dbClient.ListBouncers()
|
||||||
|
if err != nil {
|
||||||
|
return metric, err
|
||||||
|
}
|
||||||
|
for _, machine := range machines {
|
||||||
|
m := &models.MetricsSoftInfo{
|
||||||
|
Version: machine.Version,
|
||||||
|
Name: machine.MachineId,
|
||||||
|
}
|
||||||
|
metric.Machines = append(metric.Machines, m)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, bouncer := range bouncers {
|
||||||
|
m := &models.MetricsSoftInfo{
|
||||||
|
Version: bouncer.Version,
|
||||||
|
Name: bouncer.Type,
|
||||||
|
}
|
||||||
|
metric.Bouncers = append(metric.Bouncers, m)
|
||||||
|
}
|
||||||
|
return metric, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (a *apic) SendMetrics() error {
|
func (a *apic) SendMetrics() error {
|
||||||
defer types.CatchPanic("lapi/metricsToAPIC")
|
defer types.CatchPanic("lapi/metricsToAPIC")
|
||||||
|
|
||||||
|
metrics, err := a.GetMetrics()
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("unable to get metrics (%s), will retry", err)
|
||||||
|
}
|
||||||
|
_, _, err = a.apiClient.Metrics.Add(context.Background(), metrics)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("unable to send metrics (%s), will retry", err)
|
||||||
|
}
|
||||||
|
log.Infof("capi metrics: metrics sent successfully")
|
||||||
log.Infof("start crowdsec api send metrics (interval: %s)", MetricsInterval)
|
log.Infof("start crowdsec api send metrics (interval: %s)", MetricsInterval)
|
||||||
ticker := time.NewTicker(a.metricsInterval)
|
ticker := time.NewTicker(a.metricsInterval)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
version := cwversion.VersionStr()
|
metrics, err := a.GetMetrics()
|
||||||
metric := &models.Metrics{
|
|
||||||
ApilVersion: &version,
|
|
||||||
Machines: make([]*models.MetricsSoftInfo, 0),
|
|
||||||
Bouncers: make([]*models.MetricsSoftInfo, 0),
|
|
||||||
}
|
|
||||||
machines, err := a.dbClient.ListMachines()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
log.Errorf("unable to get metrics (%s), will retry", err)
|
||||||
}
|
}
|
||||||
bouncers, err := a.dbClient.ListBouncers()
|
_, _, err = a.apiClient.Metrics.Add(context.Background(), metrics)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
log.Errorf("capi metrics: failed: %s", err.Error())
|
||||||
|
} else {
|
||||||
|
log.Infof("capi metrics: metrics sent successfully")
|
||||||
}
|
}
|
||||||
for _, machine := range machines {
|
|
||||||
m := &models.MetricsSoftInfo{
|
|
||||||
Version: machine.Version,
|
|
||||||
Name: machine.MachineId,
|
|
||||||
}
|
|
||||||
metric.Machines = append(metric.Machines, m)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, bouncer := range bouncers {
|
|
||||||
m := &models.MetricsSoftInfo{
|
|
||||||
Version: bouncer.Version,
|
|
||||||
Name: bouncer.Type,
|
|
||||||
}
|
|
||||||
metric.Bouncers = append(metric.Bouncers, m)
|
|
||||||
}
|
|
||||||
_, _, err = a.apiClient.Metrics.Add(context.Background(), metric)
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "sending metrics failed")
|
|
||||||
}
|
|
||||||
log.Infof("capi metrics: metrics sent successfully")
|
|
||||||
case <-a.metricsTomb.Dying(): // if one apic routine is dying, do we kill the others?
|
case <-a.metricsTomb.Dying(): // if one apic routine is dying, do we kill the others?
|
||||||
a.pullTomb.Kill(nil)
|
a.pullTomb.Kill(nil)
|
||||||
a.pushTomb.Kill(nil)
|
a.pushTomb.Kill(nil)
|
||||||
|
|
|
@ -828,15 +828,15 @@ func (c *Client) FlushAlerts(MaxAge string, MaxItems int) error {
|
||||||
var totalAlerts int
|
var totalAlerts int
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
c.Log.Info("Flushing orphan alerts")
|
c.Log.Debug("Flushing orphan alerts")
|
||||||
c.FlushOrphans()
|
c.FlushOrphans()
|
||||||
c.Log.Info("Done flushing orphan alerts")
|
c.Log.Debug("Done flushing orphan alerts")
|
||||||
totalAlerts, err = c.TotalAlerts()
|
totalAlerts, err = c.TotalAlerts()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Log.Warningf("FlushAlerts (max items count) : %s", err)
|
c.Log.Warningf("FlushAlerts (max items count) : %s", err)
|
||||||
return errors.Wrap(err, "unable to get alerts count")
|
return errors.Wrap(err, "unable to get alerts count")
|
||||||
}
|
}
|
||||||
c.Log.Infof("FlushAlerts (Total alerts): %d", totalAlerts)
|
c.Log.Debugf("FlushAlerts (Total alerts): %d", totalAlerts)
|
||||||
if MaxAge != "" {
|
if MaxAge != "" {
|
||||||
filter := map[string][]string{
|
filter := map[string][]string{
|
||||||
"created_before": {MaxAge},
|
"created_before": {MaxAge},
|
||||||
|
@ -846,7 +846,7 @@ func (c *Client) FlushAlerts(MaxAge string, MaxItems int) error {
|
||||||
c.Log.Warningf("FlushAlerts (max age) : %s", err)
|
c.Log.Warningf("FlushAlerts (max age) : %s", err)
|
||||||
return errors.Wrapf(err, "unable to flush alerts with filter until: %s", MaxAge)
|
return errors.Wrapf(err, "unable to flush alerts with filter until: %s", MaxAge)
|
||||||
}
|
}
|
||||||
c.Log.Infof("FlushAlerts (deleted max age alerts): %d", nbDeleted)
|
c.Log.Debugf("FlushAlerts (deleted max age alerts): %d", nbDeleted)
|
||||||
deletedByAge = nbDeleted
|
deletedByAge = nbDeleted
|
||||||
}
|
}
|
||||||
if MaxItems > 0 {
|
if MaxItems > 0 {
|
||||||
|
@ -858,7 +858,7 @@ func (c *Client) FlushAlerts(MaxAge string, MaxItems int) error {
|
||||||
}
|
}
|
||||||
deleted := 0
|
deleted := 0
|
||||||
for deleted < nbToDelete {
|
for deleted < nbToDelete {
|
||||||
c.Log.Infof("FlushAlerts (before query with filter) to delete: %d", nbToDelete)
|
c.Log.Debugf("FlushAlerts (before query with filter) to delete: %d", nbToDelete)
|
||||||
alerts, err := c.QueryAlertWithFilter(map[string][]string{
|
alerts, err := c.QueryAlertWithFilter(map[string][]string{
|
||||||
"sort": {"ASC"},
|
"sort": {"ASC"},
|
||||||
"limit": {strconv.Itoa(batchSize)},
|
"limit": {strconv.Itoa(batchSize)},
|
||||||
|
|
Loading…
Reference in a new issue