Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dev.icinga.com #11207] unable to refer to array variable inside for loop #3959

Closed
icinga-migration opened this issue Feb 23, 2016 · 6 comments
Labels
area/configuration DSL, parser, compiler, error handling bug Something isn't working

Comments

@icinga-migration
Copy link

This issue has been migrated from Redmine: https://dev.icinga.com/issues/11207

Created by kowalskimn on 2016-02-23 10:27:24 +00:00

Assignee: (none)
Status: Closed (closed on 2016-02-25 00:12:11 +00:00)
Target Version: (none)
Last Update: 2016-02-25 00:12:11 +00:00 (in Redmine)

Icinga Version: 2.4.1-1
Backport?: Not yet backported
Include in Changelog: 1

I have a host defined like so :

object Host "conniedb" {
  address = "1.2.3.4"
  check_command = "hostalive"
  import "generic-host"
  vars.os = "Linux"
  vars.postgres_databases["my_database"] = {
        checks = [ "bloat", "connection" , "dbstats", "disabled_triggers", "index_size" , "last_analyze",
                        "last_vacuum", "prepared_txns", "query_time","sequence","table_size","txn_idle" ]
  }
}

I am trying to do the following:

apply Service "Apply-postgres-nrpe-bloat" for (database => config in host.vars.postgres_databases) {
  import "generic-service"
  check_command = "nrpe"
  vars += config
  vars.nrpe_command = database + "_check_postgres_bloat"
  display_name = vars.nrpe_command
//this
  assign where database.checks.bloat
// or 
  assign where "bloat" in database.checks
}

The assign conditions fail with error

critical/config: Error: Invalid field access (for value of type 'String'): 'checks'

It seems like some kind of type interpretation mistake to me, unless this construction is totally wrong.


Relations:

@icinga-migration
Copy link
Author

Updated by gbeutner on 2016-02-23 10:51:49 +00:00

Do you have any hosts where the 'checks' attribute is set to a string?

@icinga-migration
Copy link
Author

Updated by gbeutner on 2016-02-23 10:52:28 +00:00

(Also, if so, we should probably improve error handling to mention the host's name where we encountered the error.)

@icinga-migration
Copy link
Author

Updated by kowalskimn on 2016-02-24 13:34:59 +00:00

Okay i think figured it out :

assign where "bloat" in config.pgchecks

(i renamed the variable, just in case there was a duplicate somewhere).

i have to refer by "config" variable, for some reason. This way i get correct data types (Array) and i can do the match. And it seems to work fine, too - i added two databases, one with less checks than the other, and rules apply correctly.

This is on 2.4.2-1 , contrary to original bug report.

@icinga-migration
Copy link
Author

Updated by mfriedrich on 2016-02-24 19:36:35 +00:00

  • Category set to Configuration

apply for iterates over the databases dictionary using the key => value notation. In your example, the following applies for the first iteration:

  • database = my_database
  • config is a dictionary with this content
{
        checks = [ "bloat", "connection" , "dbstats", "disabled_triggers", "index_size" , "last_analyze",
                        "last_vacuum", "prepared_txns", "query_time","sequence","table_size","txn_idle" ]
  }

Since the for "condition" is evaluated first, and then the assign/ignore where expressions it is possible to reference local variables inside the apply for definition. For example "config".

Using the indexer "." to access the "checks" key inside your "config" directory, and you'll get the value - your array where you'll use intersection to check availability.

So your attempt is totally fine, just wrap your head around what's happening :)

You could also use the script debugger and print the objects and attributes in this specific context then. This will also unveil the objects name where this problem comes up.
Details: http://docs.icinga.org/icinga2/latest/doc/module/icinga2/chapter/script-debugger#script-debugger

As Gunnar suggested we should fix the log message adding more visible context to it.

@icinga-migration
Copy link
Author

Updated by mfriedrich on 2016-02-25 00:11:37 +00:00

  • Relates set to 9182

@icinga-migration
Copy link
Author

Updated by mfriedrich on 2016-02-25 00:12:11 +00:00

  • Status changed from New to Closed

Just found #9182 ... I'm closing this one here, and we'll fix the other one.

@icinga-migration icinga-migration added bug Something isn't working area/configuration DSL, parser, compiler, error handling labels Jan 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/configuration DSL, parser, compiler, error handling bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant