# N8N Setup Instructions

This guide walks through configuring the N8N health check workflow.

## Prerequisites

- N8N running at http://10.10.0.210:5678
- SSH access configured between N8N LXC and Claude Code LXC (already done)
- Discord webhook configured (already done)

## Step 1: Create SSH Credential in N8N

1. Open N8N at http://10.10.0.210:5678
2. Go to **Settings** (gear icon) → **Credentials**
3. Click **Add Credential**
4. Search for and select **SSH**
5. Configure:
   - **Credential Name:** `Claude Code LXC`
   - **Host:** `10.10.0.148`
   - **Port:** `22`
   - **Username:** `root`
   - **Authentication:** `Private Key`
   - **Private Key:** Paste the following:

```
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACAtHWX+TOsNkiL7TP1niwpO5AJjBThghODpa1q93g7bagAAAJiq1/Cyqtfw
sgAAAAtzc2gtZWQyNTUxOQAAACAtHWX+TOsNkiL7TP1niwpO5AJjBThghODpa1q93g7bag
AAAEBVXct4LSCsRexl9JYZtrYx4YVoioYBdWHlPinW+PudBS0dZf5M6w2SIvtM/WeLCk7k
AmMFOGCE4OlrWr3eDttqAAAAEm44bi10by1jbGF1ZGUtY29kZQECAw==
-----END OPENSSH PRIVATE KEY-----
```

6. Click **Save**

## Step 2: Import the Workflow

1. Go to **Workflows** in N8N
2. Click **Add Workflow** → **Import from File**
3. Select: `/mnt/NV2/Development/headless-claude/n8n-workflow-import.json`
   - Or copy the workflow from this repo
4. After import, open the workflow

## Step 3: Link the SSH Credential

1. Click on the **Run Claude Diagnostics** node
2. In the **Credential** dropdown, select `Claude Code LXC`
3. Click **Save**

## Step 4: Test the Workflow

1. Click **Execute Workflow** (play button)
2. Watch each node execute:
   - **Every 5 Minutes** → triggers
   - **Run Claude Diagnostics** → SSHs to Claude Code LXC
   - **Parse Claude Response** → extracts result
   - **Has Issues?** → routes based on health
   - **Discord Alert** or **Discord OK** → sends notification

3. Check Discord for the notification

## Step 5: Activate the Workflow

1. Toggle the workflow to **Active** (top right)
2. The workflow will now run every 5 minutes automatically

## Workflow Behavior

### Schedule
- Runs every 5 minutes by default
- Adjust in "Every 5 Minutes" node settings

### Health Check
- Calls Claude Code headless mode
- Runs `python3 ~/.claude/skills/server-diagnostics/client.py health paper-dynasty`
- Claude analyzes results and summarizes

### Notifications
- **Issues Found**: Red Discord embed with alert details
- **All Healthy**: Green Discord embed (disabled by default - enable "Discord OK" node if wanted)

### Cost
- Each health check costs ~$0.08 (Claude API usage)
- At 5-minute intervals: ~$0.08 × 12 × 24 = ~$23/day
- Consider increasing interval to 15-30 minutes for cost savings

## Customization

### Change Check Interval
1. Open "Every 5 Minutes" node
2. Adjust `minutesInterval` value

### Add More Servers
1. Update config.yaml on Claude Code LXC
2. Modify the Claude prompt in "Run Claude Diagnostics" node

### Enable OK Notifications
1. Click "Discord OK (Optional)" node
2. Toggle off "Disabled" setting

## Troubleshooting

### SSH Connection Failed
```bash
# Test from N8N LXC
ssh -i ~/.ssh/n8n_to_claude root@10.10.0.148 "hostname"
```

### Claude Command Not Found
```bash
# Verify Claude path on Claude Code LXC
/root/.local/bin/claude --version
```

### Authentication Expired
If Claude returns auth errors, SSH to Claude Code LXC and re-authenticate:
```bash
ssh root@10.10.0.148
/root/.local/bin/claude  # Follow device code flow
```

## Architecture

```
N8N (10.10.0.210)
    │
    ├── Schedule Trigger (every 5 min)
    │
    └── SSH to Claude Code LXC (10.10.0.148)
            │
            └── Claude Code Headless Mode
                    │
                    └── server-diagnostics skill
                            │
                            └── SSH to Paper Dynasty (10.10.0.88)
                                    │
                                    └── Docker health checks
```
