Skip to content

Commit

Permalink
Merge pull request #1125 from hitalos/feature/improve-monitor
Browse files Browse the repository at this point in the history
🚀 Adding total ram to monitor
  • Loading branch information
Fenny authored Jan 22, 2021
2 parents 3762a85 + bba55cf commit ba7fc03
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
29 changes: 27 additions & 2 deletions middleware/monitor/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ var index = []byte(`<!DOCTYPE html>
color: #777;
}
h2 span.ram_os {
color: rgba(255, 150, 0, .8);
}
h2 span.ram_total {
color: rgba(0, 200, 0, .8);
}
canvas {
width: 200px;
height: 180px;
Expand Down Expand Up @@ -92,7 +100,7 @@ var index = []byte(`<!DOCTYPE html>
<div class="row">
<div class="column">
<div class="metric">Memory Usage</div>
<h2 id="ramMetric">0.00 MB</h2>
<h2 id="ramMetric" title="PID used / OS used / OS total">0.00 MB</h2>
</div>
<div class="column">
<canvas id="ramChart"></canvas>
Expand Down Expand Up @@ -200,6 +208,21 @@ var index = []byte(`<!DOCTYPE html>
});
}
ramChart.data.datasets.push({
data: [],
lineTension: 0.2,
pointRadius: 0,
backgroundColor: 'rgba(255, 200, 0, .6)',
borderColor: 'rgba(255, 150, 0, .8)',
})
ramChart.data.datasets.push({
data: [],
lineTension: 0.2,
pointRadius: 0,
backgroundColor: 'rgba(0, 255, 0, .4)',
borderColor: 'rgba(0, 200, 0, .8)',
})
// function init() {
// charts.forEach(chart => {
// chart.data.datasets[0].data = JSON.parse(localStorage.getItem(chart.canvas.id)) || []
Expand All @@ -212,11 +235,13 @@ var index = []byte(`<!DOCTYPE html>
cpuOS = json.os.cpu.toFixed(1);
cpuMetric.innerHTML = cpu + '% <span>' + cpuOS + '%</span>';
ramMetric.innerHTML = formatBytes(json.pid.ram) + ' <span>' + formatBytes(json.os.ram) + '</span>';
ramMetric.innerHTML = formatBytes(json.pid.ram) + '<span> / </span><span class="ram_os">' + formatBytes(json.os.ram) + '<span><span> / </span><span class="ram_total">' + formatBytes(json.os.total_ram) + '</span>';
rtimeMetric.innerHTML = rtime + 'ms <span>client</span>';
connsMetric.innerHTML = json.pid.conns + ' <span>' + json.os.conns + '</span>';
cpuChart.data.datasets[0].data.push(cpu);
ramChart.data.datasets[2].data.push((json.os.total_ram / 1e6).toFixed(2));
ramChart.data.datasets[1].data.push((json.os.ram / 1e6).toFixed(2));
ramChart.data.datasets[0].data.push((json.pid.ram / 1e6).toFixed(2));
rtimeChart.data.datasets[0].data.push(rtime);
connsChart.data.datasets[0].data.push(json.pid.conns);
Expand Down
29 changes: 27 additions & 2 deletions middleware/monitor/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@
color: #777;
}

h2 span.ram_os {
color: rgba(255, 150, 0, .8);
}

h2 span.ram_total {
color: rgba(0, 200, 0, .8);
}

canvas {
width: 200px;
height: 180px;
Expand Down Expand Up @@ -90,7 +98,7 @@ <h2 id="cpuMetric">0.00%</h2>
<div class="row">
<div class="column">
<div class="metric">Memory Usage</div>
<h2 id="ramMetric">0.00 MB</h2>
<h2 id="ramMetric" title="PID used / OS used / OS total">0.00 MB</h2>
</div>
<div class="column">
<canvas id="ramChart"></canvas>
Expand Down Expand Up @@ -198,6 +206,21 @@ <h2 id="connsMetric">0</h2>
});
}

ramChart.data.datasets.push({
data: [],
lineTension: 0.2,
pointRadius: 0,
backgroundColor: 'rgba(255, 200, 0, .6)',
borderColor: 'rgba(255, 150, 0, .8)',
})
ramChart.data.datasets.push({
data: [],
lineTension: 0.2,
pointRadius: 0,
backgroundColor: 'rgba(0, 255, 0, .4)',
borderColor: 'rgba(0, 200, 0, .8)',
})

// function init() {
// charts.forEach(chart => {
// chart.data.datasets[0].data = JSON.parse(localStorage.getItem(chart.canvas.id)) || []
Expand All @@ -210,11 +233,13 @@ <h2 id="connsMetric">0</h2>
cpuOS = json.os.cpu.toFixed(1);

cpuMetric.innerHTML = cpu + '% <span>' + cpuOS + '%</span>';
ramMetric.innerHTML = formatBytes(json.pid.ram) + ' <span>' + formatBytes(json.os.ram) + '</span>';
ramMetric.innerHTML = formatBytes(json.pid.ram) + '<span> / </span><span class="ram_os">' + formatBytes(json.os.ram) + '<span><span> / </span><span class="ram_total">' + formatBytes(json.os.total_ram) + '</span>';
rtimeMetric.innerHTML = rtime + 'ms <span>client</span>';
connsMetric.innerHTML = json.pid.conns + ' <span>' + json.os.conns + '</span>';

cpuChart.data.datasets[0].data.push(cpu);
ramChart.data.datasets[2].data.push((json.os.total_ram / 1e6).toFixed(2));
ramChart.data.datasets[1].data.push((json.os.ram / 1e6).toFixed(2));
ramChart.data.datasets[0].data.push((json.pid.ram / 1e6).toFixed(2));
rtimeChart.data.datasets[0].data.push(rtime);
connsChart.data.datasets[0].data.push(json.pid.conns);
Expand Down
16 changes: 10 additions & 6 deletions middleware/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@ type statsPID struct {
Conns int `json:"conns"`
}
type statsOS struct {
CPU float64 `json:"cpu"`
RAM uint64 `json:"ram"`
Conns int `json:"conns"`
CPU float64 `json:"cpu"`
RAM uint64 `json:"ram"`
TotalRAM uint64 `json:"total_ram"`
Conns int `json:"conns"`
}

var (
monitPidCpu atomic.Value
monitPidRam atomic.Value
monitPidConns atomic.Value

monitOsCpu atomic.Value
monitOsRam atomic.Value
monitOsConns atomic.Value
monitOsCpu atomic.Value
monitOsRam atomic.Value
monitOsTotalRam atomic.Value
monitOsConns atomic.Value
)

var (
Expand Down Expand Up @@ -78,6 +80,7 @@ func New() fiber.Handler {

data.OS.CPU = monitOsCpu.Load().(float64)
data.OS.RAM = monitOsRam.Load().(uint64)
data.OS.TotalRAM = monitOsTotalRam.Load().(uint64)
data.OS.Conns = monitOsConns.Load().(int)
mutex.Unlock()
return c.Status(fiber.StatusOK).JSON(data)
Expand All @@ -101,6 +104,7 @@ func updateStatistics(p *process.Process) {

if osMem, _ := mem.VirtualMemory(); osMem != nil {
monitOsRam.Store(osMem.Used)
monitOsTotalRam.Store(osMem.Total)
}

pidConns, _ := net.ConnectionsPid("tcp", p.Pid)
Expand Down

0 comments on commit ba7fc03

Please sign in to comment.