Skip to content

Commit cf179db

Browse files
authored
Merge pull request #22 from yubiuser/development
v2.0.0
2 parents ac4fd8c + 2d2fefd commit cf179db

File tree

13 files changed

+637
-193
lines changed

13 files changed

+637
-193
lines changed

.devcontainer/Dockerfile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
From golang:1.21-alpine3.18
1+
ARG alpine_version=3.19
2+
ARG golang_version=1.21
3+
4+
FROM golang:${golang_version}-alpine${alpine_version}
25
RUN apk add --no-cache \
36
git \
47
nano\
@@ -15,3 +18,7 @@ RUN adduser $USERNAME -s /bin/sh -D -u $USER_UID $USER_GID && \
1518
chmod 0440 /etc/sudoers.d/$USERNAME
1619

1720
USER $USERNAME
21+
22+
RUN go install -v golang.org/x/tools/gopls@latest
23+
RUN go install -v github.com/go-delve/delve/cmd/dlv@latest
24+
RUN go install -v honnef.co/go/tools/cmd/staticcheck@latest

.devcontainer/devcontainer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
"vscode": {
99
"settings": {},
1010
"extensions": [
11-
"ms-vscode.go",
1211
"eamodio.gitlens",
1312
"EditorConfig.EditorConfig",
14-
"golang.Go"
13+
"golang.Go",
14+
"github.vscode-github-actions"
1515
]
1616
}
1717
},

.github/dependabot.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ updates:
99
open-pull-requests-limit: 10
1010
target-branch: development
1111
- package-ecosystem: gomod
12-
directory: "/"
12+
directory: "/src/"
1313
schedule:
1414
interval: weekly
1515
day: sunday
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Remove obsolet PR images from registry
2+
3+
on:
4+
pull_request:
5+
types: [closed]
6+
7+
env:
8+
PACKAGE_NAME: docker-event-monitor
9+
10+
jobs:
11+
Delete_PR_image:
12+
if: |
13+
github.event_name == 'pull_request'
14+
&& github.event.pull_request.head.repo.full_name == github.repository
15+
runs-on: ubuntu-latest
16+
permissions:
17+
packages: write
18+
steps:
19+
- name: Get image ID of PR
20+
id: version
21+
run: |
22+
curl -sSL \
23+
-H "Accept: application/vnd.github+json" \
24+
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
25+
-H "X-GitHub-Api-Version: 2022-11-28" \
26+
https://api.github.com/users/${{ github.repository_owner }}/packages/container/${{ env.PACKAGE_NAME }}/versions >> containerMeta.json ;
27+
echo "VERSION_ID=$(jq -r '.[] | select(.metadata.container.tags[] == "pr-${{ github.event.pull_request.number }}").id' containerMeta.json)" >> "$GITHUB_ENV" ;
28+
29+
- name: Delete PR image
30+
uses: actions/[email protected]
31+
if: ${{ env.VERSION_ID != '' }}
32+
with:
33+
package-version-ids: ${{ env.VERSION_ID }}
34+
package-type: container
35+
package-name: ${{ env.PACKAGE_NAME }}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Remove untagged images from registry
2+
3+
on:
4+
workflow_dispatch:
5+
schedule:
6+
- cron: "0 0 * * *"
7+
8+
env:
9+
PACKAGE_NAME: docker-event-monitor
10+
11+
jobs:
12+
Delete_untagged_images:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Login to GitHub Container Registry
16+
uses: docker/[email protected]
17+
with:
18+
registry: ghcr.io
19+
username: ${{ github.actor }}
20+
password: ${{ secrets.PAT_TOKEN }}
21+
22+
- name: Delete all images from repository without tags
23+
uses: Chizkiyahu/[email protected]
24+
with:
25+
token: ${{ secrets.PAT_TOKEN }}
26+
repository_owner: ${{ github.repository_owner }}
27+
repository: ${{ github.repository }}
28+
package_name: ${{ env.PACKAGE_NAME }}
29+
untagged_only: true
30+
owner_type: user
31+
except_untagged_multiplatform: true

.github/workflows/ghcr.yml

Lines changed: 97 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,128 @@ on:
55
release:
66
types: [published]
77
pull_request:
8+
push:
9+
branches:
10+
- development
11+
12+
env:
13+
REGISTRY_IMAGE: ghcr.io/${{ github.repository }}
814

915
jobs:
10-
build-and-push:
16+
build:
1117
runs-on: ubuntu-latest
1218
permissions:
1319
contents: read
1420
packages: write
21+
strategy:
22+
fail-fast: true
23+
matrix:
24+
include:
25+
- platform: linux/amd64
26+
- platform: linux/arm64
27+
- platform: linux/arm/v6
28+
- platform: linux/arm/v7
29+
- platform: linux/386
1530
steps:
31+
- name: Prepare name for digest up/download
32+
run: |
33+
platform=${{ matrix.platform }}
34+
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
35+
1636
- name: Checkout Code
17-
uses: actions/checkout@v3.6.0
37+
uses: actions/checkout@v4.1.1
1838

1939
- name: Set up QEMU
20-
uses: docker/setup-qemu-action@v2.2.0
40+
uses: docker/setup-qemu-action@v3.0.0
2141

2242
- name: Set up Docker Buildx
23-
uses: docker/[email protected]
43+
uses: docker/[email protected]
44+
with:
45+
buildkitd-flags: --debug
2446

2547
- name: Extract metadata (tags, labels) for Docker
2648
id: meta
27-
uses: docker/metadata-action@v4
49+
uses: docker/metadata-action@v5
2850
with:
2951
images: |
30-
ghcr.io/${{ github.repository_owner }}/${{ github.repository }}
31-
flavor: latest=${{ startsWith(github.ref, 'refs/tags/') }}
32-
tags: |
33-
type=semver,pattern={{version}}
34-
type=semver,pattern={{major}}.{{minor}}
35-
type=semver,pattern={{major}}
36-
type=sha, enable=${{ !startsWith(github.ref, 'refs/tags/') }}
37-
type=edge
38-
52+
${{ env.REGISTRY_IMAGE }}
3953
- name: Login to GitHub Container Registry
40-
if: github.event_name != 'pull_request'
41-
uses: docker/[email protected]
54+
uses: docker/[email protected]
4255
with:
4356
registry: ghcr.io
4457
username: ${{ github.repository_owner }}
4558
password: ${{ secrets.GITHUB_TOKEN }}
4659

4760
- name: Build and push Docker image
48-
uses: docker/[email protected]
61+
uses: docker/[email protected]
62+
id: build
4963
with:
5064
context: .
5165
file: ./Dockerfile
52-
platforms: linux/amd64, linux/arm64, linux/386, linux/arm/v7, linux/arm/v6
53-
push: ${{ github.event_name != 'pull_request' }}
54-
cache-from: type=gha
55-
cache-to: type=gha
56-
provenance: false
57-
tags: ${{ steps.meta.outputs.tags }}
66+
platforms: ${{ matrix.platform }}
5867
labels: ${{ steps.meta.outputs.labels }}
68+
provenance: false
69+
outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=${{ github.event.pull_request.head.repo.full_name == github.repository || github.event_name == 'push' || github.event_name == 'release' }}
70+
- name: Export digest
71+
run: |
72+
mkdir -p /tmp/digests
73+
digest="${{ steps.build.outputs.digest }}"
74+
touch "/tmp/digests/${digest#sha256:}"
75+
- name: Upload digest
76+
uses: actions/[email protected]
77+
with:
78+
name: digests-${{ env.PLATFORM_PAIR }}
79+
path: /tmp/digests/*
80+
if-no-files-found: error
81+
retention-days: 1
82+
83+
merge:
84+
runs-on: ubuntu-latest
85+
needs:
86+
- build
87+
if: |
88+
github.actor != 'dependabot[bot]'
89+
&& ( github.event.pull_request.head.repo.full_name == github.repository || github.event_name == 'push' || github.event_name == 'release' )
90+
permissions:
91+
contents: read
92+
packages: write
93+
steps:
94+
- name: Download digests
95+
uses: actions/[email protected]
96+
with:
97+
path: /tmp/digests
98+
pattern: digests-*
99+
merge-multiple: true
100+
101+
- name: Set up Docker Buildx
102+
uses: docker/[email protected]
103+
- name: Docker meta
104+
id: meta
105+
uses: docker/metadata-action@v5
106+
with:
107+
images: |
108+
${{ env.REGISTRY_IMAGE }}
109+
flavor: latest=${{ startsWith(github.ref, 'refs/tags/') }}
110+
tags: |
111+
type=semver,pattern={{version}}
112+
type=semver,pattern={{major}}.{{minor}}
113+
type=semver,pattern={{major}}
114+
type=sha,enable=${{ github.event_name == 'workflow_dispatch' }}
115+
type=ref,event=pr
116+
type=ref,event=branch
117+
- name: Login to GitHub Container Registry
118+
uses: docker/[email protected]
119+
with:
120+
registry: ghcr.io
121+
username: ${{ github.repository_owner }}
122+
password: ${{ secrets.GITHUB_TOKEN }}
123+
- name: Create manifest list and push
124+
working-directory: /tmp/digests
125+
run: |
126+
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
127+
$(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *)
128+
- name: Inspect image
129+
run: |
130+
docker buildx imagetools inspect --raw ${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }}
131+
132+

.github/workflows/sync-back-to-dev.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
name: Syncing branches
1919
steps:
2020
- name: Checkout
21-
uses: actions/checkout@v3.6.0
21+
uses: actions/checkout@v4.1.1
2222
- name: Opening pull request
2323
run: gh pr create -B development -H main --title 'Sync main back into development' --body 'Created by Github action' --label 'internal'
2424
env:

Dockerfile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
From golang:1.21-alpine3.18 as builder
1+
ARG alpine_version=3.19
2+
ARG golang_version=1.21
3+
4+
FROM golang:${golang_version}-alpine${alpine_version} as builder
25

36
COPY /src /src
47
WORKDIR /src
58
RUN go mod download
69
RUN CGO_ENABLED=0 go build -ldflags "-s -w" docker-event-monitor.go
710

8-
From scratch as deploy
11+
FROM scratch as deploy
912
COPY --from=builder /src/docker-event-monitor docker-event-monitor
1013
# the tls certificates:
1114
# this pulls directly from the upstream image, which already has ca-certificates:

Readme.md

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ Monitor Docker events and send push notifications for each event.
77
## Features
88

99
- Small memory and CPU footprint
10-
- Pushover integration
11-
- Gotify integration
10+
- Pushover notification
11+
- Gotify notification
12+
- E-Mail notification (SMTP)
1213
- Filter events
1314

1415
## Background
1516

16-
I've been using [Monocker](https://github.com/petersem/monocker) to monitor my Docker containers and get push notifications on status changes. However, it's using polling (with hard lower limit of 10 sec) to poll for status changes. This is too long to catch all status changes (e.g. watchtower updating an container). While I did remove the limit in my own Monocker fork, I noticed that the CPU usage goes up quite a bit for polling times < 1sec.
17+
I've been using [Monocker](https://github.com/petersem/monocker) to monitor my Docker containers and get push notifications on status changes. However, it's using polling (with hard lower limit of 10 sec) to poll for status changes. This is too long to catch all status changes (e.g. watchtower updating a container). While I did remove the limit in my own Monocker fork, I noticed that the CPU usage goes up quite a bit for polling times < 1sec.
1718
I needed another soultion, and found [docker-events-notifier](https://github.com/hasnat/docker-events-notifier), but Pushover integration was missing.
1819

1920
So I started to develop my own solution which ended up being a `bash` script doing exactly what I wanted it to do (you can still find it in `/legacy/`). However, the used `jq` caused CPU spikes for each processed event. As I could not find a good solution, I decied to write my own application and to learn something new - [Go](https://go.dev/).
@@ -29,7 +30,7 @@ The application uses Docker's API to connect to the [event stream](https://docs.
2930
The simplest way to use the docker event monitor is to run the docker container. It'a very small ( < 10MB) image. You can download it via
3031

3132
```shell
32-
docker pull ghcr.io/yubiuser/yubiuser/docker-event-monitor:latest
33+
docker pull ghcr.io/yubiuser/docker-event-monitor:latest
3334
```
3435

3536
### Docker compose
@@ -40,7 +41,7 @@ version: '2.4'
4041
services:
4142
docker-event-monitor:
4243
container_name: docker-event-monitor
43-
image: ghcr.io/yubiuser/yubiuser/docker-event-monitor:latest
44+
image: ghcr.io/yubiuser/docker-event-monitor:latest
4445
volumes:
4546
- /var/run/docker.sock:/var/run/docker.sock:ro
4647
- /etc/localtime:/etc/localtime:ro
@@ -52,6 +53,13 @@ services:
5253
GOTIFY: false
5354
GOTIFY_URL: 'URL'
5455
GOTIFY_TOKEN: 'TOKEN'
56+
MAIL: false
57+
MAIL_FROM: '[email protected]'
58+
MAIL_TO: '[email protected]'
59+
MAIL_USER: 'SMTP USER'
60+
MAIL_PASSWORD: 'PASSWORD'
61+
MAIL_PORT: 587
62+
MAIL_HOST: '[email protected]'
5563
FILTER: 'event=start,event=stop,type=container'
5664
DELAY: '500ms'
5765
LOG_LEVEL: 'info'
@@ -74,14 +82,22 @@ If you have a suitable `Go` environment set up, you can build the binary from `/
7482

7583
Configurations can use the CLI flags or environment variables. The table below outlines all supported options and their respective env vars.
7684

77-
| Flag | Env Variable | Default | Details |
78-
| ---------------- | ---------------------- | ------- |-------- |
79-
| `--pushover` | `PUSHOVER` | `false` |Enable/Disable Pushover notification|
80-
| `--pushoverapitoken` | `PUSHOVER_APITOKEN` | `""` | |
81-
| `--pushoveruserkey` | `PUSHOVER_USER` | `""` | |
82-
| `--delay` | `DELAY` | `500ms` |Delay befor processing next event. Can be useful if messages arrive in wrong order |
83-
| `--gotify` | `GOTIFY` | `false` |Enable/Disable Gotify notification|
84-
| `--gotifyurl` | `GOTIFY_URL` | `""` | |
85-
| `--gotifytoken` | `GOTIFY_TOKEN` | `""` | |
86-
| `--filter` | `FILTER` | `""` | Filter events. Uses the same filters as `docker events` (see [here](https://docs.docker.com/engine/reference/commandline/events/#filter)) |
87-
| `--loglevel` | `LOG_LEVEL` | `"info"`| Use `debug` for more verbose logging` |
85+
| Flag | Env Variable | Default | Details |
86+
| ---------------- | ---------------------- | ------- |-------- |
87+
| `--pushover` | `PUSHOVER` | `false` | Enable/Disable Pushover notification|
88+
| `--pushoverapitoken` | `PUSHOVER_APITOKEN` | `""` | |
89+
| `--pushoveruserkey` | `PUSHOVER_USER` | `""` | |
90+
| `--delay` | `DELAY` | `500ms` | Delay befor processing next event. Can be useful if messages arrive in wrong order |
91+
| `--gotify` | `GOTIFY` | `false` | Enable/Disable Gotify notification|
92+
| `--gotifyurl` | `GOTIFY_URL` | `""` | |
93+
| `--gotifytoken` | `GOTIFY_TOKEN` | `""` | |
94+
| `--mail` | `MAIL` | `false` | Enable/Disable E-Mail (SMTP) notification|
95+
| `--mailfrom` | `MAIL_FROM` | `""` | optional: `[email protected]`, set to MAIL_USER if empty/unset |
96+
| `--mailto` | `MAIL_TO` | `""` | `[email protected]` |
97+
| `--mailuser` | `MAIL_USER` | `""` | SMTP username |
98+
| `--mailpassword` | `MAIL_PASSWORD` | `""` | |
99+
| `--mailport` | `MAIL_PORT` | `587` | |
100+
| `--mailhost` | `MAIL_HOST` | `""` | `[email protected]` |
101+
| `--filter` | `FILTER` | `""` | Filter events. Uses the same filters as `docker events` (see [here](https://docs.docker.com/engine/reference/commandline/events/#filter)) |
102+
| `--loglevel` | `LOG_LEVEL` | `"info"`| Use `debug` for more verbose logging |
103+
| `--servertag` | `SERVER_TAG` | `""` | Prefix to include in the title of notifications. Useful when running docker-event-monitors on multiple machines |

docker-compose.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version: '2.4'
33
services:
44
docker-event-monitor:
55
container_name: docker-event-monitor
6-
image: ghcr.io/yubiuser/yubiuser/docker-event-monitor:latest
6+
image: ghcr.io/yubiuser/docker-event-monitor:latest
77
volumes:
88
- /var/run/docker.sock:/var/run/docker.sock:ro
99
- /etc/localtime:/etc/localtime:ro
@@ -15,6 +15,13 @@ services:
1515
GOTIFY: false
1616
GOTIFY_URL: 'URL'
1717
GOTIFY_TOKEN: 'TOKEN'
18+
MAIL: false
19+
MAIL_FROM: '[email protected]'
20+
MAIL_TO: '[email protected]'
21+
MAIL_USER: 'SMTP USER'
22+
MAIL_PASSWORD: 'PASSWORD'
23+
MAIL_PORT: 587
24+
MAIL_HOST: '[email protected]'
1825
FILTER: 'event=start,event=stop,type=container'
1926
DELAY: '500ms'
2027
LOG_LEVEL: 'info'

0 commit comments

Comments
 (0)