8.0 KiB
Script Files — Line by Line Explanation
1. deploy_exporters.sh
Purpose: Installs Node Exporter and (optionally) Nvidia GPU Exporter on a remote client machine as permanent background services.
How it is used: Pushed to remote machines via MeshCentral's "Run Commands" feature. Runs as root. Never needs to be touched manually on each machine.
Full Script with Explanation
#!/bin/bash
Shebang line. Tells the operating system: "Use the Bash shell to run this file." Without this, the OS doesn't know which interpreter to use.
set -e
Exit on any error. If ANY command in the script fails, the entire script stops immediately instead of blindly continuing. Without this, a failed download might be followed by an attempt to install a corrupt file — causing silent broken installs.
echo "Starting Automated Exporter Installation..."
if [ "$EUID" -ne 0 ]; then echo "❌ Error: Please run this script with sudo."; exit 1; fi
Root check. $EUID is the current user's ID. Root's ID is always 0. If the script is not running as root, it prints an error and exits immediately. All the commands below (installing files to /usr/local/bin, creating system users, writing systemd services) require root — if we don't check here, they will fail silently.
NODE_VERSION="1.7.0"
GPU_VERSION="1.4.1"
Version variables. Defined once at the top so you only need to change the version number in one place when upgrading. Without this, you would have to find and replace the version string in 4 different places.
cd /tmp
wget -q https://github.com/prometheus/node_exporter/releases/download/v${NODE_VERSION}/node_exporter-${NODE_VERSION}.linux-amd64.tar.gz
tar -xf node_exporter-${NODE_VERSION}.linux-amd64.tar.gz
Download and extract. cd /tmp — work in a temporary folder that all users can write to. wget -q — download silently (no progress bar, cleaner output for remote execution). tar -xf — extract the downloaded archive.
if ! id "node_exporter" &>/dev/null; then useradd --no-create-home --shell /bin/false node_exporter; fi
Create a dedicated system user (if it doesn't already exist). This is a security best practice — Node Exporter runs as its own unprivileged user node_exporter instead of root.
id "node_exporter"— checks if the user already exists! id— if it does NOT exist, run the useradd command--no-create-home— no home directory needed (it's a service account, not a human)--shell /bin/false— this user cannot log in interactively (prevents abuse if account is compromised)
cp node_exporter-${NODE_VERSION}.linux-amd64/node_exporter /usr/local/bin/
chown node_exporter:node_exporter /usr/local/bin/node_exporter
Install the binary. cp — copy the extracted binary to /usr/local/bin/ which is the standard location for manually installed programs on Linux. chown — change the file owner to the node_exporter user so only that user can execute it.
cat << 'EOF' > /etc/systemd/system/node_exporter.service
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target
[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter
[Install]
WantedBy=multi-user.target
EOF
Write the systemd service file. This is what makes Node Exporter a background service that:
- Starts automatically on every reboot (
WantedBy=multi-user.target) - Waits for the network to be ready before starting (
After=network-online.target) - Runs as the
node_exporteruser (not root) - Automatically restarts if it crashes
cat << 'EOF' > file is a "here document" — it writes everything between EOF markers directly into the file without needing a separate text editor.
systemctl daemon-reload
systemctl enable --now node_exporter
Register and start the service.
daemon-reload— tells systemd to re-read all service files (required after creating a new .service file)enable— registers the service to start on boot--now— also starts it immediately right now (instead of waiting for next reboot)
if command -v nvidia-smi &> /dev/null; then
# ... install GPU exporter ...
else
echo "No Nvidia GPU detected. Skipping GPU Exporter installation."
fi
Conditional GPU detection. command -v nvidia-smi checks if the nvidia-smi tool exists (it only exists on machines with Nvidia drivers installed). If found, the GPU exporter is installed. If not found, the script silently skips — so the same script works on both GPU and non-GPU machines without any modification.
2. test_install.sh
Purpose: A safe, harmless script to verify that MeshCentral's remote execution works before deploying the real deploy_exporters.sh. It installs cowsay and htop — two completely harmless utilities — just to prove the remote execution pipeline works end to end.
How it is used: Run first on a test machine via MeshCentral before ever running deploy_exporters.sh. If this works, you know deploy_exporters.sh will work too.
Full Script with Explanation
#!/bin/bash
set -e
Same as above — Bash interpreter + exit on error.
echo "Starting Dummy Installation Test..."
if [ "$EUID" -ne 0 ]; then
echo "❌ Error: Please run this script with sudo."
exit 1
fi
Same root check as in deploy_exporters.sh. Even a test script needs root because apt-get install requires root.
apt-get update -yq
apt-get install -yq cowsay htop
Install the test tools.
apt-get update— refresh the package list from Ubuntu's package servers-y— automatically answer "yes" to all prompts (required for non-interactive remote execution — there is no human at the keyboard to press Y)-q— quiet mode, minimal output
cowsay — prints ASCII art of a cow saying a message. Completely harmless.
htop — an interactive process viewer, like a better Task Manager for Linux.
if command -v htop &> /dev/null; then
echo "✅ 'htop' installed successfully!"
fi
if command -v /usr/games/cowsay &> /dev/null; then
echo "✅ 'cowsay' installed successfully!"
/usr/games/cowsay "Hello from the automated script! ..."
fi
Verify the installation actually worked. Instead of trusting that apt-get succeeded, the script independently checks whether the commands now exist using command -v. If htop is not found after the install, something silently failed and this catches it.
/usr/games/cowsay is the full path because cowsay installs into /usr/games/ which is sometimes not in the system PATH.
echo "To clean up and remove these test tools later, just run:"
echo "sudo apt-get remove --purge -y cowsay htop"
Cleanup instructions. Tells you exactly how to undo what the script did. Good practice for any test script.
Summary: How the Scripts Relate to the Whole System
Central Server (you)
└── MeshCentral Dashboard (browser)
└── Group Action → Run Commands
├── test_install.sh ← Run FIRST to verify pipeline works
└── deploy_exporters.sh ← Run AFTER test passes, on all real machines
├── Installs Node Exporter (always)
└── Installs GPU Exporter (only if Nvidia GPU detected)
│
▼
Client Machine now exposes:
├── port 9100 → Node Exporter (CPU, RAM, Disk, Network)
└── port 9835 → GPU Exporter (temp, VRAM, utilization)
│
▼
Prometheus scrapes these ports via Tailscale IP
│
▼
Grafana displays the data as dashboards
│
▼
Alertmanager sends alerts to Rocket.Chat / Gmail