|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/docker/docker/api/types" |
| 10 | + "github.com/docker/docker/client" |
| 11 | + "github.com/docker/docker/api/types/filters" |
| 12 | + "github.com/docker/docker/api/types/events" |
| 13 | + |
| 14 | + "github.com/gregdel/pushover" |
| 15 | + log "github.com/sirupsen/logrus" |
| 16 | + "github.com/jnovack/flag" |
| 17 | +) |
| 18 | + |
| 19 | +// todo |
| 20 | +// Argument Parsing - filters |
| 21 | +// memory leak? |
| 22 | +// generalize to all event/typs |
| 23 | + |
| 24 | +// Global variables |
| 25 | +var PUSHOVER_APITOKEN, PUSHOVER_USER string |
| 26 | +var PUSHOVER_DELAY int64 |
| 27 | + |
| 28 | +func sendPushover(message,title string) bool { |
| 29 | + |
| 30 | + |
| 31 | + // Create a new pushover app with a API token |
| 32 | + app := pushover.New(PUSHOVER_APITOKEN) |
| 33 | + |
| 34 | + // Create a new recipient (user key) |
| 35 | + recipient := pushover.NewRecipient(PUSHOVER_USER) |
| 36 | + |
| 37 | + // Create the message to send |
| 38 | + pushmessage := pushover.NewMessageWithTitle(message,title) |
| 39 | + |
| 40 | + // Send the message to the recipient |
| 41 | + response, err := app.SendMessage(pushmessage, recipient) |
| 42 | + if err != nil { |
| 43 | + log.Panic(err) |
| 44 | + } |
| 45 | + if response != nil { |
| 46 | + log.Debugf("%s", response) |
| 47 | + } |
| 48 | + |
| 49 | + if (*response).Status == 1 { |
| 50 | + // Pushover returns 1 if the message request to the API was valid |
| 51 | + // https://pushover.net/api#response |
| 52 | + return true |
| 53 | + } |
| 54 | + // default to false if response Status !=1 |
| 55 | + return false |
| 56 | +} |
| 57 | + |
| 58 | +func processEvent(event events.Message) { |
| 59 | + // the Docker Events endpoint will return a struct events.Message |
| 60 | + // https://pkg.go.dev/github.com/docker/docker/api/types/events#Message |
| 61 | + |
| 62 | + timestamp := time.Unix(event.Time, 0).Format("02-01-2006 15:04:05") |
| 63 | + status := event.Status |
| 64 | + from := event.From |
| 65 | + ID := event.ID[:8] |
| 66 | + |
| 67 | + message := fmt.Sprintf("%s New status: %s",timestamp, status) |
| 68 | + title := fmt.Sprintf("Container %s from image %s",ID, from) |
| 69 | + |
| 70 | + log.Infof("Container %s from image %s with new status: %s",ID,from,status) |
| 71 | + |
| 72 | + |
| 73 | + delivered := sendPushover(message,title) |
| 74 | + if delivered == true { |
| 75 | + log.Debugf("Message delivered") |
| 76 | + } else { |
| 77 | + log.Warnf("Message not delivered") |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func init() { |
| 82 | + // Output to stdout instead of the default stderr |
| 83 | + // Can be any io.Writer, see below for File example |
| 84 | + log.SetOutput(os.Stdout) |
| 85 | + |
| 86 | + // Set log level - defaults to log.InfoLevel) |
| 87 | + //log.SetLevel(log.DebugLevel) |
| 88 | + |
| 89 | + // Parse flags/config/env variables |
| 90 | + flag.StringVar(&PUSHOVER_APITOKEN, "PUSHOVER_APITOKEN", "", "Pushover's API token") |
| 91 | + flag.StringVar(&PUSHOVER_USER, "PUSHOVER_USER", "", "Pushover's user key") |
| 92 | + flag.Int64Var(&PUSHOVER_DELAY, "PUSHOVER_DELAY",0, "Delay before sending next Pushover message") |
| 93 | + flag.Parse() |
| 94 | + } |
| 95 | + |
| 96 | +func main() { |
| 97 | + // set filters |
| 98 | + // https://docs.docker.com/engine/reference/commandline/events/ |
| 99 | + filter := filters.NewArgs() |
| 100 | + filter.Add("type", "container") |
| 101 | + filter.Add("event", "start") |
| 102 | + filter.Add("event", "stop") |
| 103 | + filter.Add("event", "create") |
| 104 | + filter.Add("event", "die") |
| 105 | + filter.Add("event", "kill") |
| 106 | + |
| 107 | + log.Infof("Starting docker event monitor") |
| 108 | + log.Infof("Using Pushover API Token: %s", PUSHOVER_APITOKEN) |
| 109 | + log.Infof("Using Pushover User Key %s", PUSHOVER_USER) |
| 110 | + log.Infof("Using Pushover delay of %d ms", PUSHOVER_DELAY) |
| 111 | + |
| 112 | + sendPushover(time.Now().Format("02-01-2006 15:04:05"), "Starting docker event monitor") |
| 113 | + |
| 114 | + |
| 115 | + cli, err := client.NewClientWithOpts(client.FromEnv) |
| 116 | + if err != nil { |
| 117 | + log.Panic(err) |
| 118 | + } |
| 119 | + |
| 120 | + // receives events |
| 121 | + event_chan, errs := cli.Events(context.Background(), types.EventsOptions{Filters: filter}) |
| 122 | + |
| 123 | + |
| 124 | + for { |
| 125 | + select { |
| 126 | + case err := <-errs:log.Panic(err) |
| 127 | + case event := <-event_chan: |
| 128 | + processEvent(event) |
| 129 | + // Adding a small configurable delay here |
| 130 | + // Sometimes events are pushed through the channel really quickly, but |
| 131 | + // they arrive on the Pushover clients in wrong order (probably due to message delivery time) |
| 132 | + // Consuming the events with a small delay solves the issue |
| 133 | + time.Sleep(time.Duration(PUSHOVER_DELAY) * time.Millisecond) |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments