#! /usr/bin/lua -- Description: Print load averages and suchlike for dwm status display function read_proc_stat() ------------------------------------------------- f=io.open('/proc/stat','r') s=f:read('*l') f:close() _, _, t_usr, t_nice, t_sys, t_idle = string.find(s, "^cpu%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)") t_total = t_usr + t_nice + t_sys + t_idle -- only do calcs if we have successfully got some data to calc from if t_usr and t_nice and t_sys and t_idle and t_total_prev then t_step_jiffies = t_total - t_total_prev if t_step_jiffies<1 then t_step_jiffies = -1 end t_step = t_step_jiffies / 100 c_used = 1 - (t_idle - t_idle_prev) / t_step_jiffies else timestep = 100 t_step = 1 c_used = 0 end t_total_prev, t_idle_prev = t_total, t_idle -- find weighted rolling average if c_used_avg then c_used_avg = (c_used*2 + c_used_avg)/3 else c_used_avg = c_used end c_str = string.format("u%d", math.floor(c_used_avg*100+0.5)) return c_str, t_step end function read_proc_meminfo() ---------------------------------------------- f=io.open('/proc/meminfo','r') s=f:read('*all') f:close() -- get ram usage info _, _, m_total = string.find(s, "MemTotal:%s*(%d+)") _, _, m_free = string.find(s, "MemFree:%s*(%d+)") _, _, m_buffers = string.find(s, "Buffers:%s*(%d+)") _, _, m_cached = string.find(s, "Cached:%s*(%d+)") -- only do calcs if we have successfully got some data to calc from if m_total and m_free and m_buffers and m_cached then m_percent = math.floor((m_total-m_free-m_buffers-m_cached)/m_total * 100+0.5) -- don't display anything unless memory usage is high if m_percent>70 then m_str = string.format(" r%d", m_percent) else m_str = "" end else m_str = " r?" end -- get swap usage info _, _, s_total = string.find(s, "SwapTotal:%s*(%d+)") _, _, s_free = string.find(s, "SwapFree:%s*(%d+)") -- only do calcs if we have successfully got some data to calc from if s_total and s_free then s_percent = math.floor((s_total-s_free) / s_total * 100+0.5) -- don't display anything if swap usage is low if s_percent>1 then s_str = string.format(" s%d",s_percent) else s_str = "" end else s_str = " s?" end return m_str, s_str end function read_proc_net_dev(time_interval) ---------------------------------------------- f=io.open('/proc/net/dev','r') s=f:read('*all') f:close() -- Inter-| Receive | Transmit -- face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed -- lo: 4094 44 0 0 0 0 0 0 4094 44 0 0 0 0 0 0 -- eth0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 _, _, n_eth0_rxbytes, n_eth0_txbytes = string.find(s, "eth0:%s*(%d+)%s+%d+%s+%d+%s+%d+%s+%d+%s+%d+%s+%d+%s+%d+%s+(%d+)") _, _, n_ppp0_rxbytes, n_ppp0_txbytes = string.find(s, "ppp0:%s*(%d+)%s+%d+%s+%d+%s+%d+%s+%d+%s+%d+%s+%d+%s+%d+%s+(%d+)") -- only do calcs if we have successfully got some data to calc from if n_eth0_txbytes and n_eth0_rxbytes and n_eth0_rxbytes_prev then n_str=string.format(" n%d/%d", math.floor((n_eth0_rxbytes - n_eth0_rxbytes_prev) / time_interval*0.008+0.9), math.floor((n_eth0_txbytes - n_eth0_txbytes_prev) / time_interval*0.008+0.9) ) else n_str = ' n?/?' end -- only do calcs if we have successfully got some data to calc from if n_ppp0_txbytes and n_ppp0_rxbytes then if not n_ppp0_rxbytes_prev then n_ppp0_rx = 0 n_ppp0_tx = 0 p_str = ' p?/?' else --n_ppp0_rx = (n_ppp0_rxbytes - n_ppp0_rxbytes_prev) / time_interval --n_ppp0_tx = (n_ppp0_txbytes - n_ppp0_txbytes_prev) / time_interval p_str=string.format(" p%d/%d", math.floor((n_ppp0_rxbytes - n_ppp0_rxbytes_prev) / time_interval*0.008+0.9), math.floor((n_ppp0_txbytes - n_ppp0_txbytes_prev) / time_interval*0.008+0.9) ) end else p_str = nil n_ppp0_rx = nil n_ppp0_tx = nil end n_eth0_rxbytes_prev, n_eth0_txbytes_prev = n_eth0_rxbytes, n_eth0_txbytes n_ppp0_rxbytes_prev, n_ppp0_txbytes_prev = n_ppp0_rxbytes, n_ppp0_txbytes if p_str then return n_str..p_str else return n_str end end function read_proc_mounts() ----------------------------------------------- f=io.open('/proc/mounts','r') s=f:read('*all') f:close() mnt=' ' -- Old version which only showed things mounted on /media --for mountpoint in string.gmatch(s, "%S+%s+/media/(%S+)%s+") do -- mnt = mnt..' '..mountpoint --end -- Find all the mounted floppy/loop/scsi devices (and possibly some others). for device,mountpoint in string.gmatch(s, "/dev/([^h]%S+)%s+(/%S+)%s+") do mediamount = string.match(mountpoint, "^/media/(%S+)$") if mediamount then -- For anything mounted on /media, just show the last part of the mountpoint. -- These are normal mounts of things like flash drive, camera, etc. mnt = mnt..' '..mediamount elseif mountpoint ~= '/dev/.static/dev' then -- For anything mounted other than on /media, show the whole mountpoint. -- These are 'not-normal' mounts which I want to be notified of! -- Might need to do a test like the next line if undesired devices start slipping through: -- if string.match(device, "^s") or string.match(device, "^loop") then mnt = mnt..' '..mountpoint end end -- $ cat /proc/mounts -- rootfs / rootfs rw 0 0 -- none /sys sysfs rw 0 0 -- none /proc proc rw 0 0 -- udev /dev tmpfs rw 0 0 -- /dev/hda1 / ext3 rw,noatime,data=ordered 0 0 -- /dev/hda1 /dev/.static/dev ext3 rw,data=ordered 0 0 -- tmpfs /lib/init/rw tmpfs rw,nosuid 0 0 -- usbfs /proc/bus/usb usbfs rw,nosuid,nodev,noexec 0 0 -- tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0 -- devpts /dev/pts devpts rw,nosuid,noexec 0 0 -- /dev/hdc2 /home/karl/extra_space ext3 rw,nosuid,nodev,noexec,noatime,data=ordered 0 0 -- ckeys /home/karl/.ckeys tmpfs rw,nosuid,nodev,noexec,noatime 0 0 -- none /home/karl/mnt shfs rw 0 0 -- sshfs#karl@L73:/media/nuh7 /media/nuh7 fuse rw,nosuid,nodev,user_id=1000,group_id=1000,allow_other,max_read=65536 0 0 -- Find all the fuse and shfs mounts. --for device,mountpoint in string.gmatch(s, "(sshfs#%S+)%s+(/%S+)%s+") do --for device,mountpoint in string.gmatch(s, "(%S+[:#]%S+)%s+(/%S+)%s+") do for mountpoint,mounttype in string.gmatch(s, "%S+%s+(/%S+)%s+(%S+)%s") do if mounttype == 'fuse' then typechar = ' #' elseif mounttype == 'shfs' then typechar = ' $' else typechar = Nil end if typechar then mediamount = string.match(mountpoint, "^/media/(%S+)$") if mediamount then -- For anything mounted on /media, just show the last part of the mountpoint. -- These are normal mounts of things like flash drive, camera, etc. mnt = mnt..typechar..mediamount else -- For anything mounted other than on /media, show the whole mountpoint. -- These are 'not-normal' mounts which I want to be notified of! mnt = mnt..typechar..mountpoint end end end if mnt==' ' then return '' else return mnt end end --/\/--/\/--/\/--/\/--/\/--/\/--/\/--/\/--/\/--/\/--/\/--/\/--/\/--/\/--/\/ require("posix") while true do cpustr, timestep = read_proc_stat() netstr = read_proc_net_dev(timestep) memstr, swapstr = read_proc_meminfo() mntstr = read_proc_mounts() print( cpustr.. memstr.. swapstr.. netstr.. ' '..os.date("%d %a %k:%M")..mntstr ) io.flush() posix.sleep(1) end