Skip to content

Commit 20bc29a

Browse files
committed
Don't error if containers is empty
Signed-off-by: Christian König <ckoenig@posteo.de>
1 parent b5289e0 commit 20bc29a

File tree

1 file changed

+43
-40
lines changed

1 file changed

+43
-40
lines changed

index.js

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -118,58 +118,61 @@ async function list(){
118118
docker.listContainers(opts, function(err, containers) {
119119
// check for changes in status (first run is populating data only)
120120
let newConArray = [];
121-
containers.forEach(c => {
122-
// if label_enable is false then exclude any specifically false labelled containers
123-
if(LABEL_ENABLE=='false' && JSON.stringify(c.Labels).includes('"monocker.enable":"false"')){
124-
if(isFirstRun==true){
125-
console.log(' - Excluding: ' + c.Names[0].replace("/",""));
126-
//send('Excluding: ' + c.Names[0].replace("/",""));
127-
messages += 'Excluding: ' + c.Names[0].replace("/","") + "\r\n";
128-
}
129-
}
130-
else{
131-
// If label_enable is true, list the specifically included containers
132-
if(LABEL_ENABLE=='true' && JSON.stringify(c.Labels).includes('"monocker.enable":"true"')){
133-
if(isFirstRun==true){
134-
console.log(' - Monitoring: ' + c.Names[0].replace("/",""));
135-
//send('Monitoring: ' + c.Names[0].replace("/",""));
136-
messages += 'Monitoring: ' + c.Names[0].replace("/","") + "\r\n";
121+
if (containers > 0) {
122+
containers.forEach(c => {
123+
// if label_enable is false then exclude any specifically false labelled containers
124+
if (LABEL_ENABLE == 'false' && JSON.stringify(c.Labels).includes('"monocker.enable":"false"')) {
125+
if (isFirstRun == true) {
126+
console.log(' - Excluding: ' + c.Names[0].replace("/", ""));
127+
//send('Excluding: ' + c.Names[0].replace("/",""));
128+
messages += 'Excluding: ' + c.Names[0].replace("/", "") + "\r\n";
137129
}
138130
}
139-
// determine if covered by healthcheck
140-
let hcStatus = "";
141-
if(c.Status.includes("(healthy)")) hcStatus="(healthy)"
142-
if(c.Status.includes("(unhealthy)")) hcStatus="(unhealthy)"
143-
if(monContainers.includes(c.Id + "," + c.State + "," + c.Names[0] + "," + hcStatus) == false && monContainers.length !== 0 ){
144-
// exclude exited status if set
145-
if(EXCLUDE_EXITED == 'true' && c.State.toLocaleLowerCase() == 'exited'){
146-
// ignore
131+
else {
132+
// If label_enable is true, list the specifically included containers
133+
if (LABEL_ENABLE == 'true' && JSON.stringify(c.Labels).includes('"monocker.enable":"true"')) {
134+
if (isFirstRun == true) {
135+
console.log(' - Monitoring: ' + c.Names[0].replace("/", ""));
136+
//send('Monitoring: ' + c.Names[0].replace("/",""));
137+
messages += 'Monitoring: ' + c.Names[0].replace("/", "") + "\r\n";
138+
}
147139
}
148-
else{
149-
// if only offline is set, then only show state changes that are offline
150-
var output = c.Names[0].replace("/","") + ": " + c.State + " " + hcStatus;
151-
if(SHA.toLowerCase()=='true'){
152-
output += " " + c.ImageID
140+
// determine if covered by healthcheck
141+
let hcStatus = "";
142+
if (c.Status.includes("(healthy)")) hcStatus = "(healthy)"
143+
if (c.Status.includes("(unhealthy)")) hcStatus = "(unhealthy)"
144+
if (monContainers.includes(c.Id + "," + c.State + "," + c.Names[0] + "," + hcStatus) == false && monContainers.length !== 0) {
145+
// exclude exited status if set
146+
if (EXCLUDE_EXITED == 'true' && c.State.toLocaleLowerCase() == 'exited') {
147+
// ignore
153148
}
154-
if(ONLY_OFFLINE_STATES=='true'){
155-
if(offlineStates.includes(c.State) || offlineStates.includes(c.State + " " + hcStatus)){
149+
else {
150+
// if only offline is set, then only show state changes that are offline
151+
var output = c.Names[0].replace("/", "") + ": " + c.State + " " + hcStatus;
152+
if (SHA.toLowerCase() == 'true') {
153+
output += " " + c.ImageID
154+
}
155+
if (ONLY_OFFLINE_STATES == 'true') {
156+
if (offlineStates.includes(c.State) || offlineStates.includes(c.State + " " + hcStatus)) {
157+
console.log(" - " + output);
158+
//send(output);
159+
messages += output + "\r\n";
160+
}
161+
}
162+
else {
156163
console.log(" - " + output);
157164
//send(output);
165+
console.log('*****' + output);
158166
messages += output + "\r\n";
159167
}
160168
}
161-
else{
162-
console.log(" - " + output);
163-
//send(output);
164-
console.log('*****' + output);
165-
messages += output+ "\r\n";
166-
}
167169
}
170+
// create new container array
171+
newConArray.push(c.Id + "," + c.State + "," + c.Names[0] + "," + hcStatus);
168172
}
169-
// create new container array
170-
newConArray.push(c.Id + "," + c.State + "," + c.Names[0] + "," + hcStatus);
171173
}
172-
});
174+
);
175+
}
173176
if(isFirstRun==true){
174177
console.log(" - Currently monitoring " + newConArray.length + " (running) containers");
175178
if(DISABLE_STARTUP_MSG.toLowerCase()!='true'){

0 commit comments

Comments
 (0)