Compare commits
19 Commits
Author | SHA1 | Date | |
---|---|---|---|
a7113d0387 | |||
61d4e9037a | |||
caced2718f | |||
8516056f84 | |||
07ec9d7595 | |||
d14ba1dd65 | |||
7d595fa175 | |||
df417432b0 | |||
e5f1ebf343 | |||
3ff0dd7ac8 | |||
bb87316dd3 | |||
d6e0a1a274 | |||
95fa4f8b0b | |||
c2f2f1e2ee | |||
936f86c346 | |||
7ff1a7da36 | |||
a87710144c | |||
23fd5cc5cd | |||
fb4d776bdd |
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@serve.zone/nupst",
|
||||
"version": "4.0.3",
|
||||
"version": "4.1.6",
|
||||
"exports": "./mod.ts",
|
||||
"tasks": {
|
||||
"dev": "deno run --allow-all mod.ts",
|
||||
|
84
install.sh
84
install.sh
@@ -10,15 +10,7 @@
|
||||
# With version specification:
|
||||
# curl -sSL https://code.foss.global/serve.zone/nupst/raw/branch/main/install.sh | sudo bash -s -- --version v4.0.0
|
||||
#
|
||||
# Non-interactive mode (auto-confirm):
|
||||
# curl -sSL https://code.foss.global/serve.zone/nupst/raw/branch/main/install.sh | sudo bash -s -- -y
|
||||
#
|
||||
# Downloaded script:
|
||||
# curl -sSL https://code.foss.global/serve.zone/nupst/raw/branch/main/install.sh -o nupst-install.sh
|
||||
# sudo bash nupst-install.sh
|
||||
#
|
||||
# Options:
|
||||
# -y, --yes Automatically answer yes to all prompts
|
||||
# -h, --help Show this help message
|
||||
# --version VERSION Install specific version (e.g., v4.0.0)
|
||||
# --install-dir DIR Installation directory (default: /opt/nupst)
|
||||
@@ -26,7 +18,6 @@
|
||||
set -e
|
||||
|
||||
# Default values
|
||||
AUTO_YES=0
|
||||
SHOW_HELP=0
|
||||
SPECIFIED_VERSION=""
|
||||
INSTALL_DIR="/opt/nupst"
|
||||
@@ -36,10 +27,6 @@ GITEA_REPO="serve.zone/nupst"
|
||||
# Parse command line arguments
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case $1 in
|
||||
-y|--yes)
|
||||
AUTO_YES=1
|
||||
shift
|
||||
;;
|
||||
-h|--help)
|
||||
SHOW_HELP=1
|
||||
shift
|
||||
@@ -67,7 +54,6 @@ if [ $SHOW_HELP -eq 1 ]; then
|
||||
echo "Usage: $0 [options]"
|
||||
echo ""
|
||||
echo "Options:"
|
||||
echo " -y, --yes Automatically answer yes to all prompts"
|
||||
echo " -h, --help Show this help message"
|
||||
echo " --version VERSION Install specific version (e.g., v4.0.0)"
|
||||
echo " --install-dir DIR Installation directory (default: /opt/nupst)"
|
||||
@@ -78,9 +64,6 @@ if [ $SHOW_HELP -eq 1 ]; then
|
||||
echo ""
|
||||
echo " # Install specific version"
|
||||
echo " curl -sSL https://code.foss.global/serve.zone/nupst/raw/branch/main/install.sh | sudo bash -s -- --version v4.0.0"
|
||||
echo ""
|
||||
echo " # Non-interactive installation"
|
||||
echo " curl -sSL https://code.foss.global/serve.zone/nupst/raw/branch/main/install.sh | sudo bash -s -- -y"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
@@ -90,32 +73,6 @@ if [ "$EUID" -ne 0 ]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Detect if script is being piped or run directly
|
||||
INTERACTIVE=1
|
||||
if [ ! -t 0 ] || [ ! -t 1 ]; then
|
||||
# Either stdin or stdout is not a terminal
|
||||
if [ $AUTO_YES -ne 1 ]; then
|
||||
echo "Script detected it's running in a non-interactive environment without -y flag."
|
||||
echo "Attempting to find a controlling terminal for interactive prompts..."
|
||||
# Try to use a controlling terminal for user input
|
||||
exec < /dev/tty 2>/dev/null || INTERACTIVE=0
|
||||
|
||||
if [ $INTERACTIVE -eq 0 ]; then
|
||||
echo "ERROR: No controlling terminal available for interactive prompts."
|
||||
echo ""
|
||||
echo "For interactive installation (RECOMMENDED):"
|
||||
echo " curl -sSL https://code.foss.global/serve.zone/nupst/raw/branch/main/install.sh -o nupst-install.sh"
|
||||
echo " sudo bash nupst-install.sh"
|
||||
echo ""
|
||||
echo "For non-interactive installation with auto-confirm:"
|
||||
echo " curl -sSL https://code.foss.global/serve.zone/nupst/raw/branch/main/install.sh | sudo bash -s -- -y"
|
||||
exit 1
|
||||
else
|
||||
echo "Interactive terminal found, continuing with prompts..."
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
# Helper function to detect OS and architecture
|
||||
detect_platform() {
|
||||
local os=$(uname -s)
|
||||
@@ -225,22 +182,6 @@ if [ -d "$INSTALL_DIR" ]; then
|
||||
echo ""
|
||||
fi
|
||||
|
||||
if [ $AUTO_YES -eq 0 ] && [ $INTERACTIVE -eq 1 ]; then
|
||||
if [ $OLD_NODE_INSTALL -eq 1 ]; then
|
||||
echo "This will replace your Node.js installation with a pre-compiled binary."
|
||||
echo "Your configuration in /etc/nupst/config.json will be preserved."
|
||||
echo ""
|
||||
fi
|
||||
echo "Installation directory already exists: $INSTALL_DIR"
|
||||
echo "Do you want to update/reinstall? (Y/n): "
|
||||
read -r update_confirm
|
||||
|
||||
if [[ "$update_confirm" =~ ^[Nn]$ ]]; then
|
||||
echo "Installation cancelled."
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "Updating existing installation at $INSTALL_DIR..."
|
||||
|
||||
# Check if service exists (enabled or running) and stop it if active
|
||||
@@ -269,17 +210,6 @@ if [ -d "$INSTALL_DIR" ]; then
|
||||
echo "Old installation files removed."
|
||||
fi
|
||||
else
|
||||
if [ $AUTO_YES -eq 0 ] && [ $INTERACTIVE -eq 1 ]; then
|
||||
echo "NUPST will be installed to: $INSTALL_DIR"
|
||||
echo "Continue? (Y/n): "
|
||||
read -r install_confirm
|
||||
|
||||
if [[ "$install_confirm" =~ ^[Nn]$ ]]; then
|
||||
echo "Installation cancelled."
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "Creating installation directory: $INSTALL_DIR"
|
||||
mkdir -p "$INSTALL_DIR"
|
||||
fi
|
||||
@@ -325,22 +255,8 @@ else
|
||||
fi
|
||||
|
||||
# Create symlink for global access
|
||||
if [ $AUTO_YES -eq 0 ] && [ $INTERACTIVE -eq 1 ]; then
|
||||
echo "Create symlink in $BIN_DIR for global access? (Y/n): "
|
||||
read -r symlink_confirm
|
||||
|
||||
if [[ ! "$symlink_confirm" =~ ^[Nn]$ ]]; then
|
||||
ln -sf "$BINARY_PATH" "$BIN_DIR/nupst"
|
||||
echo "Symlink created: $BIN_DIR/nupst -> $BINARY_PATH"
|
||||
else
|
||||
echo "Symlink creation skipped."
|
||||
echo "To use NUPST, run: $BINARY_PATH"
|
||||
echo "Or manually create symlink: sudo ln -sf $BINARY_PATH $BIN_DIR/nupst"
|
||||
fi
|
||||
else
|
||||
ln -sf "$BINARY_PATH" "$BIN_DIR/nupst"
|
||||
echo "Symlink created: $BIN_DIR/nupst -> $BINARY_PATH"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
|
||||
|
16
readme.md
16
readme.md
@@ -29,15 +29,8 @@ dependencies.
|
||||
The easiest way to install NUPST is using the automated installer:
|
||||
|
||||
```bash
|
||||
# Download and run installer (most reliable)
|
||||
curl -sSL https://code.foss.global/serve.zone/nupst/raw/branch/main/install.sh -o nupst-install.sh
|
||||
sudo bash nupst-install.sh
|
||||
rm nupst-install.sh
|
||||
```
|
||||
|
||||
```bash
|
||||
# One-line installation (non-interactive with auto-confirm)
|
||||
curl -sSL https://code.foss.global/serve.zone/nupst/raw/branch/main/install.sh | sudo bash -s -- -y
|
||||
# One-line installation
|
||||
curl -sSL https://code.foss.global/serve.zone/nupst/raw/branch/main/install.sh | sudo bash
|
||||
```
|
||||
|
||||
The installer will:
|
||||
@@ -76,7 +69,6 @@ sudo mv nupst /usr/local/bin/nupst
|
||||
The installer script (`install.sh`) supports the following options:
|
||||
|
||||
```
|
||||
-y, --yes Automatically answer yes to all prompts
|
||||
-h, --help Show help message
|
||||
--version VERSION Install specific version (e.g., --version v4.0.0)
|
||||
--install-dir DIR Custom installation directory (default: /opt/nupst)
|
||||
@@ -373,7 +365,7 @@ sudo nupst service disable
|
||||
Re-run the installer to update to the latest version:
|
||||
|
||||
```bash
|
||||
curl -sSL https://code.foss.global/serve.zone/nupst/raw/branch/main/install.sh | sudo bash -s -- -y
|
||||
curl -sSL https://code.foss.global/serve.zone/nupst/raw/branch/main/install.sh | sudo bash
|
||||
```
|
||||
|
||||
The installer will:
|
||||
@@ -461,7 +453,7 @@ The installer script automatically handles the entire migration while preserving
|
||||
|
||||
```bash
|
||||
# Run the installer (handles stop/update/restart automatically)
|
||||
curl -sSL https://code.foss.global/serve.zone/nupst/raw/branch/main/install.sh | sudo bash -s -- -y
|
||||
curl -sSL https://code.foss.global/serve.zone/nupst/raw/branch/main/install.sh | sudo bash
|
||||
|
||||
# Verify
|
||||
nupst service status
|
||||
|
168
test/manualdocker/00-test-fresh-v4-install.sh
Executable file
168
test/manualdocker/00-test-fresh-v4-install.sh
Executable file
@@ -0,0 +1,168 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Test fresh v4 installation from scratch
|
||||
# Tests the most common user scenario: clean install using curl | bash
|
||||
#
|
||||
|
||||
set -e
|
||||
|
||||
CONTAINER_NAME="nupst-test-fresh-v4"
|
||||
|
||||
echo "================================================"
|
||||
echo " NUPST Fresh v4 Installation Test"
|
||||
echo "================================================"
|
||||
echo ""
|
||||
|
||||
# Check if container already exists
|
||||
if docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
|
||||
echo "⚠️ Container ${CONTAINER_NAME} already exists"
|
||||
read -p "Remove and recreate? (y/N): " -n 1 -r
|
||||
echo
|
||||
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
||||
echo "→ Stopping and removing existing container..."
|
||||
docker stop ${CONTAINER_NAME} 2>/dev/null || true
|
||||
docker rm ${CONTAINER_NAME} 2>/dev/null || true
|
||||
else
|
||||
echo "Exiting. Remove manually with: docker rm -f ${CONTAINER_NAME}"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "→ Creating Docker container with systemd..."
|
||||
docker run -d \
|
||||
--name ${CONTAINER_NAME} \
|
||||
--privileged \
|
||||
--cgroupns=host \
|
||||
-v /sys/fs/cgroup:/sys/fs/cgroup:rw \
|
||||
ubuntu:22.04 \
|
||||
/bin/bash -c "apt-get update && apt-get install -y systemd systemd-sysv && exec /sbin/init"
|
||||
|
||||
echo "→ Waiting for systemd to initialize..."
|
||||
sleep 10
|
||||
|
||||
echo "→ Waiting for dpkg lock to be released..."
|
||||
docker exec ${CONTAINER_NAME} bash -c "
|
||||
while fuser /var/lib/dpkg/lock-frontend >/dev/null 2>&1; do
|
||||
echo ' Waiting for dpkg lock...'
|
||||
sleep 2
|
||||
done
|
||||
echo ' dpkg lock released'
|
||||
"
|
||||
|
||||
echo "→ Installing prerequisites (curl)..."
|
||||
docker exec ${CONTAINER_NAME} bash -c "
|
||||
apt-get update -qq
|
||||
apt-get install -y -qq curl
|
||||
"
|
||||
|
||||
echo ""
|
||||
echo "→ Installing NUPST v4 using curl | bash..."
|
||||
echo " Command: curl -sSL https://code.foss.global/serve.zone/nupst/raw/branch/main/install.sh | bash -s -- -y"
|
||||
echo ""
|
||||
|
||||
docker exec ${CONTAINER_NAME} bash -c "
|
||||
curl -sSL https://code.foss.global/serve.zone/nupst/raw/branch/main/install.sh | bash -s -- -y
|
||||
"
|
||||
|
||||
echo ""
|
||||
echo "================================================"
|
||||
echo " Verifying Installation"
|
||||
echo "================================================"
|
||||
echo ""
|
||||
|
||||
echo "→ Checking binary location..."
|
||||
docker exec ${CONTAINER_NAME} bash -c "
|
||||
if [ -f /opt/nupst/nupst ]; then
|
||||
echo ' ✓ Binary exists at /opt/nupst/nupst'
|
||||
ls -lh /opt/nupst/nupst
|
||||
else
|
||||
echo ' ✗ Binary not found at /opt/nupst/nupst'
|
||||
exit 1
|
||||
fi
|
||||
"
|
||||
|
||||
echo ""
|
||||
echo "→ Checking symlink..."
|
||||
docker exec ${CONTAINER_NAME} bash -c "
|
||||
if [ -L /usr/local/bin/nupst ]; then
|
||||
echo ' ✓ Symlink exists at /usr/local/bin/nupst'
|
||||
ls -lh /usr/local/bin/nupst
|
||||
elif [ -L /usr/bin/nupst ]; then
|
||||
echo ' ✓ Symlink exists at /usr/bin/nupst'
|
||||
ls -lh /usr/bin/nupst
|
||||
else
|
||||
echo ' ✗ Symlink not found in /usr/local/bin or /usr/bin'
|
||||
exit 1
|
||||
fi
|
||||
"
|
||||
|
||||
echo ""
|
||||
echo "→ Checking PATH integration..."
|
||||
docker exec ${CONTAINER_NAME} bash -c "
|
||||
NUPST_PATH=\$(which nupst 2>/dev/null)
|
||||
if [ -n \"\$NUPST_PATH\" ]; then
|
||||
echo ' ✓ nupst found in PATH at: '\$NUPST_PATH
|
||||
else
|
||||
echo ' ✗ nupst not found in PATH'
|
||||
echo ' PATH contents:'
|
||||
echo \$PATH
|
||||
exit 1
|
||||
fi
|
||||
"
|
||||
|
||||
echo ""
|
||||
echo "→ Testing nupst command execution..."
|
||||
docker exec ${CONTAINER_NAME} nupst --version
|
||||
|
||||
echo ""
|
||||
echo "→ Creating minimal config for service test..."
|
||||
docker exec ${CONTAINER_NAME} bash -c "
|
||||
mkdir -p /etc/nupst
|
||||
cat > /etc/nupst/config.json << 'EOF'
|
||||
{
|
||||
\"version\": \"4.0\",
|
||||
\"upsDevices\": [],
|
||||
\"groups\": [],
|
||||
\"checkInterval\": 30000
|
||||
}
|
||||
EOF
|
||||
echo ' ✓ Minimal config created'
|
||||
"
|
||||
|
||||
echo ""
|
||||
echo "→ Testing service creation..."
|
||||
docker exec ${CONTAINER_NAME} bash -c "
|
||||
echo ' Running: nupst service enable'
|
||||
nupst service enable
|
||||
|
||||
if [ -f /etc/systemd/system/nupst.service ]; then
|
||||
echo ' ✓ Service file created successfully'
|
||||
else
|
||||
echo ' ✗ Service file creation failed'
|
||||
exit 1
|
||||
fi
|
||||
"
|
||||
|
||||
echo ""
|
||||
echo "→ Checking if service is enabled..."
|
||||
docker exec ${CONTAINER_NAME} systemctl is-enabled nupst
|
||||
|
||||
echo ""
|
||||
echo "================================================"
|
||||
echo " ✓ Fresh v4 Installation Test Complete"
|
||||
echo "================================================"
|
||||
echo ""
|
||||
echo "Installation verified successfully:"
|
||||
echo " • Binary installed to /opt/nupst/nupst"
|
||||
echo " • Symlink created for global access"
|
||||
echo " • nupst command available in PATH"
|
||||
echo " • Command executes correctly"
|
||||
echo " • Systemd service file created"
|
||||
echo ""
|
||||
echo "Useful commands:"
|
||||
echo " docker exec -it ${CONTAINER_NAME} bash"
|
||||
echo " docker exec ${CONTAINER_NAME} nupst --help"
|
||||
echo " docker exec ${CONTAINER_NAME} nupst service status"
|
||||
echo " docker stop ${CONTAINER_NAME}"
|
||||
echo " docker rm -f ${CONTAINER_NAME}"
|
||||
echo ""
|
@@ -6,5 +6,5 @@ import denoConfig from '../deno.json' with { type: 'json' };
|
||||
export const commitinfo = {
|
||||
name: denoConfig.name,
|
||||
version: denoConfig.version,
|
||||
description: 'Deno-powered UPS monitoring tool for SNMP-enabled UPS devices',
|
||||
description: 'Network UPS Shutdown Tool (https://nupst.serve.zone)',
|
||||
};
|
||||
|
246
ts/cli.ts
246
ts/cli.ts
@@ -1,6 +1,6 @@
|
||||
import { execSync } from 'node:child_process';
|
||||
import { Nupst } from './nupst.ts';
|
||||
import { logger } from './logger.ts';
|
||||
import { logger, type ITableColumn } from './logger.ts';
|
||||
import { theme, symbols } from './colors.ts';
|
||||
|
||||
/**
|
||||
@@ -303,154 +303,164 @@ export class NupstCli {
|
||||
try {
|
||||
await this.nupst.getDaemon().loadConfig();
|
||||
} catch (_error) {
|
||||
const errorBoxWidth = 45;
|
||||
logger.logBoxTitle('Configuration Error', errorBoxWidth);
|
||||
logger.logBoxLine('No configuration found.');
|
||||
logger.logBoxLine("Please run 'nupst setup' first to create a configuration.");
|
||||
logger.logBoxEnd();
|
||||
logger.logBox('Configuration Error', [
|
||||
'No configuration found.',
|
||||
"Please run 'nupst ups add' first to create a configuration.",
|
||||
], 50, 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
// Get current configuration
|
||||
const config = this.nupst.getDaemon().getConfig();
|
||||
|
||||
const boxWidth = 50;
|
||||
logger.logBoxTitle('NUPST Configuration', boxWidth);
|
||||
|
||||
// Check if multi-UPS config
|
||||
if (config.upsDevices && Array.isArray(config.upsDevices)) {
|
||||
// Multi-UPS configuration
|
||||
logger.logBoxLine(`UPS Devices: ${config.upsDevices.length}`);
|
||||
logger.logBoxLine(`Groups: ${config.groups ? config.groups.length : 0}`);
|
||||
logger.logBoxLine(`Check Interval: ${config.checkInterval / 1000} seconds`);
|
||||
logger.logBoxLine('');
|
||||
logger.logBoxLine('Configuration File Location:');
|
||||
logger.logBoxLine(' /etc/nupst/config.json');
|
||||
logger.logBoxEnd();
|
||||
// === Multi-UPS Configuration ===
|
||||
|
||||
// Show UPS devices
|
||||
// Overview Box
|
||||
logger.log('');
|
||||
logger.logBox('NUPST Configuration', [
|
||||
`UPS Devices: ${theme.highlight(String(config.upsDevices.length))}`,
|
||||
`Groups: ${theme.highlight(String(config.groups ? config.groups.length : 0))}`,
|
||||
`Check Interval: ${theme.info(String(config.checkInterval / 1000))} seconds`,
|
||||
'',
|
||||
theme.dim('Configuration File:'),
|
||||
` ${theme.path('/etc/nupst/config.json')}`,
|
||||
], 60, 'info');
|
||||
|
||||
// UPS Devices Table
|
||||
if (config.upsDevices.length > 0) {
|
||||
logger.logBoxTitle('UPS Devices', boxWidth);
|
||||
for (const ups of config.upsDevices) {
|
||||
logger.logBoxLine(`${ups.name} (${ups.id}):`);
|
||||
logger.logBoxLine(` Host: ${ups.snmp.host}:${ups.snmp.port}`);
|
||||
logger.logBoxLine(` Model: ${ups.snmp.upsModel}`);
|
||||
logger.logBoxLine(
|
||||
` Thresholds: ${ups.thresholds.battery}% battery, ${ups.thresholds.runtime} min runtime`,
|
||||
);
|
||||
logger.logBoxLine(
|
||||
` Groups: ${ups.groups.length > 0 ? ups.groups.join(', ') : 'None'}`,
|
||||
);
|
||||
logger.logBoxLine('');
|
||||
}
|
||||
logger.logBoxEnd();
|
||||
const upsRows = config.upsDevices.map((ups) => ({
|
||||
name: ups.name,
|
||||
id: theme.dim(ups.id),
|
||||
host: `${ups.snmp.host}:${ups.snmp.port}`,
|
||||
model: ups.snmp.upsModel || 'cyberpower',
|
||||
thresholds: `${ups.thresholds.battery}% / ${ups.thresholds.runtime}min`,
|
||||
groups: ups.groups.length > 0 ? ups.groups.join(', ') : theme.dim('None'),
|
||||
}));
|
||||
|
||||
const upsColumns: ITableColumn[] = [
|
||||
{ header: 'Name', key: 'name', align: 'left', color: theme.highlight },
|
||||
{ header: 'ID', key: 'id', align: 'left' },
|
||||
{ header: 'Host:Port', key: 'host', align: 'left', color: theme.info },
|
||||
{ header: 'Model', key: 'model', align: 'left' },
|
||||
{ header: 'Battery/Runtime', key: 'thresholds', align: 'left' },
|
||||
{ header: 'Groups', key: 'groups', align: 'left' },
|
||||
];
|
||||
|
||||
logger.log('');
|
||||
logger.info(`UPS Devices (${config.upsDevices.length}):`);
|
||||
logger.log('');
|
||||
logger.logTable(upsColumns, upsRows);
|
||||
}
|
||||
|
||||
// Show groups
|
||||
// Groups Table
|
||||
if (config.groups && config.groups.length > 0) {
|
||||
logger.logBoxTitle('UPS Groups', boxWidth);
|
||||
for (const group of config.groups) {
|
||||
logger.logBoxLine(`${group.name} (${group.id}):`);
|
||||
logger.logBoxLine(` Mode: ${group.mode}`);
|
||||
if (group.description) {
|
||||
logger.logBoxLine(` Description: ${group.description}`);
|
||||
}
|
||||
|
||||
// List UPS devices in this group
|
||||
const groupRows = config.groups.map((group) => {
|
||||
const upsInGroup = config.upsDevices.filter((ups) =>
|
||||
ups.groups && ups.groups.includes(group.id)
|
||||
);
|
||||
logger.logBoxLine(
|
||||
` UPS Devices: ${
|
||||
upsInGroup.length > 0 ? upsInGroup.map((ups) => ups.name).join(', ') : 'None'
|
||||
}`,
|
||||
);
|
||||
logger.logBoxLine('');
|
||||
}
|
||||
logger.logBoxEnd();
|
||||
return {
|
||||
name: group.name,
|
||||
id: theme.dim(group.id),
|
||||
mode: group.mode,
|
||||
upsCount: String(upsInGroup.length),
|
||||
ups: upsInGroup.length > 0
|
||||
? upsInGroup.map((ups) => ups.name).join(', ')
|
||||
: theme.dim('None'),
|
||||
description: group.description || theme.dim('—'),
|
||||
};
|
||||
});
|
||||
|
||||
const groupColumns: ITableColumn[] = [
|
||||
{ header: 'Name', key: 'name', align: 'left', color: theme.highlight },
|
||||
{ header: 'ID', key: 'id', align: 'left' },
|
||||
{ header: 'Mode', key: 'mode', align: 'left', color: theme.info },
|
||||
{ header: 'UPS', key: 'upsCount', align: 'right' },
|
||||
{ header: 'UPS Devices', key: 'ups', align: 'left' },
|
||||
{ header: 'Description', key: 'description', align: 'left' },
|
||||
];
|
||||
|
||||
logger.log('');
|
||||
logger.info(`UPS Groups (${config.groups.length}):`);
|
||||
logger.log('');
|
||||
logger.logTable(groupColumns, groupRows);
|
||||
}
|
||||
} else {
|
||||
// Legacy single UPS configuration
|
||||
if (!config.snmp) {
|
||||
logger.logBoxLine('Error: Legacy configuration missing SNMP settings');
|
||||
} else {
|
||||
// SNMP Settings
|
||||
logger.logBoxLine('SNMP Settings:');
|
||||
logger.logBoxLine(` Host: ${config.snmp.host}`);
|
||||
logger.logBoxLine(` Port: ${config.snmp.port}`);
|
||||
logger.logBoxLine(` Version: ${config.snmp.version}`);
|
||||
logger.logBoxLine(` UPS Model: ${config.snmp.upsModel || 'cyberpower'}`);
|
||||
// === Legacy Single UPS Configuration ===
|
||||
|
||||
if (config.snmp.version === 1 || config.snmp.version === 2) {
|
||||
logger.logBoxLine(` Community: ${config.snmp.community}`);
|
||||
} else if (config.snmp.version === 3) {
|
||||
logger.logBoxLine(` Security Level: ${config.snmp.securityLevel}`);
|
||||
logger.logBoxLine(` Username: ${config.snmp.username}`);
|
||||
|
||||
// Show auth and privacy details based on security level
|
||||
if (
|
||||
config.snmp.securityLevel === 'authNoPriv' ||
|
||||
config.snmp.securityLevel === 'authPriv'
|
||||
) {
|
||||
logger.logBoxLine(` Auth Protocol: ${config.snmp.authProtocol || 'None'}`);
|
||||
if (!config.snmp || !config.thresholds) {
|
||||
logger.logBox('Configuration Error', [
|
||||
'Error: Legacy configuration missing SNMP or threshold settings',
|
||||
], 60, 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
if (config.snmp.securityLevel === 'authPriv') {
|
||||
logger.logBoxLine(` Privacy Protocol: ${config.snmp.privProtocol || 'None'}`);
|
||||
}
|
||||
|
||||
// Show timeout value
|
||||
logger.logBoxLine(` Timeout: ${config.snmp.timeout / 1000} seconds`);
|
||||
}
|
||||
|
||||
// Show OIDs if custom model is selected
|
||||
if (config.snmp.upsModel === 'custom' && config.snmp.customOIDs) {
|
||||
logger.logBoxLine('Custom OIDs:');
|
||||
logger.logBoxLine(
|
||||
logger.log('');
|
||||
logger.logBox('NUPST Configuration (Legacy)', [
|
||||
theme.warning('Legacy single-UPS configuration format'),
|
||||
'',
|
||||
theme.dim('SNMP Settings:'),
|
||||
` Host: ${theme.info(config.snmp.host)}`,
|
||||
` Port: ${theme.info(String(config.snmp.port))}`,
|
||||
` Version: ${config.snmp.version}`,
|
||||
` UPS Model: ${config.snmp.upsModel || 'cyberpower'}`,
|
||||
...(config.snmp.version === 1 || config.snmp.version === 2
|
||||
? [` Community: ${config.snmp.community}`]
|
||||
: []
|
||||
),
|
||||
...(config.snmp.version === 3
|
||||
? [
|
||||
` Security Level: ${config.snmp.securityLevel}`,
|
||||
` Username: ${config.snmp.username}`,
|
||||
...(config.snmp.securityLevel === 'authNoPriv' || config.snmp.securityLevel === 'authPriv'
|
||||
? [` Auth Protocol: ${config.snmp.authProtocol || 'None'}`]
|
||||
: []
|
||||
),
|
||||
...(config.snmp.securityLevel === 'authPriv'
|
||||
? [` Privacy Protocol: ${config.snmp.privProtocol || 'None'}`]
|
||||
: []
|
||||
),
|
||||
` Timeout: ${config.snmp.timeout / 1000} seconds`,
|
||||
]
|
||||
: []
|
||||
),
|
||||
...(config.snmp.upsModel === 'custom' && config.snmp.customOIDs
|
||||
? [
|
||||
theme.dim('Custom OIDs:'),
|
||||
` Power Status: ${config.snmp.customOIDs.POWER_STATUS || 'Not set'}`,
|
||||
);
|
||||
logger.logBoxLine(
|
||||
` Battery Capacity: ${config.snmp.customOIDs.BATTERY_CAPACITY || 'Not set'}`,
|
||||
);
|
||||
logger.logBoxLine(
|
||||
` Battery Runtime: ${config.snmp.customOIDs.BATTERY_RUNTIME || 'Not set'}`,
|
||||
);
|
||||
}
|
||||
]
|
||||
: []
|
||||
),
|
||||
'',
|
||||
theme.dim('Thresholds:'),
|
||||
` Battery: ${theme.highlight(String(config.thresholds.battery))}%`,
|
||||
` Runtime: ${theme.highlight(String(config.thresholds.runtime))} minutes`,
|
||||
` Check Interval: ${config.checkInterval / 1000} seconds`,
|
||||
'',
|
||||
theme.dim('Configuration File:'),
|
||||
` ${theme.path('/etc/nupst/config.json')}`,
|
||||
'',
|
||||
theme.warning('Note: Using legacy single-UPS configuration format.'),
|
||||
`Consider using ${theme.command('nupst ups add')} to migrate to multi-UPS format.`,
|
||||
], 70, 'warning');
|
||||
}
|
||||
|
||||
// Thresholds
|
||||
if (!config.thresholds) {
|
||||
logger.logBoxLine('Error: Legacy configuration missing threshold settings');
|
||||
} else {
|
||||
logger.logBoxLine('Thresholds:');
|
||||
logger.logBoxLine(` Battery: ${config.thresholds.battery}%`);
|
||||
logger.logBoxLine(` Runtime: ${config.thresholds.runtime} minutes`);
|
||||
}
|
||||
logger.logBoxLine(`Check Interval: ${config.checkInterval / 1000} seconds`);
|
||||
|
||||
// Configuration file location
|
||||
logger.logBoxLine('');
|
||||
logger.logBoxLine('Configuration File Location:');
|
||||
logger.logBoxLine(' /etc/nupst/config.json');
|
||||
logger.logBoxLine('');
|
||||
logger.logBoxLine('Note: Using legacy single-UPS configuration format.');
|
||||
logger.logBoxLine('Consider using "nupst add" to migrate to multi-UPS format.');
|
||||
|
||||
logger.logBoxEnd();
|
||||
}
|
||||
|
||||
// Show service status
|
||||
// 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 statusBoxWidth = 45;
|
||||
logger.logBoxTitle('Service Status', statusBoxWidth);
|
||||
logger.logBoxLine(`Service Active: ${isActive ? 'Yes' : 'No'}`);
|
||||
logger.logBoxLine(`Service Enabled: ${isEnabled ? 'Yes' : 'No'}`);
|
||||
logger.logBoxEnd();
|
||||
logger.log('');
|
||||
logger.logBox('Service Status', [
|
||||
`Active: ${isActive ? theme.success('Yes') : theme.dim('No')}`,
|
||||
`Enabled: ${isEnabled ? theme.success('Yes') : theme.dim('No')}`,
|
||||
], 50, isActive ? 'success' : 'default');
|
||||
logger.log('');
|
||||
} catch (_error) {
|
||||
// Ignore errors checking service status
|
||||
}
|
||||
@@ -469,7 +479,7 @@ export class NupstCli {
|
||||
private showVersion(): void {
|
||||
const version = this.nupst.getVersion();
|
||||
logger.log(`NUPST version ${version}`);
|
||||
logger.log('Deno-powered UPS monitoring tool');
|
||||
logger.log('Network UPS Shutdown Tool (https://nupst.serve.zone)');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -1,6 +1,7 @@
|
||||
import process from 'node:process';
|
||||
import { Nupst } from '../nupst.ts';
|
||||
import { logger } from '../logger.ts';
|
||||
import { logger, type ITableColumn } from '../logger.ts';
|
||||
import { theme } from '../colors.ts';
|
||||
import * as helpers from '../helpers/index.ts';
|
||||
import { type IGroupConfig } from '../daemon.ts';
|
||||
|
||||
@@ -28,11 +29,10 @@ export class GroupHandler {
|
||||
try {
|
||||
await this.nupst.getDaemon().loadConfig();
|
||||
} catch (error) {
|
||||
const errorBoxWidth = 45;
|
||||
logger.logBoxTitle('Configuration Error', errorBoxWidth);
|
||||
logger.logBoxLine('No configuration found.');
|
||||
logger.logBoxLine("Please run 'nupst setup' first to create a configuration.");
|
||||
logger.logBoxEnd();
|
||||
logger.logBox('Configuration Error', [
|
||||
'No configuration found.',
|
||||
"Please run 'nupst ups add' first to create a configuration.",
|
||||
], 50, 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -41,43 +41,53 @@ export class GroupHandler {
|
||||
|
||||
// Check if multi-UPS config
|
||||
if (!config.groups || !Array.isArray(config.groups)) {
|
||||
// Legacy or missing groups configuration
|
||||
const boxWidth = 45;
|
||||
logger.logBoxTitle('UPS Groups', boxWidth);
|
||||
logger.logBoxLine('No groups configured.');
|
||||
logger.logBoxLine('Use "nupst group add" to add a UPS group.');
|
||||
logger.logBoxEnd();
|
||||
logger.logBox('UPS Groups', [
|
||||
'No groups configured.',
|
||||
'',
|
||||
`${theme.dim('Run')} ${theme.command('nupst group add')} ${theme.dim('to add a group')}`,
|
||||
], 50, 'info');
|
||||
return;
|
||||
}
|
||||
|
||||
// Display group list
|
||||
const boxWidth = 60;
|
||||
logger.logBoxTitle('UPS Groups', boxWidth);
|
||||
|
||||
// Display group list with modern table
|
||||
if (config.groups.length === 0) {
|
||||
logger.logBoxLine('No UPS groups configured.');
|
||||
logger.logBoxLine('Use "nupst group add" to add a UPS group.');
|
||||
} else {
|
||||
logger.logBoxLine(`Found ${config.groups.length} group(s)`);
|
||||
logger.logBoxLine('');
|
||||
logger.logBoxLine('ID | Name | Mode | UPS Devices');
|
||||
logger.logBoxLine('-----------+----------------------+--------------+----------------');
|
||||
|
||||
for (const group of config.groups) {
|
||||
const id = group.id.padEnd(10, ' ').substring(0, 10);
|
||||
const name = (group.name || '').padEnd(20, ' ').substring(0, 20);
|
||||
const mode = (group.mode || 'unknown').padEnd(12, ' ').substring(0, 12);
|
||||
logger.logBox('UPS Groups', [
|
||||
'No UPS groups configured.',
|
||||
'',
|
||||
`${theme.dim('Run')} ${theme.command('nupst group add')} ${theme.dim('to add a group')}`,
|
||||
], 60, 'info');
|
||||
return;
|
||||
}
|
||||
|
||||
// Prepare table data
|
||||
const rows = config.groups.map((group) => {
|
||||
// Count UPS devices in this group
|
||||
const upsInGroup = config.upsDevices.filter((ups) => ups.groups.includes(group.id));
|
||||
const upsCount = upsInGroup.length;
|
||||
const upsNames = upsInGroup.map((ups) => ups.name).join(', ');
|
||||
|
||||
logger.logBoxLine(`${id} | ${name} | ${mode} | ${upsCount > 0 ? upsNames : 'None'}`);
|
||||
}
|
||||
}
|
||||
return {
|
||||
id: group.id,
|
||||
name: group.name || '',
|
||||
mode: group.mode || 'unknown',
|
||||
count: String(upsCount),
|
||||
devices: upsCount > 0 ? upsNames : theme.dim('None'),
|
||||
};
|
||||
});
|
||||
|
||||
logger.logBoxEnd();
|
||||
const columns: ITableColumn[] = [
|
||||
{ header: 'ID', key: 'id', align: 'left', color: theme.highlight },
|
||||
{ header: 'Name', key: 'name', align: 'left' },
|
||||
{ header: 'Mode', key: 'mode', align: 'left', color: theme.info },
|
||||
{ header: 'UPS Count', key: 'count', align: 'right' },
|
||||
{ header: 'UPS Devices', key: 'devices', align: 'left' },
|
||||
];
|
||||
|
||||
logger.log('');
|
||||
logger.info(`UPS Groups (${config.groups.length}):`);
|
||||
logger.log('');
|
||||
logger.logTable(columns, rows);
|
||||
logger.log('');
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
`Failed to list UPS groups: ${error instanceof Error ? error.message : String(error)}`,
|
||||
@@ -192,6 +202,7 @@ export class GroupHandler {
|
||||
logger.log('\nGroup setup complete!');
|
||||
} finally {
|
||||
rl.close();
|
||||
process.stdin.destroy();
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(`Add group error: ${error instanceof Error ? error.message : String(error)}`);
|
||||
@@ -309,6 +320,7 @@ export class GroupHandler {
|
||||
logger.log('\nGroup edit complete!');
|
||||
} finally {
|
||||
rl.close();
|
||||
process.stdin.destroy();
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(`Edit group error: ${error instanceof Error ? error.message : String(error)}`);
|
||||
@@ -366,6 +378,7 @@ export class GroupHandler {
|
||||
});
|
||||
|
||||
rl.close();
|
||||
process.stdin.destroy();
|
||||
|
||||
if (confirm !== 'y' && confirm !== 'yes') {
|
||||
logger.log('Deletion cancelled.');
|
||||
|
@@ -129,81 +129,57 @@ export class ServiceHandler {
|
||||
try {
|
||||
// Check if running as root
|
||||
this.checkRootAccess(
|
||||
'This command must be run as root to update NUPST and refresh the systemd service.',
|
||||
'This command must be run as root to update NUPST.',
|
||||
);
|
||||
|
||||
const boxWidth = 45;
|
||||
logger.logBoxTitle('NUPST Update Process', boxWidth);
|
||||
logger.logBoxLine('Updating NUPST from repository...');
|
||||
|
||||
// Determine the installation directory (assuming it's either /opt/nupst or the current directory)
|
||||
const { existsSync } = await import('fs');
|
||||
let installDir = '/opt/nupst';
|
||||
|
||||
if (!existsSync(installDir)) {
|
||||
// If not installed in /opt/nupst, use the current directory
|
||||
const { dirname } = await import('path');
|
||||
installDir = dirname(dirname(process.argv[1])); // Go up two levels from the executable
|
||||
logger.logBoxLine(`Using local installation directory: ${installDir}`);
|
||||
}
|
||||
console.log('');
|
||||
logger.info('Checking for updates...');
|
||||
|
||||
try {
|
||||
// 1. Update the repository
|
||||
logger.logBoxLine('Pulling latest changes from git repository...');
|
||||
execSync(`cd ${installDir} && git fetch origin && git reset --hard origin/main`, {
|
||||
stdio: 'pipe',
|
||||
// Get current version
|
||||
const currentVersion = this.nupst.getVersion();
|
||||
|
||||
// Fetch latest version from Gitea API
|
||||
const apiUrl = 'https://code.foss.global/api/v1/repos/serve.zone/nupst/releases/latest';
|
||||
const response = execSync(`curl -sSL ${apiUrl}`).toString();
|
||||
const release = JSON.parse(response);
|
||||
const latestVersion = release.tag_name; // e.g., "v4.0.7"
|
||||
|
||||
// Normalize versions for comparison (ensure both have "v" prefix)
|
||||
const normalizedCurrent = currentVersion.startsWith('v') ? currentVersion : `v${currentVersion}`;
|
||||
const normalizedLatest = latestVersion.startsWith('v') ? latestVersion : `v${latestVersion}`;
|
||||
|
||||
logger.dim(`Current version: ${normalizedCurrent}`);
|
||||
logger.dim(`Latest version: ${normalizedLatest}`);
|
||||
console.log('');
|
||||
|
||||
// Compare normalized versions
|
||||
if (normalizedCurrent === normalizedLatest) {
|
||||
logger.success('Already up to date!');
|
||||
console.log('');
|
||||
return;
|
||||
}
|
||||
|
||||
logger.info(`New version available: ${latestVersion}`);
|
||||
logger.dim('Downloading and installing...');
|
||||
console.log('');
|
||||
|
||||
// Download and run the install script
|
||||
// This handles everything: download binary, stop service, replace, restart
|
||||
const installUrl = 'https://code.foss.global/serve.zone/nupst/raw/branch/main/install.sh';
|
||||
|
||||
execSync(`curl -sSL ${installUrl} | bash`, {
|
||||
stdio: 'inherit', // Show install script output to user
|
||||
});
|
||||
|
||||
// 2. Run the install.sh script
|
||||
logger.logBoxLine('Running install.sh to update NUPST...');
|
||||
execSync(`cd ${installDir} && bash ./install.sh`, { stdio: 'pipe' });
|
||||
|
||||
// 3. Run the setup.sh script with force flag to update Node.js and dependencies
|
||||
logger.logBoxLine('Running setup.sh to update Node.js and dependencies...');
|
||||
execSync(`cd ${installDir} && bash ./setup.sh --force`, { stdio: 'pipe' });
|
||||
|
||||
// 4. Refresh the systemd service
|
||||
logger.logBoxLine('Refreshing systemd service...');
|
||||
|
||||
// First check if service exists
|
||||
let serviceExists = false;
|
||||
try {
|
||||
const output = execSync('systemctl list-unit-files | grep nupst.service').toString();
|
||||
serviceExists = output.includes('nupst.service');
|
||||
console.log('');
|
||||
logger.success(`Updated to ${latestVersion}`);
|
||||
console.log('');
|
||||
} 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';
|
||||
if (isRunning) {
|
||||
logger.logBoxLine('Stopping nupst service...');
|
||||
execSync('systemctl stop nupst.service');
|
||||
}
|
||||
|
||||
// Reinstall the service
|
||||
logger.logBoxLine('Reinstalling systemd service...');
|
||||
await this.nupst.getSystemd().install();
|
||||
|
||||
// Restart the service if it was running
|
||||
if (isRunning) {
|
||||
logger.logBoxLine('Restarting nupst service...');
|
||||
execSync('systemctl start nupst.service');
|
||||
}
|
||||
} else {
|
||||
logger.logBoxLine('Systemd service not installed, skipping service refresh.');
|
||||
logger.logBoxLine('Run "nupst enable" to install the service.');
|
||||
}
|
||||
|
||||
logger.logBoxLine('Update completed successfully!');
|
||||
logger.logBoxEnd();
|
||||
} catch (error) {
|
||||
logger.logBoxLine('Error during update process:');
|
||||
logger.logBoxLine(`${error instanceof Error ? error.message : String(error)}`);
|
||||
logger.logBoxEnd();
|
||||
console.log('');
|
||||
logger.error('Update failed');
|
||||
logger.dim(`${error instanceof Error ? error.message : String(error)}`);
|
||||
console.log('');
|
||||
process.exit(1);
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -237,9 +213,11 @@ export class ServiceHandler {
|
||||
});
|
||||
};
|
||||
|
||||
console.log('\nNUPST Uninstaller');
|
||||
console.log('===============');
|
||||
console.log('This will completely remove NUPST from your system.\n');
|
||||
logger.log('');
|
||||
logger.highlight('NUPST Uninstaller');
|
||||
logger.dim('===============');
|
||||
logger.log('This will completely remove NUPST from your system.');
|
||||
logger.log('');
|
||||
|
||||
// Ask about removing configuration
|
||||
const removeConfig = await prompt(
|
||||
@@ -275,17 +253,20 @@ export class ServiceHandler {
|
||||
}
|
||||
|
||||
if (!uninstallScriptPath) {
|
||||
console.error('Could not locate uninstall.sh script. Aborting uninstall.');
|
||||
logger.error('Could not locate uninstall.sh script. Aborting uninstall.');
|
||||
rl.close();
|
||||
process.stdin.destroy();
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Close readline before executing script
|
||||
rl.close();
|
||||
process.stdin.destroy();
|
||||
|
||||
// Execute uninstall.sh with the appropriate option
|
||||
console.log(`\nRunning uninstaller from ${uninstallScriptPath}...`);
|
||||
logger.log('');
|
||||
logger.log(`Running uninstaller from ${uninstallScriptPath}...`);
|
||||
|
||||
// Pass the configuration removal option as an environment variable
|
||||
const env = {
|
||||
@@ -301,7 +282,7 @@ export class ServiceHandler {
|
||||
stdio: 'inherit', // Show output in the terminal
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(`Uninstall failed: ${error instanceof Error ? error.message : String(error)}`);
|
||||
logger.error(`Uninstall failed: ${error instanceof Error ? error.message : String(error)}`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
@@ -1,7 +1,8 @@
|
||||
import process from 'node:process';
|
||||
import { execSync } from 'node:child_process';
|
||||
import { Nupst } from '../nupst.ts';
|
||||
import { logger } from '../logger.ts';
|
||||
import { logger, type ITableColumn } from '../logger.ts';
|
||||
import { theme } from '../colors.ts';
|
||||
import * as helpers from '../helpers/index.ts';
|
||||
import type { TUpsModel } from '../snmp/types.ts';
|
||||
import type { INupstConfig } from '../daemon.ts';
|
||||
@@ -47,6 +48,7 @@ export class UpsHandler {
|
||||
await this.runAddProcess(prompt);
|
||||
} finally {
|
||||
rl.close();
|
||||
process.stdin.destroy();
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(`Add UPS error: ${error instanceof Error ? error.message : String(error)}`);
|
||||
@@ -178,6 +180,7 @@ export class UpsHandler {
|
||||
await this.runEditProcess(upsId, prompt);
|
||||
} finally {
|
||||
rl.close();
|
||||
process.stdin.destroy();
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(`Edit UPS error: ${error instanceof Error ? error.message : String(error)}`);
|
||||
@@ -344,6 +347,7 @@ export class UpsHandler {
|
||||
});
|
||||
|
||||
rl.close();
|
||||
process.stdin.destroy();
|
||||
|
||||
if (confirm !== 'y' && confirm !== 'yes') {
|
||||
logger.log('Deletion cancelled.');
|
||||
@@ -376,11 +380,10 @@ export class UpsHandler {
|
||||
try {
|
||||
await this.nupst.getDaemon().loadConfig();
|
||||
} catch (error) {
|
||||
const errorBoxWidth = 45;
|
||||
logger.logBoxTitle('Configuration Error', errorBoxWidth);
|
||||
logger.logBoxLine('No configuration found.');
|
||||
logger.logBoxLine("Please run 'nupst setup' first to create a configuration.");
|
||||
logger.logBoxEnd();
|
||||
logger.logBox('Configuration Error', [
|
||||
'No configuration found.',
|
||||
"Please run 'nupst ups add' first to create a configuration.",
|
||||
], 50, 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -390,58 +393,57 @@ export class UpsHandler {
|
||||
// Check if multi-UPS config
|
||||
if (!config.upsDevices || !Array.isArray(config.upsDevices)) {
|
||||
// Legacy single UPS configuration
|
||||
const boxWidth = 45;
|
||||
logger.logBoxTitle('UPS Devices', boxWidth);
|
||||
logger.logBoxLine('Legacy single-UPS configuration detected.');
|
||||
if (!config.snmp || !config.thresholds) {
|
||||
logger.logBoxLine('');
|
||||
logger.logBoxLine('Error: Configuration missing SNMP or threshold settings');
|
||||
logger.logBoxEnd();
|
||||
return;
|
||||
}
|
||||
logger.logBoxLine('');
|
||||
logger.logBoxLine('Default UPS:');
|
||||
logger.logBoxLine(` Host: ${config.snmp.host}:${config.snmp.port}`);
|
||||
logger.logBoxLine(` Model: ${config.snmp.upsModel || 'cyberpower'}`);
|
||||
logger.logBoxLine(
|
||||
logger.logBox('UPS Devices', [
|
||||
'Legacy single-UPS configuration detected.',
|
||||
'',
|
||||
...((!config.snmp || !config.thresholds)
|
||||
? ['Error: Configuration missing SNMP or threshold settings']
|
||||
: [
|
||||
'Default UPS:',
|
||||
` Host: ${config.snmp.host}:${config.snmp.port}`,
|
||||
` Model: ${config.snmp.upsModel || 'cyberpower'}`,
|
||||
` Thresholds: ${config.thresholds.battery}% battery, ${config.thresholds.runtime} min runtime`,
|
||||
);
|
||||
logger.logBoxLine('');
|
||||
logger.logBoxLine('Use "nupst add" to add more UPS devices and migrate');
|
||||
logger.logBoxLine('to the multi-UPS configuration format.');
|
||||
logger.logBoxEnd();
|
||||
'',
|
||||
'Use "nupst ups add" to add more UPS devices and migrate',
|
||||
'to the multi-UPS configuration format.',
|
||||
]
|
||||
),
|
||||
], 60, 'warning');
|
||||
return;
|
||||
}
|
||||
|
||||
// Display UPS list
|
||||
const boxWidth = 60;
|
||||
logger.logBoxTitle('UPS Devices', boxWidth);
|
||||
|
||||
// Display UPS list with modern table
|
||||
if (config.upsDevices.length === 0) {
|
||||
logger.logBoxLine('No UPS devices configured.');
|
||||
logger.logBoxLine('Use "nupst add" to add a UPS device.');
|
||||
} else {
|
||||
logger.logBoxLine(`Found ${config.upsDevices.length} UPS device(s)`);
|
||||
logger.logBoxLine('');
|
||||
logger.logBoxLine(
|
||||
'ID | Name | Host | Mode | Groups',
|
||||
);
|
||||
logger.logBoxLine(
|
||||
'-----------+----------------------+-----------------+--------------+----------------',
|
||||
);
|
||||
|
||||
for (const ups of config.upsDevices) {
|
||||
const id = ups.id.padEnd(10, ' ').substring(0, 10);
|
||||
const name = (ups.name || '').padEnd(20, ' ').substring(0, 20);
|
||||
const host = `${ups.snmp.host}:${ups.snmp.port}`.padEnd(15, ' ').substring(0, 15);
|
||||
const model = (ups.snmp.upsModel || 'cyberpower').padEnd(12, ' ').substring(0, 12);
|
||||
const groups = ups.groups.length > 0 ? ups.groups.join(', ') : 'None';
|
||||
|
||||
logger.logBoxLine(`${id} | ${name} | ${host} | ${model} | ${groups}`);
|
||||
}
|
||||
logger.logBox('UPS Devices', [
|
||||
'No UPS devices configured.',
|
||||
'',
|
||||
`${theme.dim('Run')} ${theme.command('nupst ups add')} ${theme.dim('to add a device')}`,
|
||||
], 60, 'info');
|
||||
return;
|
||||
}
|
||||
|
||||
logger.logBoxEnd();
|
||||
// Prepare table data
|
||||
const rows = config.upsDevices.map((ups) => ({
|
||||
id: ups.id,
|
||||
name: ups.name || '',
|
||||
host: `${ups.snmp.host}:${ups.snmp.port}`,
|
||||
model: ups.snmp.upsModel || 'cyberpower',
|
||||
groups: ups.groups.length > 0 ? ups.groups.join(', ') : theme.dim('None'),
|
||||
}));
|
||||
|
||||
const columns: ITableColumn[] = [
|
||||
{ header: 'ID', key: 'id', align: 'left', color: theme.highlight },
|
||||
{ header: 'Name', key: 'name', align: 'left' },
|
||||
{ header: 'Host:Port', key: 'host', align: 'left', color: theme.info },
|
||||
{ header: 'Model', key: 'model', align: 'left' },
|
||||
{ header: 'Groups', key: 'groups', align: 'left' },
|
||||
];
|
||||
|
||||
logger.log('');
|
||||
logger.info(`UPS Devices (${config.upsDevices.length}):`);
|
||||
logger.log('');
|
||||
logger.logTable(columns, rows);
|
||||
logger.log('');
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
`Failed to list UPS devices: ${error instanceof Error ? error.message : String(error)}`,
|
||||
@@ -667,10 +669,11 @@ export class UpsHandler {
|
||||
|
||||
// SNMP Version
|
||||
const defaultVersion = snmpConfig.version || 1;
|
||||
console.log('\nSNMP Version:');
|
||||
console.log(' 1) SNMPv1');
|
||||
console.log(' 2) SNMPv2c');
|
||||
console.log(' 3) SNMPv3 (with security features)');
|
||||
logger.log('');
|
||||
logger.info('SNMP Version:');
|
||||
logger.dim(' 1) SNMPv1');
|
||||
logger.dim(' 2) SNMPv2c');
|
||||
logger.dim(' 3) SNMPv3 (with security features)');
|
||||
const versionInput = await prompt(`Select SNMP version [${defaultVersion}]: `);
|
||||
const version = parseInt(versionInput, 10);
|
||||
snmpConfig.version = versionInput.trim() && (version === 1 || version === 2 || version === 3)
|
||||
@@ -697,13 +700,15 @@ export class UpsHandler {
|
||||
snmpConfig: any,
|
||||
prompt: (question: string) => Promise<string>,
|
||||
): Promise<void> {
|
||||
console.log('\nSNMPv3 Security Settings:');
|
||||
logger.log('');
|
||||
logger.info('SNMPv3 Security Settings:');
|
||||
|
||||
// Security Level
|
||||
console.log('\nSecurity Level:');
|
||||
console.log(' 1) noAuthNoPriv (No Authentication, No Privacy)');
|
||||
console.log(' 2) authNoPriv (Authentication, No Privacy)');
|
||||
console.log(' 3) authPriv (Authentication and Privacy)');
|
||||
logger.log('');
|
||||
logger.info('Security Level:');
|
||||
logger.dim(' 1) noAuthNoPriv (No Authentication, No Privacy)');
|
||||
logger.dim(' 2) authNoPriv (Authentication, No Privacy)');
|
||||
logger.dim(' 3) authPriv (Authentication and Privacy)');
|
||||
const defaultSecLevel = snmpConfig.securityLevel
|
||||
? snmpConfig.securityLevel === 'noAuthNoPriv'
|
||||
? 1
|
||||
@@ -752,8 +757,9 @@ export class UpsHandler {
|
||||
|
||||
// Allow customizing the timeout value
|
||||
const defaultTimeout = snmpConfig.timeout / 1000; // Convert from ms to seconds for display
|
||||
console.log(
|
||||
'\nSNMPv3 operations with authentication and privacy may require longer timeouts.',
|
||||
logger.log('');
|
||||
logger.info(
|
||||
'SNMPv3 operations with authentication and privacy may require longer timeouts.',
|
||||
);
|
||||
const timeoutInput = await prompt(`SNMP Timeout in seconds [${defaultTimeout}]: `);
|
||||
const timeout = parseInt(timeoutInput, 10);
|
||||
@@ -773,9 +779,10 @@ export class UpsHandler {
|
||||
prompt: (question: string) => Promise<string>,
|
||||
): Promise<void> {
|
||||
// Authentication protocol
|
||||
console.log('\nAuthentication Protocol:');
|
||||
console.log(' 1) MD5');
|
||||
console.log(' 2) SHA');
|
||||
logger.log('');
|
||||
logger.info('Authentication Protocol:');
|
||||
logger.dim(' 1) MD5');
|
||||
logger.dim(' 2) SHA');
|
||||
const defaultAuthProtocol = snmpConfig.authProtocol === 'SHA' ? 2 : 1;
|
||||
const authProtocolInput = await prompt(
|
||||
`Select Authentication Protocol [${defaultAuthProtocol}]: `,
|
||||
@@ -799,9 +806,10 @@ export class UpsHandler {
|
||||
prompt: (question: string) => Promise<string>,
|
||||
): Promise<void> {
|
||||
// Privacy protocol
|
||||
console.log('\nPrivacy Protocol:');
|
||||
console.log(' 1) DES');
|
||||
console.log(' 2) AES');
|
||||
logger.log('');
|
||||
logger.info('Privacy Protocol:');
|
||||
logger.dim(' 1) DES');
|
||||
logger.dim(' 2) AES');
|
||||
const defaultPrivProtocol = snmpConfig.privProtocol === 'AES' ? 2 : 1;
|
||||
const privProtocolInput = await prompt(`Select Privacy Protocol [${defaultPrivProtocol}]: `);
|
||||
const privProtocol = parseInt(privProtocolInput, 10) || defaultPrivProtocol;
|
||||
@@ -822,7 +830,8 @@ export class UpsHandler {
|
||||
thresholds: any,
|
||||
prompt: (question: string) => Promise<string>,
|
||||
): Promise<void> {
|
||||
console.log('\nShutdown Thresholds:');
|
||||
logger.log('');
|
||||
logger.info('Shutdown Thresholds:');
|
||||
|
||||
// Battery threshold
|
||||
const defaultBatteryThreshold = thresholds.battery || 60;
|
||||
@@ -854,13 +863,14 @@ export class UpsHandler {
|
||||
snmpConfig: any,
|
||||
prompt: (question: string) => Promise<string>,
|
||||
): Promise<void> {
|
||||
console.log('\nUPS Model Selection:');
|
||||
console.log(' 1) CyberPower');
|
||||
console.log(' 2) APC');
|
||||
console.log(' 3) Eaton');
|
||||
console.log(' 4) TrippLite');
|
||||
console.log(' 5) Liebert/Vertiv');
|
||||
console.log(' 6) Custom (Advanced)');
|
||||
logger.log('');
|
||||
logger.info('UPS Model Selection:');
|
||||
logger.dim(' 1) CyberPower');
|
||||
logger.dim(' 2) APC');
|
||||
logger.dim(' 3) Eaton');
|
||||
logger.dim(' 4) TrippLite');
|
||||
logger.dim(' 5) Liebert/Vertiv');
|
||||
logger.dim(' 6) Custom (Advanced)');
|
||||
|
||||
const defaultModelValue = snmpConfig.upsModel === 'cyberpower'
|
||||
? 1
|
||||
@@ -891,8 +901,9 @@ export class UpsHandler {
|
||||
snmpConfig.upsModel = 'liebert';
|
||||
} else if (modelValue === 6) {
|
||||
snmpConfig.upsModel = 'custom';
|
||||
console.log('\nEnter custom OIDs for your UPS:');
|
||||
console.log('(Leave blank to use standard RFC 1628 OIDs as fallback)');
|
||||
logger.log('');
|
||||
logger.info('Enter custom OIDs for your UPS:');
|
||||
logger.dim('(Leave blank to use standard RFC 1628 OIDs as fallback)');
|
||||
|
||||
// Custom OIDs
|
||||
const powerStatusOID = await prompt('Power Status OID: ');
|
||||
|
213
ts/daemon.ts
213
ts/daemon.ts
@@ -5,8 +5,9 @@ import { exec, execFile } from 'node:child_process';
|
||||
import { promisify } from 'node:util';
|
||||
import { NupstSnmp } from './snmp/manager.ts';
|
||||
import type { ISnmpConfig } from './snmp/types.ts';
|
||||
import { logger } from './logger.ts';
|
||||
import { logger, type ITableColumn } from './logger.ts';
|
||||
import { MigrationRunner } from './migrations/index.ts';
|
||||
import { theme, symbols, getBatteryColor, getRuntimeColor, formatPowerStatus } from './colors.ts';
|
||||
|
||||
const execAsync = promisify(exec);
|
||||
const execFileAsync = promisify(execFile);
|
||||
@@ -207,11 +208,9 @@ export class NupstDaemon {
|
||||
fs.writeFileSync(this.CONFIG_PATH, JSON.stringify(configToSave, null, 2));
|
||||
this.config = configToSave;
|
||||
|
||||
console.log('┌─ Configuration Saved ─────────────────────┐');
|
||||
console.log(`│ Location: ${this.CONFIG_PATH}`);
|
||||
console.log('└──────────────────────────────────────────┘');
|
||||
logger.logBox('Configuration Saved', [`Location: ${this.CONFIG_PATH}`], 45, 'success');
|
||||
} catch (error) {
|
||||
console.error('Error saving configuration:', error);
|
||||
logger.error(`Error saving configuration: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -219,10 +218,7 @@ export class NupstDaemon {
|
||||
* Helper method to log configuration errors consistently
|
||||
*/
|
||||
private logConfigError(message: string): void {
|
||||
console.error('┌─ Configuration Error ─────────────────────┐');
|
||||
console.error(`│ ${message}`);
|
||||
console.error("│ Please run 'nupst setup' first to create a configuration.");
|
||||
console.error('└───────────────────────────────────────────┘');
|
||||
logger.logBox('Configuration Error', [message, "Please run 'nupst setup' first to create a configuration."], 45, 'error');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -315,29 +311,57 @@ export class NupstDaemon {
|
||||
* Log the loaded configuration settings
|
||||
*/
|
||||
private logConfigLoaded(): void {
|
||||
const boxWidth = 50;
|
||||
logger.logBoxTitle('Configuration Loaded', boxWidth);
|
||||
|
||||
if (this.config.upsDevices && this.config.upsDevices.length > 0) {
|
||||
logger.logBoxLine(`UPS Devices: ${this.config.upsDevices.length}`);
|
||||
for (const ups of this.config.upsDevices) {
|
||||
logger.logBoxLine(` - ${ups.name} (${ups.id}): ${ups.snmp.host}:${ups.snmp.port}`);
|
||||
}
|
||||
} else {
|
||||
logger.logBoxLine('No UPS devices configured');
|
||||
}
|
||||
|
||||
if (this.config.groups && this.config.groups.length > 0) {
|
||||
logger.logBoxLine(`Groups: ${this.config.groups.length}`);
|
||||
for (const group of this.config.groups) {
|
||||
logger.logBoxLine(` - ${group.name} (${group.id}): ${group.mode} mode`);
|
||||
}
|
||||
} else {
|
||||
logger.logBoxLine('No Groups configured');
|
||||
}
|
||||
|
||||
logger.log('');
|
||||
logger.logBoxTitle('Configuration Loaded', 70, 'success');
|
||||
logger.logBoxLine(`Check Interval: ${this.config.checkInterval / 1000} seconds`);
|
||||
logger.logBoxEnd();
|
||||
logger.log('');
|
||||
|
||||
// Display UPS devices in a table
|
||||
if (this.config.upsDevices && this.config.upsDevices.length > 0) {
|
||||
logger.info(`UPS Devices (${this.config.upsDevices.length}):`);
|
||||
|
||||
const upsColumns: Array<{ header: string; key: string; align?: 'left' | 'right'; color?: (val: string) => string }> = [
|
||||
{ header: 'Name', key: 'name', align: 'left', color: theme.highlight },
|
||||
{ header: 'ID', key: 'id', align: 'left', color: theme.dim },
|
||||
{ header: 'Host:Port', key: 'host', align: 'left', color: theme.info },
|
||||
{ header: 'Battery/Runtime', key: 'thresholds', align: 'left' },
|
||||
];
|
||||
|
||||
const upsRows: Array<Record<string, string>> = this.config.upsDevices.map((ups) => ({
|
||||
name: ups.name,
|
||||
id: ups.id,
|
||||
host: `${ups.snmp.host}:${ups.snmp.port}`,
|
||||
thresholds: `${ups.thresholds.battery}% / ${ups.thresholds.runtime} min`,
|
||||
}));
|
||||
|
||||
logger.logTable(upsColumns, upsRows);
|
||||
logger.log('');
|
||||
} else {
|
||||
logger.warn('No UPS devices configured');
|
||||
logger.log('');
|
||||
}
|
||||
|
||||
// Display groups in a table
|
||||
if (this.config.groups && this.config.groups.length > 0) {
|
||||
logger.info(`Groups (${this.config.groups.length}):`);
|
||||
|
||||
const groupColumns: Array<{ header: string; key: string; align?: 'left' | 'right'; color?: (val: string) => string }> = [
|
||||
{ header: 'Name', key: 'name', align: 'left', color: theme.highlight },
|
||||
{ header: 'ID', key: 'id', align: 'left', color: theme.dim },
|
||||
{ header: 'Mode', key: 'mode', align: 'left', color: theme.info },
|
||||
];
|
||||
|
||||
const groupRows: Array<Record<string, string>> = this.config.groups.map((group) => ({
|
||||
name: group.name,
|
||||
id: group.id,
|
||||
mode: group.mode,
|
||||
}));
|
||||
|
||||
logger.logTable(groupColumns, groupRows);
|
||||
logger.log('');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -433,9 +457,13 @@ export class NupstDaemon {
|
||||
|
||||
// Check if power status changed
|
||||
if (currentStatus && currentStatus.powerStatus !== status.powerStatus) {
|
||||
logger.logBoxTitle(`Power Status Change: ${ups.name}`, 50);
|
||||
logger.logBoxLine(`Status changed: ${currentStatus.powerStatus} → ${status.powerStatus}`);
|
||||
logger.log('');
|
||||
logger.logBoxTitle(`Power Status Change: ${ups.name}`, 60, 'warning');
|
||||
logger.logBoxLine(`Previous: ${formatPowerStatus(currentStatus.powerStatus)}`);
|
||||
logger.logBoxLine(`Current: ${formatPowerStatus(status.powerStatus)}`);
|
||||
logger.logBoxLine(`Time: ${new Date().toISOString()}`);
|
||||
logger.logBoxEnd();
|
||||
logger.log('');
|
||||
|
||||
updatedStatus.lastStatusChange = currentTime;
|
||||
}
|
||||
@@ -457,21 +485,38 @@ export class NupstDaemon {
|
||||
*/
|
||||
private logAllUpsStatus(): void {
|
||||
const timestamp = new Date().toISOString();
|
||||
const boxWidth = 60;
|
||||
logger.logBoxTitle('Periodic Status Update', boxWidth);
|
||||
logger.logBoxLine(`Timestamp: ${timestamp}`);
|
||||
logger.logBoxLine('');
|
||||
|
||||
logger.log('');
|
||||
logger.logBoxTitle('Periodic Status Update', 70, 'info');
|
||||
logger.logBoxLine(`Timestamp: ${timestamp}`);
|
||||
logger.logBoxEnd();
|
||||
logger.log('');
|
||||
|
||||
// Build table data
|
||||
const columns: Array<{ header: string; key: string; align?: 'left' | 'right'; color?: (val: string) => string }> = [
|
||||
{ header: 'UPS Name', key: 'name', align: 'left', color: theme.highlight },
|
||||
{ header: 'ID', key: 'id', align: 'left', color: theme.dim },
|
||||
{ header: 'Power Status', key: 'powerStatus', align: 'left' },
|
||||
{ header: 'Battery', key: 'battery', align: 'right' },
|
||||
{ header: 'Runtime', key: 'runtime', align: 'right' },
|
||||
];
|
||||
|
||||
const rows: Array<Record<string, string>> = [];
|
||||
for (const [id, status] of this.upsStatus.entries()) {
|
||||
logger.logBoxLine(`UPS: ${status.name} (${id})`);
|
||||
logger.logBoxLine(` Power Status: ${status.powerStatus}`);
|
||||
logger.logBoxLine(
|
||||
` Battery: ${status.batteryCapacity}% | Runtime: ${status.batteryRuntime} min`,
|
||||
);
|
||||
logger.logBoxLine('');
|
||||
const batteryColor = getBatteryColor(status.batteryCapacity);
|
||||
const runtimeColor = getRuntimeColor(status.batteryRuntime);
|
||||
|
||||
rows.push({
|
||||
name: status.name,
|
||||
id: id,
|
||||
powerStatus: formatPowerStatus(status.powerStatus),
|
||||
battery: batteryColor(status.batteryCapacity + '%'),
|
||||
runtime: runtimeColor(status.batteryRuntime + ' min'),
|
||||
});
|
||||
}
|
||||
|
||||
logger.logBoxEnd();
|
||||
logger.logTable(columns, rows);
|
||||
logger.log('');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -750,38 +795,61 @@ export class NupstDaemon {
|
||||
const MAX_MONITORING_TIME = 5 * 60 * 1000; // Max 5 minutes of monitoring
|
||||
const startTime = Date.now();
|
||||
|
||||
logger.log(
|
||||
`Emergency shutdown threshold: ${EMERGENCY_RUNTIME_THRESHOLD} minutes remaining battery runtime`,
|
||||
);
|
||||
logger.log('');
|
||||
logger.logBoxTitle('Shutdown Monitoring Active', 60, 'warning');
|
||||
logger.logBoxLine(`Emergency threshold: ${EMERGENCY_RUNTIME_THRESHOLD} minutes runtime`);
|
||||
logger.logBoxLine(`Check interval: ${CHECK_INTERVAL / 1000} seconds`);
|
||||
logger.logBoxLine(`Max monitoring time: ${MAX_MONITORING_TIME / 1000} seconds`);
|
||||
logger.logBoxEnd();
|
||||
logger.log('');
|
||||
|
||||
// Continue monitoring until max monitoring time is reached
|
||||
while (Date.now() - startTime < MAX_MONITORING_TIME) {
|
||||
try {
|
||||
logger.log('Checking UPS status during shutdown...');
|
||||
logger.info('Checking UPS status during shutdown...');
|
||||
|
||||
// Build table for UPS status during shutdown
|
||||
const columns: Array<{ header: string; key: string; align?: 'left' | 'right'; color?: (val: string) => string }> = [
|
||||
{ header: 'UPS Name', key: 'name', align: 'left', color: theme.highlight },
|
||||
{ header: 'Battery', key: 'battery', align: 'right' },
|
||||
{ header: 'Runtime', key: 'runtime', align: 'right' },
|
||||
{ header: 'Status', key: 'status', align: 'left' },
|
||||
];
|
||||
|
||||
const rows: Array<Record<string, string>> = [];
|
||||
let emergencyDetected = false;
|
||||
let emergencyUps: any = null;
|
||||
|
||||
// Check all UPS devices
|
||||
for (const ups of this.config.upsDevices) {
|
||||
try {
|
||||
const status = await this.snmp.getUpsStatus(ups.snmp);
|
||||
|
||||
logger.log(
|
||||
`UPS ${ups.name}: Battery ${status.batteryCapacity}%, Runtime: ${status.batteryRuntime} minutes`,
|
||||
);
|
||||
const batteryColor = getBatteryColor(status.batteryCapacity);
|
||||
const runtimeColor = getRuntimeColor(status.batteryRuntime);
|
||||
|
||||
// If any UPS battery runtime gets critically low, force immediate shutdown
|
||||
if (status.batteryRuntime < EMERGENCY_RUNTIME_THRESHOLD) {
|
||||
logger.logBoxTitle('EMERGENCY SHUTDOWN', 50);
|
||||
logger.logBoxLine(
|
||||
`UPS ${ups.name} runtime critically low: ${status.batteryRuntime} minutes`,
|
||||
);
|
||||
logger.logBoxLine('Forcing immediate shutdown!');
|
||||
logger.logBoxEnd();
|
||||
const isCritical = status.batteryRuntime < EMERGENCY_RUNTIME_THRESHOLD;
|
||||
|
||||
// Force immediate shutdown
|
||||
await this.forceImmediateShutdown();
|
||||
return;
|
||||
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 };
|
||||
}
|
||||
} catch (upsError) {
|
||||
rows.push({
|
||||
name: ups.name,
|
||||
battery: theme.error('N/A'),
|
||||
runtime: theme.error('N/A'),
|
||||
status: theme.error('ERROR'),
|
||||
});
|
||||
|
||||
logger.error(
|
||||
`Error checking UPS ${ups.name} during shutdown: ${
|
||||
upsError instanceof Error ? upsError.message : String(upsError)
|
||||
@@ -790,6 +858,27 @@ export class NupstDaemon {
|
||||
}
|
||||
}
|
||||
|
||||
// Display the table
|
||||
logger.logTable(columns, rows);
|
||||
logger.log('');
|
||||
|
||||
// If emergency detected, trigger immediate shutdown
|
||||
if (emergencyDetected && emergencyUps) {
|
||||
logger.log('');
|
||||
logger.logBoxTitle('EMERGENCY SHUTDOWN', 60, 'error');
|
||||
logger.logBoxLine(
|
||||
`UPS ${emergencyUps.ups.name} runtime critically low: ${emergencyUps.status.batteryRuntime} minutes`,
|
||||
);
|
||||
logger.logBoxLine(`Emergency threshold: ${EMERGENCY_RUNTIME_THRESHOLD} minutes`);
|
||||
logger.logBoxLine('Forcing immediate shutdown!');
|
||||
logger.logBoxEnd();
|
||||
logger.log('');
|
||||
|
||||
// Force immediate shutdown
|
||||
await this.forceImmediateShutdown();
|
||||
return;
|
||||
}
|
||||
|
||||
// Wait before checking again
|
||||
await this.sleep(CHECK_INTERVAL);
|
||||
} catch (error) {
|
||||
@@ -802,7 +891,9 @@ export class NupstDaemon {
|
||||
}
|
||||
}
|
||||
|
||||
logger.log('UPS monitoring during shutdown completed');
|
||||
logger.log('');
|
||||
logger.success('UPS monitoring during shutdown completed');
|
||||
logger.log('');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -34,12 +34,14 @@ export class MigrationRunner {
|
||||
let currentConfig = config;
|
||||
let anyMigrationsRan = false;
|
||||
|
||||
logger.dim('Checking for required config migrations...');
|
||||
|
||||
for (const migration of this.migrations) {
|
||||
const shouldRun = await migration.shouldRun(currentConfig);
|
||||
|
||||
if (shouldRun) {
|
||||
// Only show "checking" message when we actually need to migrate
|
||||
if (!anyMigrationsRan) {
|
||||
logger.dim('Checking for required config migrations...');
|
||||
}
|
||||
logger.info(`Running ${migration.getName()}...`);
|
||||
currentConfig = await migration.migrate(currentConfig);
|
||||
anyMigrationsRan = true;
|
||||
@@ -49,7 +51,7 @@ export class MigrationRunner {
|
||||
if (anyMigrationsRan) {
|
||||
logger.success('Configuration migrations complete');
|
||||
} else {
|
||||
logger.dim('No migrations needed');
|
||||
logger.success('config format ok');
|
||||
}
|
||||
|
||||
return {
|
||||
|
@@ -4,19 +4,38 @@ import { logger } from '../logger.ts';
|
||||
/**
|
||||
* Migration from v3 (upsList) to v4 (upsDevices)
|
||||
*
|
||||
* Detects v3 format:
|
||||
* Transforms v3 format with flat SNMP config:
|
||||
* {
|
||||
* upsList: [ ... ],
|
||||
* groups: [ ... ],
|
||||
* checkInterval: 30000
|
||||
* upsList: [
|
||||
* {
|
||||
* id: "ups-1",
|
||||
* name: "UPS 1",
|
||||
* host: "192.168.1.1",
|
||||
* port: 161,
|
||||
* community: "public",
|
||||
* version: "1" // string
|
||||
* }
|
||||
* ]
|
||||
* }
|
||||
*
|
||||
* Converts to:
|
||||
* To v4 format with nested SNMP config:
|
||||
* {
|
||||
* version: "4.0",
|
||||
* upsDevices: [ ... ], // renamed from upsList
|
||||
* groups: [ ... ],
|
||||
* checkInterval: 30000
|
||||
* upsDevices: [
|
||||
* {
|
||||
* id: "ups-1",
|
||||
* name: "UPS 1",
|
||||
* snmp: {
|
||||
* host: "192.168.1.1",
|
||||
* port: 161,
|
||||
* community: "public",
|
||||
* version: 1, // number
|
||||
* timeout: 5000
|
||||
* },
|
||||
* thresholds: { battery: 60, runtime: 20 },
|
||||
* groups: []
|
||||
* }
|
||||
* ]
|
||||
* }
|
||||
*/
|
||||
export class MigrationV3ToV4 extends BaseMigration {
|
||||
@@ -25,21 +44,76 @@ export class MigrationV3ToV4 extends BaseMigration {
|
||||
readonly toVersion = '4.0';
|
||||
|
||||
async shouldRun(config: any): Promise<boolean> {
|
||||
// V3 format has upsList instead of upsDevices
|
||||
return !!config.upsList && !config.upsDevices;
|
||||
// V3 format has upsList OR has upsDevices with flat structure (host at top level)
|
||||
if (config.upsList && !config.upsDevices) {
|
||||
return true; // Classic v3 with upsList
|
||||
}
|
||||
|
||||
// Check if upsDevices exists but has flat structure (v3 format)
|
||||
if (config.upsDevices && config.upsDevices.length > 0) {
|
||||
const firstDevice = config.upsDevices[0];
|
||||
// V3 has host at top level, v4 has it nested in snmp object
|
||||
return !!firstDevice.host && !firstDevice.snmp;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
async migrate(config: any): Promise<any> {
|
||||
logger.info(`${this.getName()}: Renaming upsList to upsDevices...`);
|
||||
logger.info(`${this.getName()}: Migrating v3 config to v4 format...`);
|
||||
logger.dim(` - Restructuring UPS devices (flat → nested snmp config)`);
|
||||
|
||||
// Get devices from either upsList or upsDevices (for partially migrated configs)
|
||||
const sourceDevices = config.upsList || config.upsDevices;
|
||||
|
||||
// Transform each UPS device from v3 flat structure to v4 nested structure
|
||||
const transformedDevices = sourceDevices.map((device: any) => {
|
||||
// Build SNMP config object
|
||||
const snmpConfig: any = {
|
||||
host: device.host,
|
||||
port: device.port || 161,
|
||||
version: typeof device.version === 'string' ? parseInt(device.version, 10) : device.version,
|
||||
timeout: device.timeout || 5000,
|
||||
};
|
||||
|
||||
// Add SNMPv1/v2c fields
|
||||
if (device.community) {
|
||||
snmpConfig.community = device.community;
|
||||
}
|
||||
|
||||
// Add SNMPv3 fields
|
||||
if (device.securityLevel) snmpConfig.securityLevel = device.securityLevel;
|
||||
if (device.username) snmpConfig.username = device.username;
|
||||
if (device.authProtocol) snmpConfig.authProtocol = device.authProtocol;
|
||||
if (device.authKey) snmpConfig.authKey = device.authKey;
|
||||
if (device.privProtocol) snmpConfig.privProtocol = device.privProtocol;
|
||||
if (device.privKey) snmpConfig.privKey = device.privKey;
|
||||
|
||||
// Add UPS model if present
|
||||
if (device.upsModel) snmpConfig.upsModel = device.upsModel;
|
||||
if (device.customOIDs) snmpConfig.customOIDs = device.customOIDs;
|
||||
|
||||
// Return v4 format with nested structure
|
||||
return {
|
||||
id: device.id,
|
||||
name: device.name,
|
||||
snmp: snmpConfig,
|
||||
thresholds: device.thresholds || {
|
||||
battery: 60,
|
||||
runtime: 20,
|
||||
},
|
||||
groups: device.groups || [],
|
||||
};
|
||||
});
|
||||
|
||||
const migrated = {
|
||||
version: this.toVersion,
|
||||
upsDevices: config.upsList, // Rename upsList → upsDevices
|
||||
upsDevices: transformedDevices,
|
||||
groups: config.groups || [],
|
||||
checkInterval: config.checkInterval || 30000,
|
||||
};
|
||||
|
||||
logger.success(`${this.getName()}: Migration complete`);
|
||||
logger.success(`${this.getName()}: Migration complete (${transformedDevices.length} devices transformed)`);
|
||||
return migrated;
|
||||
}
|
||||
}
|
||||
|
@@ -525,6 +525,7 @@ export class NupstSnmp {
|
||||
|
||||
/**
|
||||
* Determine power status based on UPS model and raw value
|
||||
* Uses the value mappings defined in the OID sets
|
||||
* @param upsModel UPS model
|
||||
* @param powerStatusValue Raw power status value
|
||||
* @returns Standardized power status
|
||||
@@ -533,38 +534,27 @@ export class NupstSnmp {
|
||||
upsModel: TUpsModel | undefined,
|
||||
powerStatusValue: number,
|
||||
): 'online' | 'onBattery' | 'unknown' {
|
||||
if (upsModel === 'cyberpower') {
|
||||
// CyberPower RMCARD205: upsBaseOutputStatus values
|
||||
// 2=onLine, 3=onBattery, 4=onBoost, 5=onSleep, 6=off, etc.
|
||||
if (powerStatusValue === 2) {
|
||||
// Get the OID set for this UPS model
|
||||
if (upsModel && upsModel !== 'custom') {
|
||||
const oidSet = UpsOidSets.getOidSet(upsModel);
|
||||
|
||||
// Use the value mappings if available
|
||||
if (oidSet.POWER_STATUS_VALUES) {
|
||||
if (powerStatusValue === oidSet.POWER_STATUS_VALUES.online) {
|
||||
return 'online';
|
||||
} else if (powerStatusValue === 3) {
|
||||
} else if (powerStatusValue === oidSet.POWER_STATUS_VALUES.onBattery) {
|
||||
return 'onBattery';
|
||||
}
|
||||
} else if (upsModel === 'eaton') {
|
||||
// Eaton UPS: xupsOutputSource values
|
||||
// 3=normal/mains, 5=battery, etc.
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback for custom or undefined models (RFC 1628 standard)
|
||||
// upsOutputSource: 3=normal (mains), 5=battery
|
||||
if (powerStatusValue === 3) {
|
||||
return 'online';
|
||||
} else if (powerStatusValue === 5) {
|
||||
return 'onBattery';
|
||||
}
|
||||
} else if (upsModel === 'apc') {
|
||||
// APC UPS: upsBasicOutputStatus values
|
||||
// 2=online, 3=onBattery, etc.
|
||||
if (powerStatusValue === 2) {
|
||||
return 'online';
|
||||
} else if (powerStatusValue === 3) {
|
||||
return 'onBattery';
|
||||
}
|
||||
} else {
|
||||
// Default interpretation for other UPS models
|
||||
if (powerStatusValue === 1) {
|
||||
return 'online';
|
||||
} else if (powerStatusValue === 2) {
|
||||
return 'onBattery';
|
||||
}
|
||||
}
|
||||
|
||||
return 'unknown';
|
||||
}
|
||||
|
@@ -11,37 +11,57 @@ export class UpsOidSets {
|
||||
private static readonly UPS_OID_SETS: Record<TUpsModel, IOidSet> = {
|
||||
// Cyberpower OIDs for RMCARD205 (based on CyberPower_MIB_v2.11)
|
||||
cyberpower: {
|
||||
POWER_STATUS: '1.3.6.1.4.1.3808.1.1.1.4.1.1.0', // upsBaseOutputStatus (2=online, 3=on battery)
|
||||
POWER_STATUS: '1.3.6.1.4.1.3808.1.1.1.4.1.1.0', // upsBaseOutputStatus
|
||||
BATTERY_CAPACITY: '1.3.6.1.4.1.3808.1.1.1.2.2.1.0', // upsAdvanceBatteryCapacity (percentage)
|
||||
BATTERY_RUNTIME: '1.3.6.1.4.1.3808.1.1.1.2.2.4.0', // upsAdvanceBatteryRunTimeRemaining (TimeTicks)
|
||||
POWER_STATUS_VALUES: {
|
||||
online: 2, // upsBaseOutputStatus: 2=onLine
|
||||
onBattery: 3, // upsBaseOutputStatus: 3=onBattery
|
||||
},
|
||||
},
|
||||
|
||||
// APC OIDs
|
||||
apc: {
|
||||
POWER_STATUS: '1.3.6.1.4.1.318.1.1.1.4.1.1.0', // Power status (1=online, 2=on battery)
|
||||
POWER_STATUS: '1.3.6.1.4.1.318.1.1.1.4.1.1.0', // upsBasicOutputStatus
|
||||
BATTERY_CAPACITY: '1.3.6.1.4.1.318.1.1.1.2.2.1.0', // Battery capacity in percentage
|
||||
BATTERY_RUNTIME: '1.3.6.1.4.1.318.1.1.1.2.2.3.0', // Remaining runtime in minutes
|
||||
POWER_STATUS_VALUES: {
|
||||
online: 2, // upsBasicOutputStatus: 2=onLine
|
||||
onBattery: 3, // upsBasicOutputStatus: 3=onBattery
|
||||
},
|
||||
},
|
||||
|
||||
// Eaton OIDs
|
||||
eaton: {
|
||||
POWER_STATUS: '1.3.6.1.4.1.534.1.4.4.0', // xupsOutputSource (3=normal/mains, 5=battery)
|
||||
POWER_STATUS: '1.3.6.1.4.1.534.1.4.4.0', // xupsOutputSource
|
||||
BATTERY_CAPACITY: '1.3.6.1.4.1.534.1.2.4.0', // xupsBatCapacity (percentage)
|
||||
BATTERY_RUNTIME: '1.3.6.1.4.1.534.1.2.1.0', // xupsBatTimeRemaining (seconds)
|
||||
POWER_STATUS_VALUES: {
|
||||
online: 3, // xupsOutputSource: 3=normal (mains power)
|
||||
onBattery: 5, // xupsOutputSource: 5=battery
|
||||
},
|
||||
},
|
||||
|
||||
// TrippLite OIDs
|
||||
tripplite: {
|
||||
POWER_STATUS: '1.3.6.1.4.1.850.1.1.3.1.1.1.0', // Power status
|
||||
POWER_STATUS: '1.3.6.1.4.1.850.1.1.3.1.1.1.0', // tlUpsOutputSource
|
||||
BATTERY_CAPACITY: '1.3.6.1.4.1.850.1.1.3.2.4.1.0', // Battery capacity in percentage
|
||||
BATTERY_RUNTIME: '1.3.6.1.4.1.850.1.1.3.2.2.1.0', // Remaining runtime in minutes
|
||||
POWER_STATUS_VALUES: {
|
||||
online: 2, // tlUpsOutputSource: 2=normal (mains power)
|
||||
onBattery: 3, // tlUpsOutputSource: 3=onBattery
|
||||
},
|
||||
},
|
||||
|
||||
// Liebert/Vertiv OIDs
|
||||
liebert: {
|
||||
POWER_STATUS: '1.3.6.1.4.1.476.1.42.3.9.20.1.20.1.2.1.2.1', // Power status
|
||||
POWER_STATUS: '1.3.6.1.4.1.476.1.42.3.9.20.1.20.1.2.1.2.1', // lgpPwrOutputSource
|
||||
BATTERY_CAPACITY: '1.3.6.1.4.1.476.1.42.3.9.20.1.20.1.2.1.4.1', // Battery capacity in percentage
|
||||
BATTERY_RUNTIME: '1.3.6.1.4.1.476.1.42.3.9.20.1.20.1.2.1.5.1', // Remaining runtime in minutes
|
||||
POWER_STATUS_VALUES: {
|
||||
online: 2, // lgpPwrOutputSource: 2=normal (mains power)
|
||||
onBattery: 3, // lgpPwrOutputSource: 3=onBattery
|
||||
},
|
||||
},
|
||||
|
||||
// Custom OIDs (to be provided by the user)
|
||||
|
@@ -28,6 +28,13 @@ export interface IOidSet {
|
||||
BATTERY_CAPACITY: string;
|
||||
/** OID for battery runtime */
|
||||
BATTERY_RUNTIME: string;
|
||||
/** Power status value mappings */
|
||||
POWER_STATUS_VALUES?: {
|
||||
/** SNMP value that indicates UPS is online (on AC power) */
|
||||
online: number;
|
||||
/** SNMP value that indicates UPS is on battery */
|
||||
onBattery: number;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
210
ts/systemd.ts
210
ts/systemd.ts
@@ -3,6 +3,7 @@ import { promises as fs } from 'node:fs';
|
||||
import { execSync } from 'node:child_process';
|
||||
import { NupstDaemon } from './daemon.ts';
|
||||
import { logger } from './logger.ts';
|
||||
import { theme, symbols, getBatteryColor, getRuntimeColor, formatPowerStatus } from './colors.ts';
|
||||
|
||||
/**
|
||||
* Class for managing systemd service
|
||||
@@ -49,11 +50,11 @@ WantedBy=multi-user.target
|
||||
try {
|
||||
await fs.access(configPath);
|
||||
} catch (error) {
|
||||
const boxWidth = 50;
|
||||
logger.logBoxTitle('Configuration Error', boxWidth);
|
||||
logger.logBoxLine(`No configuration file found at ${configPath}`);
|
||||
logger.logBoxLine("Please run 'nupst add' first to create a UPS configuration.");
|
||||
logger.logBoxEnd();
|
||||
logger.log('');
|
||||
logger.error('No configuration found');
|
||||
logger.log(` ${theme.dim('Config file:')} ${configPath}`);
|
||||
logger.log(` ${theme.dim('Run')} ${theme.command('nupst ups add')} ${theme.dim('to create a configuration')}`);
|
||||
logger.log('');
|
||||
throw new Error('Configuration not found');
|
||||
}
|
||||
}
|
||||
@@ -133,21 +134,59 @@ WantedBy=multi-user.target
|
||||
* Get status of the systemd service and UPS
|
||||
* @param debugMode Whether to enable debug mode for SNMP
|
||||
*/
|
||||
/**
|
||||
* Display version information and update status
|
||||
* @private
|
||||
*/
|
||||
private async displayVersionInfo(): Promise<void> {
|
||||
try {
|
||||
const nupst = this.daemon.getNupstSnmp().getNupst();
|
||||
const version = nupst.getVersion();
|
||||
|
||||
// Check for updates
|
||||
const updateAvailable = await nupst.checkForUpdates();
|
||||
|
||||
// Display version info
|
||||
if (updateAvailable) {
|
||||
const updateStatus = nupst.getUpdateStatus();
|
||||
logger.log('');
|
||||
logger.log(
|
||||
`${theme.dim('NUPST')} ${theme.dim('v' + version)} ${symbols.warning} ${theme.statusWarning(`Update available: v${updateStatus.latestVersion}`)}`,
|
||||
);
|
||||
logger.log(` ${theme.dim('Run')} ${theme.command('sudo nupst update')} ${theme.dim('to upgrade')}`);
|
||||
} else {
|
||||
logger.log('');
|
||||
logger.log(
|
||||
`${theme.dim('NUPST')} ${theme.dim('v' + version)} ${symbols.success} ${theme.success('Up to date')}`,
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
// If version check fails, show at least the current version
|
||||
try {
|
||||
const nupst = this.daemon.getNupstSnmp().getNupst();
|
||||
const version = nupst.getVersion();
|
||||
logger.log('');
|
||||
logger.log(`${theme.dim('NUPST')} ${theme.dim('v' + version)}`);
|
||||
} catch (_innerError) {
|
||||
// Silently fail if we can't even get the version
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async getStatus(debugMode: boolean = false): Promise<void> {
|
||||
try {
|
||||
// Enable debug mode if requested
|
||||
if (debugMode) {
|
||||
const boxWidth = 45;
|
||||
logger.logBoxTitle('Debug Mode', boxWidth);
|
||||
logger.logBoxLine('SNMP debugging enabled - detailed logs will be shown');
|
||||
logger.logBoxEnd();
|
||||
console.log('');
|
||||
logger.info('Debug Mode: SNMP debugging enabled');
|
||||
console.log('');
|
||||
this.daemon.getNupstSnmp().enableDebug();
|
||||
}
|
||||
|
||||
// Display version information
|
||||
this.daemon.getNupstSnmp().getNupst().logVersionInfo();
|
||||
// Display version and update status first
|
||||
await this.displayVersionInfo();
|
||||
|
||||
// Check if config exists first
|
||||
// Check if config exists
|
||||
try {
|
||||
await this.checkConfigExists();
|
||||
} catch (error) {
|
||||
@@ -171,18 +210,50 @@ WantedBy=multi-user.target
|
||||
private displayServiceStatus(): void {
|
||||
try {
|
||||
const serviceStatus = execSync('systemctl status nupst.service').toString();
|
||||
const boxWidth = 45;
|
||||
logger.logBoxTitle('Service Status', boxWidth);
|
||||
// Process each line of the status output
|
||||
serviceStatus.split('\n').forEach((line) => {
|
||||
logger.logBoxLine(line);
|
||||
});
|
||||
logger.logBoxEnd();
|
||||
const lines = serviceStatus.split('\n');
|
||||
|
||||
// Parse key information from systemctl output
|
||||
let isActive = false;
|
||||
let pid = '';
|
||||
let memory = '';
|
||||
let cpu = '';
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.includes('Active:')) {
|
||||
isActive = line.includes('active (running)');
|
||||
} else if (line.includes('Main PID:')) {
|
||||
const match = line.match(/Main PID:\s+(\d+)/);
|
||||
if (match) pid = match[1];
|
||||
} else if (line.includes('Memory:')) {
|
||||
const match = line.match(/Memory:\s+([\d.]+[A-Z])/);
|
||||
if (match) memory = match[1];
|
||||
} else if (line.includes('CPU:')) {
|
||||
const match = line.match(/CPU:\s+([\d.]+(?:ms|s))/);
|
||||
if (match) cpu = match[1];
|
||||
}
|
||||
}
|
||||
|
||||
// Display beautiful status
|
||||
logger.log('');
|
||||
if (isActive) {
|
||||
logger.log(`${symbols.running} ${theme.success('Service:')} ${theme.statusActive('active (running)')}`);
|
||||
} else {
|
||||
logger.log(`${symbols.stopped} ${theme.dim('Service:')} ${theme.statusInactive('inactive')}`);
|
||||
}
|
||||
|
||||
if (pid || memory || cpu) {
|
||||
const details = [];
|
||||
if (pid) details.push(`PID: ${theme.dim(pid)}`);
|
||||
if (memory) details.push(`Memory: ${theme.dim(memory)}`);
|
||||
if (cpu) details.push(`CPU: ${theme.dim(cpu)}`);
|
||||
logger.log(` ${details.join(' ')}`);
|
||||
}
|
||||
logger.log('');
|
||||
|
||||
} catch (error) {
|
||||
const boxWidth = 45;
|
||||
logger.logBoxTitle('Service Status', boxWidth);
|
||||
logger.logBoxLine('Service is not running');
|
||||
logger.logBoxEnd();
|
||||
logger.log('');
|
||||
logger.log(`${symbols.stopped} ${theme.dim('Service:')} ${theme.statusInactive('not installed')}`);
|
||||
logger.log('');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -199,7 +270,7 @@ WantedBy=multi-user.target
|
||||
|
||||
// Check if we have the new multi-UPS config format
|
||||
if (config.upsDevices && Array.isArray(config.upsDevices) && config.upsDevices.length > 0) {
|
||||
logger.log(`Found ${config.upsDevices.length} UPS device(s) in configuration`);
|
||||
logger.info(`UPS Devices (${config.upsDevices.length}):`);
|
||||
|
||||
// Show status for each UPS
|
||||
for (const ups of config.upsDevices) {
|
||||
@@ -207,6 +278,7 @@ WantedBy=multi-user.target
|
||||
}
|
||||
} else if (config.snmp) {
|
||||
// Legacy single UPS configuration
|
||||
logger.info('UPS Devices (1):');
|
||||
const legacyUps = {
|
||||
id: 'default',
|
||||
name: 'Default UPS',
|
||||
@@ -217,15 +289,16 @@ WantedBy=multi-user.target
|
||||
|
||||
await this.displaySingleUpsStatus(legacyUps, snmp);
|
||||
} else {
|
||||
logger.error('No UPS devices found in configuration');
|
||||
logger.log('');
|
||||
logger.warn('No UPS devices configured');
|
||||
logger.log(` ${theme.dim('Run')} ${theme.command('nupst ups add')} ${theme.dim('to add a device')}`);
|
||||
logger.log('');
|
||||
}
|
||||
} catch (error) {
|
||||
const boxWidth = 45;
|
||||
logger.logBoxTitle('UPS Status', boxWidth);
|
||||
logger.logBoxLine(
|
||||
`Failed to retrieve UPS status: ${error instanceof Error ? error.message : String(error)}`,
|
||||
);
|
||||
logger.logBoxEnd();
|
||||
logger.log('');
|
||||
logger.error('Failed to retrieve UPS status');
|
||||
logger.log(` ${theme.dim(error instanceof Error ? error.message : String(error))}`);
|
||||
logger.log('');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -235,24 +308,6 @@ WantedBy=multi-user.target
|
||||
* @param snmp SNMP manager
|
||||
*/
|
||||
private async displaySingleUpsStatus(ups: any, snmp: any): Promise<void> {
|
||||
const boxWidth = 45;
|
||||
logger.logBoxTitle(`Connecting to UPS: ${ups.name}`, boxWidth);
|
||||
logger.logBoxLine(`ID: ${ups.id}`);
|
||||
logger.logBoxLine(`Host: ${ups.snmp.host}:${ups.snmp.port}`);
|
||||
logger.logBoxLine(`UPS Model: ${ups.snmp.upsModel || 'cyberpower'}`);
|
||||
|
||||
if (ups.groups && ups.groups.length > 0) {
|
||||
// Get group names if available
|
||||
const config = this.daemon.getConfig();
|
||||
const groupNames = ups.groups.map((groupId: string) => {
|
||||
const group = config.groups?.find((g: { id: string }) => g.id === groupId);
|
||||
return group ? group.name : groupId;
|
||||
});
|
||||
logger.logBoxLine(`Groups: ${groupNames.join(', ')}`);
|
||||
}
|
||||
|
||||
logger.logBoxEnd();
|
||||
|
||||
try {
|
||||
// Create a test config with a short timeout
|
||||
const testConfig = {
|
||||
@@ -262,32 +317,43 @@ WantedBy=multi-user.target
|
||||
|
||||
const status = await snmp.getUpsStatus(testConfig);
|
||||
|
||||
logger.logBoxTitle(`UPS Status: ${ups.name}`, boxWidth);
|
||||
logger.logBoxLine(`Power Status: ${status.powerStatus}`);
|
||||
logger.logBoxLine(`Battery Capacity: ${status.batteryCapacity}%`);
|
||||
logger.logBoxLine(`Runtime Remaining: ${status.batteryRuntime} minutes`);
|
||||
// Determine status symbol based on power status
|
||||
let statusSymbol = symbols.unknown;
|
||||
if (status.powerStatus === 'online') {
|
||||
statusSymbol = symbols.running;
|
||||
} else if (status.powerStatus === 'onBattery') {
|
||||
statusSymbol = symbols.warning;
|
||||
}
|
||||
|
||||
// Show threshold status
|
||||
logger.logBoxLine('');
|
||||
logger.logBoxLine('Thresholds:');
|
||||
logger.logBoxLine(
|
||||
` Battery: ${status.batteryCapacity}% / ${ups.thresholds.battery}% ${
|
||||
status.batteryCapacity < ups.thresholds.battery ? '⚠️' : '✓'
|
||||
}`,
|
||||
);
|
||||
logger.logBoxLine(
|
||||
` Runtime: ${status.batteryRuntime} min / ${ups.thresholds.runtime} min ${
|
||||
status.batteryRuntime < ups.thresholds.runtime ? '⚠️' : '✓'
|
||||
}`,
|
||||
);
|
||||
// Display UPS name and power status
|
||||
logger.log(` ${statusSymbol} ${theme.highlight(ups.name)} - ${formatPowerStatus(status.powerStatus)}`);
|
||||
|
||||
// Display battery with color coding
|
||||
const batteryColor = getBatteryColor(status.batteryCapacity);
|
||||
const batterySymbol = status.batteryCapacity >= ups.thresholds.battery ? symbols.success : symbols.warning;
|
||||
logger.log(` Battery: ${batteryColor(status.batteryCapacity + '%')} ${batterySymbol} Runtime: ${getRuntimeColor(status.batteryRuntime)(status.batteryRuntime + ' min')}`);
|
||||
|
||||
// Display host info
|
||||
logger.log(` ${theme.dim(`Host: ${ups.snmp.host}:${ups.snmp.port}`)}`);
|
||||
|
||||
// Display groups if any
|
||||
if (ups.groups && ups.groups.length > 0) {
|
||||
const config = this.daemon.getConfig();
|
||||
const groupNames = ups.groups.map((groupId: string) => {
|
||||
const group = config.groups?.find((g: { id: string }) => g.id === groupId);
|
||||
return group ? group.name : groupId;
|
||||
});
|
||||
logger.log(` ${theme.dim(`Groups: ${groupNames.join(', ')}`)}`);
|
||||
}
|
||||
|
||||
logger.log('');
|
||||
|
||||
logger.logBoxEnd();
|
||||
} catch (error) {
|
||||
logger.logBoxTitle(`UPS Status: ${ups.name}`, boxWidth);
|
||||
logger.logBoxLine(
|
||||
`Failed to retrieve UPS status: ${error instanceof Error ? error.message : String(error)}`,
|
||||
);
|
||||
logger.logBoxEnd();
|
||||
// Display error for this UPS
|
||||
logger.log(` ${symbols.error} ${theme.highlight(ups.name)} - ${theme.error('Connection failed')}`);
|
||||
logger.log(` ${theme.dim(error instanceof Error ? error.message : String(error))}`);
|
||||
logger.log(` ${theme.dim(`Host: ${ups.snmp.host}:${ups.snmp.port}`)}`);
|
||||
logger.log('');
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user