Skip to content

Commit

Permalink
Merge pull request #19 from RobinHeidenis/feat/add-hostname-option
Browse files Browse the repository at this point in the history
feat: 🎸 add hostname option
  • Loading branch information
RobinHeidenis authored Nov 30, 2023
2 parents f6ca698 + 44a30e5 commit f580eac
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 46 deletions.
25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<li><a href="#running-containers">Running containers</a></li>
<li><a href="#all-containers">All containers</a></li>
</ul>
<li><a href="#notifications-with-a-hostname">Notifications with a hostname</a></li>
</ul>
</li>
</ol>
Expand Down Expand Up @@ -131,8 +132,6 @@ Be sure to download the right binary for your operating system.
| ![macOS](https://img.shields.io/badge/mac%20os-000000?style=for-the-badge&logo=macos&logoColor=F0F0F0) | heimdall_Darwin_x86_64 |

Other binaries are available in case you have a different architecture or operating system.
Other binaries are available in case you have a different architecture or operating system.


After downloading the release, you can run it by executing the following command:
```bash
Expand All @@ -155,15 +154,16 @@ sudo ln -s ~/.docker/desktop/docker.sock /var/run/docker.sock
## Customisation
Heimdall can be customised by using flags or environment variables. Either works, but flags take precedence over environment variables. Short flags can be used in place of the longer flags to save some typing.

| Long flag | Short flag | Environment Variable | Default | Required | Explanation |
|---------------------------|------------|----------------------------------|-----------|--------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------|
| `--periodic-notification` | `-n` | `HEIMDALL_PERIODIC_NOTIFICATION` | `false` | No | Enable periodic notifications |
| `--notification-interval` | `-i` | `HEIMDALL_NOTIFICATION_INTERVAL` | `60` | Only if periodic notifications are enabled and you want a different value than default | How often (in minutes) periodic notifications should be sent |
| `--all-containers` | `-a` | `HEIMDALL_ALL_CONTAINERS` | `false` | Only if periodic notifications are enabled and you want periodic notifications on all containers | Enable periodic notification reporting on all containers, including stopped ones |
| `--retry` | `-r` | `HEIMDALL_RETRY` | `10` | No | How long Heimdall should sleep before retrying in case the Docker event stream ends unexpectedly |
| `--provider` | `-p` | `HEIMDALL_PROVIDER` | `discord` | No | What notification provider should be used. Possible values: `discord` |
| `--webhook-url` | `-w` | `HEIMDALL_WEBHOOK_URL` | - | Yes | What URL Heimdall should use to send notifications to |
| `--debug` | `-d` | `HEIMDALL_DEBUG` | `false` | No | Enable extra debug messages |
| Long flag | Short flag | Environment Variable | Default | Required | Explanation |
|---------------------------|------------|----------------------------------|-----------|--------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `--periodic-notification` | `-n` | `HEIMDALL_PERIODIC_NOTIFICATION` | `false` | No | Enable periodic notifications |
| `--notification-interval` | `-i` | `HEIMDALL_NOTIFICATION_INTERVAL` | `60` | Only if periodic notifications are enabled and you want a different value than default | How often (in minutes) periodic notifications should be sent |
| `--all-containers` | `-a` | `HEIMDALL_ALL_CONTAINERS` | `false` | Only if periodic notifications are enabled and you want periodic notifications on all containers | Enable periodic notification reporting on all containers, including stopped ones |
| `--retry` | `-r` | `HEIMDALL_RETRY` | `10` | No | How long Heimdall should sleep before retrying in case the Docker event stream ends unexpectedly |
| `--provider` | `-p` | `HEIMDALL_PROVIDER` | `discord` | No | What notification provider should be used. Possible values: `discord` |
| `--webhook-url` | `-w` | `HEIMDALL_WEBHOOK_URL` | - | Yes | What URL Heimdall should use to send notifications to |
| `--hostname` | `-h` | `HEIMDALL_HOSTNAME` | - | No, but recommended in case you run Heimdall on multiple devices | The name of the host Heimdall is running on. This will cause Heimdall to send status updates with the device name in them: `Docker Container Status Update (Hostname)` |
| `--debug` | `-d` | `HEIMDALL_DEBUG` | `false` | No | Enable extra debug messages |


## Technologies
Expand Down Expand Up @@ -214,3 +214,6 @@ It uses the following technologies:

#### All containers
![container-list-all.png](public/container-list-all.png)

### Notifications with a hostname
![notifications-with-hostname.png](public/notifications-with-hostname.png)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ require (
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/time v0.4.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.16.0 // indirect
gotest.tools/v3 v3.5.1 // indirect
)
18 changes: 4 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ github.com/jedib0t/go-pretty/v6 v6.4.9 h1:vZ6bjGg2eBSrJn365qlxGcaWu09Id+LHtrfDWl
github.com/jedib0t/go-pretty/v6 v6.4.9/go.mod h1:Ndk3ase2CkQbXLLNf5QDHoYb6J9WtVfmHZu9n8rk2xs=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
Expand All @@ -48,7 +47,6 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
Expand All @@ -65,41 +63,33 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.6.0 h1:L4ZwwTvKW9gr0ZMS1yrHD9GZhIuVjOBBnaKH+SPQK0Q=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.4.0 h1:Z81tqI5ddIoXDPvVQ7/7CC9TnLM7ubaFG2qXYd5BbYY=
golang.org/x/time v0.4.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.16.0 h1:GO788SKMRunPIBCXiQyo2AaexLstOrVhuAL5YwsckQM=
golang.org/x/tools v0.16.0/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
49 changes: 29 additions & 20 deletions pkg/heimdall/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,25 @@ func New(webhookURL string) *DiscordProvider {
}
}

var AvatarUrl = "https://raw.githubusercontent.com/RobinHeidenis/heimdall/main/public/logo.png"
var BotName = "Heimdall"
var avatarUrl = "https://raw.githubusercontent.com/RobinHeidenis/heimdall/main/public/logo.png"
var botName = "Heimdall"

var footerText = "Sent by Heimdall"

var footer = discordwebhook.Footer{
Text: &footerText,
}

func getHostnameAdditionText() string {
hostnameAddition := ""
if Hostname != "" {
hostnameAddition = fmt.Sprintf(" (%s)", Hostname)
}
return hostnameAddition
}

func (p DiscordProvider) SendPeriodicContainerStatusUpdate(updateTable string) {
authorName := "Docker Container Status"
authorName := "Docker Container Status" + getHostnameAdditionText()
authorIconUrl := "https://www.docker.com/wp-content/uploads/2023/04/cropped-Docker-favicon-32x32.png"
title := "Periodic status update"
description := fmt.Sprintf("```\n%s```", updateTable)
Expand All @@ -33,11 +47,12 @@ func (p DiscordProvider) SendPeriodicContainerStatusUpdate(updateTable string) {
Author: &author,
Title: &title,
Description: &description,
Footer: &footer,
}}

message := discordwebhook.Message{
Username: &BotName,
AvatarUrl: &AvatarUrl,
Username: &botName,
AvatarUrl: &avatarUrl,
Embeds: &embeds,
}

Expand Down Expand Up @@ -66,19 +81,19 @@ func makeWebhookMessage(event ContainerEvent) discordwebhook.Message {
var title string
switch event.Container.Health {
case Running:
title = fmt.Sprintf("Container %s has started", event.Container.Name)
title = fmt.Sprintf("%s has started", event.Container.Name)
if event.Event == UnpausedEvent {
title = fmt.Sprintf("Container %s has been unpaused", event.Container.Name)
title = fmt.Sprintf("%s has been unpaused", event.Container.Name)
}
case Exited:
title = fmt.Sprintf("Container %s has stopped", event.Container.Name)
title = fmt.Sprintf("%s has stopped", event.Container.Name)
case Paused:
title = fmt.Sprintf("Container %s has been paused", event.Container.Name)
title = fmt.Sprintf("%s has been paused", event.Container.Name)
case Errored:
title = fmt.Sprintf("Container %s has errored", event.Container.Name)
title = fmt.Sprintf("%s has errored", event.Container.Name)
description = fmt.Sprintf("`%s`'s status is now `%s` with exit code `%d`", event.Container.Name, "Stopped", event.ExitCode)
default:
title = fmt.Sprintf("Container %s is %s", event.Container.Name, event.Container.Health)
title = fmt.Sprintf("%s is %s", event.Container.Name, event.Container.Health)
}

idFieldName := "ID:"
Expand All @@ -103,20 +118,14 @@ func makeWebhookMessage(event ContainerEvent) discordwebhook.Message {
fields = append(fields, discordwebhook.Field{Name: &errorFieldName, Value: &event.Error})
}

authorName := "Docker Container Status Update"
authorName := "Docker Container Status Update" + getHostnameAdditionText()
iconUrl := "https://www.docker.com/wp-content/uploads/2023/04/cropped-Docker-favicon-32x32.png"

author := discordwebhook.Author{
Name: &authorName,
IconUrl: &iconUrl,
}

footerText := "Sent by Heimdall"

footer := discordwebhook.Footer{
Text: &footerText,
}

var embedColor = "0"
switch event.Container.Health {
case Healthy:
Expand All @@ -142,7 +151,7 @@ func makeWebhookMessage(event ContainerEvent) discordwebhook.Message {

return discordwebhook.Message{
Embeds: &embeds,
Username: &BotName,
AvatarUrl: &AvatarUrl,
Username: &botName,
AvatarUrl: &avatarUrl,
}
}
6 changes: 6 additions & 0 deletions pkg/heimdall/heimdall.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ import (
)

var DebugMode bool

var PeriodicNotifications bool
var PeriodicNotificationInterval int
var AllContainers bool

var Retry int

var Provider IProvider

var Hostname string

var IsHealthy = false

func init() {
Expand All @@ -30,6 +35,7 @@ func init() {
fs.IntVar(&PeriodicNotificationInterval, 'i', "notification-interval", 60, "Interval in minutes between periodic notifications")
fs.BoolVar(&AllContainers, 'a', "all-containers", "Enable periodic notifications for all containers, including stopped ones")
fs.IntVar(&Retry, 'r', "retry", 10, "Retry in seconds when the docker event stream ends")
fs.StringVar(&Hostname, 'h', "hostname", "", "Hostname to use in notifications. Useful when running multiple instances of Heimdall")
var provider = fs.StringEnum('p', "provider", "Provider to use for notifications", "discord")
var webhookUrl = fs.String('w', "webhook-url", "", "Webhook URL to use for notifications")

Expand Down
Binary file modified public/container-list-all.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/container-list.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/notifications-with-hostname.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f580eac

Please sign in to comment.