Ask Your Question
0

Array data use within a template

asked 2015-02-12 12:49:57 -0500

tsheriffk gravatar image

I have taken the NRPE plugin (for Nagios) from the puppet forge and tweaked it in order to get it to work for NCPA as well as NRPE. Without getting too much into it, they went from an include directory in NRPE, to defining the checks in a single config file with NCPA, so in order to modify the passive checks, they need to be looped through from the array..

While I have it working functionally, I am running into issues where the manifest runs almost every check-in, as the array is not necessarily parsed in the same order as the last time it ran, so the template always reorders the commands..

What I have is :

Defined the variables in the manifest :

$ncpa_passive_checks = {  
 "check root partition" => {
     "test_name" => "disk usage",
     "args"      => "--warning 20% --critical 10% -p /",
     "ncpa_branch" => "/disk/logical/|boot/used_percent",
   },
 "swap usage" => {
      "test_name" => "swap usage",
      "args"      => "--warning 40 --critical 80",
      "ncpa_branch" => "/memory/swap/percent", 
},
   "cpu util" => {
      "test_name" => "cpu usage",
      "args"      => "--warning 85 --critical 90",
      "ncpa_branch" => "/cpu/percent",
   },
   "memory usage" => {
      "test_name" => "memory usage",
       "args"     => "--warning 80 --critical 90",
       "ncpa_branch" => "/memory/virtual/percent",
   }
},

Elsewhere in the manifest i am performing the following:

if $ncpa_passive_checks != undef {
  $passive_checks = hiera('nagios::ncpa_passive_checks', {} )
  create_resources('nagios::ncpa_passive_checks', $passive_checks)
}

and the template is doing the following in order to place the passive commands into the config file:

<% @ncpa_passive_checks.each do | key,hash | %>%HOSTNAME%|<%= hash['test_name'] %> = <%= hash['ncpa_branch']%> <%= hash['args'] %>

I do get the output that I expect to get, but the order changes each run..

I KNOW there is going to be a better way to do this, but this is really my first experience with using arrays in this manner within puppet.. Does anyone know how I should be doing this instead, so that it is going to only run when something truly has changed?

Thx for any help!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2015-02-13 14:34:39 -0500

tsheriffk gravatar image

After a fair bit of digging, i stumbled across this thread ( puppet user mail list) which led me to a simple solution that seems to be working

adding a ".sort" to the template line appears to have resolved my issue

So my template line now looks like this:

<% @ncpa_passive_checks.sort.each do | key,hash | %>%HOSTNAME%|<%= hash['test_name'] %> = <%= hash['ncpa_branch']%> <%= hash['args'] %>

So far this has worked, so i'll leave the rest as it is... but I have a feeling the other solution presented would work as well in the event my "solution" has issues...

edit flag offensive delete link more

Comments

If you don't need the checks in the same order you wrote them, then sorting them is a good solution.

Nick gravatar imageNick ( 2015-02-16 03:02:34 -0500 )edit
0

answered 2015-02-13 10:42:27 -0500

Nick gravatar image

Instead of a hash of hashes you might try an array of hashes, which will be ordered:

$ncpa_passive_checks = [  
   {
     "title" => "check root partition",
     "test_name" => "disk usage",
     "args"      => "--warning 20% --critical 10% -p /",
     "ncpa_branch" => "/disk/logical/|boot/used_percent",
   },
   {
      "title" => "swap usage",
      "test_name" => "swap usage",
      "args"      => "--warning 40 --critical 80",
      "ncpa_branch" => "/memory/swap/percent", 
   },
   {
      "title" => "cpu util",
      "test_name" => "cpu usage",
      "args"      => "--warning 85 --critical 90",
      "ncpa_branch" => "/cpu/percent",
   },
   {
       "title" => "memory usage",
       "test_name" => "memory usage",
       "args"     => "--warning 80 --critical 90",
       "ncpa_branch" => "/memory/virtual/percent",
   }
]

Then the template is very similar, just take out the key parameter

<% @ncpa_passive_checks.each do | hash | -%>
    %HOSTNAME%|<%= hash['test_name'] %> = <%= hash['ncpa_branch']%> <%= hash['args']
<%- end %>
edit flag offensive delete link more

Comments

Thanks for the suggestion! if i find that my solution I posted falls down somewhere, i will give yours a go.. I have no doubt that what you suggested would probably work as well..

tsheriffk gravatar imagetsheriffk ( 2015-02-13 14:36:11 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-02-12 12:49:57 -0500

Seen: 854 times

Last updated: Feb 13 '15