Another Linux file configuration check request

Hi again all - here’s another analysis that I’m struggling with a little.

Looking at a config file, looking for a specific stanza of the file, then looking for a option that has specific value set.

For example:

following text of first “stanza identifier {” of “/path/filename.conf” whose (exists (line of it) whose (it as lowercase contains “keyword”) whose (it as lowercase contains “optionvalueword”))

There are a few different ways of doing this, but assuming a file format of something like the following:

[stanza1]
option1=value1
option2=value2

[stanza2]
option3=value3
option4=value4

You can leverage relevance such as the following:

key "option3" of section "stanza2" of file "foo.txt"

Thanks again @Aram

I’ve not seen that type of relevance before, but I think that would work…

I’ll give that a try and see what I get back and update if that works for what I need. Again, much appreciated!

It’s perhaps not very common, but certainly useful in certain situations!

For reference: https://developer.bigfix.com/relevance/reference/file-section.html#key-string-of-file-section-string

Hmmm, looking through that just a bit more (and thinking it through), that would seem to get me back the property and show me what the setting is, which probably works well for my needs, though I’m wondering about testing that value for specific expected result?
I would guess that I could do some sort of contains or starts with check against the returned value, no?

To check for a specific value, perhaps something like the following:

exists key "option3" whose (it = "value3") of section "stanza2" of file "foo.txt"

One more question while I’m at it… how does BigFix determine the start of the stanzas?

In this case, I’m looking at a file that looks like this:

stanza {
   ## comment or other text...
   key = value,value2,value3

stanza-two {
   ## comment
  key...

So I don’t have the more traditional [ stanza heading ] in the brackets we might otherwise expect…

That’s a bit trickier (the “key” and “section” inspectors are built to match .INI file processing) but it’s possible still with the plain-text “lines of file” family.

Are there any cases where you need to deal with embedded {} characters, or are there only curly brackets used in defining stanzas?

Hi Jason, for my purposes, I think I would only find the curly braces defining sections of the .conf file that I’m looking through for values.

I had tried to use something like this:

following text of first "stanza header I would be looking for {" of "/path/file.conf" whose (exists (line of it) whose (it as lowercase contains "key_name") whose (it as lowercase contains "value_i_want_to_look_for"))

but that was not working for me (probably miss placed an “it” again :wink: )

Still haven’t figured this one out, I think I’m close with above, but just aren’t getting it…

Suggestions on what I might have gotten wrong in that relevance?

It’s a pretty complicated case you have. In order to look at “following text of” you need a single string - but if you are reading the lines of a file, you are only looking for “following text” within the same line.

Here is one potential way, probably not the best. It’s quick-and-dirty, no error handling for things like a missing stanza, missing value, or weird spacing, but if your files are all formed predictably it may be helpful. I start by concatenating all of the lines of the file together, then look for the stanza of interest, then break out just that stanza into the original line contents.

q: lines of file "c:\temp\test-stanza.txt"
A: stanza {
A:    ## comment or other text...
A:    key1 = stanza1-value,value2,value3
A: }
A: 
A: stanza-two {
A:    ## comment
A:   key1=stanza-two-value
A: }
T: 0.440 ms
I: plural file line

q: concatenation "|" of lines of file "c:\temp\test-stanza.txt"
A: stanza {|   ## comment or other text...|   key1 = stanza1-value,value2,value3|}||stanza-two {|   ## comment|  key1=stanza-two-value|}
T: 0.501 ms
I: singular string

q: preceding text of first "}" of following text of first "stanza {" of concatenation "|" of lines of file "c:\temp\test-stanza.txt"
A: |   ## comment or other text...|   key1 = stanza1-value,value2,value3|
T: 0.526 ms
I: singular substring

q: substrings separated by "|" whose (it as trimmed string starts with "key1") of preceding text of first "}" of following text of first "stanza {" of concatenation "|" of lines of file "c:\temp\test-stanza.txt"
A:    key1 = stanza1-value,value2,value3
T: 0.580 ms
I: plural substring

q: following texts of firsts "=" of substrings separated by "|" whose (it as trimmed string starts with "key1") of preceding text of first "}" of following text of first "stanza {" of concatenation "|" of lines of file "c:\temp\test-stanza.txt"
A:  stanza1-value,value2,value3
T: 0.604 ms
I: plural substring
3 Likes

Thanks Jason, I think you’ve nailed it for me.
I don’t need that much error checking and such, so either of those last two examples put me where I need to be (I think).
I like the one that shows the key1 = better, but either of them should work. (The output for that would look a little better I think)
That seems to be doing exactly what I need though as it makes it easy to confirm that the key is set correctly.

Got exactly what I needed now. It shows me the key value for quick and easy inspection. Awesome, thanks again @JasonWalker and others for the assistance. Much appreciated!

1 Like