forked from engineering/cloudservices
109 lines
6.8 KiB
TypeScript
109 lines
6.8 KiB
TypeScript
/* eslint-disable no-useless-escape */
|
|
/* eslint-disable no-continue */
|
|
/* eslint-disable no-await-in-loop */
|
|
import { Client, RichEmbed } from '../class';
|
|
import { Tiers } from '../models';
|
|
|
|
const channelID = '691824484230889546';
|
|
|
|
export default function memory(client: Client) {
|
|
const set = new Set<string>();
|
|
setInterval(async () => {
|
|
try {
|
|
const accounts = await client.db.Account.find();
|
|
for (const acc of accounts) {
|
|
if (acc.root === true) continue;
|
|
// memory in bytes
|
|
const mem = Number(await client.util.exec(`memory ${acc.username}`)) * 1000;
|
|
// memory in megabytes
|
|
const memoryConversion = mem / 1024 / 1024;
|
|
const userLimits: { soft?: number, hard?: number } = {};
|
|
// @ts-ignore
|
|
const tier: Tiers = await client.db.Tier.findOne({ id: acc.tier }).lean().exec();
|
|
userLimits.soft = acc.ramLimitNotification;
|
|
userLimits.hard = tier.resourceLimits.ram;
|
|
if ((memoryConversion <= userLimits.soft) && (acc.ramLimitNotification !== 0)) {
|
|
set.delete(acc.username);
|
|
}
|
|
/* if the user has exceeded their soft memory limit, which is the one described in the
|
|
resource limit guidelines, we'll inform staff.
|
|
*/
|
|
if ((memoryConversion >= userLimits.hard) && set.has(acc.username)) {
|
|
client.signale.info(`RAM Hard Limit Reached | ${acc.username} | ${memoryConversion}/${userLimits.hard} MB`);
|
|
await client.util.sendMessageToUserTerminal(acc.username, 'REACHED RAM LIMIT; SENDING KILL SIGNAL');
|
|
client.util.exec(`killall -9 -u ${acc.username}`);
|
|
const embed = new RichEmbed();
|
|
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.');
|
|
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.hard)} MB`, true);
|
|
client.createMessage(channelID, { embed });
|
|
client.util.createModerationLog(acc.userID, client.guilds.get('446067825673633794').members.get(client.user.id), 1, `You have exceeded your resource limit of '${String(userLimits.hard)} MB'. Any process running on your user account has been sent a STOP/KILL signal. If you have any questions, please contact a Technician.`);
|
|
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 Technician. Please get the underlying issue resolved to avoid <i>possible</i> moderative action.</p>
|
|
<p><strong>Reason:</strong> You have exceeded your resource limit of '${String(userLimits.hard)} MB'. Any process running on your user account has been sent a STOP/KILL signal. If you have any questions, please contact a Technician.</p>
|
|
<p><strong>Moderator:</strong> SYSTEM</p>
|
|
|
|
<b><i>Library of Code sp-us | Support Team</i></b>
|
|
`,
|
|
});
|
|
client.createMessage(channelID, { embed });
|
|
set.delete(acc.username);
|
|
} else if ((memoryConversion >= userLimits.soft) && !set.has(acc.username)) {
|
|
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.setTitle('Resource Limit Notification');
|
|
embed.setDescription('Someone has reached the (soft) resource limit for their tier on RAM.');
|
|
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.hard)} MB`, true);
|
|
embed.setFooter(client.user.username, client.user.avatarURL);
|
|
embed.setTimestamp();
|
|
if (acc.ramLimitNotification !== 0) {
|
|
await client.createMessage(channelID, { embed });
|
|
}
|
|
if ((memoryConversion >= acc.ramLimitNotification) && (acc.ramLimitNotification !== 0)) {
|
|
const notifyEmbed = new RichEmbed()
|
|
.setTitle('Cloud Account | Notification')
|
|
.setDescription(`You are about to reach your RAM resource limits, you are currently using '${String(Math.round(memoryConversion))} MB' and your limit is '${String(userLimits.hard)} MB'. Please correct your usage to avoid further action.`)
|
|
.addField('User', `${acc.username} | <@${acc.userID}>`, true)
|
|
.addField('Technician', 'SYSTEM', true)
|
|
.addField('Additional Information', 'This notification was sent by the system. You can set your notification preferences by running \`=limits set-ram-notification <preferred ram threshold in MB>\`, you can disable these notifications by running \`=limits set-ram-notification -1\`.')
|
|
.setFooter(client.user.username, client.user.avatarURL)
|
|
.setTimestamp();
|
|
client.getDMChannel(acc.userID).then((channel) => {
|
|
channel.createMessage({ embed: notifyEmbed });
|
|
});
|
|
await client.util.sendMessageToUserTerminal(acc.username, `You are about to reach your RAM resource limits, you are currently using '${String(Math.round(memoryConversion))} MB' and your limit is '${String(userLimits.hard)} MB'. Please correct your usage to avoid further action.`).catch(() => { });
|
|
client.util.transport.sendMail({
|
|
to: acc.emailAddress,
|
|
from: 'Library of Code sp-us | Cloud Services <help@libraryofcode.org>',
|
|
subject: 'Account Notification',
|
|
html: `
|
|
<h1>Library of Code sp-us | Cloud Services</h1>
|
|
<p>You are about to reach your RAM resource limits, you are currently using '${String(memoryConversion)} MB' and your limit is '${String(userLimits.hard)} MB'. Please correct your usage to avoid further action.</p>
|
|
<p><strong>Technician:</strong> SYSTEM</p>
|
|
|
|
<b><i>Library of Code sp-us | Support Team</i></b>
|
|
`,
|
|
});
|
|
client.createMessage('580950455581147146', { embed: notifyEmbed });
|
|
}
|
|
set.add(acc.username);
|
|
}
|
|
}
|
|
} catch (err) {
|
|
client.util.handleError(err);
|
|
}
|
|
}, 60000);
|
|
}
|