From 9ebd6cf62c0ea8c39e91c3d9c732568a28143a47 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 12 Aug 2018 12:34:46 +0900 Subject: [PATCH] =?UTF-8?q?CPU=E4=BD=BF=E7=94=A8=E7=8E=87=E3=81=8C?= =?UTF-8?q?=E9=AB=98=E3=81=8F=E3=81=A6=E3=82=82=E5=86=8D=E8=B5=B7=E5=8B=95?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/server/index.ts | 43 +++++++++++++++++++++++++++++-------- src/serifs.ts | 9 ++++++-- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/modules/server/index.ts b/src/modules/server/index.ts index d52d627..3d1427b 100644 --- a/src/modules/server/index.ts +++ b/src/modules/server/index.ts @@ -13,6 +13,12 @@ export default class ServerModule implements IModule { private preventScheduleReboot = false; private rebootTimer: NodeJS.Timer; private rebootTimerSub: NodeJS.Timer; + private recentStat: any[] = []; + + /** + * 1秒後とのログ1分間分 + */ + private statsLogs: any; public install = (ai: 藍) => { this.ai = ai; @@ -34,6 +40,31 @@ export default class ServerModule implements IModule { this.onConnectionMessage(msg); }); + + setInterval(() => { + this.statsLogs.push(this.recentStat); + if (this.statsLogs.length > 60) this.statsLogs.unshift(); + }, 1000); + + setInterval(() => { + this.check(); + }, 1000); + } + + private check = () => { + const average = (arr) => arr.reduce((a, b) => a + b) / arr.length; + + const memPercentages = this.statsLogs.map(s => (s.mem.total - s.mem.used) * 100); + const memPercentage = average(memPercentages); + if (memPercentage >= 90) { + this.scheduleReboot('mem'); + } + + const cpuPercentages = this.statsLogs.map(s => s.cpu_usage * 100); + const cpuPercentage = average(cpuPercentages); + if (cpuPercentage >= 90) { + this.scheduleReboot('cpu'); + } } private onConnectionMessage = (msg: any) => { @@ -50,22 +81,16 @@ export default class ServerModule implements IModule { } private onStats = async (stats: any) => { - const memUsage = Math.round((stats.mem.used / stats.mem.total) * 100); - - console.log(`[SERVER] MEM: ${memUsage}%`); - - if (memUsage >= 90) { - this.scheduleReboot(); - } + this.recentStat = stats; } - private scheduleReboot = () => { + private scheduleReboot = (reason: string) => { if (this.preventScheduleReboot) return; this.preventScheduleReboot = true; this.ai.post({ - text: serifs.REBOOT_SCHEDULED + text: reason == 'cpu' ? serifs.REBOOT_SCHEDULED_CPU : serifs.REBOOT_SCHEDULED_MEM }); this.rebootTimer = setTimeout(() => { diff --git a/src/serifs.ts b/src/serifs.ts index dc3b7bc..bf383f4 100644 --- a/src/serifs.ts +++ b/src/serifs.ts @@ -10,9 +10,14 @@ export default { REVERSI_DECLINE: 'ごめんなさい、今リバーシはするなと言われてます...', /** - * (メモリが足りないので)再起動の予定が出来たとき + * (メモリが足りないので)再起動がスケジュールされたとき */ - REBOOT_SCHEDULED: 'サーバーの空きメモリが少なくなってきたので、1分後にサーバー再起動しますね!', + REBOOT_SCHEDULED_MEM: 'サーバーの空きメモリが少なくなってきたので、1分後にサーバー再起動しますね!', + + /** + * (CPU使用率が高いので)再起動がスケジュールされたとき + */ + REBOOT_SCHEDULED_CPU: 'サーバーの負荷が高いので、1分後にサーバー再起動しますね!', /** * まもなく再起動されるとき