fix memory checking issue

merge-requests/4/head
Matthew 2020-03-29 05:33:43 -04:00
parent 4ca2e9b188
commit 271cfe1959
No known key found for this signature in database
GPG Key ID: 766BE43AE75F7559
1 changed files with 64 additions and 58 deletions

View File

@ -15,66 +15,72 @@ export const memoryLimits = {
export default function memory(client: Client) {
setInterval(async () => {
const accounts = await client.db.Account.find();
console.log(accounts);
for (const acc of accounts) {
console.log(acc);
if (acc.root) return;
// memory in bytes
const mem = Number(await client.util.exec(`memory ${acc.username}`)) * 1000;
// memory in megabytes
const memoryConversion = mem / 1024 / 1024;
let userLimits: { soft: number, hard: number };
if (acc.tier === 1) {
userLimits = { soft: memoryLimits.TIER_1_SOFT, hard: memoryLimits.TIER_1_HARD };
} else if (acc.tier === 2) {
userLimits = { soft: memoryLimits.TIER_2_SOFT, hard: memoryLimits.TIER_2_HARD };
} else if (acc.tier === 3) {
userLimits = { soft: memoryLimits.TIER_3_SOFT, hard: memoryLimits.TIER_3_HARD };
}
/* if the user has exceeded their soft memory limit, which is the one described in the
resource limit guidelines, we'll inform staff.
*/
console.log(memoryConversion, userLimits.soft, userLimits.hard);
if (memoryConversion >= userLimits.soft) {
client.signale.info(`RAM Soft Limit Reached | ${acc.username} | ${memoryConversion}/${userLimits.soft} MB`);
const embed = new RichEmbed();
if (client.users.get(acc.userID)) embed.setThumbnail(client.users.get(acc.userID).avatarURL);
embed.addField('User', `${acc.username} | <@${acc.userID}> | ${acc.userID}`, true);
embed.addField('Tier', String(acc.tier), true);
embed.addField('Memory Usage', `${String(memoryConversion)} MB`, true);
embed.addField('Memory Limit', `${String(userLimits.soft)} MB`, true);
embed.setFooter(client.user.username, client.user.avatarURL);
embed.setTimestamp();
// if they exceed the hard limit, we'll kill all of their processes.
if (memoryConversion >= userLimits.hard) {
client.signale.info(`RAM Hard Limit Reached | ${acc.username} | ${memoryConversion}/${userLimits.hard} MB`);
client.util.exec(`killall -9 -u ${acc.username}`);
embed.setTitle('Resource Enforcement Notification');
embed.setDescription('Someone has reached the (hard) resource limit for their tier on RAM. The system has automatically killed all of their processes.');
client.util.createModerationLog(acc.userID, client.guilds.get('446067825673633794').members.get(client.user.id), 1, '[AUTO] Exceeded resource limit for RAM.');
client.util.transport.sendMail({
to: acc.emailAddress,
from: 'Library of Code sp-us | Cloud Services <help@libraryofcode.org>',
subject: 'Your account has been warned',
html: `
<h1>Library of Code sp-us | Cloud Services</h1>
<p>Your account has received an official warning from a Moderator. Please get the underlying issue resolved to avoid <i>possible</i> moderative action.</p>
<p><strong>Reason:</strong> [AUTO] Exceeded resource limit for RAM.</p>
<p><strong>Moderator:</strong> ${client.user.username}</p>
<b><i>Library of Code sp-us | Support Team</i></b>
`,
});
} else {
embed.setTitle('Resource Limit Notification');
embed.setDescription('Someone has reached the (soft) resource limit for their tier on RAM.');
try {
const accounts = await client.db.Account.find();
console.log(accounts);
for (const acc of accounts) {
console.log(acc);
if (acc.root === true) return;
// memory in bytes
const mem = Number(await client.util.exec(`memory ${acc.username}`)) * 1000;
console.log(mem);
// memory in megabytes
const memoryConversion = mem / 1024 / 1024;
console.log(memoryConversion);
let userLimits: { soft: number, hard: number };
if (acc.tier === 1) {
userLimits = { soft: memoryLimits.TIER_1_SOFT, hard: memoryLimits.TIER_1_HARD };
} else if (acc.tier === 2) {
userLimits = { soft: memoryLimits.TIER_2_SOFT, hard: memoryLimits.TIER_2_HARD };
} else if (acc.tier === 3) {
userLimits = { soft: memoryLimits.TIER_3_SOFT, hard: memoryLimits.TIER_3_HARD };
}
/* if the user has exceeded their soft memory limit, which is the one described in the
resource limit guidelines, we'll inform staff.
*/
console.log(memoryConversion, userLimits.soft, userLimits.hard);
if (memoryConversion >= userLimits.soft) {
client.signale.info(`RAM Soft Limit Reached | ${acc.username} | ${memoryConversion}/${userLimits.soft} MB`);
const embed = new RichEmbed();
if (client.users.get(acc.userID)) embed.setThumbnail(client.users.get(acc.userID).avatarURL);
embed.addField('User', `${acc.username} | <@${acc.userID}> | ${acc.userID}`, true);
embed.addField('Tier', String(acc.tier), true);
embed.addField('Memory Usage', `${String(memoryConversion)} MB`, true);
embed.addField('Memory Limit', `${String(userLimits.soft)} MB`, true);
embed.setFooter(client.user.username, client.user.avatarURL);
embed.setTimestamp();
// if they exceed the hard limit, we'll kill all of their processes.
if (memoryConversion >= userLimits.hard) {
client.signale.info(`RAM Hard Limit Reached | ${acc.username} | ${memoryConversion}/${userLimits.hard} MB`);
client.util.exec(`killall -9 -u ${acc.username}`);
embed.setTitle('Resource Enforcement Notification');
embed.setDescription('Someone has reached the (hard) resource limit for their tier on RAM. The system has automatically killed all of their processes.');
client.util.createModerationLog(acc.userID, client.guilds.get('446067825673633794').members.get(client.user.id), 1, '[AUTO] Exceeded resource limit for RAM.');
client.util.transport.sendMail({
to: acc.emailAddress,
from: 'Library of Code sp-us | Cloud Services <help@libraryofcode.org>',
subject: 'Your account has been warned',
html: `
<h1>Library of Code sp-us | Cloud Services</h1>
<p>Your account has received an official warning from a Moderator. Please get the underlying issue resolved to avoid <i>possible</i> moderative action.</p>
<p><strong>Reason:</strong> [AUTO] Exceeded resource limit for RAM.</p>
<p><strong>Moderator:</strong> ${client.user.username}</p>
<b><i>Library of Code sp-us | Support Team</i></b>
`,
});
} else {
embed.setTitle('Resource Limit Notification');
embed.setDescription('Someone has reached the (soft) resource limit for their tier on RAM.');
}
// @ts-ignore
client.createMessage(channelID, { embed });
}
// @ts-ignore
client.createMessage(channelID, { embed });
}
} catch (err) {
client.util.handleError(err);
}
}, 60000);
}