From 7bf7c177ab3f67d5de1689842204c258fca083e4 Mon Sep 17 00:00:00 2001 From: baude Date: Wed, 27 Mar 2019 13:50:54 -0500 Subject: journald event logging add the ability for podman to read and write events to journald instead of just a logfile. This can be controlled in libpod.conf with the `events_logger` attribute of `journald` or `file`. The default will be set to `journald`. Signed-off-by: baude --- libpod/events/events.go | 148 ++++++++---------------------------------------- 1 file changed, 24 insertions(+), 124 deletions(-) (limited to 'libpod/events/events.go') diff --git a/libpod/events/events.go b/libpod/events/events.go index 074a3ba5b..e8c61faa0 100644 --- a/libpod/events/events.go +++ b/libpod/events/events.go @@ -6,109 +6,18 @@ import ( "os" "time" - "github.com/containers/storage" + "github.com/hpcloud/tail" "github.com/pkg/errors" ) -// Event describes the attributes of a libpod event -type Event struct { - // ContainerExitCode is for storing the exit code of a container which can - // be used for "internal" event notification - ContainerExitCode int - // ID can be for the container, image, volume, etc - ID string - // Image used where applicable - Image string - // Name where applicable - Name string - // Status describes the event that occurred - Status Status - // Time the event occurred - Time time.Time - // Type of event that occurred - Type Type -} - -// Type of event that occurred (container, volume, image, pod, etc) -type Type string - -// Status describes the actual event action (stop, start, create, kill) -type Status string - -const ( - // If you add or subtract any values to the following lists, make sure you also update - // the switch statements below and the enums for EventType or EventStatus in the - // varlink description file. - - // Container - event is related to containers - Container Type = "container" - // Image - event is related to images - Image Type = "image" - // Pod - event is related to pods - Pod Type = "pod" - // Volume - event is related to volumes - Volume Type = "volume" +// String returns a string representation of EventerType +func (et EventerType) String() string { + if et == LogFile { + return "file" - // Attach ... - Attach Status = "attach" - // Checkpoint ... - Checkpoint Status = "checkpoint" - // Cleanup ... - Cleanup Status = "cleanup" - // Commit ... - Commit Status = "commit" - // Create ... - Create Status = "create" - // Exec ... - Exec Status = "exec" - // Exited indicates that a container's process died - Exited Status = "died" - // Export ... - Export Status = "export" - // History ... - History Status = "history" - // Import ... - Import Status = "import" - // Init ... - Init Status = "init" - // Kill ... - Kill Status = "kill" - // LoadFromArchive ... - LoadFromArchive Status = "status" - // Mount ... - Mount Status = "mount" - // Pause ... - Pause Status = "pause" - // Prune ... - Prune Status = "prune" - // Pull ... - Pull Status = "pull" - // Push ... - Push Status = "push" - // Remove ... - Remove Status = "remove" - // Restore ... - Restore Status = "restore" - // Save ... - Save Status = "save" - // Start ... - Start Status = "start" - // Stop ... - Stop Status = "stop" - // Sync ... - Sync Status = "sync" - // Tag ... - Tag Status = "tag" - // Unmount ... - Unmount Status = "unmount" - // Unpause ... - Unpause Status = "unpause" - // Untag ... - Untag Status = "untag" -) - -// EventFilter for filtering events -type EventFilter func(*Event) bool + } + return "journald" +} // NewEvent creates a event struct and populates with // the given status and time. @@ -119,30 +28,6 @@ func NewEvent(status Status) Event { } } -// Write will record the event to the given path -func (e *Event) Write(path string) error { - // We need to lock events file - lock, err := storage.GetLockfile(path + ".lock") - if err != nil { - return err - } - lock.Lock() - defer lock.Unlock() - f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0700) - if err != nil { - return err - } - defer f.Close() - eventJSONString, err := e.ToJSONString() - if err != nil { - return err - } - if _, err := f.WriteString(fmt.Sprintf("%s\n", eventJSONString)); err != nil { - return err - } - return nil -} - // Recycle checks if the event log has reach a limit and if so // renames the current log and starts a new one. The remove bool // indicates the old log file should be deleted. @@ -172,7 +57,7 @@ func (e *Event) ToHumanReadable() string { // NewEventFromString takes stringified json and converts // it to an event -func NewEventFromString(event string) (*Event, error) { +func newEventFromJSONString(event string) (*Event, error) { e := Event{} if err := json.Unmarshal([]byte(event), &e); err != nil { return nil, err @@ -222,6 +107,7 @@ func StringToStatus(name string) (Status, error) { case Commit.String(): return Commit, nil case Create.String(): + return Create, nil case Exec.String(): return Exec, nil @@ -270,3 +156,17 @@ func StringToStatus(name string) (Status, error) { } return "", errors.Errorf("unknown event status %s", name) } + +func (e EventLogFile) getTail(options ReadOptions) (*tail.Tail, error) { + reopen := true + seek := tail.SeekInfo{Offset: 0, Whence: os.SEEK_END} + if options.FromStart || !options.Stream { + seek.Whence = 0 + reopen = false + } + stream := options.Stream + if len(options.Until) > 0 { + stream = false + } + return tail.TailFile(e.options.LogFilePath, tail.Config{ReOpen: reopen, Follow: stream, Location: &seek, Logger: tail.DiscardingLogger}) +} -- cgit v1.2.3-54-g00ecf