Skip to content

Commit

Permalink
chore: refine
Browse files Browse the repository at this point in the history
  • Loading branch information
vernesong committed Nov 8, 2024
1 parent 4a31252 commit 6c1f40e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
8 changes: 2 additions & 6 deletions luci-app-openclash/luasrc/controller/openclash.lua
Original file line number Diff line number Diff line change
Expand Up @@ -830,23 +830,19 @@ end

function action_toolbar_show_sys()
local pid = luci.sys.exec("pidof clash |head -1 |tr -d '\n' 2>/dev/null")
local mem, cpu
local cpu
if pid and pid ~= "" then
mem = tonumber(luci.sys.exec(string.format("cat /proc/%s/status 2>/dev/null |grep -w VmRSS |awk '{print $2}'", pid)))
cpu = luci.sys.exec(string.format("top -b -n1 |grep -E '%s' 2>/dev/null |grep -v grep |awk '{for (i=1;i<=NF;i++) {if ($i ~ /clash/) break; else cpu=i}}; {print $cpu}' 2>/dev/null", pid))
if mem and cpu then
mem = fs.filesize(mem*1024) or "0 KB"
if cpu then
cpu = string.match(cpu, "%d+") or "0"
else
mem = "0 KB"
cpu = "0"
end
else
return
end
luci.http.prepare_content("application/json")
luci.http.write_json({
mem = mem,
cpu = cpu;
})
end
Expand Down
18 changes: 16 additions & 2 deletions luci-app-openclash/luasrc/view/openclash/status.htm
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@
var luci_protocol;
var ws_t;
var ws_c;
var ws_m;
var state_refresh;
var s;
var gr;
Expand Down Expand Up @@ -295,10 +296,15 @@
};

function ws_open() {
if (ws_t) {ws_t.close();}
if (ws_c) {ws_c.close();}
if (ws_m) {ws_m.close();}
ws_t = new WebSocket(luci_protocol+"/traffic?token="+status.dase);
ws_c = new WebSocket(luci_protocol+"/connections?token="+status.dase);
ws_m = new WebSocket(luci_protocol+"/memory?token="+status.dase);
ws_t.onmessage = ws_tmessage;
ws_c.onmessage = ws_cmessage;
ws_m.onmessage = ws_mmessage;
ws_t.onerror = ws_terror;
ws_t.onopen = function (event) {
ws_connect = true;
Expand Down Expand Up @@ -612,7 +618,6 @@
function show_sys() {
XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "toolbar_show_sys")%>', null, function(x, status) {
if (x && x.status == 200 && x.responseText != "") {
document.getElementById("mem_t").innerHTML = "<font style=\"color:green\">"+status.mem+"</font>";
if (status.cpu <= 50) {
document.getElementById("cpu_t").innerHTML = "<font style=\"color:green\">"+status.cpu+" %</font>";
}
Expand All @@ -627,7 +632,6 @@
}
}
else {
document.getElementById("mem_t").innerHTML = "<font style=\"color:green\">0 KB</font>";
document.getElementById("cpu_t").innerHTML = "<font style=\"color:green\">0 %</font>";
}
});
Expand Down Expand Up @@ -669,6 +673,16 @@
else {
document.getElementById("connect_t").innerHTML = "<font style=\"color:green\">0</font>";
}
};

function ws_mmessage(event) {
var data = JSON.parse(event.data)
if (data.inuse) {
document.getElementById("mem_t").innerHTML = "<font style=\"color:green\">"+bytesToSize(data.inuse)+"/S</font>";
}
else {
document.getElementById("mem_t").innerHTML = "<font style=\"color:green\">0 KB</font>";
}
show_sys();
};

Expand Down

0 comments on commit 6c1f40e

Please sign in to comment.