Use less threads, they're expensive [#9 state:closed]
[amazing.git] / lib / amazing / widgets / memory.rb
blob6c37f25e69c078e2a073f130c36e2da6a9f8be29
1 # Copyright 2008 Dag Odenhall <dag.odenhall@gmail.com>
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 #    http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 require 'amazing/widget'
16 require 'amazing/proc_file'
18 module Amazing
19   module Widgets
20     class Memory < Widget
21       description "Various memory related data"
22       field :total, "Total kilobytes of memory"
23       field :free, "Free kilobytes of memory"
24       field :buffers, "Buffered kilobytes of memory"
25       field :cached, "Cached kilobytes of memory"
26       field :usage, "Percentage of used memory"
27       field :swap_total, "Total kilobytes of swap"
28       field :swap_free, "Free kilobytes of swap"
29       field :swap_cached, "Cached kilobytes of swap"
30       field :swap_usage, "Percentage of used swap"
31       default { @usage.to_i }
33       init do
34         meminfo = ProcFile.parse_file("meminfo")[0]
35         @total = meminfo["MemTotal"].to_i
36         @free = meminfo["MemFree"].to_i
37         @buffers = meminfo["Buffers"].to_i
38         @cached = meminfo["Cached"].to_i
39         @usage = ((@total - @free - @cached - @buffers) * 100) / @total.to_f
40         @swap_total = meminfo["SwapTotal"].to_i
41         @swap_free = meminfo["SwapFree"].to_i
42         @swap_cached = meminfo["SwapCached"].to_i
43         @swap_usage = ((@swap_total - @swap_free - @swap_cached) * 100) / @swap_total.to_f
44       end
45     end
46   end
47 end