@aerocorp/cli
Version:
AeroCorp CLI 5.1.0 - Future-Proofed Enterprise Infrastructure with Live Preview, Tunneling & Advanced DevOps
223 lines ⢠10.7 kB
JavaScript
;
/**
* AeroCorp CLI 4.0.0 - Monitoring Service
* Advanced monitoring and observability for hybrid infrastructure
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MonitoringService = void 0;
const axios_1 = __importDefault(require("axios"));
const chalk_1 = __importDefault(require("chalk"));
const config_1 = require("./config");
const auth_1 = require("./auth");
class MonitoringService {
constructor() {
this.configService = new config_1.ConfigService();
this.authService = new auth_1.AuthService();
}
async getMetrics() {
try {
const config = await this.configService.getConfig();
const auth = await this.authService.getAuth();
const response = await axios_1.default.get(`${config.apiUrl}/api/monitoring/metrics`, {
headers: {
'Authorization': `Bearer ${auth.token}`,
'Content-Type': 'application/json'
},
timeout: 10000
});
return response.data;
}
catch (error) {
throw new Error(`Failed to get metrics: ${error.message}`);
}
}
async getAlerts(options = {}) {
try {
const config = await this.configService.getConfig();
const auth = await this.authService.getAuth();
const params = new URLSearchParams();
if (options.acknowledged !== undefined) {
params.append('acknowledged', options.acknowledged.toString());
}
if (options.severity) {
params.append('severity', options.severity);
}
const response = await axios_1.default.get(`${config.apiUrl}/api/monitoring/alerts?${params}`, {
headers: {
'Authorization': `Bearer ${auth.token}`,
'Content-Type': 'application/json'
},
timeout: 10000
});
return response.data.alerts;
}
catch (error) {
throw new Error(`Failed to get alerts: ${error.message}`);
}
}
async acknowledgeAlert(alertId) {
try {
const config = await this.configService.getConfig();
const auth = await this.authService.getAuth();
await axios_1.default.post(`${config.apiUrl}/api/monitoring/alerts/${alertId}/acknowledge`, {}, {
headers: {
'Authorization': `Bearer ${auth.token}`,
'Content-Type': 'application/json'
},
timeout: 10000
});
console.log(chalk_1.default.green(`ā Alert ${alertId} acknowledged`));
}
catch (error) {
throw new Error(`Failed to acknowledge alert: ${error.message}`);
}
}
async getLogs(options = {}) {
try {
const config = await this.configService.getConfig();
const auth = await this.authService.getAuth();
const params = new URLSearchParams();
if (options.service)
params.append('service', options.service);
if (options.level)
params.append('level', options.level);
if (options.limit)
params.append('limit', options.limit.toString());
const response = await axios_1.default.get(`${config.apiUrl}/api/monitoring/logs?${params}`, {
headers: {
'Authorization': `Bearer ${auth.token}`,
'Content-Type': 'application/json'
},
timeout: 10000
});
return response.data;
}
catch (error) {
throw new Error(`Failed to get logs: ${error.message}`);
}
}
async getUptime() {
try {
const config = await this.configService.getConfig();
const auth = await this.authService.getAuth();
const response = await axios_1.default.get(`${config.apiUrl}/api/monitoring/uptime`, {
headers: {
'Authorization': `Bearer ${auth.token}`,
'Content-Type': 'application/json'
},
timeout: 10000
});
return response.data;
}
catch (error) {
throw new Error(`Failed to get uptime: ${error.message}`);
}
}
displayMetrics(metrics) {
console.log(chalk_1.default.cyan.bold('\nš System Metrics'));
console.log(chalk_1.default.gray(`Last updated: ${new Date(metrics.timestamp).toLocaleString()}`));
console.log(chalk_1.default.white('\nš„ļø System Resources:'));
console.log(` CPU: ${this.getProgressBar(metrics.cpu)} ${metrics.cpu.toFixed(1)}%`);
console.log(` Memory: ${this.getProgressBar(metrics.memory)} ${metrics.memory.toFixed(1)}%`);
console.log(` Disk: ${this.getProgressBar(metrics.disk)} ${metrics.disk.toFixed(1)}%`);
console.log(` Network: ${this.getProgressBar(metrics.network)} ${metrics.network.toFixed(1)}%`);
console.log(chalk_1.default.white('\nš§ Services Status:'));
metrics.services.forEach(service => {
const statusIcon = service.status === 'healthy' ? 'ā
' :
service.status === 'degraded' ? 'ā ļø' : 'ā';
const statusColor = service.status === 'healthy' ? chalk_1.default.green :
service.status === 'degraded' ? chalk_1.default.yellow : chalk_1.default.red;
console.log(` ${statusIcon} ${chalk_1.default.white(service.name.padEnd(20))} ${statusColor(service.status.padEnd(10))} ${service.responseTime}ms`);
});
if (metrics.alerts.length > 0) {
console.log(chalk_1.default.white('\nšØ Active Alerts:'));
metrics.alerts.forEach(alert => {
const alertIcon = alert.type === 'critical' ? 'š“' :
alert.type === 'warning' ? 'š”' :
alert.type === 'error' ? 'š ' : 'šµ';
console.log(` ${alertIcon} ${chalk_1.default.white(alert.message)}`);
});
}
}
displayAlerts(alerts) {
console.log(chalk_1.default.cyan.bold('\nšØ System Alerts'));
if (alerts.length === 0) {
console.log(chalk_1.default.green('ā
No active alerts'));
return;
}
alerts.forEach(alert => {
const alertIcon = alert.type === 'critical' ? 'š“' :
alert.type === 'warning' ? 'š”' :
alert.type === 'error' ? 'š ' : 'šµ';
const severityColor = alert.severity === 'critical' ? chalk_1.default.red :
alert.severity === 'high' ? chalk_1.default.red :
alert.severity === 'medium' ? chalk_1.default.yellow : chalk_1.default.blue;
console.log(`\n${alertIcon} ${severityColor.bold(alert.type.toUpperCase())} - ${alert.message}`);
if (alert.service) {
console.log(` Service: ${chalk_1.default.cyan(alert.service)}`);
}
console.log(` Time: ${chalk_1.default.gray(new Date(alert.timestamp).toLocaleString())}`);
console.log(` Acknowledged: ${alert.acknowledged ? chalk_1.default.green('Yes') : chalk_1.default.red('No')}`);
});
}
displayLogs(logs) {
console.log(chalk_1.default.cyan.bold('\nš System Logs'));
console.log(chalk_1.default.gray(`Showing ${logs.logs.length} of ${logs.total} logs`));
logs.logs.forEach((log) => {
const levelColor = log.level === 'error' ? chalk_1.default.red :
log.level === 'warn' ? chalk_1.default.yellow :
log.level === 'info' ? chalk_1.default.blue : chalk_1.default.white;
const timestamp = new Date(log.timestamp).toLocaleTimeString();
console.log(`${chalk_1.default.gray(timestamp)} ${levelColor(log.level.toUpperCase().padEnd(5))} ${chalk_1.default.cyan(log.service.padEnd(15))} ${log.message}`);
});
}
displayUptime(uptime) {
console.log(chalk_1.default.cyan.bold('\nā±ļø System Uptime'));
console.log(`Overall: ${chalk_1.default.green(uptime.overall.percentage + '%')} (${uptime.overall.downtime} downtime)`);
console.log(chalk_1.default.white('\nš§ Service Uptime:'));
uptime.services.forEach((service) => {
const statusIcon = service.status === 'operational' ? 'ā
' :
service.status === 'degraded' ? 'ā ļø' : 'ā';
console.log(` ${statusIcon} ${chalk_1.default.white(service.name.padEnd(20))} ${chalk_1.default.green(service.uptime + '%')}`);
});
if (uptime.incidents.length > 0) {
console.log(chalk_1.default.white('\nšØ Active Incidents:'));
uptime.incidents.forEach((incident) => {
console.log(` š“ ${chalk_1.default.white(incident.title)} - ${chalk_1.default.yellow(incident.status)}`);
});
}
}
getProgressBar(percentage, width = 20) {
const filled = Math.round((percentage / 100) * width);
const empty = width - filled;
const color = percentage > 80 ? chalk_1.default.red : percentage > 60 ? chalk_1.default.yellow : chalk_1.default.green;
return color('ā'.repeat(filled)) + chalk_1.default.gray('ā'.repeat(empty));
}
async startRealTimeMonitoring(interval = 5000) {
console.log(chalk_1.default.cyan.bold('š Starting real-time monitoring...'));
console.log(chalk_1.default.gray(`Refresh interval: ${interval / 1000}s`));
console.log(chalk_1.default.gray('Press Ctrl+C to stop\n'));
const monitor = setInterval(async () => {
try {
// Clear screen
process.stdout.write('\x1Bc');
const metrics = await this.getMetrics();
this.displayMetrics(metrics);
}
catch (error) {
console.error(chalk_1.default.red('ā Monitoring error:'), error.message);
}
}, interval);
// Handle graceful shutdown
process.on('SIGINT', () => {
clearInterval(monitor);
console.log(chalk_1.default.yellow('\nā¹ļø Monitoring stopped'));
process.exit(0);
});
}
}
exports.MonitoringService = MonitoringService;
//# sourceMappingURL=monitoring.js.map