1 -- Authors: Randall Wald <randy@rwald.com>
2 -- License: GPL, version 2
3 -- Last Changed: Unknown
6 -- CPU, Mem, and Swap information script
7 -- Written by Randall Wald
8 -- email: randy@rwald.com
9 -- Released under the GPL
11 -- This script is based on parsing 'top' output.
13 -- Unfortunately top output is inconsistent among versions. Some versions
14 -- (such as 3.2.8) are known not to work, as they do not support the 'b'
15 -- mode. Other versions do not work correctly with '-n 1', so we pass '-n 2'
18 -- We currently recognise 2 output formats for the command
19 -- top b -n 2 -d 1 -p 0|grep Cpu|tail -n 1
21 -- Cpu(s): 16.9% us, 5.1% sy, 0.0% ni, 70.8% id, 6.5% wa, 0.1% hi, 0.5% si
22 -- %Cpu(s): 4.5 us, 1.0 sy, 0.0 ni, 93.5 id, 1.0 wa, 0.0 hi, 0.0 si, 0.0 st
24 -- Let us know when you encounter another variation, perhaps we can support it, too.
26 -- Available monitors:
27 -- %info_CPU_user Percentage of CPU used by user programs
28 -- %info_CPU_system Percentage of CPU used by services
29 -- %info_CPU_idle Percentage of CPU idle
30 -- %info_CPU_ni The time the CPU has spent running users'’processes that have been niced.
31 -- %info_CPU_wa Amount of time the CPU has been waiting for I/O to complete.
32 -- %info_CPU_hi The amount of time the CPU has been servicing hardware interrupts.
33 -- %info_CPU_si The amount of time the CPU has been servicing software interrupts.
34 -- %info_RAM_total Total amount of RAM
35 -- %info_RAM_used Amount of RAM used
36 -- %info_RAM_free Amount of RAM free
37 -- %info_RAM_shared Amount of RAM shared
38 -- %info_RAM_buffers Amount of RAM in buffers
39 -- %info_RAM_cached Amount of RAM cached
40 -- %info_swap_total Total amount of swap
41 -- %info_swap_used Amount of swap currently used
42 -- %info_swap_free Amount of swap currently free
45 -- (Note that the units are milliseconds)
47 local update_interval
= 0.1 * 1000
49 -- Memory monitors need a factor:
54 local mem_dimension
= "M"
56 -- Defines the factor for dividing the memory amount
57 if mem_dimension
== "" then
59 elseif mem_dimension
== "K" then
61 elseif mem_dimension
== "M" then
67 local function get_CPU_info()
68 local f
=io
.popen('top b -n 2 -d 1 -p 0|grep Cpu|tail -n 1','r')
69 local s
=f
:read('*all')
78 info_CPU_si
= string.find(s
, "Cpu%(s%):%s*(%d+%.%d+%%?)%s*us,%s*(%d+%.%d+%%?)%s*sy,%s*(%d+%.%d+%%?)%s*ni,%s*(%d+%.%d+%%?)%s*id,%s*(%d+%.%d+%%?)%s*wa,%s*(%d+%.%d+%%?)%s*hi,%s*(%d+%.%d+%%?)%s*si")
79 return info_CPU_user
.."", info_CPU_system
.."", info_CPU_ni
.."", info_CPU_idle
.."", info_CPU_wa
.."", info_CPU_hi
.."", info_CPU_si
..""
82 local function process_memory(value
)
83 local memory
= value
/ mem_factor
84 -- Truncate to just two digits after the decimal place
85 memory
= string.gsub(memory
,"(%d+%.%d%d)(%d*)","%1")
89 local function get_RAM_info()
90 local f
=io
.popen('free -b','r')
91 local s
=f
:read('*all')
99 info_RAM_cached
= string.find(s
, "Mem:%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)")
100 info_RAM_total
= process_memory(info_RAM_total
)
101 info_RAM_used
= process_memory(info_RAM_used
)
102 info_RAM_free
= process_memory(info_RAM_free
)
103 info_RAM_shared
= process_memory(info_RAM_shared
)
104 info_RAM_buffers
= process_memory(info_RAM_buffers
)
105 info_RAM_cached
= process_memory(info_RAM_cached
)
109 info_swap_free
= string.find(s
, "Swap:%s+(%d+)%s+(%d+)%s+(%d+)")
110 info_swap_total
= process_memory(info_swap_total
)
111 info_swap_used
= process_memory(info_swap_used
)
112 info_swap_free
= process_memory(info_swap_free
)
113 return info_RAM_total
..mem_dimension
, info_RAM_used
..mem_dimension
, info_RAM_free
..mem_dimension
, info_RAM_shared
..mem_dimension
, info_RAM_buffers
..mem_dimension
, info_RAM_cached
..mem_dimension
, info_swap_total
..mem_dimension
, info_swap_used
..mem_dimension
, info_swap_free
..mem_dimension
116 local function inform_info(name
, value
)
117 if statusd
~= nil then
118 statusd
.inform(name
, value
)
120 io
.stdout
:write(name
..": "..value
.."\n")
124 if statusd
~= nil then
125 status_timer
= statusd
.create_timer()
128 local function update_info()
129 local info_CPU_user
, info_CPU_system
, info_CPU_ni
, info_CPU_idle
, info_CPU_wa
, info_CPU_hi
, info_CPU_si
= get_CPU_info()
130 local info_RAM_total
, info_RAM_used
, info_RAM_free
, info_RAM_shared
, info_RAM_buffers
, info_RAM_cached
, info_swap_total
, info_swap_used
, info_swap_free
= get_RAM_info()
131 inform_info("info_CPU_user", info_CPU_user
)
132 inform_info("info_CPU_system", info_CPU_system
)
133 inform_info("info_CPU_ni", info_CPU_ni
)
134 inform_info("info_CPU_idle", info_CPU_idle
)
135 inform_info("info_CPU_wa", info_CPU_wa
)
136 inform_info("info_CPU_hi", info_CPU_hi
)
137 inform_info("info_CPU_si", info_CPU_si
)
138 inform_info("info_RAM_total", info_RAM_total
)
139 inform_info("info_RAM_used", info_RAM_used
)
140 inform_info("info_RAM_free", info_RAM_free
)
141 inform_info("info_RAM_shared", info_RAM_shared
)
142 inform_info("info_RAM_buffers", info_RAM_buffers
)
143 inform_info("info_RAM_cached", info_RAM_cached
)
144 inform_info("info_swap_total", info_swap_total
)
145 inform_info("info_swap_used", info_swap_used
)
146 inform_info("info_swap_free", info_swap_free
)
147 if statusd
~= nil then
148 status_timer
:set(update_interval
, update_info
)