Compare commits

...

18 Commits

Author SHA1 Message Date
d01e878310 2.6.8 2025-03-26 17:49:50 +00:00
588aeabf4b fix(cli): Improve CLI formatting, refine debug option filtering, and remove unused dgram import in SNMP manager 2025-03-26 17:49:50 +00:00
87005e72f1 2.6.7 2025-03-26 15:56:31 +00:00
f799c2ee66 fix(setup.sh): Clarify net-snmp dependency installation message in setup.sh 2025-03-26 15:56:31 +00:00
1a029ba493 2.6.6 2025-03-26 15:53:38 +00:00
5b756dd223 fix(setup.sh): Improve setup script to detect and execute npm-cli.js directly using the Node.js binary 2025-03-26 15:53:38 +00:00
4cac599a58 2.6.5 2025-03-26 15:49:54 +00:00
be6a7314c3 fix(daemon, setup): Improve shutdown command detection and fallback logic; update setup script to use absolute Node/npm paths 2025-03-26 15:49:54 +00:00
83ba9c2611 2.6.4 2025-03-26 14:09:01 +00:00
22ab472e58 fix(setup): Improve installation process in setup script by cleaning up package files and ensuring a minimal net-snmp dependency installation. 2025-03-26 14:09:01 +00:00
9a77030377 2.6.3 2025-03-26 14:05:44 +00:00
ceff285ff5 fix(setup): Update setup script to install only net-snmp dependency and create a minimal package-lock.json for better dependency control. 2025-03-26 14:05:44 +00:00
d8bfbf0be3 2.6.2 2025-03-26 13:54:49 +00:00
3e6b883b38 fix(setup/readme): Improve force update instructions and dependency installation process in setup.sh and readme.md 2025-03-26 13:54:49 +00:00
47ef918128 2.6.1 2025-03-26 13:51:45 +00:00
5951638967 fix(setup): Update setup.sh to temporarily add vendor Node.js binary to PATH for dependency installation, log Node and npm versions, and restore the original PATH afterwards. 2025-03-26 13:51:45 +00:00
b06e2b2273 2.6.0 2025-03-26 13:49:47 +00:00
cc1cfe894c feat(setup): Add --force update flag to setup script and update installation instructions 2025-03-26 13:49:47 +00:00
9 changed files with 601 additions and 215 deletions

View File

@ -1,5 +1,69 @@
# Changelog
## 2025-03-26 - 2.6.8 - fix(cli)
Improve CLI formatting, refine debug option filtering, and remove unused dgram import in SNMP manager
- Standardize whitespace and formatting in ts/cli.ts for consistency
- Refine argument filtering for debug mode and prompt messages
- Remove unused 'dgram' import from ts/snmp/manager.ts
## 2025-03-26 - 2.6.7 - fix(setup.sh)
Clarify net-snmp dependency installation message in setup.sh
- Updated echo statement to indicate installation of net-snmp along with 2 subdependencies
- Improves clarity on dependency installation during setup
## 2025-03-26 - 2.6.6 - fix(setup.sh)
Improve setup script to detect and execute npm-cli.js directly using the Node.js binary
- Replace use of the npm binary with direct execution of npm-cli.js
- Add fallback logic to locate npm-cli.js when not found at the expected path
- Simplify cleanup by removing unnecessary PATH modifications
## 2025-03-26 - 2.6.5 - fix(daemon, setup)
Improve shutdown command detection and fallback logic; update setup script to use absolute Node/npm paths
- Use execFileAsync to execute shutdown commands reliably
- Add multiple fallback alternatives for shutdown and emergency shutdown handling
- Update setup.sh to log the Node and NPM versions using absolute paths without modifying PATH
## 2025-03-26 - 2.6.4 - fix(setup)
Improve installation process in setup script by cleaning up package files and ensuring a minimal net-snmp dependency installation.
- Remove existing package-lock.json along with node_modules to prevent stale artifacts.
- Back up the original package.json before modifying it.
- Create a minimal package.json with only the net-snmp dependency based on the backed-up version.
- Use a clean install to guarantee that only net-snmp is installed.
- Restore the original package.json if the installation fails.
## 2025-03-26 - 2.6.3 - fix(setup)
Update setup script to install only net-snmp dependency and create a minimal package-lock.json for better dependency control.
- Removed full production dependency install in favor of installing only net-snmp@3.20.0
- Added verification step to confirm net-snmp installation
- Generate a minimal package-lock.json if one does not exist
## 2025-03-26 - 2.6.2 - fix(setup/readme)
Improve force update instructions and dependency installation process in setup.sh and readme.md
- Clarify force update commands with explicit paths in readme.md
- Remove existing node_modules before installing dependencies in setup.sh
- Switch from 'npm ci --only=production' to 'npm install --omit=dev' with updated error instructions
## 2025-03-26 - 2.6.1 - fix(setup)
Update setup.sh to temporarily add vendor Node.js binary to PATH for dependency installation, log Node and npm versions, and restore the original PATH afterwards.
- Temporarily prepend vendor Node.js binary directory to PATH to ensure proper npm execution.
- Log Node.js and npm versions for debugging purposes.
- Restore the original PATH after installing dependencies.
## 2025-03-26 - 2.6.0 - feat(setup)
Add --force update flag to setup script and update installation instructions
- Implemented --force option in setup.sh to force-update Node.js binary and dependencies
- Updated readme.md to document the --force flag and revised update steps
- Modified ts/cli.ts update command to pass the --force flag to setup.sh
## 2025-03-26 - 2.5.2 - fix(installer)
Improve Node.js binary detection, dependency management, and SNMPv3 fallback logic

View File

@ -1,6 +1,6 @@
{
"name": "@serve.zone/nupst",
"version": "2.5.2",
"version": "2.6.8",
"description": "Node.js UPS Shutdown Tool for SNMP-enabled UPS devices",
"main": "dist/index.js",
"bin": {

2
pnpm-lock.yaml generated
View File

@ -9,7 +9,7 @@ importers:
.:
dependencies:
net-snmp:
specifier: ^3.20.0
specifier: 3.20.0
version: 3.20.0
devDependencies:
'@git.zone/tsbuild':

View File

@ -227,8 +227,19 @@ sudo nupst update
This will:
1. Pull the latest changes from the git repository
2. Run the installation scripts
3. Refresh the systemd service configuration
4. Restart the service if it was running
3. Force-update Node.js and all dependencies, even if they already exist
4. Refresh the systemd service configuration
5. Restart the service if it was running
You can also manually run the setup script with the force flag to update Node.js and dependencies without updating the application code:
```bash
# If you're in the nupst directory:
bash ./setup.sh --force
# If you're in another directory, specify the full path:
bash /opt/nupst/setup.sh --force
```
## Security

110
setup.sh
View File

@ -2,6 +2,22 @@
# NUPST Setup Script
# Downloads the appropriate Node.js binary for the current platform
# and installs production dependencies
# Parse command line arguments
FORCE_UPDATE=0
for arg in "$@"; do
case $arg in
--force|-f)
FORCE_UPDATE=1
shift
;;
*)
# Unknown option
;;
esac
done
# Find the directory where this script is located
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
@ -74,8 +90,9 @@ case "$OS" in
esac
# Check if we already have the Node.js binary
if [ -f "$SCRIPT_DIR/vendor/$NODE_DIR/bin/node" ]; then
if [ -f "$SCRIPT_DIR/vendor/$NODE_DIR/bin/node" ] && [ $FORCE_UPDATE -eq 0 ]; then
echo "Node.js binary already exists for $OS-$ARCH. Skipping download."
echo "Use --force or -f to force update Node.js."
else
echo "Downloading Node.js v$NODE_VERSION for $OS-$ARCH..."
@ -222,25 +239,90 @@ echo "dist_ts directory successfully downloaded from npm registry."
# Make launcher script executable
chmod +x "$SCRIPT_DIR/bin/nupst"
# Install production dependencies
echo "Installing production dependencies..."
"$SCRIPT_DIR/vendor/$NODE_DIR/bin/npm" --prefix "$SCRIPT_DIR" ci --only=production --no-audit --no-fund
# Set up Node.js binary path
NODE_BIN_DIR="$SCRIPT_DIR/vendor/$NODE_DIR/bin"
NODE_BIN="$NODE_BIN_DIR/node"
NPM_CLI_JS="$NODE_BIN_DIR/../lib/node_modules/npm/bin/npm-cli.js"
if [ $? -ne 0 ]; then
echo "Warning: Failed to install dependencies with 'npm ci'. Trying 'npm install'..."
"$SCRIPT_DIR/vendor/$NODE_DIR/bin/npm" --prefix "$SCRIPT_DIR" install --only=production --no-audit --no-fund
# Ensure we have executable permissions
chmod +x "$NODE_BIN"
if [ $? -ne 0 ]; then
echo "Error: Failed to install dependencies. NUPST may not function correctly."
echo "You can try to install dependencies manually by running:"
echo "cd $SCRIPT_DIR && npm install --only=production"
# Make sure the npm-cli.js exists
if [ ! -f "$NPM_CLI_JS" ]; then
# Try to find npm-cli.js
NPM_CLI_JS=$(find "$NODE_BIN_DIR/.." -name "npm-cli.js" | head -1)
if [ -z "$NPM_CLI_JS" ]; then
echo "Warning: Could not find npm-cli.js, npm commands may fail"
# Set to a fallback value so code can continue
NPM_CLI_JS="$NODE_BIN_DIR/npm"
else
echo "Dependencies installed successfully with 'npm install'."
echo "Found npm-cli.js at: $NPM_CLI_JS"
fi
else
echo "Dependencies installed successfully with 'npm ci'."
fi
# Display which binaries we're using
echo "Using Node binary: $NODE_BIN"
echo "Using NPM CLI JS: $NPM_CLI_JS"
# Remove existing node_modules directory and package files
echo "Cleaning up existing installation..."
rm -rf "$SCRIPT_DIR/node_modules"
rm -f "$SCRIPT_DIR/package-lock.json"
# Back up existing package.json if it exists
if [ -f "$SCRIPT_DIR/package.json" ]; then
echo "Backing up existing package.json..."
cp "$SCRIPT_DIR/package.json" "$SCRIPT_DIR/package.json.bak"
fi
# Create a clean minimal package.json with ONLY net-snmp dependency
echo "Creating minimal package.json with only net-snmp dependency..."
VERSION=$(grep -o '"version": "[^"]*"' "$SCRIPT_DIR/package.json.bak" | head -1 | cut -d'"' -f4 || echo "2.6.3")
echo '{
"name": "@serve.zone/nupst",
"version": "'$VERSION'",
"description": "Node.js UPS Shutdown Tool for SNMP-enabled UPS devices",
"main": "dist_ts/index.js",
"type": "module",
"bin": {
"nupst": "bin/nupst"
},
"dependencies": {
"net-snmp": "3.20.0"
},
"engines": {
"node": ">=16.0.0"
},
"private": true
}' > "$SCRIPT_DIR/package.json"
# Install ONLY net-snmp
echo "Installing ONLY net-snmp dependency (+ 2 subdependencies)..."
echo "Node version: $("$NODE_BIN" --version)"
echo "Executing NPM directly with Node.js"
# Execute npm-cli.js directly with our Node.js binary
"$NODE_BIN" "$NPM_CLI_JS" --prefix "$SCRIPT_DIR" install --no-audit --no-fund
INSTALL_STATUS=$?
if [ $INSTALL_STATUS -ne 0 ]; then
echo "Error: Failed to install net-snmp dependency. NUPST may not function correctly."
echo "Restoring original package.json..."
mv "$SCRIPT_DIR/package.json.bak" "$SCRIPT_DIR/package.json"
exit 1
else
echo "net-snmp dependency installed successfully."
# Show what's actually installed
echo "Installed modules:"
find "$SCRIPT_DIR/node_modules" -maxdepth 1 -type d | grep -v "^$SCRIPT_DIR/node_modules$" | sort
# Remove backup if successful
rm -f "$SCRIPT_DIR/package.json.bak"
fi
# No temporary files to clean up
echo "NUPST setup completed successfully."
echo "You can now run NUPST using: $SCRIPT_DIR/bin/nupst"
echo "To install NUPST globally, run: sudo ln -s $SCRIPT_DIR/bin/nupst /usr/local/bin/nupst"

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@serve.zone/nupst',
version: '2.5.2',
version: '2.6.8',
description: 'Node.js UPS Shutdown Tool for SNMP-enabled UPS devices'
}

228
ts/cli.ts
View File

@ -46,7 +46,7 @@ export class NupstCli {
private extractDebugOptions(args: string[]): { debugMode: boolean; cleanedArgs: string[] } {
const debugMode = args.includes('--debug') || args.includes('-d');
// Remove debug flags from args
const cleanedArgs = args.filter(arg => arg !== '--debug' && arg !== '-d');
const cleanedArgs = args.filter((arg) => arg !== '--debug' && arg !== '-d');
return { debugMode, cleanedArgs };
}
@ -151,7 +151,7 @@ export class NupstCli {
console.log('Tailing nupst service logs (Ctrl+C to exit)...\n');
const journalctl = spawn('journalctl', ['-u', 'nupst.service', '-n', '50', '-f'], {
stdio: ['ignore', 'inherit', 'inherit']
stdio: ['ignore', 'inherit', 'inherit'],
});
// Forward signals to child process
@ -236,7 +236,7 @@ export class NupstCli {
} catch (error) {
console.error('┌─ Configuration Error ─────────────────────┐');
console.error('│ No configuration found.');
console.error('│ Please run \'nupst setup\' first to create a configuration.');
console.error("│ Please run 'nupst setup' first to create a configuration.");
console.error('└──────────────────────────────────────────┘');
return;
}
@ -306,7 +306,7 @@ export class NupstCli {
// Create a test config with a short timeout
const testConfig = {
...config.snmp,
timeout: Math.min(config.snmp.timeout, 10000) // Use at most 10 seconds for testing
timeout: Math.min(config.snmp.timeout, 10000), // Use at most 10 seconds for testing
};
const status = await this.nupst.getSnmp().getUpsStatus(testConfig);
@ -326,7 +326,7 @@ export class NupstCli {
console.error('┌─ Connection Failed! ───────────────────────┐');
console.error(`│ Error: ${error.message}`);
console.error('└──────────────────────────────────────────┘');
console.log('\nPlease check your settings and run \'nupst setup\' to reconfigure.');
console.log("\nPlease check your settings and run 'nupst setup' to reconfigure.");
}
}
@ -340,20 +340,28 @@ export class NupstCli {
if (status.batteryCapacity < config.thresholds.battery) {
console.log('│ ⚠️ WARNING: Battery capacity below threshold');
console.log(`│ Current: ${status.batteryCapacity}% | Threshold: ${config.thresholds.battery}%`);
console.log(
`│ Current: ${status.batteryCapacity}% | Threshold: ${config.thresholds.battery}%`
);
console.log('│ System would initiate shutdown');
} else {
console.log('│ ✓ Battery capacity above threshold');
console.log(`│ Current: ${status.batteryCapacity}% | Threshold: ${config.thresholds.battery}%`);
console.log(
`│ Current: ${status.batteryCapacity}% | Threshold: ${config.thresholds.battery}%`
);
}
if (status.batteryRuntime < config.thresholds.runtime) {
console.log('│ ⚠️ WARNING: Runtime below threshold');
console.log(`│ Current: ${status.batteryRuntime} min | Threshold: ${config.thresholds.runtime} min`);
console.log(
`│ Current: ${status.batteryRuntime} min | Threshold: ${config.thresholds.runtime} min`
);
console.log('│ System would initiate shutdown');
} else {
console.log('│ ✓ Runtime above threshold');
console.log(`│ Current: ${status.batteryRuntime} min | Threshold: ${config.thresholds.runtime} min`);
console.log(
`│ Current: ${status.batteryRuntime} min | Threshold: ${config.thresholds.runtime} min`
);
}
console.log('└──────────────────────────────────────────┘');
@ -393,7 +401,9 @@ Options:
private async update(): Promise<void> {
try {
// Check if running as root
this.checkRootAccess('This command must be run as root to update NUPST and refresh the systemd service.');
this.checkRootAccess(
'This command must be run as root to update NUPST and refresh the systemd service.'
);
console.log('┌─ NUPST Update Process ──────────────────┐');
console.log('│ Updating NUPST from repository...');
@ -412,25 +422,35 @@ Options:
try {
// 1. Update the repository
console.log('│ Pulling latest changes from git repository...');
execSync(`cd ${installDir} && git fetch origin && git reset --hard origin/main`, { stdio: 'pipe' });
execSync(`cd ${installDir} && git fetch origin && git reset --hard origin/main`, {
stdio: 'pipe',
});
// 2. Run the install.sh script
console.log('│ Running install.sh to update NUPST...');
execSync(`cd ${installDir} && bash ./install.sh`, { stdio: 'pipe' });
// 3. Run the setup.sh script
console.log('│ Running setup.sh to update dependencies...');
execSync(`cd ${installDir} && bash ./setup.sh`, { stdio: 'pipe' });
// 3. Run the setup.sh script with force flag to update Node.js and dependencies
console.log('│ Running setup.sh to update Node.js and dependencies...');
execSync(`cd ${installDir} && bash ./setup.sh --force`, { stdio: 'pipe' });
// 4. Refresh the systemd service
console.log('│ Refreshing systemd service...');
// First check if service exists
const serviceExists = execSync('systemctl list-unit-files | grep nupst.service').toString().includes('nupst.service');
let serviceExists = false;
try {
const output = execSync('systemctl list-unit-files | grep nupst.service').toString();
serviceExists = output.includes('nupst.service');
} catch (error) {
// If grep fails (service not found), serviceExists remains false
serviceExists = false;
}
if (serviceExists) {
// Stop the service if it's running
const isRunning = execSync('systemctl is-active nupst.service || true').toString().trim() === 'active';
const isRunning =
execSync('systemctl is-active nupst.service || true').toString().trim() === 'active';
if (isRunning) {
console.log('│ Stopping nupst service...');
execSync('systemctl stop nupst.service');
@ -474,7 +494,7 @@ Options:
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
output: process.stdout,
});
// Helper function to prompt for input
@ -546,7 +566,10 @@ Options:
* @param prompt Function to prompt for user input
* @returns Updated configuration
*/
private async gatherSnmpSettings(config: any, prompt: (question: string) => Promise<string>): Promise<any> {
private async gatherSnmpSettings(
config: any,
prompt: (question: string) => Promise<string>
): Promise<any> {
// SNMP IP Address
const defaultHost = config.snmp.host;
const host = await prompt(`UPS IP Address [${defaultHost}]: `);
@ -556,7 +579,7 @@ Options:
const defaultPort = config.snmp.port;
const portInput = await prompt(`SNMP Port [${defaultPort}]: `);
const port = parseInt(portInput, 10);
config.snmp.port = (portInput.trim() && !isNaN(port)) ? port : defaultPort;
config.snmp.port = portInput.trim() && !isNaN(port) ? port : defaultPort;
// SNMP Version
const defaultVersion = config.snmp.version;
@ -566,7 +589,10 @@ Options:
console.log(' 3) SNMPv3 (with security features)');
const versionInput = await prompt(`Select SNMP version [${defaultVersion}]: `);
const version = parseInt(versionInput, 10);
config.snmp.version = (versionInput.trim() && (version === 1 || version === 2 || version === 3)) ? version : defaultVersion;
config.snmp.version =
versionInput.trim() && (version === 1 || version === 2 || version === 3)
? version
: defaultVersion;
if (config.snmp.version === 1 || config.snmp.version === 2) {
// SNMP Community String (for v1/v2c)
@ -587,7 +613,10 @@ Options:
* @param prompt Function to prompt for user input
* @returns Updated configuration
*/
private async gatherSnmpV3Settings(config: any, prompt: (question: string) => Promise<string>): Promise<any> {
private async gatherSnmpV3Settings(
config: any,
prompt: (question: string) => Promise<string>
): Promise<any> {
console.log('\nSNMPv3 Security Settings:');
// Security Level
@ -595,9 +624,13 @@ Options:
console.log(' 1) noAuthNoPriv (No Authentication, No Privacy)');
console.log(' 2) authNoPriv (Authentication, No Privacy)');
console.log(' 3) authPriv (Authentication and Privacy)');
const defaultSecLevel = config.snmp.securityLevel ?
(config.snmp.securityLevel === 'noAuthNoPriv' ? 1 :
config.snmp.securityLevel === 'authNoPriv' ? 2 : 3) : 3;
const defaultSecLevel = config.snmp.securityLevel
? config.snmp.securityLevel === 'noAuthNoPriv'
? 1
: config.snmp.securityLevel === 'authNoPriv'
? 2
: 3
: 3;
const secLevelInput = await prompt(`Select Security Level [${defaultSecLevel}]: `);
const secLevel = parseInt(secLevelInput, 10) || defaultSecLevel;
@ -639,7 +672,9 @@ Options:
// Allow customizing the timeout value
const defaultTimeout = config.snmp.timeout / 1000; // Convert from ms to seconds for display
console.log('\nSNMPv3 operations with authentication and privacy may require longer timeouts.');
console.log(
'\nSNMPv3 operations with authentication and privacy may require longer timeouts.'
);
const timeoutInput = await prompt(`SNMP Timeout in seconds [${defaultTimeout}]: `);
const timeout = parseInt(timeoutInput, 10);
if (timeoutInput.trim() && !isNaN(timeout)) {
@ -656,13 +691,18 @@ Options:
* @param prompt Function to prompt for user input
* @returns Updated configuration
*/
private async gatherAuthenticationSettings(config: any, prompt: (question: string) => Promise<string>): Promise<any> {
private async gatherAuthenticationSettings(
config: any,
prompt: (question: string) => Promise<string>
): Promise<any> {
// Authentication protocol
console.log('\nAuthentication Protocol:');
console.log(' 1) MD5');
console.log(' 2) SHA');
const defaultAuthProtocol = config.snmp.authProtocol === 'SHA' ? 2 : 1;
const authProtocolInput = await prompt(`Select Authentication Protocol [${defaultAuthProtocol}]: `);
const authProtocolInput = await prompt(
`Select Authentication Protocol [${defaultAuthProtocol}]: `
);
const authProtocol = parseInt(authProtocolInput, 10) || defaultAuthProtocol;
config.snmp.authProtocol = authProtocol === 2 ? 'SHA' : 'MD5';
@ -680,7 +720,10 @@ Options:
* @param prompt Function to prompt for user input
* @returns Updated configuration
*/
private async gatherPrivacySettings(config: any, prompt: (question: string) => Promise<string>): Promise<any> {
private async gatherPrivacySettings(
config: any,
prompt: (question: string) => Promise<string>
): Promise<any> {
// Privacy protocol
console.log('\nPrivacy Protocol:');
console.log(' 1) DES');
@ -704,32 +747,42 @@ Options:
* @param prompt Function to prompt for user input
* @returns Updated configuration
*/
private async gatherThresholdSettings(config: any, prompt: (question: string) => Promise<string>): Promise<any> {
private async gatherThresholdSettings(
config: any,
prompt: (question: string) => Promise<string>
): Promise<any> {
console.log('\nShutdown Thresholds:');
// Battery threshold
const defaultBatteryThreshold = config.thresholds.battery;
const batteryThresholdInput = await prompt(`Battery percentage threshold [${defaultBatteryThreshold}%]: `);
const batteryThresholdInput = await prompt(
`Battery percentage threshold [${defaultBatteryThreshold}%]: `
);
const batteryThreshold = parseInt(batteryThresholdInput, 10);
config.thresholds.battery = (batteryThresholdInput.trim() && !isNaN(batteryThreshold))
? batteryThreshold
: defaultBatteryThreshold;
config.thresholds.battery =
batteryThresholdInput.trim() && !isNaN(batteryThreshold)
? batteryThreshold
: defaultBatteryThreshold;
// Runtime threshold
const defaultRuntimeThreshold = config.thresholds.runtime;
const runtimeThresholdInput = await prompt(`Runtime minutes threshold [${defaultRuntimeThreshold} minutes]: `);
const runtimeThresholdInput = await prompt(
`Runtime minutes threshold [${defaultRuntimeThreshold} minutes]: `
);
const runtimeThreshold = parseInt(runtimeThresholdInput, 10);
config.thresholds.runtime = (runtimeThresholdInput.trim() && !isNaN(runtimeThreshold))
? runtimeThreshold
: defaultRuntimeThreshold;
config.thresholds.runtime =
runtimeThresholdInput.trim() && !isNaN(runtimeThreshold)
? runtimeThreshold
: defaultRuntimeThreshold;
// Check interval
const defaultInterval = config.checkInterval / 1000; // Convert from ms to seconds for display
const intervalInput = await prompt(`Check interval in seconds [${defaultInterval}]: `);
const interval = parseInt(intervalInput, 10);
config.checkInterval = (intervalInput.trim() && !isNaN(interval))
? interval * 1000 // Convert to ms
: defaultInterval * 1000;
config.checkInterval =
intervalInput.trim() && !isNaN(interval)
? interval * 1000 // Convert to ms
: defaultInterval * 1000;
return config;
}
@ -740,7 +793,10 @@ Options:
* @param prompt Function to prompt for user input
* @returns Updated configuration
*/
private async gatherUpsModelSettings(config: any, prompt: (question: string) => Promise<string>): Promise<any> {
private async gatherUpsModelSettings(
config: any,
prompt: (question: string) => Promise<string>
): Promise<any> {
console.log('\nUPS Model Selection:');
console.log(' 1) CyberPower');
console.log(' 2) APC');
@ -749,12 +805,20 @@ Options:
console.log(' 5) Liebert/Vertiv');
console.log(' 6) Custom (Advanced)');
const defaultModelValue = config.snmp.upsModel === 'cyberpower' ? 1 :
config.snmp.upsModel === 'apc' ? 2 :
config.snmp.upsModel === 'eaton' ? 3 :
config.snmp.upsModel === 'tripplite' ? 4 :
config.snmp.upsModel === 'liebert' ? 5 :
config.snmp.upsModel === 'custom' ? 6 : 1;
const defaultModelValue =
config.snmp.upsModel === 'cyberpower'
? 1
: config.snmp.upsModel === 'apc'
? 2
: config.snmp.upsModel === 'eaton'
? 3
: config.snmp.upsModel === 'tripplite'
? 4
: config.snmp.upsModel === 'liebert'
? 5
: config.snmp.upsModel === 'custom'
? 6
: 1;
const modelInput = await prompt(`Select UPS model [${defaultModelValue}]: `);
const modelValue = parseInt(modelInput, 10) || defaultModelValue;
@ -783,7 +847,7 @@ Options:
config.snmp.customOIDs = {
POWER_STATUS: powerStatusOID.trim(),
BATTERY_CAPACITY: batteryCapacityOID.trim(),
BATTERY_RUNTIME: batteryRuntimeOID.trim()
BATTERY_RUNTIME: batteryRuntimeOID.trim(),
};
}
@ -799,8 +863,10 @@ Options:
console.log(`│ SNMP Host: ${config.snmp.host}:${config.snmp.port}`);
console.log(`│ SNMP Version: ${config.snmp.version}`);
console.log(`│ UPS Model: ${config.snmp.upsModel}`);
console.log(`│ Thresholds: ${config.thresholds.battery}% battery, ${config.thresholds.runtime} min runtime`);
console.log(`│ Check Interval: ${config.checkInterval/1000} seconds`);
console.log(
`│ Thresholds: ${config.thresholds.battery}% battery, ${config.thresholds.runtime} min runtime`
);
console.log(`│ Check Interval: ${config.checkInterval / 1000} seconds`);
console.log('└──────────────────────────────────────────┘\n');
}
@ -809,15 +875,20 @@ Options:
* @param config Current configuration
* @param prompt Function to prompt for user input
*/
private async optionallyTestConnection(config: any, prompt: (question: string) => Promise<string>): Promise<void> {
const testConnection = await prompt('Would you like to test the connection to your UPS? (y/N): ');
private async optionallyTestConnection(
config: any,
prompt: (question: string) => Promise<string>
): Promise<void> {
const testConnection = await prompt(
'Would you like to test the connection to your UPS? (y/N): '
);
if (testConnection.toLowerCase() === 'y') {
console.log('\nTesting connection to UPS...');
try {
// Create a test config with a short timeout
const testConfig = {
...config.snmp,
timeout: Math.min(config.snmp.timeout, 10000) // Use at most 10 seconds for testing
timeout: Math.min(config.snmp.timeout, 10000), // Use at most 10 seconds for testing
};
const status = await this.nupst.getSnmp().getUpsStatus(testConfig);
@ -843,7 +914,8 @@ Options:
private async restartServiceIfRunning(): Promise<void> {
try {
// Check if the service is active
const isActive = execSync('systemctl is-active nupst.service || true').toString().trim() === 'active';
const isActive =
execSync('systemctl is-active nupst.service || true').toString().trim() === 'active';
if (isActive) {
// Service is running, restart it
@ -878,18 +950,24 @@ Options:
* Optionally enable and start systemd service
* @param prompt Function to prompt for user input
*/
private async optionallyEnableService(prompt: (question: string) => Promise<string>): Promise<void> {
private async optionallyEnableService(
prompt: (question: string) => Promise<string>
): Promise<void> {
if (process.getuid && process.getuid() !== 0) {
console.log('\nNote: Run "sudo nupst enable" to set up NUPST as a system service.');
} else {
const setupService = await prompt('Would you like to enable NUPST as a system service? (y/N): ');
const setupService = await prompt(
'Would you like to enable NUPST as a system service? (y/N): '
);
if (setupService.toLowerCase() === 'y') {
try {
await this.nupst.getSystemd().install();
console.log('Service installed and enabled to start on boot.');
// Ask if the user wants to start the service now
const startService = await prompt('Would you like to start the NUPST service now? (Y/n): ');
const startService = await prompt(
'Would you like to start the NUPST service now? (Y/n): '
);
if (startService.toLowerCase() !== 'n') {
await this.nupst.getSystemd().start();
console.log('NUPST service started successfully.');
@ -914,7 +992,7 @@ Options:
} catch (error) {
console.error('┌─ Configuration Error ─────────────────────┐');
console.error('│ No configuration found.');
console.error('│ Please run \'nupst setup\' first to create a configuration.');
console.error("│ Please run 'nupst setup' first to create a configuration.");
console.error('└──────────────────────────────────────────┘');
return;
}
@ -938,7 +1016,10 @@ Options:
console.log(`│ Username: ${config.snmp.username}`);
// Show auth and privacy details based on security level
if (config.snmp.securityLevel === 'authNoPriv' || config.snmp.securityLevel === 'authPriv') {
if (
config.snmp.securityLevel === 'authNoPriv' ||
config.snmp.securityLevel === 'authPriv'
) {
console.log(`│ Auth Protocol: ${config.snmp.authProtocol || 'None'}`);
}
@ -954,7 +1035,9 @@ Options:
if (config.snmp.upsModel === 'custom' && config.snmp.customOIDs) {
console.log('│ Custom OIDs:');
console.log(`│ Power Status: ${config.snmp.customOIDs.POWER_STATUS || 'Not set'}`);
console.log(`│ Battery Capacity: ${config.snmp.customOIDs.BATTERY_CAPACITY || 'Not set'}`);
console.log(
`│ Battery Capacity: ${config.snmp.customOIDs.BATTERY_CAPACITY || 'Not set'}`
);
console.log(`│ Battery Runtime: ${config.snmp.customOIDs.BATTERY_RUNTIME || 'Not set'}`);
}
@ -973,8 +1056,10 @@ Options:
// Show service status
try {
const isActive = execSync('systemctl is-active nupst.service || true').toString().trim() === 'active';
const isEnabled = execSync('systemctl is-enabled nupst.service || true').toString().trim() === 'enabled';
const isActive =
execSync('systemctl is-active nupst.service || true').toString().trim() === 'active';
const isEnabled =
execSync('systemctl is-enabled nupst.service || true').toString().trim() === 'enabled';
console.log('┌─ Service Status ─────────────────────────┐');
console.log(`│ Service Active: ${isActive ? 'Yes' : 'No'}`);
@ -983,7 +1068,6 @@ Options:
} catch (error) {
// Ignore errors checking service status
}
} catch (error) {
console.error(`Failed to display configuration: ${error.message}`);
}
@ -1002,7 +1086,7 @@ Options:
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
output: process.stdout,
});
// Helper function to prompt for input
@ -1019,7 +1103,9 @@ Options:
console.log('This will completely remove NUPST from your system.\n');
// Ask about removing configuration
const removeConfig = await prompt('Do you want to remove the NUPST configuration files? (y/N): ');
const removeConfig = await prompt(
'Do you want to remove the NUPST configuration files? (y/N): '
);
// Find the uninstall.sh script location
let uninstallScriptPath: string;
@ -1036,10 +1122,7 @@ Options:
await fs.access(uninstallScriptPath);
} catch (error) {
// If we can't find it in the expected location, try common installation paths
const commonPaths = [
'/opt/nupst/uninstall.sh',
join(process.cwd(), 'uninstall.sh')
];
const commonPaths = ['/opt/nupst/uninstall.sh', join(process.cwd(), 'uninstall.sh')];
for (const path of commonPaths) {
try {
@ -1068,16 +1151,15 @@ Options:
const env = {
...process.env,
REMOVE_CONFIG: removeConfig.toLowerCase() === 'y' ? 'yes' : 'no',
REMOVE_REPO: 'yes', // Always remove repo as requested
NUPST_CLI_CALL: 'true' // Flag to indicate this is being called from CLI
REMOVE_REPO: 'yes', // Always remove repo as requested
NUPST_CLI_CALL: 'true', // Flag to indicate this is being called from CLI
};
// Run the uninstall script with sudo
execSync(`sudo bash ${uninstallScriptPath}`, {
env,
stdio: 'inherit' // Show output in the terminal
stdio: 'inherit', // Show output in the terminal
});
} catch (error) {
console.error(`Uninstall failed: ${error.message}`);
process.exit(1);

View File

@ -1,11 +1,12 @@
import * as fs from 'fs';
import * as path from 'path';
import { exec } from 'child_process';
import { exec, execFile } from 'child_process';
import { promisify } from 'util';
import { NupstSnmp } from './snmp/manager.js';
import type { ISnmpConfig } from './snmp/types.js';
const execAsync = promisify(exec);
const execFileAsync = promisify(execFile);
/**
* Configuration interface for the daemon
@ -298,23 +299,101 @@ export class NupstDaemon {
const shutdownDelayMinutes = 5;
try {
// Execute shutdown command with delay to allow for VM graceful shutdown
const { stdout } = await execAsync(`shutdown -h +${shutdownDelayMinutes} "UPS battery critical, shutting down in ${shutdownDelayMinutes} minutes"`);
console.log('Shutdown initiated:', stdout);
console.log(`Allowing ${shutdownDelayMinutes} minutes for VMs to shut down safely`);
// 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;
console.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
console.log(`Executing: ${shutdownCmd} -h +${shutdownDelayMinutes} "UPS battery critical..."`);
const { stdout } = await execFileAsync(shutdownCmd, [
'-h',
`+${shutdownDelayMinutes}`,
`UPS battery critical, shutting down in ${shutdownDelayMinutes} minutes`
]);
console.log('Shutdown initiated:', stdout);
console.log(`Allowing ${shutdownDelayMinutes} minutes for VMs to shut down safely`);
} else {
// Try using the PATH to find shutdown
try {
console.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
});
console.log('Shutdown initiated:', stdout);
} catch (e) {
throw new Error(`Shutdown command not found: ${e.message}`);
}
}
// Monitor UPS during shutdown and force immediate shutdown if battery gets too low
console.log('Monitoring UPS during shutdown process...');
await this.monitorDuringShutdown();
} catch (error) {
console.error('Failed to initiate shutdown:', error);
// Try a different method if first one fails
try {
console.log('Trying alternative shutdown method...');
await execAsync('poweroff --force');
} catch (innerError) {
console.error('All shutdown methods failed:', innerError);
// 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) {
console.log(`Trying alternative shutdown method: ${cmdPath} ${alt.args.join(' ')}`);
await execFileAsync(cmdPath, alt.args);
return; // Exit if successful
} else {
// Try using PATH environment
console.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) {
console.error(`Alternative method ${alt.cmd} failed:`, altError);
// Continue to next method
}
}
console.error('All shutdown methods failed');
}
}
@ -346,10 +425,79 @@ export class NupstDaemon {
console.log('└──────────────────────────────────────────┘');
try {
await execAsync('shutdown -h now "EMERGENCY: UPS battery critically low, shutting down NOW"');
// 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;
console.log(`Found shutdown command at: ${shutdownCmd}`);
break;
}
}
if (shutdownCmd) {
console.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
console.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
});
}
} catch (error) {
console.error('Emergency shutdown failed, trying alternative method...');
await execAsync('poweroff --force');
console.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) {
console.log(`Emergency: using ${cmdPath} ${alt.args.join(' ')}`);
await execFileAsync(cmdPath, alt.args);
return; // Exit if successful
} else {
// Try using PATH
console.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
}
}
console.error('All emergency shutdown methods failed');
}
// Stop monitoring after initiating emergency shutdown

View File

@ -1,4 +1,3 @@
import * as dgram from 'dgram';
import * as snmp from 'net-snmp';
import type { IOidSet, ISnmpConfig, TUpsModel, IUpsStatus } from './types.js';
import { UpsOidSets } from './oid-sets.js';