-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
246 lines (213 loc) · 6.39 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
"use strict";
import dayjs from 'dayjs';
import * as os from 'os';
import osName from 'os-name';
import getos from 'getos';
import macosRelease from 'macos-release';
import winRelease from 'win-release';
import humem from 'humem';
import internalIp from 'internal-ip';
import publicIp from 'public-ip';
import netmask from 'ipmask';
import osUptime from 'os-uptime';
import ipLocation from 'iplocation';
/**
* @description The main class for the Server Stats npm module
* @export
* @class ServerStats
*/
export class ServerStats {
/**
* Creates an instance of ServerStats.
* @param {String} model
* @memberof ServerStats
*/
constructor(model) {
this.model = model;
}
/**
* @description Gets the model object
* @returns {Object} object
* @memberof ServerStats
*/
get() {
if(this.model === 'basic') {
return this.basicModel();
} else if (this.model === 'advanced') {
return this.advancedModel();
}
}
/**
* @description Gathers data for the basic model
* @returns {Object} object
* @memberof ServerStats
*/
basicModel() {
return {
uptime: this.getUptime(),
currentDate: this.getCurrentDate(),
activeDate: this.getActiveDate(),
time: this.getTime(),
location: this.location
};
}
/**
* @description Gathers data for the advanced model
* @returns {Object} object
* @memberof ServerStats
*/
advancedModel() {
let object = {
os: this.getOS(),
processor: this.parseCPUModel(),
architecture: os.arch(),
totalMem: this.parseTotalMem(),
hostname: os.hostname(),
networkMask: netmask.netmask,
mac: netmask.mac,
localIp: this.localIp,
publicIp: this.publicIp,
};
return Object.assign(object, this.basicModel());
}
/**
* @description Calculates the current uptime, using the difference between the current time and the OS time.
* @returns {Object} Object containing days, hours, minutes
* @memberof ServerStats
*/
getUptime() {
let diffSeconds = dayjs().diff(osUptime, 'seconds'),
calcMinutes = diffSeconds / 60,
calcHours = calcMinutes / 60,
days = Math.floor(calcHours / 24),
hours = Math.floor(calcHours - (days * 24)),
minutes = Math.floor(calcMinutes - (days * 60 * 24) - (hours * 60));
return {
days: this.pad(days),
hours: this.pad(hours),
minutes: this.pad(minutes)
};
}
/**
* @description Returns the current date
* @returns {String} String containing the current date
* @memberof ServerStats
*/
getCurrentDate() {
return dayjs().format('MMMM DD, YYYY');
}
/**
* @description returns the date when the server became active
* @returns {String} String containing the activation date
* @memberof ServerStats
*/
getActiveDate() {
return dayjs(osUptime).format('MMMM DD, YYYY');
}
/**
* @description Returns the current time
* @returns {String} String containing the current time
* @memberof ServerStats
*/
getTime() {
return {
hh: dayjs().format('hh'),
mm: dayjs().format('mm'),
p: dayjs().format('a')
};
}
/**
* @description Retrives the current location after it receives the public IP.
* @param {function} callback
* @param {function} next
* @memberof ServerStats
*/
getLocation(callback, next) {
publicIp.then(ip => {
ipLocation(ip, (error, data) => {
if (callback) {
return callback(data);
}
});
}).catch(next);
}
/**
* @description Returns the OS distribution and release
* @returns {Object} Object containing the OS distribution and release
* @memberof ServerStats
*/
getOS() {
let getPlatform = os.platform(),
tempDist, dist, release;
if (getPlatform === 'linux') {
tempDist = getos((e, os) => { return os.dist; });
release = getos((e, os) => { return os.release; });
} else if (getPlatform === 'darwin') {
release = macosRelease().version;
} else if (getPlatform === 'win32') {
release = winRelease();
}
if (getPlatform === 'darwin' || getPlatform === 'win32') {
tempDist = osName();
}
tempDist = tempDist.split(' ');
dist = tempDist[0];
return { dist, release };
}
/**
* @description Parses the CPU model retrived by the os module
* @returns {String} String containing the frquency and model name
* @memberof ServerStats
*/
parseCPUModel() {
let model = os.cpus()[0].model,
split = model.split('@'),
modelName = split[0].trim(),
frequency = split[1].trim();
let newModelName = modelName.split('-');
modelName = newModelName[0].replace('CPU', '').replace(/\(.*?\)/g, '');
return `${frequency} ${modelName}`;
}
/**
* @description Parses the total memory of the server
* @returns {String} String with the total memory
* @memberof ServerStats
*/
parseTotalMem() {
let humemTotalMem = humem.totalmem,
split = humemTotalMem.split(' '),
round = Math.floor(split[0]),
output = `${round} ${split[1]}`;
return output;
}
/**
* @description Retrives the current server internal IP and external IP.
* @param {function} callback
* @param {function} next
* @memberof ServerStats
*/
getIpObject(callback, next) {
publicIp.then(ip => {
let ipObject = {
localIp: internalIp,
publicIp: ip
};
if (callback) {
return callback(ipObject);
}
}).catch(next);
}
/**
* @description Adds leading zero to number
* @param {number} number
* @returns {number} Number with leading zero
* @memberof ServerStats
*/
pad(number) {
if (number < 10) {
return `0${number}`;
} else {
return number;
}
}
}