Skip to content

Commit f2270f4

Browse files
committed
V2.9.2 - Enable docker socket connections, fix period bug, and reduce number of messages sent (grouping)
1 parent bf95c85 commit f2270f4

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

docker-compose.yml

+6-4
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,24 @@ services:
1313
# Optional - regardless of any other settings, you can ignore or include 'exited'
1414
EXCLUDE_EXITED: 'false'
1515
# Optional - only show when container state changes to being offline (paused, exited, running (unhealthy), or dead)
16-
ONLY_OFFLINE_STATES: 'true'
16+
ONLY_OFFLINE_STATES: 'false'
1717
# [Optional] - set the poll period in seconds. Defaults to 10 seconds, which is also the minimum.
18-
PERIOD: 10
18+
PERIOD: 30
1919
# [Optional] - Supress startup messages from being sent. Default is false
2020
DISABLE_STARTUP_MSG: 'false'
2121
# Specify the messaging platform and details, or omit this if only wanting container logs
22-
MESSAGE_PLATFORM: 'telegram@your_bot_id@your_chat_id'
22+
MESSAGE_PLATFORM: 'discord@webhookURL'
2323
# MESSAGE_PLATFORM: 'pushbullet@your_api_key@your_device_id'
2424
# MESSAGE_PLATFORM: 'pushover@your_user_key@your_app_api_token'
2525
# MESSAGE_PLATFORM: 'discord@webhook_url'
2626

27+
# [Optional] - Set this value to a docker socket URL, if you want to connect to docker this way. Leave it blank or omit it otherwise
28+
# DOCKER_HOST: 'tcp://192.168.1.134:2375'
29+
2730
# Filtering monitored containers - Optional - see readme
2831
# If missing or set to false (default), any containers with a "monocker.enable: 'false'" label will be excluded, and all others monitored.
2932
# If set to true, only containers with a "monocker.enable: 'true'" label will be included.
3033
LABEL_ENABLE: 'false'
31-
3234
volumes:
3335
- /var/run/docker.sock:/var/run/docker.sock:ro
3436
restart: unless-stopped

index.js

+30-14
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const server = http.createServer(requestListener);
1717
server.listen(port, host, () => {});
1818

1919
// main program
20-
let docker = new Docker();
20+
let docker = new Docker({socketPath: '/var/run/docker.sock'});
2121
// var docker = new Docker({
2222
// protocol: 'http', //you can enforce a protocol
2323
// host: '192.168.1.135',
@@ -103,7 +103,7 @@ async function send(message) {
103103

104104
async function list(){
105105
let opts;
106-
106+
let messages = "";
107107
if(LABEL_ENABLE=='true'){
108108
opts = {
109109
"filters": '{"label": ["monocker.enable=true"]}'
@@ -123,15 +123,17 @@ async function list(){
123123
if(LABEL_ENABLE=='false' && JSON.stringify(c.Labels).includes('"monocker.enable":"false"')){
124124
if(isFirstRun==true){
125125
console.log(' - Excluding: ' + c.Names[0].replace("/",""));
126-
send('Excluding: ' + c.Names[0].replace("/",""));
126+
//send('Excluding: ' + c.Names[0].replace("/",""));
127+
messages += 'Excluding: ' + c.Names[0].replace("/","") + "\r\n";
127128
}
128129
}
129130
else{
130131
// If label_enable is true, list the specifically included containers
131132
if(LABEL_ENABLE=='true' && JSON.stringify(c.Labels).includes('"monocker.enable":"true"')){
132133
if(isFirstRun==true){
133134
console.log(' - Monitoring: ' + c.Names[0].replace("/",""));
134-
send('Monitoring: ' + c.Names[0].replace("/",""));
135+
//send('Monitoring: ' + c.Names[0].replace("/",""));
136+
messages += 'Monitoring: ' + c.Names[0].replace("/","") + "\r\n";
135137
}
136138
}
137139
// determine if covered by healthcheck
@@ -152,12 +154,15 @@ async function list(){
152154
if(ONLY_OFFLINE_STATES=='true'){
153155
if(offlineStates.includes(c.State) || offlineStates.includes(c.State + " " + hcStatus)){
154156
console.log(" - " + output);
155-
send(output);
157+
//send(output);
158+
messages += output + "\r\n";
156159
}
157160
}
158161
else{
159162
console.log(" - " + output);
160-
send(output);
163+
//send(output);
164+
console.log('*****' + output);
165+
messages += output+ "\r\n";
161166
}
162167
}
163168
}
@@ -168,7 +173,8 @@ async function list(){
168173
if(isFirstRun==true){
169174
console.log(" - Currently monitoring " + newConArray.length + " (running) containers");
170175
if(DISABLE_STARTUP_MSG.toLowerCase()!='true'){
171-
send("Currently monitoring " + newConArray.length + " (running) containers");
176+
//send("Currently monitoring " + newConArray.length + " (running) containers");
177+
messages += "Currently monitoring " + newConArray.length + " (running) containers" + "\r\n";
172178
}
173179
isFirstRun=false;
174180
}
@@ -184,11 +190,19 @@ async function list(){
184190
output += " " + c.ImageID
185191
}
186192
console.log(" - " + output);
187-
send(output)
193+
//send(output)
194+
messages += output + "\r\n";
188195
}
189196
});
190197
}
191198

199+
// do final send of any messages generated
200+
//send(messages.length);
201+
if(messages.length != 0){
202+
send(messages);
203+
}
204+
// let now = new Date();
205+
// send('tick: ' + now.toLocaleString());
192206
// assign new array to be current array state
193207
monContainers = newConArray;
194208
}, Promise.resolve(0));
@@ -203,6 +217,7 @@ async function run(){
203217
runClock = setInterval(run,(PERIOD * 1000));
204218
}
205219

220+
206221
console.log(`Monitoring started
207222
- Messaging platform: ` + MESSAGE_PLATFORM.split("@")[0] + `
208223
- Polling period: ` + PERIOD + ` seconds
@@ -215,12 +230,13 @@ console.log(`Monitoring started
215230
console.log()
216231
if(DISABLE_STARTUP_MSG.toLowerCase()!='true'){
217232
send(`Monitoring started
218-
- Messaging platform: ` + MESSAGE_PLATFORM.split("@")[0] + `
219-
- Only offline state monitoring: ` + ONLY_OFFLINE_STATES + `
220-
- Only include labelled containers: ` + LABEL_ENABLE + `
221-
- Do not monitor 'Exited': ` + EXCLUDE_EXITED + `
222-
- Disable Startup Messages: ` + DISABLE_STARTUP_MSG + `
223-
- Display SHA ID: ` + SHA);
233+
-- Messaging platform: ` + MESSAGE_PLATFORM.split("@")[0] +`
234+
-- Polling period: ` + PERIOD + ` seconds` +`
235+
-- Only offline state monitoring: ` + ONLY_OFFLINE_STATES +`
236+
-- Only include labelled containers: ` + LABEL_ENABLE +`
237+
-- Do not monitor 'Exited': ` + EXCLUDE_EXITED +`
238+
-- Disable Startup Messages: ` + DISABLE_STARTUP_MSG +`
239+
-- Display SHA ID: ` + SHA);
224240
}
225241

226242
// start processing

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "monocker",
3-
"version": "2.9.1",
3+
"version": "2.9.2",
44
"description": "Monitors and alerts for docker containers state changes",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)