On 30-06-11 11:20, Matthias Saou wrote:
> Andreas Kuntzagk <[email protected]> wrote:
>
>> I want some config depending on memorysize.
>>
>> What I tried was
>> if ($memorysize >= 256 * 1024*1024) {
>> ...
>> }
>>
>> But this fails because $memorysize is a string (and contains a "G")
>> and can't be compared to an int.
>>
>> Are all facts strings? How do I work with numbers?
>
> Typical problem. Not to mention that you happen to have "G" but that
> could very easily be "M". Here's my workaround for that, which I use
> for calculations to then set some sysctl.conf values accordingly :
>
> # This is ugly, but very useful to get a standard kiB total RAM
> # to base further calculations upon. Note that we get a string
> $mem = inline_template("<%
> mem,unit = scope.lookupvar('::memorysize').split
> mem = mem.to_f
> # Normalize mem to KiB
> case unit
> when nil: mem *= (1<<0)
> when 'kB': mem *= (1<<10)
> when 'MB': mem *= (1<<20)
> when 'GB': mem *= (1<<30)
> when 'TB': mem *= (1<<40)
> end
> %><%= mem.to_i %>")
I use a custom fact, that returns the amount of system memory in
megabytes. This is, however, Linux-only, since it uses /proc/meminfo:
$ cat modules/common/lib/facter/memorysize_mb.rb
require 'facter'
Facter.add("memorysize_mb") do
confine :kernel => :Linux
ram = 0
# Steal linux's meminfo
File.open( "/proc/meminfo" , 'r' ) do |f|
f.grep( /^MemTotal:/ ) { |mem|
ram = mem.split( / +/ )[1].to_i / 1024
}
end
setcode do
ram
end
end
> Here's an example of how I then use it :
>
> # kernel.shmmax
> if $shmmax {
> $shmmax_final = $shmmax
> } else {
> if $oracle {
> # For non-shm half the RAM for <= 4G, 2G otherwise
> if $mem <= 4294967296 {
> $shmmax_final = $mem / 2
> } else {
> $shmmax_final = $mem - 2147483648
> }
> } else {
> $shmmax_final = $mem
> }
> }
Best regards,
Martijn Grendelman
--
You received this message because you are subscribed to the Google Groups
"Puppet Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/puppet-users?hl=en.