File size: 4,771 Bytes
279efce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# Monitor System Resources

You are helping the user monitor live system resource usage (CPU, RAM, disk I/O, network).

## Task

1. **Quick resource overview:**
   ```bash
   # System overview
   top -b -n 1 | head -20

   # Better overview with htop (if installed)
   htop

   # Modern alternative: btop
   btop
   ```

2. **CPU monitoring:**
   ```bash
   # CPU usage summary
   mpstat 1 5  # 5 samples, 1 second interval

   # Per-core usage
   mpstat -P ALL 1 5

   # Top CPU consumers
   ps aux --sort=-%cpu | head -10

   # CPU frequency and temperature
   watch -n 1 "grep MHz /proc/cpuinfo | head -20 && sensors"
   ```

3. **Memory monitoring:**
   ```bash
   # Memory usage
   free -h

   # Detailed memory info
   cat /proc/meminfo

   # Top memory consumers
   ps aux --sort=-%mem | head -10

   # Memory usage by process
   smem -tk  # If smem is installed

   # Watch memory usage
   watch -n 1 free -h
   ```

4. **Disk I/O monitoring:**
   ```bash
   # I/O statistics
   iostat -x 1 5

   # Disk usage by device
   iotop -o  # Only show active I/O

   # Per-process I/O
   iotop -P

   # Watch disk I/O
   watch -n 1 "iostat -x | grep -E 'Device|sd|nvme'"
   ```

5. **Network monitoring:**
   ```bash
   # Network interface statistics
   ifstat 1 5

   # Bandwidth per interface
   bmon

   # Network speed
   iftop

   # Connection summary
   ss -s

   # Active connections
   nethogs
   ```

6. **Combined system monitoring:**
   ```bash
   # All-in-one monitoring
   glances

   # Custom dashboard
   watch -n 1 '
   echo "=== CPU ==="
   top -b -n 1 | head -5 | tail -2
   echo ""
   echo "=== Memory ==="
   free -h | grep -E "Mem|Swap"
   echo ""
   echo "=== Disk I/O ==="
   iostat -x 1 1 | grep -E "Device|sd|nvme" | head -5
   echo ""
   echo "=== Network ==="
   ifstat 1 1 | tail -1
   '
   ```

7. **GPU monitoring (AMD):**
   ```bash
   # AMD GPU usage
   radeontop

   # GPU sensors
   watch -n 1 "rocm-smi"

   # Detailed GPU info
   watch -n 1 "rocm-smi --showuse --showmemuse --showtemp"
   ```

8. **Process tree with resource usage:**
   ```bash
   # Process tree
   pstree -p

   # Resource-aware process tree
   ps auxf

   # Find resource hogs
   ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head -20
   ```

9. **System load monitoring:**
   ```bash
   # Load average
   uptime

   # Load over time
   watch -n 1 "uptime && cat /proc/loadavg"

   # Who's causing load
   tload -d 1
   ```

10. **Create monitoring script:**
    ```bash
    cat > /tmp/system-monitor.sh << 'EOF'
    #!/bin/bash
    echo "System Resource Monitor - $(date)"
    echo "========================================"
    echo ""
    echo "CPU Usage:"
    mpstat 1 1 | tail -1
    echo ""
    echo "Memory:"
    free -h | grep -E "Mem|Swap"
    echo ""
    echo "Load Average:"
    uptime
    echo ""
    echo "Top 5 CPU Processes:"
    ps aux --sort=-%cpu | head -6 | tail -5
    echo ""
    echo "Top 5 Memory Processes:"
    ps aux --sort=-%mem | head -6 | tail -5
    echo ""
    echo "Disk Usage:"
    df -h / /home
    echo ""
    echo "Disk I/O:"
    iostat -x 1 1 | grep -E "sd|nvme"
    EOF

    chmod +x /tmp/system-monitor.sh
    /tmp/system-monitor.sh
    ```

## Present Summary to User

Provide snapshot of:
- **CPU:** Usage %, load average, top processes
- **Memory:** Used/Free, swap usage, top consumers
- **Disk:** Usage %, I/O wait, active reads/writes
- **Network:** Bandwidth usage, active connections
- **GPU:** Usage % (if applicable)

Flag any concerning patterns:
- High CPU usage (>80% sustained)
- Low memory (<10% free)
- High swap usage
- Disk I/O bottlenecks
- Network saturation

## Install Monitoring Tools if Needed

```bash
# Install commonly needed tools
sudo apt install -y \
    htop \
    btop \
    iotop \
    iftop \
    nethogs \
    glances \
    sysstat \
    bmon \
    radeontop
```

## Troubleshooting High Usage

**High CPU:**
- Identify process: `top` or `htop`
- Check if legitimate (updates, backups, encoding)
- Kill if necessary: `kill -15 PID`

**High Memory:**
- Check for memory leaks: `ps aux --sort=-%mem`
- Clear caches: `sudo sync && sudo sysctl -w vm.drop_caches=3`
- Check for swap thrashing

**High Disk I/O:**
- Identify process: `iotop -o`
- Check if expected (backups, indexing, updates)
- Monitor disk health: `/check-disk-errors`

**High Network:**
- Identify connections: `nethogs` or `iftop`
- Check for unexpected traffic
- Use `ss -tunap` to see connections

## Notes

- Most monitoring tools require sudo for full functionality
- `glances` provides best all-in-one view
- `btop` is modern, colorful alternative to `htop`
- Some tools need to be installed separately
- For persistent monitoring, consider setting up Prometheus/Grafana
- Check `systemd-cgtop` for cgroup resource usage