Fix issue 1033 (#1034)

* Fix issue 1033
This commit is contained in:
AlteredCoder 2021-11-02 12:16:33 +01:00 committed by GitHub
parent d1ce543440
commit fb54388e93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 35 deletions

View file

@ -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 {
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)
ticker := time.NewTicker(a.metricsInterval)
for {
select {
case <-ticker.C:
version := cwversion.VersionStr()
metric := &models.Metrics{
ApilVersion: &version,
Machines: make([]*models.MetricsSoftInfo, 0),
Bouncers: make([]*models.MetricsSoftInfo, 0),
}
machines, err := a.dbClient.ListMachines()
metrics, err := a.GetMetrics()
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 {
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?
a.pullTomb.Kill(nil)
a.pushTomb.Kill(nil)

View file

@ -828,15 +828,15 @@ func (c *Client) FlushAlerts(MaxAge string, MaxItems int) error {
var totalAlerts int
var err error
c.Log.Info("Flushing orphan alerts")
c.Log.Debug("Flushing orphan alerts")
c.FlushOrphans()
c.Log.Info("Done flushing orphan alerts")
c.Log.Debug("Done flushing orphan alerts")
totalAlerts, err = c.TotalAlerts()
if err != nil {
c.Log.Warningf("FlushAlerts (max items count) : %s", err)
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 != "" {
filter := map[string][]string{
"created_before": {MaxAge},
@ -846,7 +846,7 @@ func (c *Client) FlushAlerts(MaxAge string, MaxItems int) error {
c.Log.Warningf("FlushAlerts (max age) : %s", err)
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
}
if MaxItems > 0 {
@ -858,7 +858,7 @@ func (c *Client) FlushAlerts(MaxAge string, MaxItems int) error {
}
deleted := 0
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{
"sort": {"ASC"},
"limit": {strconv.Itoa(batchSize)},