OpenWrt 19.07.2 状态页面增加 CPU 信息显示


安装软件包:lm-sensorslm-sensors-detect

修改 /usr/libexec/rpcd/luci 文件,添加一个 ubusmethod

            return { result = args.localtime }
        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
    },

    getTimezones = {

修改 /usr/share/rpcd/acl.d/luci-base.json ,给刚刚添加的 getCPUInfo 添加权限:

    "ubus": {
        "file": [ "list", "read", "stat" ],
        "iwinfo": [ "assoclist", "freqlist", "txpowerlist", "countrylist" ],
        "luci": [ "getConntrackList", "getInitList", "getLocaltime", "getProcessList", "getRealtimeStats", "getTimezones", "getLEDs", "getUSBDevices", "getSwconfigFeatures", "getSwconfigPortState", "getBlockDevices", "getMountPoints", "getCPUInfo" ],
        "luci-rpc": [ "getBoardJSON", "getDHCPLeases", "getDSLStatus", "getDUIDHints", "getHostHints",  "getNetworkDevices", "getWirelessDevices" ],

修改 /www/luci-static/resources/view/status/include/10_system.js

    var callSystemInfo = rpc.declare({
        object: 'system',
        method: 'info'
    });
    var callCPUInfo = rpc.declare({
        object: 'luci',
        method: 'getCPUInfo'
    });
    return baseclass.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,
                _('Model'),            boardinfo.model,
                _('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
            ];

使用命令 /etc/init.d/rpcd restart 重启 rpcd 服务,最后清除浏览器页面缓存。

PS:可使用 ubus call luci getCPUInfo 检查执行是否有异常。