Skip to content

Commit

Permalink
Merge pull request #86 from petersem:NextVersion
Browse files Browse the repository at this point in the history
NextVersion
  • Loading branch information
petersem authored May 23, 2024
2 parents 2886efc + f7c3b8f commit 516d948
Show file tree
Hide file tree
Showing 12 changed files with 394 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ignore the following folders
# ignore the following folders
node_modules/*
.env
# any excluded dir with a .keep file. Will stay in github, but all other files will not :)
Expand Down
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Monocker
Monitors Docker (MONitors dOCKER) containers and alerts on 'state' change.

![Telegram Alerts](https://raw.githubusercontent.com/petersem/monocker/master/doco/telegram.PNG)
![Telegram Alerts](https://raw.githubusercontent.com/petersem/monocker/master/doco/title.PNG)

## Features
- Monitors 'state' changes for all containers (every 10 seconds)
Expand All @@ -22,8 +22,6 @@ Monitors Docker (MONitors dOCKER) containers and alerts on 'state' change.

## Installation
```ya
version: '2.4'
services:
monocker:
container_name: monocker
Expand All @@ -35,22 +33,24 @@ services:
SERVER_LABEL: 'Your server name'
# Optional avatar image URL to add to messages. Handy if you are running Monocker on different machines (discord, ntfy, and slack)
SERVER_AVATAR: 'https://content.invisioncic.com/u329766/monthly_2024_05/monocker.png.ba5ffdb390b627097d2a53645cf87350.png'
# Specify the messaging platform and details, or leave blank if only wanting container logs (pick one only)
MESSAGE_PLATFORM: 'telegram@your_bot_id@your_chat_id'
# MESSAGE_PLATFORM: 'gotify@app_token'
# Specify 'ONE' messaging platform, or leave blank if only wanting container logs
# MESSAGE_PLATFORM: 'telegram@your_bot_id@your_chat_id'
MESSAGE_PLATFORM: 'gotify@server@app_token'
# MESSAGE_PLATFORM: 'pushbullet@your_api_key@your_device_id'
# MESSAGE_PLATFORM: 'pushover@your_user_key@your_app_api_token'
# MESSAGE_PLATFORM: 'discord@webhook_url'
# MESSAGE_PLATFORM: 'ntfy@topic_title'
# MESSAGE_PLATFORM: 'slack@bot_user_oauth_token@your_chat_id'
# For Matrix, add the userid 'without' the leading @ sign. Values are server, userid, access-token, room-id
# MESSAGE_PLATFORM = matrix@https://matrix.org@user:matrix.org@access-token@room-id:matrix.org
# MESSAGE_PLATFORM: ''
# Optional - includes or excludes specified containers - default behaviour is false
LABEL_ENABLE: 'false'
# Optional - only show when container state changes to being offline (paused, exited, running (unhealthy), or dead) - default is false
ONLY_OFFLINE_STATES: 'false'
# [Optional] - Regardless of any other settings, you can ignore or include 'exited'
EXCLUDE_EXITED: 'false'
# [Optional] - Set the poll period in seconds. Defaults to 10 seconds, which is also the minimum.
# [Optional] - Set the poll period in seconds. Defaults to 10 seconds, which is also the minimum. (recommended 30, to reduce number of messages sent)
PERIOD: 30
# [Optional] - Supress startup messages from being sent. Default is false
DISABLE_STARTUP_MSG: 'false'
Expand All @@ -64,13 +64,14 @@ services:
- /var/run/docker.sock:/var/run/docker.sock:ro
restart: unless-stopped
```
- For Telegram: See documentation for how to obtain ID values
- For Telegram: See [video](doco/telegram_chatid.mkv) and use this link for how to obtain ID values. https://api.telegram.org/bot%7Bbot-id%7D/getUpdates
- For Pushbullet: Open Pushbullet in a browser and get device ID from URL [Example](https://raw.githubusercontent.com/petersem/monocker/master/doco/pbdeviceid.PNG)
- For Pushover: See pushover doco for user key and app token
- For Discord: See Discord doco for how to create a webhook and get the url
- For Slack: See [documentation](doco/slack.md) for how to obtain ID values.
- For Ntfy: create a new topic on https://ntfy.sh/app, use the name of the topic as follows: ntfy@MY_TOPIC_TITLE

- For Matrix, review these images for how to get [userID](/doc/matrix-user-id.png), [roomID](/doc/matrix-room-id.png), and [Access token](/doc/matrix-access-token.png)

If you would like to use your own ntfy server you can add the environment variable `CUSTOM_NTFY_SERVER`

If you would like to use a username and password (either on ntfy.sh or on your own server), uncomment the variables `NTFY_USER` and `NTFY_PASS`
Expand Down
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ services:
# MESSAGE_PLATFORM: 'pushbullet@your_api_key@your_device_id'
# MESSAGE_PLATFORM: 'pushover@your_user_key@your_app_api_token'
MESSAGE_PLATFORM: 'discord@webhook_url'
# MESSAGE_PLATFORM: 'gotify@app_token'
# MESSAGE_PLATFORM: 'gotify@server@app_token'
# MESSAGE_PLATFORM: 'ntfy@topic_title'
# MESSAGE_PLATFORM: 'slack@bot_user_oauth_token@your_chat_id'
# For Matrix, add the userid 'without' the leading @ sign. Values are server, userid, access-token, room-id
# MESSAGE_PLATFORM = matrix@https://matrix.org@user:matrix.org@access-token@room-id:matrix.org
# MESSAGE_PLATFORM: ''

# Optional - includes or excludes specified containers - default behaviour is false
Expand Down
Binary file added doco/matrix-access-token.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 doco/matrix-room-id.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 doco/matrix-user-id.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 doco/telegram_chatid_botid.mkv
Binary file not shown.
Binary file added doco/title.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
104 changes: 76 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Webhook } from 'discord-webhook-node';
import { NtfyClient } from 'ntfy';
import { WebClient } from '@slack/web-api';
import { gotify } from 'gotify';
import Matrix from "matrix-js-sdk";

process.on('warning', (warning) => {
console.log(warning.stack);
Expand Down Expand Up @@ -71,47 +72,91 @@ console.log(`Monitoring started
console.log()

async function sendTelegram(message) {
let notify = new Telegram({ token: msgDetails[1], chatId: msgDetails[2] });
await notify.send(message, { timeout: 10000 }, { parse_mode: "html" });
try {
let notify = new Telegram({ token: msgDetails[1], chatId: msgDetails[2] });
await notify.send(message, { timeout: 10000 }, { parse_mode: "html" });
} catch (e) {
console.error("** Telegram Exception: " + e.message);
}
}

async function sendPushbullet(title, message) {
var pusher = new PushBullet(msgDetails[1]);
pusher.note(msgDetails[2], title, message, function (err, res) {
if (err) return console.log(err.message);
console.error(res.message);
});
try {
var pusher = new PushBullet(msgDetails[1]);
pusher.note(msgDetails[2], title, message, function (err, res) {
if (err) return console.log(err.message);
console.error(res.message);
});
} catch (e) {
console.error("** Pushbullet Exception: " + e.message);
}
}

async function sendGotify(title, message) {
await gotify({
server: msgDetails[1],
app: msgDetails[2],
title: title,
message: message,
priority: 5
});
// try {
console.log(msgDetails[1]);
console.log(msgDetails[2]);

await gotify({
server: msgDetails[1],
app: msgDetails[2],
title: title,
message: message,
priority: 5
});
// } catch (e) {
// console.error("** Gotify Exception: " + e.message);
// }
}

async function sendPushover(title, message) {
var push = new Pushover({
token: msgDetails[2],
user: msgDetails[1],
});
push.send(title, message, function (err, res) {
if (err) return console.log(err);
console.error(res);
});
try {
var push = new Pushover({
token: msgDetails[2],
user: msgDetails[1],
});
push.send(title, message, function (err, res) {
if (err) return console.log(err);
console.error(res);
});
} catch (e) {
console.error("** Pushover Exception: " + e.message);
}
}

async function sendDiscord(title, message) {
const hook = new Webhook(msgDetails[1]);
hook.setUsername(title);
hook.setAvatar(SERVER_AVATAR);
try {
await hook.send(message);
const hook = new Webhook(msgDetails[1]);
hook.setUsername(title);
hook.setAvatar(SERVER_AVATAR);
try {
await hook.send(message);
} catch (e) {
console.error(e.message);
}
} catch (e) {
console.error(e.message);
console.error("** Discord Exception: " + e.message);
}
}

async function sendMatrix(title, message) {
try {
const matrixClient = Matrix.createClient({
baseUrl: msgDetails[1],
accessToken: msgDetails[3],
userId: "@" + msgDetails[2]
});
//# tag0 server1 user2 access-token3 room-id4

var content = new Object();
content.body = message;
content.msgtype = "m.text";

matrixClient.sendEvent(msgDetails[4], "m.room.message", content, "", (err, res) => {
console.log(err);
});
} catch (e) {
console.error("** Matrix Exception: " + e.message);
}
}

Expand All @@ -130,7 +175,7 @@ async function sendNtfyAuth(title, message) {
iconURL: SERVER_AVATAR
});
} catch (e) {
console.error("** NTFY Exception: " + e.message);
console.error("** NTFY Auth Exception: " + e.message);
}
}

Expand Down Expand Up @@ -190,6 +235,9 @@ async function send(message) {
case "gotify":
sendGotify(title, message);
break;
case "matrix":
sendMatrix(title, message);
break;
case "default":
// do nothing
break;
Expand Down
Loading

0 comments on commit 516d948

Please sign in to comment.