-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
42 lines (32 loc) · 1.41 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const SLACK_URL = process.env.SLACK_URL;
const HOST_NAME = process.env.HOST;
const DOCKER_LOGO_PATH = process.env.SLACK_USER_ICON || 'https://www.docker.com/sites/default/files/vertical_large.png'
const USER_NAME = process.env.SLACK_USER_NAME || 'Docker Watchdog'
const SLACK_CHANNEL = process.env.SLACK_CHANNEL_NAME
const monitor = require('node-docker-monitor');
const slack = require('slack-notify')(SLACK_URL);
const removeSlashes = name => {
return name.replace(/[\\|\/]+/i, '')
}
const mapContainers = containers => {
return containers.split(/[^\w]+/i).map(container => container.toLowerCase());
}
const SELECTED_CONTAINERS = mapContainers(process.env.WATCHDOG_CONTAINERS || '');
const sendMessage = message => {
return slack.send({icon_url: DOCKER_LOGO_PATH, username: USER_NAME, channel: SLACK_CHANNEL, text: message});
}
const onCallback = (container, docker, messageFormat) {
const name = removeSlashes(container.Names[0].toLowerCase());
if (SELECTED_CONTAINERS.length === 0 || SELECTED_CONTAINERS.indexOf(name)) {
const message = messageFormat.gsub(':name', name).gsub(':host', HOST_NAME);
sendMessage(message)
}
}
monitor({
onContainerUp: function(container, docker) {
onCallback(container, docker, 'Container :name is executing on host :host');
},
onContainerDown: function(container, docker) {
onCallback(container, docker, 'Container :name stopped working on host :host');
}
})