summaryrefslogtreecommitdiff
path: root/libpod/events/journal_linux.go
diff options
context:
space:
mode:
authorbaude <bbaude@redhat.com>2019-03-27 13:50:54 -0500
committerbaude <bbaude@redhat.com>2019-04-24 16:00:04 -0500
commit7bf7c177ab3f67d5de1689842204c258fca083e4 (patch)
tree960fd1365913209fec61e3226b96a6f8bb1fd051 /libpod/events/journal_linux.go
parentd75543fcd2ce87a9b87b8883400f355979004e91 (diff)
downloadpodman-7bf7c177ab3f67d5de1689842204c258fca083e4.tar.gz
podman-7bf7c177ab3f67d5de1689842204c258fca083e4.tar.bz2
podman-7bf7c177ab3f67d5de1689842204c258fca083e4.zip
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 <bbaude@redhat.com>
Diffstat (limited to 'libpod/events/journal_linux.go')
-rw-r--r--libpod/events/journal_linux.go131
1 files changed, 131 insertions, 0 deletions
diff --git a/libpod/events/journal_linux.go b/libpod/events/journal_linux.go
new file mode 100644
index 000000000..e6b54db1d
--- /dev/null
+++ b/libpod/events/journal_linux.go
@@ -0,0 +1,131 @@
+package events
+
+import (
+ "fmt"
+ "time"
+
+ "github.com/coreos/go-systemd/journal"
+ "github.com/coreos/go-systemd/sdjournal"
+ "github.com/pkg/errors"
+)
+
+// EventJournalD is the journald implementation of an eventer
+type EventJournalD struct {
+ options EventerOptions
+}
+
+// Write to journald
+func (e EventJournalD) Write(ee Event) error {
+ m := make(map[string]string)
+ m["SYSLOG_IDENTIFIER"] = "podman"
+ m["PODMAN_EVENT"] = ee.Status.String()
+ m["PODMAN_TYPE"] = ee.Type.String()
+ m["PODMAN_TIME"] = ee.Time.Format(time.RFC3339Nano)
+
+ // Add specialized information based on the podman type
+ switch ee.Type {
+ case Image:
+ m["PODMAN_NAME"] = ee.Name
+ m["PODMAN_ID"] = ee.ID
+ case Container, Pod:
+ m["PODMAN_IMAGE"] = ee.Image
+ m["PODMAN_NAME"] = ee.Name
+ m["PODMAN_ID"] = ee.ID
+ case Volume:
+ m["PODMAN_NAME"] = ee.Name
+ }
+ return journal.Send(fmt.Sprintf("%s", ee.ToHumanReadable()), journal.PriInfo, m)
+}
+
+// Read reads events from the journal and sends qualified events to the event channel
+func (e EventJournalD) Read(options ReadOptions) error {
+ eventOptions, err := generateEventOptions(options.Filters, options.Since, options.Until)
+ if err != nil {
+ return errors.Wrapf(err, "failed to generate event options")
+ }
+ podmanJournal := sdjournal.Match{Field: "SYSLOG_IDENTIFIER", Value: "podman"} //nolint
+ j, err := sdjournal.NewJournal() //nolint
+ if err != nil {
+ return err
+ }
+ if err := j.AddMatch(podmanJournal.String()); err != nil {
+ return errors.Wrap(err, "failed to add filter for event log")
+ }
+ if len(options.Since) == 0 && len(options.Until) == 0 && options.Stream {
+ if err := j.SeekTail(); err != nil {
+ return errors.Wrap(err, "failed to seek end of journal")
+ }
+ }
+ // the api requires a next|prev before getting a cursor
+ if _, err := j.Next(); err != nil {
+ return err
+ }
+ prevCursor, err := j.GetCursor()
+ if err != nil {
+ return err
+ }
+ defer close(options.EventChannel)
+ for {
+ if _, err := j.Next(); err != nil {
+ return err
+ }
+ newCursor, err := j.GetCursor()
+ if err != nil {
+ return err
+ }
+ if prevCursor == newCursor {
+ if len(options.Until) > 0 || !options.Stream {
+ break
+ }
+ _ = j.Wait(sdjournal.IndefiniteWait) //nolint
+ continue
+ }
+ prevCursor = newCursor
+ entry, err := j.GetEntry()
+ if err != nil {
+ return err
+ }
+ newEvent, err := newEventFromJournalEntry(entry)
+ if err != nil {
+ return err
+ }
+ include := true
+ for _, filter := range eventOptions {
+ include = include && filter(newEvent)
+ }
+ if include {
+ options.EventChannel <- newEvent
+ }
+ }
+ return nil
+
+}
+
+func newEventFromJournalEntry(entry *sdjournal.JournalEntry) (*Event, error) { //nolint
+ newEvent := Event{}
+ eventType, err := StringToType(entry.Fields["PODMAN_TYPE"])
+ if err != nil {
+ return nil, err
+ }
+ eventTime, err := time.Parse(time.RFC3339Nano, entry.Fields["PODMAN_TIME"])
+ if err != nil {
+ return nil, err
+ }
+ eventStatus, err := StringToStatus(entry.Fields["PODMAN_EVENT"])
+ if err != nil {
+ return nil, err
+ }
+ newEvent.Type = eventType
+ newEvent.Time = eventTime
+ newEvent.Status = eventStatus
+ newEvent.Name = entry.Fields["PODMAN_NAME"]
+
+ switch eventType {
+ case Container, Pod:
+ newEvent.ID = entry.Fields["PODMAN_ID"]
+ newEvent.Image = entry.Fields["PODMAN_IMAGE"]
+ case Image:
+ newEvent.ID = entry.Fields["PODMAN_ID"]
+ }
+ return &newEvent, nil
+}