# PCMonitor - Data Not Showing in Dashboard

## 🎯 Action Plan

Agent sudah running tapi data belum terlihat di dashboard. Ikuti langkah-langkah berikut untuk mendiagnosis:

---

## 📋 Step 1: Verifikasi Agent Service

### **Quick Check (Command Prompt/cmd.exe):**

```batch
REM Check if service exists
sc query PCMonitorAgent

REM Result harus menunjukkan: STATE : 4 RUNNING

REM Jika tidak running, start service:
net start PCMonitorAgent
```

### **Detail Check (PowerShell as Admin):**

```powershell
# Check service status
Get-Service PCMonitorAgent | Select-Object -Property Status, Name, DisplayName

# Output yang diharapkan:
# Status   Name             DisplayName
# ------   ----             -----------
# Running  PCMonitorAgent   PC Monitor Agent
```

**Jika TIDAK Running:**
```powershell
Start-Service PCMonitorAgent
Get-Service PCMonitorAgent
```

---

## 🔧 Step 2: Jalankan Diagnostic Script

### **Opsi A: Dari Command Prompt (cmd.exe)**

```batch
cd E:\xampp\htdocs\tracker\agent-csharp
DIAGNOSTIC-CMD.bat
```

### **Opsi B: Dari PowerShell (LEBIH DETAIL)**

1. Buka **PowerShell as Administrator**
   - Windows Key → Ketik `PowerShell` → Klik kanan → Run as Administrator
   
2. Navigate dan jalankan:
```batch
cd E:\xampp\htdocs\tracker\agent-csharp
.\DIAGNOSTIC.bat

```

**Script ini akan cek:**
- ✓ Service status
- ✓ Config file
- ✓ API connectivity
- ✓ Log files
- ✓ Network connection

---

## 🌐 Step 3: Akses Debug Dashboard

**Di Webhost, buka browser:**

```
http://localhost/tracker/debug.php
```

atau

```
http://tracker.lppmunud.id/debug.php
```

**Dashboard ini akan menunjukkan:**
- Jumlah agents terdaftar
- Jumlah activities yang masuk
- Daftar agents dan last seen mereka
- 20 aktivitas terakhir
- 20 proses terakhir

---

## ✅ Step 4: Verify Konfigurasi

### **Quick Check (Command Prompt):**

```batch
type C:\ProgramData\PCMonitor\config.json

REM Atau buka dengan Notepad:
notepad C:\ProgramData\PCMonitor\config.json
```

### **Detail Check (PowerShell):**

```powershell
cat C:\ProgramData\PCMonitor\config.json | ConvertFrom-Json
```

**Output harus seperti ini:**
```json
{
  "api_url": "http://tracker.lppmunud.id/tracker/api",
  "api_key": "default-api-key",
  "agent_id": "2664732425",
  "monitoring_interval": 30,
  "activity_interval": 60
}
```

**Yang perlu diverifikasi:**
- `api_url` harus: `http://tracker.lppmunud.id/tracker/api` (EXACT, no trailing slash)
- `agent_id` harus ada
- File harus di: `C:\ProgramData\PCMonitor\config.json`

**Jika ada kesalahan, edit dengan Notepad:**

```batch
REM Command Prompt:
notepad C:\ProgramData\PCMonitor\config.json

REM Kemudian restart service:
net stop PCMonitorAgent
net start PCMonitorAgent
```

```powershell
REM PowerShell:
Restart-Service PCMonitorAgent
```

---

## 🔍 Step 5: Test API Connectivity

**Di Target PC (PowerShell):**

```powershell
# Test 1: Check if API is reachable
$response = Invoke-WebRequest -Uri "http://tracker.lppmunud.id/tracker/api/status" -UseBasicParsing
$response.StatusCode  # Harus 200
$response.Content     # Harus menunjukkan status API

# Test 2: Send test activity
$testData = @{
    agent_id = "2664732425"
    activity_type = "system_startup"
    app_name = "System"
    description = "Agent diagnostic test"
} | ConvertTo-Json

$response = Invoke-WebRequest `
    -Uri "http://tracker.lppmunud.id/tracker/api/activity" `
    -Method POST `
    -Body $testData `
    -ContentType "application/json" `
    -UseBasicParsing

$response.StatusCode    # Harus 200
$response.Content       # Success message
```

**Expected Response:**
```json
{"success":true,"message":"Activity logged"}
```

---

## 📊 Step 6: Check Database

**Di Webhost, MySQL/PhpMyAdmin:**

```sql
-- Verify agent exists
SELECT * FROM agents WHERE agent_id = '2664732425';

-- Count activities
SELECT COUNT(*) as total FROM pc_activities WHERE agent_id = '2664732425';

-- See latest activities
SELECT * FROM pc_activities 
WHERE agent_id = '2664732425' 
ORDER BY created_at DESC 
LIMIT 10;
```

**Jika agent tidak ada, insert manually:**
```sql
INSERT INTO agents (agent_id, agent_name, pc_name, os_info, status) 
VALUES ('2664732425', 'Agent-001', 'PC-Target', 'Windows 10', 'online');
```

---

## 📝 Step 7: Check Log Files

**Di Target PC, baca log terakhir:**

```powershell
# Lihat file log terbaru
Get-ChildItem C:\ProgramData\PCMonitor\Logs\ | Sort-Object LastWriteTime -Descending | Select-Object -First 1

# Baca konten
Get-Content C:\ProgramData\PCMonitor\Logs\Agent_2026-03-27.log -Tail 50

# Cari error
Select-String -Path "C:\ProgramData\PCMonitor\Logs\*.log" -Pattern "error|failed|ERROR|Failed" | Select-Object -Last 20
```

**Tanda-tanda masalah:**
- ❌ `ERROR: Cannot connect to http://tracker.lppmunud.id/tracker/api`
- ❌ `ERROR: HTTP 404 - Endpoint not found`
- ❌ `ERROR: Connection timeout`
- ❌ `ERROR: Failed to send activity`

**Jika ada error, perbaiki sesuai dengan deskripsi di bawah.**

---

## 🚨 Kemungkinan Masalah & Solusi

### ❌ Problem 1: "Cannot connect to API"

**Penyebab:** Network tidak bisa mencapai webhost

**Test:**
```powershell
# Test DNS resolution
nslookup tracker.lppmunud.id

# Test connectivity
Test-NetConnection -ComputerName tracker.lppmunud.id -Port 80

# Test HTTP
curl -I http://tracker.lppmunud.id
```

**Solusi:**
1. Pastikan target PC punya internet connection
2. Pastikan firewall tidak memblokir outbound ke port 80
3. Pastikan DNS bisa resolve tracker.lppmunud.id
4. Periksa VPN/proxy settings

---

### ❌ Problem 2: "HTTP 404 - Endpoint not found"

**Penyebab:** URL di config salah

**Solusi:**
```
Config harus EXACT:
✓ http://tracker.lppmunud.id/tracker/api

Jangan:
✗ http://tracker.lppmunud.id/api  (missing /tracker)
✗ http://tracker.lppmunud.id/tracker/api/  (extra trailing slash)
✗ https://tracker.lppmunud.id/tracker/api  (https instead of http)
```

---

### ❌ Problem 3: "API received but data not in database"

**Penyebab:** Database error

**Cek:**
```sql
-- Verify tables exist
SHOW TABLES;

-- Should show:
-- agents
-- pc_activities
-- running_processes
-- remote_commands
-- bandwidth_control

-- Check table structure
DESCRIBE agents;
DESCRIBE pc_activities;
```

---

### ❌ Problem 4: "Agent not showing in dashboard despite data in DB"

**Penyebab:** Frontend filter or cache issue

**Solusi:**
1. Refresh browser: `Ctrl+F5` (hard refresh)
2. Clear browser cache
3. Check console for JavaScript errors: `F12` → Console
4. Check database directly: `SELECT * FROM agents`
5. Check if filter is applied in dashboard

---

## ✨ Complete Verification Checklist

| Step | Check | Result | Fix |
|------|-------|--------|-----|
| 1 | Service Running | `Get-Service PCMonitorAgent` | Restart if needed |
| 2 | Config File Exists | `C:\ProgramData\PCMonitor\config.json` | Create if missing |
| 3 | Config URL Correct | `api_url` = exact URL | Update if wrong |
| 4 | API Reachable | `Invoke-WebRequest .../api/status` | Check network/firewall |
| 5 | Network Connected | `ping tracker.lppmunud.id` | Check internet |
| 6 | Log Files Exist | `C:\ProgramData\PCMonitor\Logs\` | Check for errors |
| 7 | No Errors in Log | Scan logs for ERROR | Fix issues |
| 8 | Agent in DB | `SELECT * FROM agents` | Insert manually if needed |
| 9 | Data in DB | `SELECT * FROM pc_activities` | Check if API works |
| 10 | Dashboard Shows Data | Refresh dashboard | F5 or hard refresh |

---

## 📞 If Still Not Working

Kumpulkan informasi berikut:

1. **Service Status:**
   ```powershell
   Get-Service PCMonitorAgent | ConvertTo-Json
   ```

2. **Config File:**
   ```powershell
   cat C:\ProgramData\PCMonitor\config.json
   ```

3. **Latest Log (last 50 lines):**
   ```powershell
   Get-Content C:\ProgramData\PCMonitor\Logs\Agent_*.log -Tail 50 | Out-File debug_log.txt
   ```

4. **Network Test:**
   ```powershell
   Test-NetConnection -ComputerName tracker.lppmunud.id -Port 80 | ConvertTo-Json | Out-File network_test.txt
   ```

5. **Database Query:**
   ```sql
   SELECT * FROM agents;
   SELECT COUNT(*) FROM pc_activities;
   ```

---

## 🎓 Learning Resources

- [PCMonitor TROUBLESHOOTING.md](./TROUBLESHOOTING.md) - Detailed troubleshooting guide
- [API Documentation](./api/README.md) - API endpoint reference
- [Dashboard Guide](./CONTROLLERS_QUICKSTART.md) - Dashboard features guide

---

**Last Updated:** 2026-03-27  
**Status:** Action Plan Created

Silakan ikuti urutan langkah di atas dan report hasilnya!
