MCPcopy
hub / github.com/crowdsecurity/crowdsec / checkServices

Method checkServices

pkg/acquisition/modules/docker/run.go:243–295  ·  view source on GitHub ↗
(ctx context.Context, monitChan chan *ContainerConfig, deleteChan chan *ContainerConfig)

Source from the content-addressed store, hash-verified

241}
242
243func (d *Source) checkServices(ctx context.Context, monitChan chan *ContainerConfig, deleteChan chan *ContainerConfig) error {
244 // Track current running services for garbage collection
245 runningServicesID := make(map[string]bool)
246
247 services, err := d.Client.ServiceList(ctx, client.ServiceListOptions{})
248 if err != nil {
249 if strings.Contains(strings.ToLower(err.Error()), "cannot connect to the docker daemon at") {
250 d.logger.Errorf("cannot connect to docker daemon for service monitoring: %v", err)
251
252 // Kill all running service monitoring if we can't connect
253 for id, service := range d.runningServiceState.GetAll() {
254 if service.t.Alive() {
255 d.logger.Infof("killing tail for service %s", service.Name)
256 service.t.Kill(nil)
257
258 if err := service.t.Wait(); err != nil {
259 d.logger.Infof("error while waiting for death of %s : %s", service.Name, err)
260 }
261 }
262
263 d.runningServiceState.Delete(id)
264 }
265 } else {
266 d.logger.Errorf("service list err: %s", err)
267 }
268
269 return err
270 }
271
272 for _, service := range services.Items {
273 runningServicesID[service.ID] = true
274
275 // Don't need to re-eval an already monitored service
276 if _, ok := d.runningServiceState.Get(service.ID); ok {
277 continue
278 }
279
280 if serviceConfig := d.EvalService(ctx, service); serviceConfig != nil {
281 monitChan <- serviceConfig
282 }
283 }
284
285 // Send deletion notifications for services that are no longer running
286 for serviceStateID, serviceConfig := range d.runningServiceState.GetAll() {
287 if _, ok := runningServicesID[serviceStateID]; !ok {
288 deleteChan <- serviceConfig
289 }
290 }
291
292 d.logger.Tracef("Reading logs from %d services", d.runningServiceState.Len())
293
294 return nil
295}
296
297func (d *Source) checkContainers(ctx context.Context, monitChan chan *ContainerConfig, deleteChan chan *ContainerConfig) error {
298 // to track for garbage collection

Callers 1

WatchMethod · 0.95

Calls 9

EvalServiceMethod · 0.95
ServiceListMethod · 0.80
GetAllMethod · 0.80
KillMethod · 0.80
TracefMethod · 0.80
ErrorMethod · 0.65
DeleteMethod · 0.45
GetMethod · 0.45
LenMethod · 0.45

Tested by

no test coverage detected