Files
freshStartScript/freshStart.sh

167 lines
5.2 KiB
Bash
Raw Permalink Normal View History

2024-12-08 15:05:13 -05:00
#!/bin/bash
set -e
echo "Starting Proxmox fresh installation script..."
2025-09-03 13:43:46 -04:00
# Cleanup function for failed installations
cleanup() {
echo "Cleaning up on error..."
systemctl stop node_exporter 2>/dev/null || true
systemctl stop hwmon.timer 2>/dev/null || true
systemctl disable node_exporter 2>/dev/null || true
systemctl disable hwmon.timer 2>/dev/null || true
rm -f /etc/systemd/system/node_exporter.service
rm -f /etc/systemd/system/hwmon.service
rm -f /etc/systemd/system/hwmon.timer
rm -f /usr/local/bin/node_exporter
rm -rf node_exporter-*.linux-amd64.tar.gz node_exporter-*.linux-amd64
userdel node_exporter 2>/dev/null || true
systemctl daemon-reload
echo "Cleanup completed."
}
# Set trap for cleanup on error
trap cleanup ERR
2024-12-08 15:05:13 -05:00
# Install dependencies
echo "Installing required packages..."
apt-get update
2025-11-29 15:50:14 -05:00
apt-get install -y python3-pip smartmontools iperf3 python3-psutil python3-requests lm-sensors fastfetch rsync fio
2024-12-08 15:05:13 -05:00
# Install Node Exporter
echo "Installing Prometheus Node Exporter..."
NODE_EXPORTER_VERSION="1.8.2"
2025-09-03 13:43:46 -04:00
# Download Node Exporter with error handling
echo "Downloading Node Exporter..."
if ! wget --timeout=30 --tries=3 "https://github.com/prometheus/node_exporter/releases/download/v${NODE_EXPORTER_VERSION}/node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64.tar.gz"; then
echo "ERROR: Failed to download Node Exporter"
exit 1
fi
# Extract with error checking
if ! tar xvfz node_exporter-*.linux-amd64.tar.gz; then
echo "ERROR: Failed to extract Node Exporter archive"
exit 1
fi
# Check if user already exists
if ! id "node_exporter" &>/dev/null; then
echo "Creating node_exporter user..."
useradd -rs /bin/false node_exporter
else
echo "node_exporter user already exists, skipping creation..."
fi
2024-12-08 15:05:13 -05:00
# Move binary to proper location
2025-09-03 13:43:46 -04:00
if ! mv node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64/node_exporter /usr/local/bin/; then
echo "ERROR: Failed to move Node Exporter binary"
exit 1
fi
# Cleanup downloaded files
2024-12-08 15:05:13 -05:00
rm -rf node_exporter-*.linux-amd64.tar.gz node_exporter-*.linux-amd64
2025-09-03 13:43:46 -04:00
# Set proper permissions
chown node_exporter:node_exporter /usr/local/bin/node_exporter
chmod +x /usr/local/bin/node_exporter
2024-12-08 15:05:13 -05:00
# Create node_exporter service file
cat > /etc/systemd/system/node_exporter.service << 'EOL'
[Unit]
Description=Node Exporter
After=network.target
[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter
[Install]
WantedBy=multi-user.target
EOL
# Enable and start node_exporter
systemctl daemon-reload
systemctl enable node_exporter
systemctl start node_exporter
2025-09-03 13:43:46 -04:00
# Check if Node Exporter started successfully
if ! systemctl is-active --quiet node_exporter; then
echo "ERROR: Node Exporter failed to start"
systemctl status node_exporter --no-pager || true
exit 1
fi
2024-12-08 15:05:13 -05:00
# Install hwmon daemon
echo "Installing hwmon daemon..."
2025-09-03 13:43:46 -04:00
# Download hwmon service files with error handling
echo "Downloading hwmon service files..."
2025-11-25 20:47:10 -05:00
if ! curl --max-time 30 --retry 3 -o /etc/systemd/system/hwmon.service http://10.10.10.63:3000/LotusGuild/hwmonDaemon/raw/branch/main/hwmon.service; then
2025-09-03 13:43:46 -04:00
echo "ERROR: Failed to download hwmon.service"
exit 1
fi
2025-11-25 20:47:10 -05:00
if ! curl --max-time 30 --retry 3 -o /etc/systemd/system/hwmon.timer http://10.10.10.63:3000/LotusGuild/hwmonDaemon/raw/branch/main/hwmon.timer; then
2025-09-03 13:43:46 -04:00
echo "ERROR: Failed to download hwmon.timer"
exit 1
fi
# Verify downloaded files exist and are not empty
if [[ ! -s /etc/systemd/system/hwmon.service ]]; then
echo "ERROR: hwmon.service file is empty or missing"
exit 1
fi
if [[ ! -s /etc/systemd/system/hwmon.timer ]]; then
echo "ERROR: hwmon.timer file is empty or missing"
exit 1
fi
# Create log directory for hwmon
mkdir -p /var/log/hwmonDaemon
# Start the hwmon daemon
2024-12-08 15:05:13 -05:00
systemctl daemon-reload
systemctl enable hwmon.timer
systemctl start hwmon.timer
2025-09-03 13:43:46 -04:00
# Check if hwmon timer started successfully
if ! systemctl is-active --quiet hwmon.timer; then
echo "ERROR: hwmon timer failed to start"
systemctl status hwmon.timer --no-pager || true
exit 1
fi
# Final verification
echo "Verifying installation..."
echo "Node Exporter status: $(systemctl is-active node_exporter)"
echo "hwmon timer status: $(systemctl is-active hwmon.timer)"
echo "Node Exporter port check:"
if ss -tlnp | grep :9100; then
echo "✓ Node Exporter is listening on port 9100"
else
echo "WARNING: Node Exporter not listening on port 9100"
fi
# Test hwmon with error handling
2024-12-08 15:05:13 -05:00
echo "Testing hwmon dry-run..."
2025-11-25 20:48:44 -05:00
if ! /usr/bin/env python3 -c "import urllib.request; exec(urllib.request.urlopen('http://10.10.10.63:3000/LotusGuild/hwmonDaemon/raw/branch/main/hwmonDaemon.py').read().decode('utf-8'))" --dry-run; then
2025-09-03 13:43:46 -04:00
echo "WARNING: hwmon dry-run test failed, but services are installed"
fi
# Disable cleanup trap on successful completion
trap - ERR
2024-12-08 15:05:13 -05:00
2025-09-03 13:43:46 -04:00
echo "✓ Installation complete! All services are running correctly."
echo ""
echo "Services installed:"
echo " - Node Exporter: http://$(hostname -I | awk '{print $1}'):9100/metrics"
echo " - hwmon daemon: Monitoring system health every 15 minutes"
echo ""
echo "Log locations:"
echo " - Node Exporter: journalctl -u node_exporter"
echo " - hwmon: journalctl -u hwmon.service"
echo " - hwmon logs: /var/log/hwmonDaemon/"