-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
234 lines (198 loc) · 8.02 KB
/
app.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* ================================ */
/* DO NOT CHANGE THIS SETTINGS */
/* ================================ */
const settings_version = '2.0.0';
/* ================================ */
/* PACKAGES IMPORT */
/* ================================ */
const cpu = require('node-os-utils').cpu;
const Logger = require("chegs-simple-logger");
const average = require("@extra-array/average");
const PowerShell = require("powershell");
const si = require("systeminformation");
const nconf = require("nconf");
const figlet = require("figlet");
const prompt = require("prompt");
const colors = require("@colors/colors/safe");
/* ================================ */
/* CONFIGURATIONS */
/* ================================ */
nconf.argv()
.env()
.file({file: './config.conf'});
let log = new Logger({
logGeneral: true,
logWarning: true,
logError: true,
logDetail: true,
logDebug: nconf.get('debug') ?? true
});
if (nconf.get('settings_version') !== settings_version) {
console.log(
colors.rainbow(figlet.textSync('Setting up', {
horizontalLayout: 'default',
verticalLayout: 'default',
whitespaceBreak: true
}))
);
const warning_number = 'You must enter numbers only!',
warning_boolean = 'You must enter \'true\', \'t\', \'false\', \'f\' only!';
const schema = {
properties: {
trigger_interval: {
required: true,
default: nconf.get('trigger_interval') ?? 5,
type: 'number',
pattern: /\d+/,
message: warning_number,
description: 'Status Checking Interval (seconds)'
},
average_interval_reset: {
required: true,
default: nconf.get('average_interval_reset') ?? 60,
type: 'number',
pattern: /\d+/,
message: warning_number,
description: 'Average Interval Reset (seconds)'
},
trigger_shutdown_times: {
required: true,
default: nconf.get('trigger_shutdown_times') ?? 60,
type: 'number',
pattern: /\d+/,
message: warning_number,
description: 'Auto-shutdown maximum requirements met (times)'
},
trigger_shutdown_countdown: {
required: true,
default: nconf.get('trigger_shutdown_countdown') ?? 60,
type: 'number',
pattern: /\d+/,
message: warning_number,
description: 'Auto-shutdown countdown (seconds)'
},
trigger_cpu_usage_target: {
required: true,
default: nconf.get('trigger_cpu_usage_target') ?? 15,
type: 'number',
pattern: /\d+/,
message: warning_number,
description: 'Maximum CPU usage percentage (%)'
},
trigger_network_usage_target: {
required: true,
default: nconf.get('trigger_network_usage_target') ?? 100,
type: 'number',
pattern: /\d+/,
message: warning_number,
description: 'Maximum Network usage percentage (Mbps)'
},
debug: {
required: false,
default: nconf.get('debug') ?? true,
type: 'boolean',
message: warning_boolean,
description: 'Debug'
}
}
}
prompt.message = colors.rainbow("Setup");
prompt.delimiter = colors.green(" > ");
prompt.start();
prompt.get(schema, function (err, result) {
nconf.set('settings_version', settings_version);
nconf.set('trigger_interval', result.trigger_interval);
nconf.set('average_interval_reset', result.average_interval_reset);
nconf.set('trigger_shutdown_times', result.trigger_shutdown_times);
nconf.set('trigger_shutdown_countdown', result.trigger_shutdown_countdown);
nconf.set('trigger_cpu_usage_target', result.trigger_cpu_usage_target);
nconf.set('trigger_network_usage_target', result.trigger_network_usage_target);
nconf.set('debug', result.debug);
nconf.save();
log.detail(colors.green('Config saved!'));
log.detail(colors.red('Please restart application again!'));
process.exit();
})
} else {
welcome();
}
/* ================================ */
/* WELCOME, USER! */
/* ================================ */
function welcome() {
console.log(
colors.yellow(figlet.textSync(`Welcome, ${require("os").userInfo().username}!`, {
horizontalLayout: 'default',
verticalLayout: 'default',
whitespaceBreak: true
}))
);
}
/* ================================ */
/* VARIABLES */
/* ================================ */
let cpu_usage = Array(),
network_usage_tx = Array(),
network_usage_rx = Array();
let trigger_shutdown = 0;
let active_trigger = 0;
const measure_system_reset = setInterval(function () {
cpu_usage = Array();
network_usage_rx = Array();
network_usage_tx = Array();
}, nconf.get('average_interval_reset') * 1000 - 500);
const measure_system_data = setInterval(function () {
si.networkStats()
.then(data => {
network_usage_tx.push(data[0].tx_sec);
network_usage_rx.push(data[0].rx_sec);
})
.catch(error => log.error(error));
cpu.usage()
.then(cpuPercentage => {
cpu_usage.push(cpuPercentage);
})
.catch(error => console.error(error));
}, 1000)
const measure_system = setInterval(function () {
const avg_cpu = average(cpu_usage);
let avg_network_tx = average(network_usage_tx) / 125000;
let avg_network_rx = average(network_usage_rx) / 125000;
log.debug(`${colors.blue('CPU')}: ${parseFloat(avg_cpu).toFixed(5)} ${colors.yellow('%')}, ${colors.blue('Network Transmitted')}: ${avg_network_tx.toFixed(5)} ${colors.yellow('Mbps')}, ${colors.blue('Network Received')}: ${avg_network_rx.toFixed(5)} ${colors.yellow('Mbps')}`);
if (avg_cpu < nconf.get('trigger_cpu_usage_target') && avg_network_tx < nconf.get('trigger_network_usage_target') && avg_network_rx < nconf.get('trigger_network_usage_target')) {
trigger_shutdown++;
const shutdown_countdown = nconf.get('trigger_shutdown_times') * nconf.get('trigger_interval') - (nconf.get('trigger_interval') * trigger_shutdown);
active_trigger = 1;
log.warning(`Shutting down in ${colors.bgRed(shutdown_countdown + ' seconds')} due to inactivity or low usage on your computer.`);
} else {
trigger_shutdown = 0;
if (active_trigger === 1) {
log.detail('Your Windows is back to active!');
active_trigger = 0;
}
}
if (trigger_shutdown === nconf.get('trigger_shutdown_times')) {
trigger_shutdown = 0;
log.detail('Attempting to shutdown your computer.')
console.log(
colors.red(figlet.textSync('Goodbye, ' + require("os").userInfo().username, {
horizontalLayout: 'default',
verticalLayout: 'default',
whitespaceBreak: true
}))
);
new PowerShell(
`shutdown -s -t ${nconf.get('trigger_shutdown_countdown')} -c "Your system will be shutdown in ${nconf.get('trigger_shutdown_countdown')} seconds by Inactivity Shutdown Windows (in ${nconf.get('trigger_shutdown_times') * nconf.get('trigger_interval')} seconds)."`,
false,
function () {
log.detail('Success attempted to sending shutdown signal and application will exit in 10 seconds')
}
);
clearInterval(measure_system);
clearInterval(measure_system_data);
clearInterval(measure_system_reset);
setTimeout(function () {
process.exit();
}, 10000);
}
}, nconf.get('trigger_interval') * 1000)