fix(cli,daemon,snmp): normalize CLI argument parsing and extract daemon monitoring helpers with stronger SNMP typing
This commit is contained in:
+174
-472
@@ -1,8 +1,6 @@
|
||||
import process from 'node:process';
|
||||
import * as fs from 'node:fs';
|
||||
import * as path from 'node:path';
|
||||
import { exec, execFile } from 'node:child_process';
|
||||
import { promisify } from 'node:util';
|
||||
import { NupstSnmp } from './snmp/manager.ts';
|
||||
import type { ISnmpConfig, IUpsStatus as ISnmpUpsStatus } from './snmp/types.ts';
|
||||
import { NupstUpsd } from './upsd/client.ts';
|
||||
@@ -13,12 +11,29 @@ import { logger } from './logger.ts';
|
||||
import { MigrationRunner } from './migrations/index.ts';
|
||||
import { formatPowerStatus, getBatteryColor, getRuntimeColor, theme } from './colors.ts';
|
||||
import type { IActionConfig } from './actions/base-action.ts';
|
||||
import { ActionManager, type IActionContext, type TPowerStatus } from './actions/index.ts';
|
||||
import { ActionManager } from './actions/index.ts';
|
||||
import { decideUpsActionExecution, type TUpsTriggerReason } from './action-orchestration.ts';
|
||||
import { NupstHttpServer } from './http-server.ts';
|
||||
import { NETWORK, PAUSE, THRESHOLDS, TIMING, UI } from './constants.ts';
|
||||
|
||||
const execAsync = promisify(exec);
|
||||
const execFileAsync = promisify(execFile);
|
||||
import {
|
||||
analyzeConfigReload,
|
||||
shouldRefreshPauseState,
|
||||
shouldReloadConfig,
|
||||
} from './config-watch.ts';
|
||||
import { type IPauseState, loadPauseSnapshot } from './pause-state.ts';
|
||||
import { ShutdownExecutor } from './shutdown-executor.ts';
|
||||
import {
|
||||
buildFailedUpsPollSnapshot,
|
||||
buildSuccessfulUpsPollSnapshot,
|
||||
ensureUpsStatus,
|
||||
hasThresholdViolation,
|
||||
} from './ups-monitoring.ts';
|
||||
import {
|
||||
buildShutdownErrorRow,
|
||||
buildShutdownStatusRow,
|
||||
selectEmergencyCandidate,
|
||||
} from './shutdown-monitoring.ts';
|
||||
import { createInitialUpsStatus, type IUpsStatus } from './ups-status.ts';
|
||||
|
||||
/**
|
||||
* UPS configuration interface
|
||||
@@ -70,20 +85,6 @@ export interface IHttpServerConfig {
|
||||
authToken: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pause state interface
|
||||
*/
|
||||
export interface IPauseState {
|
||||
/** Timestamp when pause was activated */
|
||||
pausedAt: number;
|
||||
/** Who initiated the pause (e.g., 'cli', 'api') */
|
||||
pausedBy: string;
|
||||
/** Optional reason for pausing */
|
||||
reason?: string;
|
||||
/** When to auto-resume (null = indefinite, timestamp in ms) */
|
||||
resumeAt?: number | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configuration interface for the daemon
|
||||
*/
|
||||
@@ -113,25 +114,6 @@ export interface INupstConfig {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* UPS status tracking interface
|
||||
*/
|
||||
export interface IUpsStatus {
|
||||
id: string;
|
||||
name: string;
|
||||
powerStatus: 'online' | 'onBattery' | 'unknown' | 'unreachable';
|
||||
batteryCapacity: number;
|
||||
batteryRuntime: number;
|
||||
outputLoad: number; // Load percentage (0-100%)
|
||||
outputPower: number; // Power in watts
|
||||
outputVoltage: number; // Voltage in volts
|
||||
outputCurrent: number; // Current in amps
|
||||
lastStatusChange: number;
|
||||
lastCheckTime: number;
|
||||
consecutiveFailures: number;
|
||||
unreachableSince: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Daemon class for monitoring UPS and handling shutdown
|
||||
* Responsible for loading/saving config and monitoring the UPS status
|
||||
@@ -191,6 +173,7 @@ export class NupstDaemon {
|
||||
private pauseState: IPauseState | null = null;
|
||||
private upsStatus: Map<string, IUpsStatus> = new Map();
|
||||
private httpServer?: NupstHttpServer;
|
||||
private readonly shutdownExecutor: ShutdownExecutor;
|
||||
|
||||
/**
|
||||
* Create a new daemon instance with the given protocol managers
|
||||
@@ -199,6 +182,7 @@ export class NupstDaemon {
|
||||
this.snmp = snmp;
|
||||
this.upsd = upsd;
|
||||
this.protocolResolver = new ProtocolResolver(snmp, upsd);
|
||||
this.shutdownExecutor = new ShutdownExecutor();
|
||||
this.config = this.DEFAULT_CONFIG;
|
||||
}
|
||||
|
||||
@@ -283,7 +267,7 @@ export class NupstDaemon {
|
||||
private logConfigError(message: string): void {
|
||||
logger.logBox(
|
||||
'Configuration Error',
|
||||
[message, "Please run 'nupst setup' first to create a configuration."],
|
||||
[message, "Please run 'nupst ups add' first to create a configuration."],
|
||||
45,
|
||||
'error',
|
||||
);
|
||||
@@ -388,21 +372,7 @@ export class NupstDaemon {
|
||||
|
||||
if (this.config.upsDevices && this.config.upsDevices.length > 0) {
|
||||
for (const ups of this.config.upsDevices) {
|
||||
this.upsStatus.set(ups.id, {
|
||||
id: ups.id,
|
||||
name: ups.name,
|
||||
powerStatus: 'unknown',
|
||||
batteryCapacity: 100,
|
||||
batteryRuntime: 999, // High value as default
|
||||
outputLoad: 0,
|
||||
outputPower: 0,
|
||||
outputVoltage: 0,
|
||||
outputCurrent: 0,
|
||||
lastStatusChange: Date.now(),
|
||||
lastCheckTime: 0,
|
||||
consecutiveFailures: 0,
|
||||
unreachableSince: 0,
|
||||
});
|
||||
this.upsStatus.set(ups.id, createInitialUpsStatus(ups));
|
||||
}
|
||||
|
||||
logger.log(`Initialized status tracking for ${this.config.upsDevices.length} UPS devices`);
|
||||
@@ -507,66 +477,39 @@ export class NupstDaemon {
|
||||
* Check and update pause state from the pause file
|
||||
*/
|
||||
private checkPauseState(): void {
|
||||
try {
|
||||
if (fs.existsSync(PAUSE.FILE_PATH)) {
|
||||
const data = fs.readFileSync(PAUSE.FILE_PATH, 'utf8');
|
||||
const state = JSON.parse(data) as IPauseState;
|
||||
const snapshot = loadPauseSnapshot(PAUSE.FILE_PATH, this.isPaused);
|
||||
|
||||
// Check if auto-resume time has passed
|
||||
if (state.resumeAt && Date.now() >= state.resumeAt) {
|
||||
// Auto-resume: delete the pause file
|
||||
try {
|
||||
fs.unlinkSync(PAUSE.FILE_PATH);
|
||||
} catch (_e) {
|
||||
// Ignore deletion errors
|
||||
}
|
||||
if (this.isPaused) {
|
||||
logger.log('');
|
||||
logger.logBoxTitle('Auto-Resume', 45, 'success');
|
||||
logger.logBoxLine('Pause duration expired, resuming action monitoring');
|
||||
logger.logBoxEnd();
|
||||
logger.log('');
|
||||
}
|
||||
this.isPaused = false;
|
||||
this.pauseState = null;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.isPaused) {
|
||||
logger.log('');
|
||||
logger.logBoxTitle('Actions Paused', 45, 'warning');
|
||||
logger.logBoxLine(`Paused by: ${state.pausedBy}`);
|
||||
if (state.reason) {
|
||||
logger.logBoxLine(`Reason: ${state.reason}`);
|
||||
}
|
||||
if (state.resumeAt) {
|
||||
const remaining = Math.round((state.resumeAt - Date.now()) / 1000);
|
||||
logger.logBoxLine(`Auto-resume in: ${remaining} seconds`);
|
||||
} else {
|
||||
logger.logBoxLine('Duration: Indefinite (run "nupst resume" to resume)');
|
||||
}
|
||||
logger.logBoxEnd();
|
||||
logger.log('');
|
||||
}
|
||||
|
||||
this.isPaused = true;
|
||||
this.pauseState = state;
|
||||
} else {
|
||||
if (this.isPaused) {
|
||||
logger.log('');
|
||||
logger.logBoxTitle('Actions Resumed', 45, 'success');
|
||||
logger.logBoxLine('Action monitoring has been resumed');
|
||||
logger.logBoxEnd();
|
||||
logger.log('');
|
||||
}
|
||||
this.isPaused = false;
|
||||
this.pauseState = null;
|
||||
if (snapshot.transition === 'autoResumed') {
|
||||
logger.log('');
|
||||
logger.logBoxTitle('Auto-Resume', 45, 'success');
|
||||
logger.logBoxLine('Pause duration expired, resuming action monitoring');
|
||||
logger.logBoxEnd();
|
||||
logger.log('');
|
||||
} else if (snapshot.transition === 'paused' && snapshot.pauseState) {
|
||||
logger.log('');
|
||||
logger.logBoxTitle('Actions Paused', 45, 'warning');
|
||||
logger.logBoxLine(`Paused by: ${snapshot.pauseState.pausedBy}`);
|
||||
if (snapshot.pauseState.reason) {
|
||||
logger.logBoxLine(`Reason: ${snapshot.pauseState.reason}`);
|
||||
}
|
||||
} catch (_error) {
|
||||
// If we can't read the pause file, assume not paused
|
||||
this.isPaused = false;
|
||||
this.pauseState = null;
|
||||
if (snapshot.pauseState.resumeAt) {
|
||||
const remaining = Math.round((snapshot.pauseState.resumeAt - Date.now()) / 1000);
|
||||
logger.logBoxLine(`Auto-resume in: ${remaining} seconds`);
|
||||
} else {
|
||||
logger.logBoxLine('Duration: Indefinite (run "nupst resume" to resume)');
|
||||
}
|
||||
logger.logBoxEnd();
|
||||
logger.log('');
|
||||
} else if (snapshot.transition === 'resumed') {
|
||||
logger.log('');
|
||||
logger.logBoxTitle('Actions Resumed', 45, 'success');
|
||||
logger.logBoxLine('Action monitoring has been resumed');
|
||||
logger.logBoxEnd();
|
||||
logger.log('');
|
||||
}
|
||||
|
||||
this.isPaused = snapshot.isPaused;
|
||||
this.pauseState = snapshot.pauseState;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -619,25 +562,8 @@ export class NupstDaemon {
|
||||
private async checkAllUpsDevices(): Promise<void> {
|
||||
for (const ups of this.config.upsDevices) {
|
||||
try {
|
||||
const upsStatus = this.upsStatus.get(ups.id);
|
||||
if (!upsStatus) {
|
||||
// Initialize status for this UPS if not exists
|
||||
this.upsStatus.set(ups.id, {
|
||||
id: ups.id,
|
||||
name: ups.name,
|
||||
powerStatus: 'unknown',
|
||||
batteryCapacity: 100,
|
||||
batteryRuntime: 999,
|
||||
outputLoad: 0,
|
||||
outputPower: 0,
|
||||
outputVoltage: 0,
|
||||
outputCurrent: 0,
|
||||
lastStatusChange: Date.now(),
|
||||
lastCheckTime: 0,
|
||||
consecutiveFailures: 0,
|
||||
unreachableSince: 0,
|
||||
});
|
||||
}
|
||||
const initialStatus = ensureUpsStatus(this.upsStatus.get(ups.id), ups);
|
||||
this.upsStatus.set(ups.id, initialStatus);
|
||||
|
||||
// Check UPS status via configured protocol
|
||||
const protocol = ups.protocol || 'snmp';
|
||||
@@ -646,129 +572,100 @@ export class NupstDaemon {
|
||||
: await this.protocolResolver.getUpsStatus('snmp', ups.snmp);
|
||||
const currentTime = Date.now();
|
||||
|
||||
// Get the current status from the map
|
||||
const currentStatus = this.upsStatus.get(ups.id);
|
||||
const pollSnapshot = buildSuccessfulUpsPollSnapshot(
|
||||
ups,
|
||||
status,
|
||||
currentStatus,
|
||||
currentTime,
|
||||
);
|
||||
|
||||
// Successful query: reset consecutive failures
|
||||
const wasUnreachable = currentStatus?.powerStatus === 'unreachable';
|
||||
|
||||
// Update status with new values
|
||||
const updatedStatus: IUpsStatus = {
|
||||
id: ups.id,
|
||||
name: ups.name,
|
||||
powerStatus: status.powerStatus,
|
||||
batteryCapacity: status.batteryCapacity,
|
||||
batteryRuntime: status.batteryRuntime,
|
||||
outputLoad: status.outputLoad,
|
||||
outputPower: status.outputPower,
|
||||
outputVoltage: status.outputVoltage,
|
||||
outputCurrent: status.outputCurrent,
|
||||
lastCheckTime: currentTime,
|
||||
lastStatusChange: currentStatus?.lastStatusChange || currentTime,
|
||||
consecutiveFailures: 0,
|
||||
unreachableSince: 0,
|
||||
};
|
||||
|
||||
// If UPS was unreachable and is now reachable, log recovery
|
||||
if (wasUnreachable && currentStatus) {
|
||||
const downtime = Math.round((currentTime - currentStatus.unreachableSince) / 1000);
|
||||
if (pollSnapshot.transition === 'recovered' && pollSnapshot.previousStatus) {
|
||||
logger.log('');
|
||||
logger.logBoxTitle(`UPS Recovered: ${ups.name}`, 60, 'success');
|
||||
logger.logBoxLine(`UPS is reachable again after ${downtime} seconds`);
|
||||
logger.logBoxLine(`UPS is reachable again after ${pollSnapshot.downtimeSeconds} seconds`);
|
||||
logger.logBoxLine(`Current Status: ${formatPowerStatus(status.powerStatus)}`);
|
||||
logger.logBoxLine(`Time: ${new Date().toISOString()}`);
|
||||
logger.logBoxEnd();
|
||||
logger.log('');
|
||||
|
||||
updatedStatus.lastStatusChange = currentTime;
|
||||
|
||||
// Trigger power status change action for recovery
|
||||
await this.triggerUpsActions(ups, updatedStatus, currentStatus, 'powerStatusChange');
|
||||
} else if (currentStatus && currentStatus.powerStatus !== status.powerStatus) {
|
||||
// Check if power status changed
|
||||
await this.triggerUpsActions(
|
||||
ups,
|
||||
pollSnapshot.updatedStatus,
|
||||
pollSnapshot.previousStatus,
|
||||
'powerStatusChange',
|
||||
);
|
||||
} else if (pollSnapshot.transition === 'powerStatusChange' && pollSnapshot.previousStatus) {
|
||||
logger.log('');
|
||||
logger.logBoxTitle(`Power Status Change: ${ups.name}`, 60, 'warning');
|
||||
logger.logBoxLine(`Previous: ${formatPowerStatus(currentStatus.powerStatus)}`);
|
||||
logger.logBoxLine(
|
||||
`Previous: ${formatPowerStatus(pollSnapshot.previousStatus.powerStatus)}`,
|
||||
);
|
||||
logger.logBoxLine(`Current: ${formatPowerStatus(status.powerStatus)}`);
|
||||
logger.logBoxLine(`Time: ${new Date().toISOString()}`);
|
||||
logger.logBoxEnd();
|
||||
logger.log('');
|
||||
|
||||
updatedStatus.lastStatusChange = currentTime;
|
||||
|
||||
// Trigger actions for power status change
|
||||
await this.triggerUpsActions(ups, updatedStatus, currentStatus, 'powerStatusChange');
|
||||
await this.triggerUpsActions(
|
||||
ups,
|
||||
pollSnapshot.updatedStatus,
|
||||
pollSnapshot.previousStatus,
|
||||
'powerStatusChange',
|
||||
);
|
||||
}
|
||||
|
||||
// Check if any action's thresholds are exceeded (for threshold violation triggers)
|
||||
// Only check when on battery power
|
||||
if (status.powerStatus === 'onBattery' && ups.actions && ups.actions.length > 0) {
|
||||
let anyThresholdExceeded = false;
|
||||
|
||||
for (const actionConfig of ups.actions) {
|
||||
if (actionConfig.thresholds) {
|
||||
if (
|
||||
status.batteryCapacity < actionConfig.thresholds.battery ||
|
||||
status.batteryRuntime < actionConfig.thresholds.runtime
|
||||
) {
|
||||
anyThresholdExceeded = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Trigger actions with threshold violation reason if any threshold is exceeded
|
||||
// Actions will individually check their own thresholds in shouldExecute()
|
||||
if (anyThresholdExceeded) {
|
||||
await this.triggerUpsActions(ups, updatedStatus, currentStatus, 'thresholdViolation');
|
||||
}
|
||||
if (
|
||||
hasThresholdViolation(
|
||||
status.powerStatus,
|
||||
status.batteryCapacity,
|
||||
status.batteryRuntime,
|
||||
ups.actions,
|
||||
)
|
||||
) {
|
||||
await this.triggerUpsActions(
|
||||
ups,
|
||||
pollSnapshot.updatedStatus,
|
||||
pollSnapshot.previousStatus,
|
||||
'thresholdViolation',
|
||||
);
|
||||
}
|
||||
|
||||
// Update the status in the map
|
||||
this.upsStatus.set(ups.id, updatedStatus);
|
||||
this.upsStatus.set(ups.id, pollSnapshot.updatedStatus);
|
||||
} catch (error) {
|
||||
// Network loss / query failure tracking
|
||||
const currentTime = Date.now();
|
||||
const currentStatus = this.upsStatus.get(ups.id);
|
||||
const failures = Math.min(
|
||||
(currentStatus?.consecutiveFailures || 0) + 1,
|
||||
NETWORK.MAX_CONSECUTIVE_FAILURES,
|
||||
);
|
||||
const failureSnapshot = buildFailedUpsPollSnapshot(ups, currentStatus, currentTime);
|
||||
|
||||
logger.error(
|
||||
`Error checking UPS ${ups.name} (${ups.id}) [failure ${failures}/${NETWORK.CONSECUTIVE_FAILURE_THRESHOLD}]: ${
|
||||
`Error checking UPS ${ups.name} (${ups.id}) [failure ${failureSnapshot.failures}/${NETWORK.CONSECUTIVE_FAILURE_THRESHOLD}]: ${
|
||||
error instanceof Error ? error.message : String(error)
|
||||
}`,
|
||||
);
|
||||
|
||||
// Transition to unreachable after threshold consecutive failures
|
||||
if (
|
||||
failures >= NETWORK.CONSECUTIVE_FAILURE_THRESHOLD &&
|
||||
currentStatus &&
|
||||
currentStatus.powerStatus !== 'unreachable'
|
||||
) {
|
||||
const currentTime = Date.now();
|
||||
const previousStatus = { ...currentStatus };
|
||||
|
||||
currentStatus.powerStatus = 'unreachable';
|
||||
currentStatus.consecutiveFailures = failures;
|
||||
currentStatus.unreachableSince = currentTime;
|
||||
currentStatus.lastStatusChange = currentTime;
|
||||
this.upsStatus.set(ups.id, currentStatus);
|
||||
|
||||
if (failureSnapshot.transition === 'unreachable' && failureSnapshot.previousStatus) {
|
||||
logger.log('');
|
||||
logger.logBoxTitle(`UPS Unreachable: ${ups.name}`, 60, 'error');
|
||||
logger.logBoxLine(`${failures} consecutive communication failures`);
|
||||
logger.logBoxLine(`Last known status: ${formatPowerStatus(previousStatus.powerStatus)}`);
|
||||
logger.logBoxLine(`${failureSnapshot.failures} consecutive communication failures`);
|
||||
logger.logBoxLine(
|
||||
`Last known status: ${formatPowerStatus(failureSnapshot.previousStatus.powerStatus)}`,
|
||||
);
|
||||
logger.logBoxLine(`Time: ${new Date().toISOString()}`);
|
||||
logger.logBoxEnd();
|
||||
logger.log('');
|
||||
|
||||
// Trigger power status change action for unreachable
|
||||
await this.triggerUpsActions(ups, currentStatus, previousStatus, 'powerStatusChange');
|
||||
} else if (currentStatus) {
|
||||
currentStatus.consecutiveFailures = failures;
|
||||
this.upsStatus.set(ups.id, currentStatus);
|
||||
await this.triggerUpsActions(
|
||||
ups,
|
||||
failureSnapshot.updatedStatus,
|
||||
failureSnapshot.previousStatus,
|
||||
'powerStatusChange',
|
||||
);
|
||||
}
|
||||
|
||||
this.upsStatus.set(ups.id, failureSnapshot.updatedStatus);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -781,7 +678,11 @@ export class NupstDaemon {
|
||||
|
||||
logger.log('');
|
||||
const pauseLabel = this.isPaused ? ' [PAUSED]' : '';
|
||||
logger.logBoxTitle(`Periodic Status Update${pauseLabel}`, 70, this.isPaused ? 'warning' : 'info');
|
||||
logger.logBoxTitle(
|
||||
`Periodic Status Update${pauseLabel}`,
|
||||
70,
|
||||
this.isPaused ? 'warning' : 'info',
|
||||
);
|
||||
logger.logBoxLine(`Timestamp: ${timestamp}`);
|
||||
if (this.isPaused && this.pauseState) {
|
||||
logger.logBoxLine(`Actions paused by: ${this.pauseState.pausedBy}`);
|
||||
@@ -822,30 +723,6 @@ export class NupstDaemon {
|
||||
logger.log('');
|
||||
}
|
||||
|
||||
/**
|
||||
* Build action context from UPS state
|
||||
* @param ups UPS configuration
|
||||
* @param status Current UPS status
|
||||
* @param triggerReason Why this action is being triggered
|
||||
* @returns Action context
|
||||
*/
|
||||
private buildActionContext(
|
||||
ups: IUpsConfig,
|
||||
status: IUpsStatus,
|
||||
triggerReason: 'powerStatusChange' | 'thresholdViolation',
|
||||
): IActionContext {
|
||||
return {
|
||||
upsId: ups.id,
|
||||
upsName: ups.name,
|
||||
powerStatus: status.powerStatus as TPowerStatus,
|
||||
batteryCapacity: status.batteryCapacity,
|
||||
batteryRuntime: status.batteryRuntime,
|
||||
previousPowerStatus: 'unknown' as TPowerStatus, // Will be set from map in calling code
|
||||
timestamp: Date.now(),
|
||||
triggerReason,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger actions for a UPS device
|
||||
* @param ups UPS configuration
|
||||
@@ -857,35 +734,31 @@ export class NupstDaemon {
|
||||
ups: IUpsConfig,
|
||||
status: IUpsStatus,
|
||||
previousStatus: IUpsStatus | undefined,
|
||||
triggerReason: 'powerStatusChange' | 'thresholdViolation',
|
||||
triggerReason: TUpsTriggerReason,
|
||||
): Promise<void> {
|
||||
// Check if actions are paused
|
||||
if (this.isPaused) {
|
||||
logger.info(
|
||||
`[PAUSED] Actions suppressed for UPS ${ups.name} (trigger: ${triggerReason})`,
|
||||
);
|
||||
const decision = decideUpsActionExecution(
|
||||
this.isPaused,
|
||||
ups,
|
||||
status,
|
||||
previousStatus,
|
||||
triggerReason,
|
||||
);
|
||||
|
||||
if (decision.type === 'suppressed') {
|
||||
logger.info(decision.message);
|
||||
return;
|
||||
}
|
||||
|
||||
const actions = ups.actions || [];
|
||||
|
||||
// Backward compatibility: if no actions configured, use default shutdown behavior
|
||||
if (actions.length === 0 && triggerReason === 'thresholdViolation') {
|
||||
// Fall back to old shutdown logic for backward compatibility
|
||||
await this.initiateShutdown(`UPS "${ups.name}" battery or runtime below threshold`);
|
||||
if (decision.type === 'legacyShutdown') {
|
||||
await this.initiateShutdown(decision.reason);
|
||||
return;
|
||||
}
|
||||
|
||||
if (actions.length === 0) {
|
||||
return; // No actions to execute
|
||||
if (decision.type === 'skip') {
|
||||
return;
|
||||
}
|
||||
|
||||
// Build action context
|
||||
const context = this.buildActionContext(ups, status, triggerReason);
|
||||
context.previousPowerStatus = (previousStatus?.powerStatus || 'unknown') as TPowerStatus;
|
||||
|
||||
// Execute actions
|
||||
await ActionManager.executeActions(actions, context);
|
||||
await ActionManager.executeActions(decision.actions, decision.context);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -899,56 +772,8 @@ export class NupstDaemon {
|
||||
const shutdownDelayMinutes = 5;
|
||||
|
||||
try {
|
||||
// Find shutdown command in common system paths
|
||||
const shutdownPaths = [
|
||||
'/sbin/shutdown',
|
||||
'/usr/sbin/shutdown',
|
||||
'/bin/shutdown',
|
||||
'/usr/bin/shutdown',
|
||||
];
|
||||
|
||||
let shutdownCmd = '';
|
||||
for (const path of shutdownPaths) {
|
||||
try {
|
||||
if (fs.existsSync(path)) {
|
||||
shutdownCmd = path;
|
||||
logger.log(`Found shutdown command at: ${shutdownCmd}`);
|
||||
break;
|
||||
}
|
||||
} catch (e) {
|
||||
// Continue checking other paths
|
||||
}
|
||||
}
|
||||
|
||||
if (shutdownCmd) {
|
||||
// Execute shutdown command with delay to allow for VM graceful shutdown
|
||||
logger.log(
|
||||
`Executing: ${shutdownCmd} -h +${shutdownDelayMinutes} "UPS battery critical..."`,
|
||||
);
|
||||
const { stdout } = await execFileAsync(shutdownCmd, [
|
||||
'-h',
|
||||
`+${shutdownDelayMinutes}`,
|
||||
`UPS battery critical, shutting down in ${shutdownDelayMinutes} minutes`,
|
||||
]);
|
||||
logger.log(`Shutdown initiated: ${stdout}`);
|
||||
logger.log(`Allowing ${shutdownDelayMinutes} minutes for VMs to shut down safely`);
|
||||
} else {
|
||||
// Try using the PATH to find shutdown
|
||||
try {
|
||||
logger.log('Shutdown command not found in common paths, trying via PATH...');
|
||||
const { stdout } = await execAsync(
|
||||
`shutdown -h +${shutdownDelayMinutes} "UPS battery critical, shutting down in ${shutdownDelayMinutes} minutes"`,
|
||||
{
|
||||
env: process.env, // Pass the current environment
|
||||
},
|
||||
);
|
||||
logger.log(`Shutdown initiated: ${stdout}`);
|
||||
} catch (e) {
|
||||
throw new Error(
|
||||
`Shutdown command not found: ${e instanceof Error ? e.message : String(e)}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
await this.shutdownExecutor.scheduleShutdown(shutdownDelayMinutes);
|
||||
logger.log(`Allowing ${shutdownDelayMinutes} minutes for VMs to shut down safely`);
|
||||
|
||||
// Monitor UPS during shutdown and force immediate shutdown if battery gets too low
|
||||
logger.log('Monitoring UPS during shutdown process...');
|
||||
@@ -956,51 +781,10 @@ export class NupstDaemon {
|
||||
} catch (error) {
|
||||
logger.error(`Failed to initiate shutdown: ${error}`);
|
||||
|
||||
// Try alternative shutdown methods
|
||||
const alternatives = [
|
||||
{ cmd: 'poweroff', args: ['--force'] },
|
||||
{ cmd: 'halt', args: ['-p'] },
|
||||
{ cmd: 'systemctl', args: ['poweroff'] },
|
||||
{ cmd: 'reboot', args: ['-p'] }, // Some systems allow reboot -p for power off
|
||||
];
|
||||
|
||||
for (const alt of alternatives) {
|
||||
try {
|
||||
// First check if command exists in common system paths
|
||||
const paths = [
|
||||
`/sbin/${alt.cmd}`,
|
||||
`/usr/sbin/${alt.cmd}`,
|
||||
`/bin/${alt.cmd}`,
|
||||
`/usr/bin/${alt.cmd}`,
|
||||
];
|
||||
|
||||
let cmdPath = '';
|
||||
for (const path of paths) {
|
||||
if (fs.existsSync(path)) {
|
||||
cmdPath = path;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (cmdPath) {
|
||||
logger.log(`Trying alternative shutdown method: ${cmdPath} ${alt.args.join(' ')}`);
|
||||
await execFileAsync(cmdPath, alt.args);
|
||||
return; // Exit if successful
|
||||
} else {
|
||||
// Try using PATH environment
|
||||
logger.log(`Trying alternative via PATH: ${alt.cmd} ${alt.args.join(' ')}`);
|
||||
await execAsync(`${alt.cmd} ${alt.args.join(' ')}`, {
|
||||
env: process.env, // Pass the current environment
|
||||
});
|
||||
return; // Exit if successful
|
||||
}
|
||||
} catch (altError) {
|
||||
logger.error(`Alternative method ${alt.cmd} failed: ${altError}`);
|
||||
// Continue to next method
|
||||
}
|
||||
const shutdownTriggered = await this.shutdownExecutor.tryScheduledAlternatives();
|
||||
if (!shutdownTriggered) {
|
||||
logger.error('All shutdown methods failed');
|
||||
}
|
||||
|
||||
logger.error('All shutdown methods failed');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1037,7 +821,6 @@ export class NupstDaemon {
|
||||
];
|
||||
|
||||
const rows: Array<Record<string, string>> = [];
|
||||
let emergencyDetected = false;
|
||||
let emergencyUps: { ups: IUpsConfig; status: ISnmpUpsStatus } | null = null;
|
||||
|
||||
// Check all UPS devices
|
||||
@@ -1047,31 +830,30 @@ export class NupstDaemon {
|
||||
const status = protocol === 'upsd' && ups.upsd
|
||||
? await this.protocolResolver.getUpsStatus('upsd', undefined, ups.upsd)
|
||||
: await this.protocolResolver.getUpsStatus('snmp', ups.snmp);
|
||||
const rowSnapshot = buildShutdownStatusRow(
|
||||
ups.name,
|
||||
status,
|
||||
THRESHOLDS.EMERGENCY_RUNTIME_MINUTES,
|
||||
{
|
||||
battery: (batteryCapacity) =>
|
||||
getBatteryColor(batteryCapacity)(`${batteryCapacity}%`),
|
||||
runtime: (batteryRuntime) =>
|
||||
getRuntimeColor(batteryRuntime)(`${batteryRuntime} min`),
|
||||
ok: theme.success,
|
||||
critical: theme.error,
|
||||
error: theme.error,
|
||||
},
|
||||
);
|
||||
|
||||
const batteryColor = getBatteryColor(status.batteryCapacity);
|
||||
const runtimeColor = getRuntimeColor(status.batteryRuntime);
|
||||
|
||||
const isCritical = status.batteryRuntime < THRESHOLDS.EMERGENCY_RUNTIME_MINUTES;
|
||||
|
||||
rows.push({
|
||||
name: ups.name,
|
||||
battery: batteryColor(status.batteryCapacity + '%'),
|
||||
runtime: runtimeColor(status.batteryRuntime + ' min'),
|
||||
status: isCritical ? theme.error('CRITICAL!') : theme.success('OK'),
|
||||
});
|
||||
|
||||
// If any UPS battery runtime gets critically low, flag for immediate shutdown
|
||||
if (isCritical && !emergencyDetected) {
|
||||
emergencyDetected = true;
|
||||
emergencyUps = { ups, status };
|
||||
}
|
||||
rows.push(rowSnapshot.row);
|
||||
emergencyUps = selectEmergencyCandidate(
|
||||
emergencyUps,
|
||||
ups,
|
||||
status,
|
||||
THRESHOLDS.EMERGENCY_RUNTIME_MINUTES,
|
||||
);
|
||||
} catch (upsError) {
|
||||
rows.push({
|
||||
name: ups.name,
|
||||
battery: theme.error('N/A'),
|
||||
runtime: theme.error('N/A'),
|
||||
status: theme.error('ERROR'),
|
||||
});
|
||||
rows.push(buildShutdownErrorRow(ups.name, theme.error));
|
||||
|
||||
logger.error(
|
||||
`Error checking UPS ${ups.name} during shutdown: ${
|
||||
@@ -1086,7 +868,7 @@ export class NupstDaemon {
|
||||
logger.log('');
|
||||
|
||||
// If emergency detected, trigger immediate shutdown
|
||||
if (emergencyDetected && emergencyUps) {
|
||||
if (emergencyUps) {
|
||||
logger.log('');
|
||||
logger.logBoxTitle('EMERGENCY SHUTDOWN', 60, 'error');
|
||||
logger.logBoxLine(
|
||||
@@ -1124,86 +906,14 @@ export class NupstDaemon {
|
||||
*/
|
||||
private async forceImmediateShutdown(): Promise<void> {
|
||||
try {
|
||||
// Find shutdown command in common system paths
|
||||
const shutdownPaths = [
|
||||
'/sbin/shutdown',
|
||||
'/usr/sbin/shutdown',
|
||||
'/bin/shutdown',
|
||||
'/usr/bin/shutdown',
|
||||
];
|
||||
|
||||
let shutdownCmd = '';
|
||||
for (const path of shutdownPaths) {
|
||||
if (fs.existsSync(path)) {
|
||||
shutdownCmd = path;
|
||||
logger.log(`Found shutdown command at: ${shutdownCmd}`);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (shutdownCmd) {
|
||||
logger.log(`Executing emergency shutdown: ${shutdownCmd} -h now`);
|
||||
await execFileAsync(shutdownCmd, [
|
||||
'-h',
|
||||
'now',
|
||||
'EMERGENCY: UPS battery critically low, shutting down NOW',
|
||||
]);
|
||||
} else {
|
||||
// Try using the PATH to find shutdown
|
||||
logger.log('Shutdown command not found in common paths, trying via PATH...');
|
||||
await execAsync(
|
||||
'shutdown -h now "EMERGENCY: UPS battery critically low, shutting down NOW"',
|
||||
{
|
||||
env: process.env, // Pass the current environment
|
||||
},
|
||||
);
|
||||
}
|
||||
await this.shutdownExecutor.forceImmediateShutdown();
|
||||
} catch (error) {
|
||||
logger.error('Emergency shutdown failed, trying alternative methods...');
|
||||
|
||||
// Try alternative shutdown methods in sequence
|
||||
const alternatives = [
|
||||
{ cmd: 'poweroff', args: ['--force'] },
|
||||
{ cmd: 'halt', args: ['-p'] },
|
||||
{ cmd: 'systemctl', args: ['poweroff'] },
|
||||
];
|
||||
|
||||
for (const alt of alternatives) {
|
||||
try {
|
||||
// Check common paths
|
||||
const paths = [
|
||||
`/sbin/${alt.cmd}`,
|
||||
`/usr/sbin/${alt.cmd}`,
|
||||
`/bin/${alt.cmd}`,
|
||||
`/usr/bin/${alt.cmd}`,
|
||||
];
|
||||
|
||||
let cmdPath = '';
|
||||
for (const path of paths) {
|
||||
if (fs.existsSync(path)) {
|
||||
cmdPath = path;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (cmdPath) {
|
||||
logger.log(`Emergency: using ${cmdPath} ${alt.args.join(' ')}`);
|
||||
await execFileAsync(cmdPath, alt.args);
|
||||
return; // Exit if successful
|
||||
} else {
|
||||
// Try using PATH
|
||||
logger.log(`Emergency: trying ${alt.cmd} via PATH`);
|
||||
await execAsync(`${alt.cmd} ${alt.args.join(' ')}`, {
|
||||
env: process.env,
|
||||
});
|
||||
return; // Exit if successful
|
||||
}
|
||||
} catch (altError) {
|
||||
// Continue to next method
|
||||
}
|
||||
const shutdownTriggered = await this.shutdownExecutor.tryEmergencyAlternatives();
|
||||
if (!shutdownTriggered) {
|
||||
logger.error('All emergency shutdown methods failed');
|
||||
}
|
||||
|
||||
logger.error('All emergency shutdown methods failed');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1276,19 +986,13 @@ export class NupstDaemon {
|
||||
|
||||
for await (const event of watcher) {
|
||||
// Respond to modify events on config file
|
||||
if (
|
||||
event.kind === 'modify' &&
|
||||
event.paths.some((p) => p.includes('config.json'))
|
||||
) {
|
||||
if (shouldReloadConfig(event)) {
|
||||
logger.info('Config file changed, reloading...');
|
||||
await this.reloadConfig();
|
||||
}
|
||||
|
||||
// Detect pause file changes
|
||||
if (
|
||||
(event.kind === 'create' || event.kind === 'modify' || event.kind === 'remove') &&
|
||||
event.paths.some((p) => p.includes('pause'))
|
||||
) {
|
||||
if (shouldRefreshPauseState(event)) {
|
||||
this.checkPauseState();
|
||||
}
|
||||
|
||||
@@ -1322,18 +1026,16 @@ export class NupstDaemon {
|
||||
await this.loadConfig();
|
||||
const newDeviceCount = this.config.upsDevices?.length || 0;
|
||||
|
||||
if (newDeviceCount > 0 && oldDeviceCount === 0) {
|
||||
logger.success(`Configuration reloaded! Found ${newDeviceCount} UPS device(s)`);
|
||||
logger.info('Monitoring will start automatically...');
|
||||
} else if (newDeviceCount !== oldDeviceCount) {
|
||||
logger.success(
|
||||
`Configuration reloaded! UPS devices: ${oldDeviceCount} → ${newDeviceCount}`,
|
||||
);
|
||||
const reloadSnapshot = analyzeConfigReload(oldDeviceCount, newDeviceCount);
|
||||
logger.success(reloadSnapshot.message);
|
||||
|
||||
if (reloadSnapshot.shouldLogMonitoringStart) {
|
||||
logger.info('Monitoring will start automatically...');
|
||||
}
|
||||
|
||||
if (reloadSnapshot.shouldInitializeUpsStatus) {
|
||||
// Reinitialize UPS status tracking
|
||||
this.initializeUpsStatus();
|
||||
} else {
|
||||
logger.success('Configuration reloaded successfully');
|
||||
}
|
||||
} catch (error) {
|
||||
logger.warn(
|
||||
|
||||
Reference in New Issue
Block a user