个人博客

OPENWRT_x86(19.07.3)界面添加cpu温度和使用率

16 07月
作者:西洪室|分类:技术
编译内核,Utilitlest添加lm-sensors lm-sensors-detect

image.png

 一、修改/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状态"


浏览3988 评论4
返回
目录
返回
首页
mysql5.6 升级 mysql5.7 Cloudflare泛域名证书申请及续订

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

  • 评论列表
  • Eric 于 2019-12-06 11:04:15  回复
  • L.resolveDefault(callCPUInfo(), {}), 这行没有标红,漏改了,调了一晚上。
    • 西洪室 于 2019-12-06 13:59:55  回复
    • 多谢提醒,已经加上红标了
  • 五花肉 于 2020-03-11 01:39:58  回复
  • 请问树莓派刷openwrt该如何显示温度?sensors获取不到温度,cat temp不能更新
    • 西洪室 于 2020-04-20 20:51:51  回复
    • 不知道你是哪个版本,18.06版,cat temp不能自动更新,可能你修改的10-system.htm这个文件,应该要修改/usr/lib/lua/luci/view/admin_status/index.htm