func initiailizeMonitoring() { defer log.Infoln("Initializing autonomic monitoring") ch_aut_err := chn.GetAutonomicErrChannel() enableLogReading = cfg.GetAgentAutonomic().EnableLogReading mtr.Initialize(srv.List()) // Start log reader if needed if enableLogReading { lgr.StartLogReader() log.WithField("logreader", enableLogReading).Debugln("Log reading is enabled") } // Get the list of containers (running or not) to monitor containers, err := container.Docker().Client.ListContainers(true, false, "") if err != nil { log.WithField("err", err).Debugln("Error monitoring containers") ch_aut_err <- err } // Start the monitor for each configured service for _, c := range containers { info, _ := container.Docker().Client.InspectContainer(c.Id) status := getContainerStatus(info) service, err := srv.GetServiceByImage(c.Image) if err != nil { log.WithFields(log.Fields{ "err": err, "image": c.Image, }).Warningln("Error monitoring service") } else { e := evt.Event{ Service: service.Name, Image: c.Image, Instance: c.Id, Status: status, } evt.HandleCreateEvent(e) evt.HanldeStartEvent(e) mtr.AddInstance(c.Id) if _, ok := instBuffer[c.Id]; !ok { instBuffer[c.Id] = instanceMetricBuffer{ cpuInst: utils.BuildBuffer(c_B_SIZE), cpuSys: utils.BuildBuffer(c_B_SIZE), } } container.Docker().Client.StartMonitorStats(c.Id, statCallBack, ch_mnt_stats_err) if status == enum.PENDING && enableLogReading { startMonitorLog(c.Id) } } } }
// Events are: attach, commit, copy, create, destroy, die, exec_create, exec_start, export, kill, oom, pause, rename, resize, restart, start, stop, top, unpause, update func eventCallback(event *dockerclient.Event, ec chan error, args ...interface{}) { log.Debugln("Received event") // By now we do not handle events with type != container if event.Type != "container" { log.WithField("type", event.Type).Debugln("Received event with type different from 'container'") return } service, err := srv.GetServiceByImage(event.From) if err != nil { log.WithFields(log.Fields{ "err": err, "event": event, }).Warnln("Cannot handle event") return } e := evt.Event{ Service: service.Name, Image: event.From, Instance: event.ID, Type: event.Type, } switch event.Status { case "create": log.WithField("image", e.Image).Debugln("Received create signal") evt.HandleCreateEvent(e) container.Docker().Client.StartMonitorStats(e.Instance, statCallBack, ch_mnt_stats_err) case "start": log.WithField("image", e.Image).Debugln("Received start signal") if _, ok := instBuffer[e.Instance]; !ok { instBuffer[e.Instance] = instanceMetricBuffer{ cpuInst: utils.BuildBuffer(c_B_SIZE), cpuSys: utils.BuildBuffer(c_B_SIZE), } } e.Status = enum.PENDING evt.HanldeStartEvent(e) mtr.AddInstance(e.Instance) if enableLogReading { startMonitorLog(event.ID) } case "stop": log.WithField("image", e.Image).Debugln("Received stop signal") case "kill": log.WithField("image", e.Image).Debugln("Received kill signal") case "die": log.WithField("image", e.Image).Debugln("Received die signal") delete(instBuffer, e.Instance) mtr.RemoveInstance(e.Instance) evt.HandleStopEvent(e) case "destroy": log.WithField("id", e.Instance).Debugln("Received destroy signal") evt.HandleRemoveEvent(e) default: log.WithFields(log.Fields{ "err": "event not handled", "event": event.Status, "image": event.From, }).Debugln("Received unknown signal") } log.Debugln("Event handled") }