Hi, 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? regards, Andreas -- Andreas Kuntzagk SystemAdministrator MDC Berlin / BIMSB Tel.: +49 30 9406 2997 -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Andreas Kuntzagk <andreas.kuntzagk@mdc-berlin.de> 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 %>") 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 } } HTH, Matthias -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Martijn Grendelman
2011-Jun-30 12:00 UTC
Re: [Puppet Users] using memorysize fact in manifests
On 30-06-11 11:20, Matthias Saou wrote:> Andreas Kuntzagk <andreas.kuntzagk@mdc-berlin.de> 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 puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Chris Phillips
2011-Jun-30 12:29 UTC
Re: [Puppet Users] using memorysize fact in manifests
Well that''s odd, I was looking at the exact same issue this morning for sysctl.conf / oracle stuff. But why are people writing new facts?? Why not just take a copy of the original function and simply not run the function that normalizes the number? It seems very odd to make a more limited version of the function when it''s already there. Is it not possible to copy the code directly from utils/memory.rb in facter? Chris On 30 June 2011 13:00, Martijn Grendelman <martijn@iphion.nl> wrote:> On 30-06-11 11:20, Matthias Saou wrote: > > Andreas Kuntzagk <andreas.kuntzagk@mdc-berlin.de> 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 puppet-users@googlegroups.com. > To unsubscribe from this group, send email to > puppet-users+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/puppet-users?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Chris Phillips
2011-Jun-30 12:56 UTC
Re: [Puppet Users] using memorysize fact in manifests
Further to this, this is the normal memory code condensed with normalization removed to give "raw" versions of the facts. require ''facter'' { :MemorySizeRaw => "MemTotal", :MemoryFreeRaw => "MemFree", :SwapSizeRaw => "SwapTotal", :SwapFreeRaw => "SwapFree" }.each do |fact, name| Facter.add(fact) do confine :kernel => :linux setcode do memsize_raw = "" Thread::exclusive do File.readlines("/proc/meminfo").each do |l| memsize_raw = $1.to_i if l =~ /^#{name}:\s+(\d+)\s+\S+/ # MemoryFree == memfree + cached + buffers # (assume scales are all the same as memfree) if name == "MemFree" && l =~ /^(?:Buffers|Cached):\s+(\d+)\s+\S+/ memsize_raw += $1.to_i end end end memsize_raw end end end Thanks Chris On 30 June 2011 13:29, Chris Phillips <chris@untrepid.com> wrote:> > Well that''s odd, I was looking at the exact same issue this morning for > sysctl.conf / oracle stuff. > > But why are people writing new facts?? Why not just take a copy of the > original function and simply not run the function that normalizes the > number? It seems very odd to make a more limited version of the function > when it''s already there. > > Is it not possible to copy the code directly from utils/memory.rb in > facter? > > Chris > > On 30 June 2011 13:00, Martijn Grendelman <martijn@iphion.nl> wrote: > >> On 30-06-11 11:20, Matthias Saou wrote: >> > Andreas Kuntzagk <andreas.kuntzagk@mdc-berlin.de> 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 puppet-users@googlegroups.com. >> To unsubscribe from this group, send email to >> puppet-users+unsubscribe@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/puppet-users?hl=en. >> >> >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.