From 80d64295ee44047141e787b1f2c326d8ec2988dd Mon Sep 17 00:00:00 2001 From: Matthew R Date: Sun, 27 Oct 2019 20:05:20 -0400 Subject: [PATCH] add sysinfo cmd and dataConversion func --- src/commands/sysinfo.ts | 34 +++++++++++++++++++++++++++++++++ src/functions/dataConversion.ts | 5 +++++ 2 files changed, 39 insertions(+) create mode 100644 src/commands/sysinfo.ts create mode 100644 src/functions/dataConversion.ts diff --git a/src/commands/sysinfo.ts b/src/commands/sysinfo.ts new file mode 100644 index 0000000..35185eb --- /dev/null +++ b/src/commands/sysinfo.ts @@ -0,0 +1,34 @@ +import moment from 'moment'; +import { Message } from 'eris'; +import os, { totalmem } from 'os'; +import { Command, RichEmbed } from '../class'; +import { dataConversion } from '../functions'; +import { Client } from '..'; + +export default class SysInfo extends Command { + constructor(client: Client) { + super(client); + this.name = 'sysinfo'; + this.description = 'Provides system information.'; + this.enabled = true; + } + + public async run(message: Message) { + const availableMemory: string = await this.client.util.exec('free -b'); + const usedMemory = dataConversion(totalmem() - Number(availableMemory.split('\n')[1].split(' ').slice(-1)[0])); + const date = new Date(); + date.setMilliseconds(-(moment.duration(os.uptime(), 's').asMilliseconds())); + + const embed = new RichEmbed(); + embed.setTitle('System Information & Statistics'); + embed.addField('Hostname', os.hostname(), true); + embed.addField('Uptime', `${moment.duration(os.uptime(), 's').humanize()} | Last restart was on ${moment(date).format('dddd, MMMM Do YYYY, h:mm:ss A')} EST`, true); + embed.addField('CPU', `${os.cpus()[0].model} ${os.cpus()[0].speed / 1000}GHz | ${os.cpus().length} Cores | ${os.arch()}`, true); + embed.addField('Load Average (last 15 minutes)', os.loadavg()[2].toFixed(3), true); + embed.addField('Memory/RAM', `${usedMemory} / ${dataConversion(totalmem())}`, true); + embed.addField('Network Interfaces (IPv4)', os.networkInterfaces().eth0.filter((r) => r.family === 'IPv4')[0].address, true); + embed.addField('Network Interfaces (IPv6)', os.networkInterfaces().eth0.filter((r) => r.family === 'IPv6')[0].address, true); + // @ts-ignore + message.channel.createMessage({ embed }); + } +} diff --git a/src/functions/dataConversion.ts b/src/functions/dataConversion.ts new file mode 100644 index 0000000..e38337d --- /dev/null +++ b/src/functions/dataConversion.ts @@ -0,0 +1,5 @@ +export default function dataConversion(bytes: number) { + const i = Math.floor(Math.log(bytes) / Math.log(1024)); + const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + return `${(bytes / 1024 ** i).toFixed(2)} ${sizes[i]}`; +}