---
title: OpenWrt 19.07.2 状态页面增加 CPU 信息显示
date: 2020-08-07 12:29:15
tags:
---
{% blockquote 引用文章 https://www.popyone.com/post/30.html OPENWRT_x86(19.07.3)界面添加cpu温度和使用率 %}
{% endblockquote %}
---
安装软件包：`lm-sensors`、`lm-sensors-detect`

修改 `/usr/libexec/rpcd/luci` 文件，添加一个 `ubus` 的 `method`：
<pre style="font-family: 'SF Mono', consolas, Menlo, 'PingFang SC', 'Microsoft YaHei', monospace; font-size: 13px; line-height: 24px; white-space: break-spaces">
            return { result = args.localtime }
        end
    },
<span style="background: #dfd">
    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
    },
</span>
    getTimezones = {
</pre>

<!-- more -->

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

<pre style="font-family: 'SF Mono', consolas, Menlo, 'PingFang SC', 'Microsoft YaHei', monospace; font-size: 13px; line-height: 24px; white-space: break-spaces">
    "ubus": {
        "file": [ "list", "read", "stat" ],
        "iwinfo": [ "assoclist", "freqlist", "txpowerlist", "countrylist" ],
        "luci": [ "getConntrackList", "getInitList", "getLocaltime", "getProcessList", "getRealtimeStats", "getTimezones", "getLEDs", "getUSBDevices", "getSwconfigFeatures", "getSwconfigPortState", "getBlockDevices", "getMountPoints"<span style="background: #dfd">, "getCPUInfo"</span> ],
        "luci-rpc": [ "getBoardJSON", "getDHCPLeases", "getDSLStatus", "getDUIDHints", "getHostHints",  "getNetworkDevices", "getWirelessDevices" ],
</pre>

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

<pre style="font-family: 'SF Mono', consolas, Menlo, 'PingFang SC', 'Microsoft YaHei', monospace; font-size: 13px; line-height: 24px; white-space: break-spaces">
    var callSystemInfo = rpc.declare({
        object: 'system',
        method: 'info'
    });<span style="background: #dfd">
    var callCPUInfo = rpc.declare({
        object: 'luci',
        method: 'getCPUInfo'
    });</span>
    return baseclass.extend({
        title: _('System'),
        load: function() {
            return Promise.all([
                L.resolveDefault(callSystemBoard(), {}),
                L.resolveDefault(callSystemInfo(), {}),
<span style="background: #dfd">                L.resolveDefault(callCPUInfo(), {}),</span>
                fs.lines('/usr/lib/lua/luci/version.lua')
            ]);
        },
        render: function(data) {
            var boardinfo   = data[0],
                systeminfo  = data[1],
<span style="background: #dfd">                cpuinfo     = data[2],
                luciversion = data[3];</span>
...
            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,
<span style="background: #dfd">                _('CPU Info'),         cpuinfo.cpufreq + ' MHz / ' + cpuinfo.cputemp,</span>
                _('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
            ];
</pre>

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

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