Files
snapraid_smart_logging/setup_database.sh
T
WesandClaude Sonnet 4.5 27afedb32e Complete rewrite: SnapRAID SMART logger with BackBlaze algorithm
Fetches SMART data from Scrutiny's InfluxDB and calculates failure
probabilities using the exact BackBlaze tables from SnapRAID source.

Key features:
- Calculates both v12 (with attr 193) and v13 (without attr 193) algorithms
- v12 matches SnapRAID pre-v13.0 (includes Load Cycle Count)
- v13 matches modern SnapRAID v13.0+ (excludes Load Cycle Count)
- Stores both values for trending and comparison
- Correctly applies bit masks (16-bit for 187/188, 32-bit for others)
- Annualizes monthly rates and applies Poisson distribution
- Logs to PostgreSQL with device metadata

Solved the mystery: SnapRAID v12 uses attribute 193 which was removed
in v13.0. Load cycle count has massive impact on failure predictions
for some drives (80% vs 4% difference).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-07 06:55:39 -05:00

88 lines
2.4 KiB
Bash
Executable File

#!/bin/bash
# Setup script for PostgreSQL database and user
set -e # Exit on error
# Configuration
PG_HOST="${1:-localhost}"
PG_ADMIN_USER="postgres"
PG_ADMIN_PASS="Lkxdjfhl1d"
DB_NAME="smart_monitoring"
DB_USER="smart_logger"
DB_PASS="${2:-change_me_123}" # Pass as second argument or use default
echo "PostgreSQL Setup for Smart Logger"
echo "=================================="
echo "Host: $PG_HOST"
echo "Database: $DB_NAME"
echo "User: $DB_USER"
echo ""
# Check if psql is installed
if ! command -v psql &> /dev/null; then
echo "ERROR: psql is not installed"
echo "Install with: sudo pacman -S postgresql"
exit 1
fi
echo "Step 1: Creating database..."
PGPASSWORD="$PG_ADMIN_PASS" psql -h "$PG_HOST" -U "$PG_ADMIN_USER" -d postgres <<EOF
-- Create database if it doesn't exist
SELECT 'CREATE DATABASE $DB_NAME'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '$DB_NAME')\gexec
-- Create user if doesn't exist
DO \$\$
BEGIN
IF NOT EXISTS (SELECT FROM pg_user WHERE usename = '$DB_USER') THEN
CREATE USER $DB_USER WITH PASSWORD '$DB_PASS';
END IF;
END
\$\$;
-- Grant privileges
GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;
EOF
echo "✓ Database and user created"
echo ""
echo "Step 2: Creating schema..."
PGPASSWORD="$PG_ADMIN_PASS" psql -h "$PG_HOST" -U "$PG_ADMIN_USER" -d "$DB_NAME" -f schema_v2.sql
echo "✓ Schema created"
echo ""
echo "Step 3: Granting permissions to $DB_USER..."
PGPASSWORD="$PG_ADMIN_PASS" psql -h "$PG_HOST" -U "$PG_ADMIN_USER" -d "$DB_NAME" <<EOF
-- Grant schema usage
GRANT USAGE ON SCHEMA public TO $DB_USER;
-- Grant table permissions
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO $DB_USER;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO $DB_USER;
-- Grant default privileges for future objects
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO $DB_USER;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO $DB_USER;
EOF
echo "✓ Permissions granted"
echo ""
echo "=========================================="
echo "Setup Complete!"
echo "=========================================="
echo ""
echo "Connection details for your .env file:"
echo ""
echo "POSTGRES_HOST=$PG_HOST"
echo "POSTGRES_PORT=5432"
echo "POSTGRES_DB=$DB_NAME"
echo "POSTGRES_USER=$DB_USER"
echo "POSTGRES_PASSWORD=$DB_PASS"
echo ""
echo "Test connection with:"
echo "psql -h $PG_HOST -U $DB_USER -d $DB_NAME"