File size: 5,835 Bytes
529090e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import express from 'express';
import si from 'systeminformation';
import { eventBus } from '../mcp/EventBus.js';

const router = express.Router();

// Get top processes by CPU usage
router.get('/processes', async (req, res) => {
  try {
    const data = await si.processes();

    // Sort by CPU usage and take top 5
    const topProcesses = data.list
      .sort((a, b) => (b.cpu || 0) - (a.cpu || 0))
      .slice(0, 5)
      .map(p => ({
        name: p.name || 'Unknown',
        cpu: Number((p.cpu || 0).toFixed(1)),
        mem: Number((p.mem || 0).toFixed(1)),
        pid: p.pid
      }));

    res.json(topProcesses);
  } catch (error) {
    console.error('Error fetching processes:', error);
    res.status(500).json({ error: 'Failed to fetch process information' });
  }
});

// Get system information (CPU, memory, etc.)
router.get('/system', async (req, res) => {
  try {
    const [cpu, mem, osInfo, currentLoad, cpuTemp] = await Promise.all([
      si.cpu(),
      si.mem(),
      si.osInfo(),
      si.currentLoad(),
      si.cpuTemperature()
    ]);

    const systemInfo = {
      cpu: {
        manufacturer: cpu.manufacturer,
        brand: cpu.brand,
        cores: cpu.cores,
        physicalCores: cpu.physicalCores,
        speed: cpu.speed,
        temperature: cpuTemp.main || null
      },
      memory: {
        total: mem.total,
        used: mem.used,
        available: mem.available,
        usedPercent: Number(((mem.used / mem.total) * 100).toFixed(1))
      },
      os: {
        platform: osInfo.platform,
        distro: osInfo.distro,
        release: osInfo.release,
        arch: osInfo.arch
      },
      load: {
        avgLoad: Number(currentLoad.avgLoad.toFixed(2)),
        currentLoad: Number(currentLoad.currentLoad.toFixed(1)),
        currentLoadUser: Number(currentLoad.currentLoadUser.toFixed(1)),
        currentLoadSystem: Number(currentLoad.currentLoadSystem.toFixed(1))
      }
    };

    // Check for high load and emit event
    if (systemInfo.load.currentLoad > 90) {
      eventBus.emitEvent({
        type: 'system.alert',
        timestamp: new Date().toISOString(),
        source: 'sys.ts',
        payload: { message: 'High CPU Load detected', load: systemInfo.load.currentLoad }
      });
    }

    res.json(systemInfo);
  } catch (error) {
    console.error('Error fetching system info:', error);
    res.status(500).json({ error: 'Failed to fetch system information' });
  }
});

// Get network information
router.get('/network', async (req, res) => {
  try {
    const [networkStats, networkConnections] = await Promise.all([
      si.networkStats(),
      si.networkConnections()
    ]);

    // Get the primary network interface stats
    const primaryInterface = networkStats.find(stat => stat.rx_sec || stat.tx_sec);

    const networkInfo = {
      stats: primaryInterface ? {
        interface: primaryInterface.iface,
        rx_sec: primaryInterface.rx_sec || 0,
        tx_sec: primaryInterface.tx_sec || 0,
        rx_bytes: primaryInterface.rx_bytes || 0,
        tx_bytes: primaryInterface.tx_bytes || 0
      } : null,
      connections: networkConnections.length,
      activeConnections: networkConnections.filter(conn => conn.state === 'ESTABLISHED').length
    };

    res.json(networkInfo);
  } catch (error) {
    console.error('Error fetching network info:', error);
    res.status(500).json({ error: 'Failed to fetch network information' });
  }
});

// Get GPU information
router.get('/gpu', async (req, res) => {
  try {
    const graphics = await si.graphics();

    const gpuInfo = graphics.controllers.map(gpu => ({
      vendor: gpu.vendor,
      model: gpu.model,
      vram: gpu.vram,
      temperature: (gpu as any).temperatureGpu || null,
      utilizationGpu: gpu.utilizationGpu || null
    }));

    res.json(gpuInfo);
  } catch (error) {
    console.error('Error fetching GPU info:', error);
    res.status(500).json({ error: 'Failed to fetch GPU information' });
  }
});

// Get Real Security Anomalies (No Mock)
router.get('/security/anomalies', async (req, res) => {
  try {
    const [processes, connections] = await Promise.all([
      si.processes(),
      si.networkConnections()
    ]);

    const anomalies = [];

    // 1. High Resource Processes (Potential Crypto Miners / Runaway scripts)
    const heavyProcs = processes.list.filter(p => p.cpu > 50 || p.mem > 10); // >50% CPU or >10% Mem
    heavyProcs.forEach(p => {
      anomalies.push({
        id: `PROC-${p.pid}`,
        type: 'RESOURCE_ANOMALY',
        severity: p.cpu > 80 ? 'CRITICAL' : 'HIGH',
        source: p.name,
        details: `PID: ${p.pid} | CPU: ${p.cpu.toFixed(1)}% | MEM: ${p.mem.toFixed(1)}%`,
        path: p.path,
        timestamp: new Date().toISOString()
      });
    });

    // 2. Exposed Ports (Listening on 0.0.0.0)
    const listeningPorts = connections.filter(c => c.state === 'LISTEN' && (c.localAddress === '0.0.0.0' || c.localAddress === '::'));
    listeningPorts.forEach(c => {
        // Filter out standard safe ports if needed, but show all for visibility
        anomalies.push({
            id: `NET-${c.localPort}`,
            type: 'OPEN_PORT',
            severity: 'MEDIUM',
            source: c.process || `Port ${c.localPort}`,
            details: `Listening on ${c.localAddress}:${c.localPort} (${c.protocol})`,
            path: 'NETWORK',
            timestamp: new Date().toISOString()
        });
    });

    res.json({
        count: anomalies.length,
        critical: anomalies.filter(a => a.severity === 'CRITICAL').length,
        high: anomalies.filter(a => a.severity === 'HIGH').length,
        anomalies
    });

  } catch (error) {
    console.error('Error scanning anomalies:', error);
    res.status(500).json({ error: 'Failed to scan system anomalies' });
  }
});

export default router;