menu Chancel's blog
rss_feed
Chancel's blog
有善始者实繁,能克终者盖寡。

Kde Plasma配置conky实现性能监控

作者:Chancel Yang, 创建:2024-05-30, 字数:6025, 已阅:503, 最后更新:2024-05-30

Conky是一个轻量级的Linux系统监视工具,Conky可以显示大量关于系统状态的信息,包括但不限于CPU使用率、内存使用情况、磁盘空间、网络状态、系统负载、温度、电池状态、天气、时间以及运行的进程等

conky有不少兼容性问题,包括但不限于:

  1. 无法设置透明
  2. 在多个虚拟桌面之间切换不显示
  3. 无法处于桌面总是置顶

如果你也遇到了以上问题,可以参考一下我文末的配置文件

下面是配置过后在 Kde plasma 桌面右侧显示的监控效果

首先,在Ubuntu2204安装conky方法如下:

Bash
sudo apt update
sudo apt install conky

安装完成后,可以查看官方自带的模板文件: /etc/conky/conky.conf

其效果类似于下

创建自定义效果,首先要创建conky的配置文件夹

Bash
mkdir ~/.config/conky

并编辑conky的配置文件:~/.config/conky/conky.conf

参考我的配置文件如下:

TEXT
conky.config = {
    alignment = 'top_right',            -- 窗口对齐位置
    own_window_type = 'dock',           -- 窗口类型,设置为桌面窗口
    background = true,                  -- 是否在后台运行
    border_width = 1,                   -- 边框宽度
    cpu_avg_samples = 2,                -- CPU平均采样次数
    default_color = 'white',            -- 默认字体颜色
    default_outline_color = 'white',    -- 默认轮廓颜色
    default_shade_color = 'white',      -- 默认阴影颜色
    draw_borders = false,               -- 是否绘制边框
    draw_graph_borders = false,         -- 是否绘制图形边框
    draw_outline = false,               -- 是否绘制轮廓
    own_window_hints = 'below,sticky',  -- 窗口提示设置包含 'sticky'
    draw_shades = false,                -- 是否绘制阴影
    use_xft = true,                     -- 是否使用XFT字体
    font = 'JetBrains Mono:size=12',    -- 字体及其大小
    gap_x = 20,                         -- 窗口与屏幕边缘的水平间距
    gap_y = 20,                         -- 窗口与屏幕边缘的垂直间距
    minimum_height = 5,                 -- 最小高度
    minimum_width = 5,                  -- 最小宽度
    net_avg_samples = 2,                -- 网络平均采样次数
    double_buffer = true,               -- 是否使用双缓冲区
    out_to_console = false,             -- 是否输出到控制台
    out_to_stderr = false,              -- 是否输出错误信息到stderr
    extra_newline = false,              -- 是否添加额外的换行符
    own_window = true,                  -- 是否使用自己独立的窗口
    own_window_class = 'Conky',         -- 窗口类名
    own_window_transparent = true,      -- 窗口是否透明
    stippled_borders = 0,               -- 虚线边框的间距
    update_interval = 1.0,              -- 更新间隔时间(秒)
    uppercase = false,                  -- 是否将文本转换为大写
    use_spacer = 'none',                -- 是否使用空格
    show_graph_scale = false,           -- 是否显示图形刻度
    show_graph_range = false,           -- 是否显示图形范围
    own_window_argb_visual = true,      -- 启用ARGB视觉效果(透明度)
    own_window_argb_value = 150,        -- 窗口透明度(0完全透明,255完全不透明)
    lua_load = '~/.config/conky/conky.lua',
}


conky.text = [[
${alignc}${font JetBrains Mono:size=32:bold}${time %H:%M}${font}
${alignc}${font JetBrains Mono:bold}${time %Y/%m/%d}${font}


${font JetBrains Mono:size=16:bold}${color orange}SYSTEM ${hr 2}${font}
${color}${execi 3600 grep 'model name' /proc/cpuinfo | uniq | awk -F ': ' '{print $2}'}
${execi 3600 cat /etc/os-release | grep PRETTY_NAME | cut -d '"' -f 2} / ${kernel}
${execi 3600 echo $XDG_CURRENT_DESKTOP} / Up $uptime

# ${color orange}CPU ${hr 2}
${font JetBrains Mono:size=16:bold}${color orange}CPU ${hr 2}${font}
${color}Clock Frequency: ${freq_g}GHz  
1 minutes:  ${lua cpu_load_percent 1}%
15 minutes: ${lua cpu_load_percent 3}%

${font JetBrains Mono:size=16:bold}${color orange}MEMORY ${hr 2}${font}
${color}Usage Chart: ${membar 4}
Usage: $mem($memperc%)
Total: $memmax 

${font JetBrains Mono:size=16:bold}${color orange}DISK ${hr 2}${font}
${color}Usage Chart: ${fs_bar 4 /}
Usage: ${fs_used /}(${fs_used_perc}%)
Total: ${fs_size /} 

${font JetBrains Mono:size=16:bold}${color orange}NETWORK ${hr 2}${font}
${color}Global IP: ${execi 3600 curl icanhazip.com}
Domestic IP:${execi 3600 curl -s cip.cc | grep IP | cut -d ":" -f 2}
Down: ${lua conky_parse_downspeed wlp3s0}Mbps Upload: ${lua conky_parse_upspeed wlp3s0}Mbps
${downspeedgraph wlp3s0 25,140 000000 ffffff} ${upspeedgraph wlp3s0 25,140 000000 ffffff}

${font JetBrains Mono:size=16:bold}${color orange}PROCESSES ${hr 2}${font}
${color}MEM(MB)  ${goto 140}Name                USER 
${top_mem mem_res 1} ${goto 140}${top_mem name 1}    ${top_mem user 1}
${top_mem mem_res 2} ${goto 140}${top_mem name 2}    ${top_mem user 2}
${top_mem mem_res 3} ${goto 140}${top_mem name 3}    ${top_mem user 3}
${top_mem mem_res 4} ${goto 140}${top_mem name 4}    ${top_mem user 4}
${top_mem mem_res 5} ${goto 140}${top_mem name 5}    ${top_mem user 5}
${top_mem mem_res 6} ${goto 140}${top_mem name 6}    ${top_mem user 6}
${top_mem mem_res 7} ${goto 140}${top_mem name 7}    ${top_mem user 7}
${top_mem mem_res 8} ${goto 140}${top_mem name 8}    ${top_mem user 8}
]]

这份配置文件中部分功能实现借助了lua脚本

为了实现这部分lua的功能,编辑: ~/.config/conky/conky.lua

Lua
function conky_cpu_load_percent(index)
    local num_cores = 16  -- 你的CPU核心数量
    local loadavg = conky_parse('${loadavg ' .. index .. '}')
    local load = tonumber(loadavg)
    if load then
        return string.format("%.2f", (load / num_cores) * 100)
    else
        return "N/A"
    end
end


function conky_parse_upspeed(interface)
    local upspeed = conky_parse('${upspeedf '.. interface ..'}') 
    local upspeed_mbps = (tonumber(upspeed) * 8) / 1024
    return string.format("%.2f", upspeed_mbps)
end

function conky_parse_downspeed(interface)
    local downspeed = conky_parse('${downspeedf '.. interface ..'}') 
    local downspeed_mbps = (tonumber(downspeed) * 8) / 1024
    return string.format("%.2f", downspeed_mbps)
end


function conky_convert_time(seconds)
    local hours = math.floor(seconds / 3600)
    local minutes = math.floor((seconds % 3600) / 60)
    local secs = seconds % 60
    return string.format("%dh%02dm%02ds", hours, minutes, secs)
end

function get_ips()
    local handle = io.popen("ip -4 addr show")
    local result = handle:read("*a")
    handle:close()

    local interfaces = {}
    for line in result:gmatch("[^\r\n]+") do
        local ip, iface = line:match("inet (%d+%.%d+%.%d+%.%d+)/%d+ brd %d+%.%d+%.%d+%.%d+ scope global .* (%S+)")
        if ip and iface then
            table.insert(interfaces, iface .. ": " .. ip)
        end
    end

    return table.concat(interfaces, "\n")
end

[[replyMessage== null?"发表评论":"发表评论 @ " + replyMessage.m_author]]

account_circle
email
web_asset
textsms

评论列表([[messageResponse.total]])

还没有可以显示的留言...
gravatar
[[messageItem.m_author]] [[messageItem.m_author]]
[[messageItem.create_time]]
[[getEnviron(messageItem.m_environ)]]
[[subMessage.m_author]] [[subMessage.m_author]] @ [[subMessage.parent_message.m_author]] [[subMessage.parent_message.m_author]]
[[subMessage.create_time]]
[[getEnviron(messageItem.m_environ)]]