Re: Overriding variables.

497 views
Skip to first unread message

George Shammas

unread,
Sep 24, 2012, 4:48:10 PM9/24/12
to puppet...@googlegroups.com
Another iteration of failure is the following. This one is in the likeness of ::parms for a class, but if it nrpe:load inherits override class, then I can't declare the override class anywhere else. This was my first attempt to do this, and have tried 4 other iterations of this, but as far as I can tell, because it can't inherit the class, it will ONLY use the default values, which can only be undef or some other default value, again defeating the ability to override.

class nrpe::load {

    package { nagios-plugins-load: ensure => installed }

    if $nrpe::load::override::warn == 'UNSET' {
        $warn = $processorcount
    } else {
        $warn = $nrpe::load::override::warn
    }
    if $nrpe::load::override::crit == 'UNSET' {
        $crit = $processorcount * 2
    } else {
        $crit = $nrpe::load::override::crit
    }

    file { "/etc/nagios/command.d/load.cfg":
        owner   => root,
        group   => root,
        mode    => 640,
        content => template("nrpe/load.cfg.erb"),
        notify => Service[nrpe]
    }
}    


class nrpe::load::override($warn = 'UNSET', $crit = 'UNSET') {
}


On Monday, September 24, 2012 12:47:31 PM UTC-4, George Shammas wrote:
I am trying to override variables in a class that is defined in the default node profile. I want parent class to be included in every single node, but override its variables in others. I have tried it several different ways now, and every single time the variables either become unset (undefined) or are set to the value of the first if statement.

Here is my current iteration of failure. warn_real is always equal to processorcount, no matter if I override it or not.

class nrpe::load ($warn = 'UNSET', $crit = 'UNSET') {

    package { nagios-plugins-load: ensure => installed }

    if $warn == 'UNSET' {
        $warn_real = $processorcount
    }
    if $crit == 'UNSET' {
        $crit_real = $processorcount * 2
    }

    file { "/etc/nagios/command.d/load.cfg":
        owner   => root,
        group   => root,
        mode    => 640,
        content => template("nrpe/load.cfg.erb"),
        notify => Service[nrpe]
    }
}


class nrpe::load::override($warn = 'UNSET', $crit = 'UNSET') inherits nrpe::load {

    Class["nrpe::load"] {
        warn => $warn,
        crit => $crit
    }
}

node "somenode.tld" inherits basenode {
    class {'nrpe::load::override':
        warn => 5,
        crit => 10
    }
}

jcbollinger

unread,
Sep 25, 2012, 11:28:31 AM9/25/12
to puppet...@googlegroups.com


On Monday, September 24, 2012 11:47:31 AM UTC-5, George Shammas wrote:
I am trying to override variables in a class that is defined in the default node profile. I want parent class to be included in every single node, but override its variables in others. I have tried it several different ways now, and every single time the variables either become unset (undefined) or are set to the value of the first if statement.

You cannot override class variables.  You especially cannot override class parameters.  You can override only resource properties (and classes are not resources, appearances notwithstanding).

Moreover, subclasses do not set parameters for their parent classes, even if they themselves have parameters of the same names.  Therefore, it is usually a bad idea to inherit from a parametrized class (in fact, the v 2.7 docs say it's not supported).  If you do so, then you must explicitly declare the parent class on every node for which you want to set non-default parameters, whether or not you also declare the child class.

Since you don't actually use the variables in question in your example, I can't guess what your larger purpose may be, therefore it is difficult for me to advise you.  Nevertheless, at the low level we are focused on, you can achieve something similar to what you have asked by employing a data hierarchy instead of a class hierarchy.  That is what the Hiera module is all about.  Since hiera will be integrated into Puppet 3, getting started with it now is also forward-looking.

Once you install hiera and set up your data, the Puppet side could be as simple as this:

class nrpe::load { # no parameters
    package { 'nagios-plugins-load': ensure => installed }

    $warn_real = hiera('nrpe::warn')
    $crit_real = hiera('nrpe::crit')

    # alternatively, load $warn and $crit via hiera,
    # and retain the original logic for setting
    # $warn_real and $crit_real


    file { "/etc/nagios/command.d/load.cfg":
        owner   => root,
        group   => root,
        mode    => 640,
        content => template("nrpe/load.cfg.erb"),
        notify => Service[nrpe]
    }
}

node "somenode.tld" inherits basenode {
  # no subclass involved
}


John

George Shammas

unread,
Sep 25, 2012, 11:56:45 AM9/25/12
to puppet...@googlegroups.com
Hi John,

Thanks for the response. They variables are only used in the template, as the NRPE daemon is manged by flat files. I will do some research into hiera.

--George

Lorenzo Salvadorini

unread,
Sep 26, 2012, 4:44:19 AM9/26/12
to puppet...@googlegroups.com
I've solved the identical problem using defines instead of classes. I
know that hiera can be a better solution, but I'm waiting for next
major release to adopt hiera.

I've a generic nrpe::check define: each check is a define that use
this generic definition, so my check_load.pp in nrpe module is

define nrpe::check_load(
$warn1=8,
$warn5=4,
$warn15=2,
$crit1=20,
$crit5=10,
$crit15=5,
)
{
if ! is_numeric($warn1)
{
fail('Variable warn1 must be numeric')
}
if ! is_numeric($warn5)
{
fail('Variable warn5 must be numeric')
}
if ! is_numeric($warn15)
{
fail('Variable warn15 must be numeric')
}
if ! is_numeric($crit1)
{
fail('Variable crit1 must be numeric')
}
if ! is_numeric($crit5)
{
fail('Variable crit5 must be numeric')
}
if ! is_numeric($crit15)
{
fail('Variable crit15 must be numeric')
}

# -r plugin option make it compatible with multi-core cpu
nrpe::check{ 'load':
params => "-r -w ${warn1},${warn5},${warn15} -c
${crit1},${crit5},${crit15}"
}
}



In generic_host I create a resource with no parameters, so defaults apply:

nrpe::check_load { 'load': }


If I've a node with a medium greater load than my default I do:

node 'node-with-high-load' inherits generic_host {
Nrpe::Check_load['load']{ warn5 => 20 }
}


Hopes this helps,
L



2012/9/25 George Shammas <geo...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages