[转载]N5105 Proxmox VE (PVE) 主页添加温度硬盘等信息

sam 2023年10月11日 436次浏览

原文地址 rushb.pro

前言以下配置只适用于 N5105 使用 NVMe 硬盘的情况,其它机型请自行酌情修改。(例如更多的 CPU 核心、机械硬盘、风扇转速等) 效果图显示 CPU 实时频率、各核心温度、硬盘型号、寿命、...

以下配置只适用于 N5105 使用 NVMe 硬盘的情况,其它机型请自行酌情修改。(例如更多的 CPU 核心、机械硬盘、风扇转速等)

显示 CPU 实时频率、各核心温度、硬盘型号、寿命、温度等信息

安装 lm-sensors

apt install lm-sensors patch

初始化 sensors

sensors-detect

给予 smartctl 权限 (用于读取 NVMe 信息)

chmod +s /usr/sbin/smartctl

修改 /usr/share/perl5/PVE/API2/Nodes.pm

添加在 PVE::pvecfg::version_text(); 的下一行

$res->{sensors_json} = `sensors -j`; # 获取 CPU 、主板温度及风扇转速
    $res->{smartctl_nvme_json} = `smartctl -a -j /dev/nvme?`; # 读取 nvme 硬盘 S.M.A.R.T. 值,获取硬盘寿命、容量、温度等
    $res->{cpusensors} = `lscpu | grep MHz`; # 读取 CPU 频率

修改 /usr/share/pve-manager/js/pvemanagerlib.js

添加信息

itemId: 'version' 所处的 json 块后添加

{
        itemId: 'MHz',
        colspan: 2,
        printBar: false,
        title: gettext('CPU频率'),
        textField: 'cpusensors',
        renderer:function(value){
            var f0 = value.match(/CPU MHz.*?([\d]+)/)[1];
            var f1 = value.match(/CPU min MHz.*?([\d]+)/)[1];
            var f2 = value.match(/CPU max MHz.*?([\d]+)/)[1];
            return `实时: ${f0} MHz || 最小: ${f1} MHz | 最大: ${f2} MHz `
        }
    },
    {
        itemId: 'thermal',
        colspan: 2,
        printBar: false,
        title: gettext('CPU温度'),
        textField: 'sensors_json',
        renderer: function(value) {
            value = value.replace(/temp([0-9]{1,})_input/g,'input');
            if (value.indexOf("coretemp-isa") != -1 ) {
                value = value.replace(/coretemp-isa-(.{4})/g,'coretemp-isa');
                value = JSON.parse(value);
                try {var cpu_Intel = 'CPU: ' + value['coretemp-isa']['Package id 0']['input'].toFixed(1) + '°C';} catch(e) {var cpu_Intel = '';} 
                if (cpu_Intel.length > 0) {
                    try {var cpu0 = ' || 核心 0 : ' + value['coretemp-isa']['Core 0']['input'].toFixed(1) + '°C';} catch(e) {var cpu0 = '';} 
                    try {var cpu1 = ' | 核心 1 : ' + value['coretemp-isa']['Core 1']['input'].toFixed(1) + '°C';} catch(e) {var cpu1 = '';} 
                    try {var cpu2 = ' | 核心 2 : ' + value['coretemp-isa']['Core 2']['input'].toFixed(1) + '°C';} catch(e) {var cpu2 = '';} 
                    try {var cpu3 = ' | 核心 3 : ' + value['coretemp-isa']['Core 3']['input'].toFixed(1) + '°C';} catch(e) {var cpu3 = '';} 
                    return `${cpu_Intel}${cpu0}${cpu1}${cpu2}${cpu3}`;
                } 
            } else {
                return `提示: CPU 温度读取异常`;
            }
        }
    },
    {        
        itemId: 'nvme_ssd',
        colspan: 2,
        printBar: false,
        title: gettext('NVME'),
        textField: 'smartctl_nvme_json',
        renderer: function(value) {
            value = JSON.parse(value);
            if (value['model_name']) {
                try {var model_name = value['model_name'];} catch(e) {var model_name = '';} 
                try {var percentage_used = ' | 使用寿命: ' + value['nvme_smart_health_information_log']['percentage_used'].toFixed(0) + '% ';} catch(e) {var percentage_used = '';} 
                try {var data_units_read = value['nvme_smart_health_information_log']['data_units_read']*512/1024/1024;var data_units_read = '(读: ' + data_units_read.toFixed(2) + 'GB, ';} catch(e) {var data_units_read = '';} 
                try {var data_units_written = value['nvme_smart_health_information_log']['data_units_written']*512/1024/1024;var data_units_written = '写: ' + data_units_written.toFixed(2) + 'GB)';} catch(e) {var data_units_written = '';} 
                try {var power_on_time = ' | 通电: ' + value['power_on_time']['hours'].toFixed(0) + '小时';} catch(e) {var power_on_time = '';} 
                try {var temperature = ' | 温度: ' + value['temperature']['current'].toFixed(1) + '°C';} catch(e) {var temperature = '';} 
                return `${model_name}${percentage_used}${data_units_read}${data_units_written}${power_on_time}${temperature}`;
            } else { 
                return `提示: 未安装硬盘或已直通硬盘控制器`;
            }
        }
    },

PVE 8.0

在 PVE 8.0 中,lscpu | grep MHz 的输出格式有变化:

root@PVE:~# lscpu | grep MHz
CPU(s) scaling MHz:              93%
CPU max MHz:                     2900.0000
CPU min MHz:                     800.0000

我这里通过 CPU(s) scaling MHz 来换算实时频率,如果你的输出格式不同,请自行修改。

{
        itemId: 'MHz',
        colspan: 2,
        printBar: false,
        title: gettext('CPU频率'),
        textField: 'cpusensors',
        renderer:function(value){
                var f1 = value.match(/CPU min MHz.*?([\d]+)/)[1];
                var f2 = value.match(/CPU max MHz.*?([\d]+)/)[1];
                var f0 = value.match(/CPU.*scaling MHz.*?([\d]+)/)[1];
                var f0 = f0*f2/100;
            return `实时: ${f0} MHz || 最小: ${f1} MHz | 最大: ${f2} MHz `
        }
    },
    {
        itemId: 'thermal',
        colspan: 2,
        printBar: false,
        title: gettext('CPU温度'),
        textField: 'sensors_json',
        renderer: function(value) {
            value = value.replace(/temp([0-9]{1,})_input/g,'input');
            if (value.indexOf("coretemp-isa") != -1 ) {
                value = value.replace(/coretemp-isa-(.{4})/g,'coretemp-isa');
                value = JSON.parse(value);
                try {var cpu_Intel = 'CPU: ' + value['coretemp-isa']['Package id 0']['input'].toFixed(1) + '°C';} catch(e) {var cpu_Intel = '';} 
                if (cpu_Intel.length > 0) {
                    try {var cpu0 = ' || 核心 0 : ' + value['coretemp-isa']['Core 0']['input'].toFixed(1) + '°C';} catch(e) {var cpu0 = '';} 
                    try {var cpu1 = ' | 核心 1 : ' + value['coretemp-isa']['Core 1']['input'].toFixed(1) + '°C';} catch(e) {var cpu1 = '';} 
                    try {var cpu2 = ' | 核心 2 : ' + value['coretemp-isa']['Core 2']['input'].toFixed(1) + '°C';} catch(e) {var cpu2 = '';} 
                    try {var cpu3 = ' | 核心 3 : ' + value['coretemp-isa']['Core 3']['input'].toFixed(1) + '°C';} catch(e) {var cpu3 = '';} 
                    return `${cpu_Intel}${cpu0}${cpu1}${cpu2}${cpu3}`;
                } 
            } else {
                return `提示: CPU 温度读取异常`;
            }
        }
    },
    {        
        itemId: 'nvme_ssd',
        colspan: 2,
        printBar: false,
        title: gettext('NVME'),
        textField: 'smartctl_nvme_json',
        renderer: function(value) {
            value = JSON.parse(value);
            if (value['model_name']) {
                try {var model_name = value['model_name'];} catch(e) {var model_name = '';} 
                try {var percentage_used = ' | 使用寿命: ' + value['nvme_smart_health_information_log']['percentage_used'].toFixed(0) + '% ';} catch(e) {var percentage_used = '';} 
                try {var data_units_read = value['nvme_smart_health_information_log']['data_units_read']*512/1024/1024;var data_units_read = '(读: ' + data_units_read.toFixed(2) + 'GB, ';} catch(e) {var data_units_read = '';} 
                try {var data_units_written = value['nvme_smart_health_information_log']['data_units_written']*512/1024/1024;var data_units_written = '写: ' + data_units_written.toFixed(2) + 'GB)';} catch(e) {var data_units_written = '';} 
                try {var power_on_time = ' | 通电: ' + value['power_on_time']['hours'].toFixed(0) + '小时';} catch(e) {var power_on_time = '';} 
                try {var temperature = ' | 温度: ' + value['temperature']['current'].toFixed(1) + '°C';} catch(e) {var temperature = '';} 
                return `${model_name}${percentage_used}${data_units_read}${data_units_written}${power_on_time}${temperature}`;
            } else { 
                return `提示: 未安装硬盘或已直通硬盘控制器`;
            }
        }
    },

修改 div 高度

如不修改会导致信息显示不全
搜索 widget.pveNodeStatus

height: 390,

修改 /usr/share/javascript/proxmox-widget-toolkit/proxmoxlib.js

找到以下字段

Ext.Msg.show({
title: gettext('No valid subscription'),

修改为

void({
title: gettext('No valid subscription'),
systemctl restart pveproxy

基于 tty228 的文章进行轻量化修改,去除了一些 N5105 不支持的功能,如需要完整版请移步原文:
【Proxmox VE】PVE 首页显示 CPU、主板、NVME、硬盘 温度等信息