Welcome to Pandora FMS Community!

Find answers, ask questions, and connect with our community around the world.

Welcome to Pandora FMS Community Forums Community support Advanced troubleshooting if-selection does not handle exit codes correctly in generic_proc module

  • if-selection does not handle exit codes correctly in generic_proc module

    Posted by thomas on November 10, 2017 at 13:35

    I’m trying to do something similar to the ICMP Checks in https://wiki.pandorafms.com/index.php?title=Pandora:Documentation_en:Operations section 1.7.1
    but the outcome of module_exec does not show up as expected in the FMS Console. Running the command locally does indeed work.

    The manual gives this example:

    module_begin
    module_name Ping
    module_type generic_proc
    module_exec ping -c 1 192.168.100.54 >/dev/null 2>&1; if [ $? == 0 ]; then echo 1; else echo 0; fi
    module_end

    My intallation is base on pandorafms.agent_unix_7.0NG.714.deb, pandorafms.console_7.0NG.714.deb and pandorafms.server_7.0NG.714.deb

    Excerpt from my /etc/pandora/pandora_agent.conf, where I’ve cleaned up the configuration to make the bug more obvious:

    module_begin
    module_name Always 0
    module_type generic_proc
    module_exec echo 0
    module_end
    
    module_begin
    module_name Always 1
    module_type generic_proc
    module_exec echo 1
    module_end
    
    module_begin
    module_name Always 0 complicated
    module_type generic_proc
    module_exec false; if [ $? == 0 ]; then echo 1; else echo 0; fi
    module_end
    
    module_begin
    module_name Always 1 complicated
    module_type generic_proc
    module_exec true; if [ $? == 0 ]; then echo 1; else echo 0; fi
    module_end
    

    Running the commands manually in the shell works as expected:

    $ echo 0
    0

    $ echo 1
    1

    $  false; if [ $? == 0 ]; then echo 1; else echo 0; fi
    0

    $  true; if [ $? == 0 ]; then echo 1; else echo 0; fi
    1

    But, the FMS Console shows:

    Always 0
    0
    Always 1
    1
    Always 0 complicated
    0
    Always 1 complicated
    0

    Thus it seems like the if-selection does not work when running in the Pandora Agent, but it works when running from the command line.

    vic replied 7 years, 2 months ago 2 Members · 5 Replies
  • 5 Replies
  • thomas

    Member
    November 10, 2017 at 15:12
    0 Karma points
    Community rank: tentacle-noob-1 Tentacle noob
    Like it
    Up
    0
    Down
    Drop it
    ::
    module_begin
    module_name Simple 0
    module_type generic_proc
    module_exec true; echo $?
    module_end
    
    module_begin
    module_name Simple 1
    module_type generic_proc
    module_exec false; echo $?
    module_end

    Using the shell:

    $ true; echo $?
    0

    $ false; echo $?
    1

    And the FMS Console shows:

    Simple 0
    0

    Simple 1
    1

    This works as expected. (But it is of course not what I want to do.)

  • vic

    Administrator
    November 13, 2017 at 13:00
    1552 Karma points
    Community awards: bulb Bright ideas
    Community rank: tentacle_master_icon Tentacle Master
    Like it
    Up
    0
    Down
    Drop it
    ::

    Hi thomas,

    Can you try to create a module with this configuration?

    module_begin
    module_name Ping
    module_type generic_proc
    module_exec ping 192.168.70.127 -c 1 |grep "1 received" | wc -l
    module_end

    If the check is correct, it will show a “1”.

    Best regards,

    vic.

  • thomas

    Member
    November 13, 2017 at 13:40
    0 Karma points
    Community rank: tentacle-noob-1 Tentacle noob
    Like it
    Up
    0
    Down
    Drop it
    ::

    Let’s try to find out if it is something special with the  $? variable!

    module_begin
    module_name Always 0 fool it again
    module_type generic_proc
    module_exec a=1;  if [ $a == 0 ]; then echo 1; else echo 0; fi
    module_end
    
    module_begin
    module_name Always 1 fool it again
    module_type generic_proc
    module_exec a=0;  if [ $a == 0 ]; then echo 1; else echo 0; fi
    module_end

    Using the shell:
    $ a=1;  if [ $a == 0 ]; then echo 1; else echo 0; fi
    0
    $ a=0;  if [ $a == 0 ]; then echo 1; else echo 0; fi
    1

    The Pandora FMS Console:
    Always 0 fool it again
    0
    Always 1 fool it again
    0

    It seems I can’t even get an ordinary variable to work inside the if-clause.

  • thomas

    Member
    November 13, 2017 at 17:28
    0 Karma points
    Community rank: tentacle-noob-1 Tentacle noob
    Like it
    Up
    0
    Down
    Drop it
    ::

    I believe that I have solved the problem.

    Using -eq instead of == makes the example given in the manual work.

    module_begin
    module_name Ping
    module_type generic_proc
    module_exec ping -c 1 192.168.100.54 >/dev/null 2>&1; if [ $? -eq 0 ]; then echo 1; else echo 0; fi
    module_end
  • vic

    Administrator
    November 14, 2017 at 12:36
    1552 Karma points
    Community awards: bulb Bright ideas
    Community rank: tentacle_master_icon Tentacle Master
    Like it
    Up
    0
    Down
    Drop it
    ::

    Hi thomas,

    It seems that you are correct, I have modified the wiki, so that it doesn’t happen again.
    Thanks for reporting.

    Best regards,

    vic.