-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·188 lines (164 loc) · 5.75 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
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
#!/usr/bin/env node
const path = require('path');
const arg = require('arg');
const utils = require('./utils');
const DEFAULT_CPU_THRESHOLD = 75;
const DEFAULT_CPU_LIMIT = 0;
const DEFAULT_INTERVAL = 5;
let LATAST_TOP_PROC = null; // TODO: warn only if the same process is abot the line twice
const isMac = process.platform === 'darwin';
const isBSD = process.platform.endsWith('bsd');
const isWindows = process.platform.startsWith('win');
let notifier = null;
if (false && isMac) {
// on mac, we have to do this to change the icon and title in the notification
const NotificationCenter = require('node-notifier').NotificationCenter;
notifier = new NotificationCenter({
withFallback: true, // Use Growl Fallback if <= 10.8
// customPath: path.join(__dirname, "terminal-notifier.app/Contents/MacOS/terminal-notifier") // Relative/Absolute path to binary if you want to use your own fork of terminal-notifier
customPath: path.join(__dirname, "killcommand.app/Contents/MacOS/killcommand") // Relative/Absolute path to binary if you want to use your own fork of terminal-notifier
});
} else {
notifier = require('node-notifier');
}
let waiting = false;
const commands = {
// Types
'--interactive' : Boolean,
'--verbose' : Boolean,
'--cpu-alert' : Number,
'--cpu-limit' : Number,
'--interval' : Number,
'--ignore' : [String],
'--alert-ignored' : Boolean,
'--try-message' : Boolean,
};
commands[`--${utils.identifier}`] = Boolean;
const args = arg(commands);
function log(...data) {
if (args['--verbose']) {
console.log(...data);
}
}
if (args['--try-message']) {
log(`| Will show a test message`);
notifier.notify(
{
id: 111,
title: `Just a test`,
message: `This is the text of the message.`,
sound: true,
wait: true,
timeout: 50000,
type: 'warn',
contentImage: path.join(__dirname, "killcommand-header.png"), //"https://github.com/on2-dev/killcommand/raw/main/killcommand-header.png?raw=true",
icon: path.join(__dirname, "killcommand-header.png"),
open: undefined,
closeLabel: undefined,
dropdownLabel: undefined,
actions: ["Kill it!", "Show mercy", "Ignore it from now on"],
sound: "glass" //Sosumi, Basso, Blow, Bottle, Frog, Funk, Glass, Hero, Morse, Ping, Pop, Purr, Submarine, Tink
},
async function (err, response, metadata) {
log(`| Notification closed, got response`);
if (response === 'activate') {
console.log(metadata.activationValue);
}
}
);
process.exit(0);
}
let ignoredList = args['--ignore'] || [];
const cpuLimit = args['--cpu-limit'] || DEFAULT_CPU_LIMIT;
const cpuThreshold = args['--cpu-alert'] || DEFAULT_CPU_THRESHOLD;
// TODO: add support to memory limit
async function check () {
log('+-- CHECKING --');
if (waiting) {
return;
}
const {pid, usage, name, error} = await utils.getTopProcess() || {};
if (error) {
console.error(error);
return;
}
log(`| Current top proccess is ${name}(${pid}), consumming ${usage}% of CPU`);
if (usage > cpuThreshold) {
log(`| This crosses the threshold limit for alerts (${cpuThreshold})`);
let killOnSight = false;
if (cpuLimit && usage > cpuLimit) {
killOnSight = true;
log(`| This also crosses the upper limit (${cpuThreshold}) and should be automatically killed`);
}
if (isIgnored(name, pid)) {
log(`| Program ${name} is in the ignore list.`);
if (args['--alert-ignored']) {
log(`| But as alert-ignored is true, an alert will be triggered`);
killOnSight = false;
} else {
return;
}
}
if (killOnSight) {
utils.die(pid);
return;
}
if (LATAST_TOP_PROC === pid) {
// it's the second time in [interval] seconds that this same process is
// crossing the alert limit
log("| Showing notification");
waiting = true;
notifier.notify(
{
id: pid,
title: `Should I kill it?`,
message: `${name} (pid ${pid}) is consuming ${usage}% of your CPU`,
sound: true,
wait: true,
timeout: 50000,
type: 'warn',
contentImage: "https://github.com/on2-dev/killcommand/raw/main/killcommand-header.png?raw=true",
icon: path.join(__dirname, "killcommand-header.png"),
open: undefined,
closeLabel: undefined,
dropdownLabel: undefined,
actions: ["Kill it!", "Show mercy", "Ignore it from now on"],
sound: "glass" //Sosumi, Basso, Blow, Bottle, Frog, Funk, Glass, Hero, Morse, Ping, Pop, Purr, Submarine, Tink
},
async function (err, response, metadata) {
log(`| Notification closed, got response: `, response, metadata.activationValue);
waiting = false;
if (response === 'activate') {
if (metadata.activationValue === "Ignore it from now on") {
ignoredList.push(pid);
log(`| Will ignore ${pid} from now on (${name})`)
return;
}
if (metadata.activationValue === "Kill it!") {
utils.die(pid);
return;
}
}
}
);
} else {
LATAST_TOP_PROC = pid;
}
} {
log('| Top process is behaving well');
}
}
function isIgnored (program, pid) {
const found = ignoredList.find(ignored => {
if (pid && ignored === pid) {
return true;
}
const rx = new RegExp(`^${ignored.replace(/\%/g, '(.*)?')}$`, 'i');
if (program.match(rx)) {
return true;
}
return false;
});
return !!found;
}
setInterval(check, (args['--interval'] || DEFAULT_INTERVAL) * 1000);