OPENWRT_x86(19.07.3)界面添加cpu温度和使用率
编译内核,Utilitlest添加lm-sensors lm-sensors-detect
一、修改/usr/libexec/rpcd/luci文件,添加一个ubus的method:
getLocaltime = {
call = function(args)
return { result = os.time() }
end
},
getCPUInfo = {
call = function()
local sys = require "luci.sys"
local rv = {}
rv.cpufreq = sys.exec("grep 'MHz' /proc/cpuinfo | cut -c11- | sed -n '1p' | tr -d '\n'")
rv.cputemp = sys.exec("sensors|grep Core|awk '{print $1,$2,$3}'|tr '\n' ' '")
return rv
end
},
setLocaltime = {
args = { localtime = 0 },
call = function(args)
local sys = require "luci.sys"
local date = os.date("*t", args.localtime)
if date then
sys.call("date -s '%04d-%02d-%02d %02d:%02d:%02d' >/dev/null" %{ date.year, date.month, date.day, date.hour, date.min, date.sec })
sys.call("/etc/init.d/sysfixtime restart >/dev/null")
end
return { result = args.localtime }
end
},
二、修改/usr/share/rpcd/acl.d/luci-base.json,给刚刚添加的getCPUInfo添加权限:
vim /usr/share/rpcd/acl.d/luci-base.json
......
"file": [ "list", "read", "stat" ],
"iwinfo": [ "assoclist", "freqlist", "txpowerlist", "countrylist" ],
"luci": [ "getConntrackList", "getDUIDHints", "getInitList", "getLocaltime", "getProcessList", "getRealtimeStats", "getTimezones", "getLEDs", "getUSBDevices", "getSwconfigFeatures", "getSwconfigPortState", "getBlockDevices", "getMountPoints", "getCPUInfo" ],
"luci-rpc": [ "getBoardJSON", "getDHCPLeases", "getDSLStatus", "getHostHints", "getNetworkDevices", "getWirelessDevices" ],
......
三、修改/www/luci-static/resources/view/status/include/10_system.js
'use strict';'require fs';'require rpc';var callSystemBoard=rpc.declare({object:'system',method:'board'});var callSystemInfo=rpc.declare({object:'system',method:'info'});var callCPUInfo = rpc.declare({object: 'luci', method: 'getCPUInfo'});return L.Class.extend({title:_('System'),load:function(){return Promise.all([L.resolveDefault(callSystemBoard(),{}),L.resolveDefault(callSystemInfo(),{}),L.resolveDefault(callCPUInfo(), {}),fs.lines('/usr/lib/lua/luci/version.lua')]);},render:function(data){var boardinfo=data[0],systeminfo=data[1],cpuinfo=data[2],luciversion=data[3];luciversion=luciversion.filter(function(l){return l.match(/^\s*(luciname|luciversion)\s*=/);}).map(function(l){return l.replace(/^\s*\w+\s*=\s*['"]([^'"]+)['"].*$/,'$1');}).join(' ');var datestr=null;if(systeminfo.localtime){var date=new Date(systeminfo.localtime*1000);datestr='%04d-%02d-%02d %02d:%02d:%02d'.format(date.getUTCFullYear(),date.getUTCMonth()+1,date.getUTCDate(),date.getUTCHours(),date.getUTCMinutes(),date.getUTCSeconds());}
var fields=[_('Hostname'),boardinfo.hostname,_('Model'),boardinfo.model,_('CPU Info'),cpuinfo.cpufreq+'MHz'+cpuinfo.cputemp,_('Firmware Version'),(L.isObject(boardinfo.release)?boardinfo.release.description+' / ':'')+(luciversion||''),_('Kernel Version'),boardinfo.kernel,_('Local Time'),datestr,_('Uptime'),systeminfo.uptime?'%t'.format(systeminfo.uptime):null,_('Load Average'),Array.isArray(systeminfo.load)?'%.2f, %.2f, %.2f'.format(systeminfo.load[0]/65535.0,systeminfo.load[1]/65535.0,systeminfo.load[2]/65535.0):null];var table=E('div',{'class':'table'});for(var i=0;i<fields.length;i+=2){table.appendChild(E('div',{'class':'tr'},[E('div',{'class':'td left','width':'33%'},[fields[i]]),E('div',{'class':'td left'},[(fields[i+1]!=null)?fields[i+1]:'?'])]));}
上面是编译后的10_system.js,排版结构紧凑,下面是编译前的index.js
......
var callSystemInfo = rpc.declare({
object: 'system',
method: 'info'
});
var callCPUInfo = rpc.declare({
object: 'luci',
method: 'getCPUInfo'
});
return L.Class.extend({
title: _('System'),
load: function() {
return Promise.all([
L.resolveDefault(callSystemBoard(), {}),
L.resolveDefault(callSystemInfo(), {}),
L.resolveDefault(callCPUInfo(), {}),
fs.lines('/usr/lib/lua/luci/version.lua')
]);
},
render: function(data) {
var boardinfo = data[0],
systeminfo = data[1],
cpuinfo = data[2],
luciversion = data[3];
......
var fields = [
_('Hostname'), boardinfo.hostname,
_('Architecture'), boardinfo.system,
_('Firmware Version'), (L.isObject(boardinfo.release) ? boardinfo.release.description + ' / ' : '') + (luciversion || ''),
_('Kernel Version'), boardinfo.kernel,
_('Local Time'), datestr,
_('Uptime'), systeminfo.uptime ? '%t'.format(systeminfo.uptime) : null,
_('CPU Info'), cpuinfo.cpufreq + ' MHz ' + cpuinfo.cputemp,
_('Load Average'), Array.isArray(systeminfo.load) ? '%.2f, %.2f, %.2f'.format(
systeminfo.load[0] / 65535.0,
systeminfo.load[1] / 65535.0,
systeminfo.load[2] / 65535.0
) : null
];
......
如果需要将该界面编译到固件,可以修改下面4个文件:
feeds/luci/modules/luci-base/root/usr/libexec/rpcd/luci
feeds/luci/modules/luci-base/root/usr/share/rpcd/acl.d/luci-base.json
feeds/luci/modules/luci-mod-status/htdocs/luci-static/resources/view/status/include/10_system.js
feeds/luci/modules/luci-base/po/zh-cn/base.po
添加CPU Info中文显示
vim feeds/luci/modules/luci-base/po/zh-cn/base.po
....
#: modules/luci-mod-status/htdocs/luci-static/resources/view/status/include/10_system.js:40
msgid "Architecture"
msgstr "架构"
#: modules/luci-mod-status/htdocs/luci-static/resources/view/status/include/10_system.js:66
msgid "CPU Info"
msgstr "CPU状态"
目录 返回
首页
- 评论列表