Ad Widget

Collapse

UserParameter with awk and dollar signs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arli
    Member
    • Jan 2008
    • 71

    #1

    UserParameter with awk and dollar signs

    Does anyone know how to pass a parameter to awk print column command which is defined in zabbix agent UserParameter?

    I have a key
    swapinfo[memory,5]

    And corresponding UserParameter in zabbix_agentd.conf file
    UserParameter=swapinfo[*],swapinfo -t | grep $1 | awk -F' ' '{print $2}'

    ... so I need this to execute
    swapinfo -t | grep memory | awk -F' ' '{print $5}'

    But it's either executing awk -F' ' '{print $2}' or returnes ZBX_NOTSUPPORTED
    I also tried to use $$2 and \$$2 and \$\$2.


    Code:
    bash-4.0# swapinfo -t
                 Kb      Kb      Kb   PCT  START/      Kb
    TYPE      AVAIL    USED    FREE  USED   LIMIT RESERVE  PRI  NAME
    dev     8388608   10676 8377932    0%       0       -    1  /dev/vg00/lvol2
    dev     8388608   10556 8378052    0%       0       -    1  /dev/vg00/swap2
    reserve       - 16587652 -16587652
    memory  23805996 5099444 18706552   21%
    total   40583212 21708328 18874884   53%       -       0    -
    And no, I don't want to use wrapper script, because I have to use this feature on many servers. And maintaining those scripts means extra work.
    Do you think this can be done?
  • Alexei
    Founder, CEO
    Zabbix Certified Trainer
    Zabbix Certified SpecialistZabbix Certified Professional
    • Sep 2004
    • 5654

    #2
    You may use "print $ 1" instead of "print $2" for awk scripts.
    Alexei Vladishev
    Creator of Zabbix, Product manager
    New York | Tokyo | Riga
    My Twitter

    Comment

    • arli
      Member
      • Jan 2008
      • 71

      #3
      I tried this, but it doesn't seem to work - still not printing the column 5, that is second argument on the item configuration.

      In agent config file
      UserParameter=swapinfo[*],swapinfo -t | grep $1 | awk -F' ' '{print $ 2}'

      In agent log file
      Run remote command [swapinfo -t | grep memory | awk -F' ' '{print $ 2}'] Result [8] [23805996]...

      Comment

      • lentyai
        Junior Member
        Zabbix Certified Specialist
        • Oct 2008
        • 29

        #4
        Use '$1 $2' for simple user parameters like
        UserParameter=param.param,cmd | awk '{print $1}'

        and
        use '$$1 $$2...' for parameters with parameters.
        UserParameter=param[*],cmd | awk '{print $$1}'

        Comment

        • arli
          Member
          • Jan 2008
          • 71

          #5
          Originally posted by arli
          I also tried to use $$2 and \$$2 and \$\$2.
          UserParameter=swapinfo[*],swapinfo -t | grep $1 | awk -F' ' '{print $$2}'

          And result is the same - not using "parameter with parameter":
          Run remote command [swapinfo -t | grep memory | awk -F' ' '{print $2}'] Result [8] [23805996]...

          Comment

          • lentyai
            Junior Member
            Zabbix Certified Specialist
            • Oct 2008
            • 29

            #6
            Oh!
            Sorry!

            I don't understood what you want pass awk field number as parameter.

            I think, it's not good idea.

            Comment

            • arli
              Member
              • Jan 2008
              • 71

              #7
              The idea was to get all the memory related values with only one user parameter - so there would be less things to add to the agent conf file.
              If it turns out, that this can't be done, then I guess I'll have to use one parameter for each column.

              Comment

              • arli
                Member
                • Jan 2008
                • 71

                #8
                I actually found solution to that problem and now when I accidentally stumbled upon this old thread againt, I think it's good idea to post the solution here too

                UserParameter=swapinfo[*],swapinfo -t | grep $1 | awk -F' ' "{ print $\"$2\" }"

                This thread helped me:
                http://stackoverflow.com/questions/2...in-bash-scriptYou need double quotes to allow variable interpolation, which means you then need to escape the other dollar signs with backslashes so $1 and $2 are not interpolated. Also you need double quotes around "$col1type".

                Comment

                • tobster
                  Junior Member
                  • Mar 2014
                  • 5

                  #9
                  UserCommand with awk no result

                  Hi forum

                  sorry to warm up this older thread but I really got a weird problem with a UserParameter which uses awk as well.
                  I need my zabbix-agentd to pull the pakets stats from its iptables. So I added zabbix to sudoers
                  Code:
                  zabbix	ALL=NOPASSWD:	/sbin/iptables -L INPUT 3 -nvx
                  then I added the following to zabbix_agentd.conf
                  Code:
                  UserParameter=ipt.pkt.stat[*],sudo /sbin/iptables -L $1 $2 -nvx|awk '{print $$1}'
                  But if I use zabbix_get on my zabbix server to get the data I always get an empty response
                  Code:
                  zabbix_get -s 192.168.199.130 -k ipt.pkt.stat\[INPUT,3\]
                  According to the zabbix-agentd logs the remote command looks fine but produces empty response
                  Code:
                  20966:20140321:192934.965 In zbx_popen() command:'sudo /sbin/iptables -L INPUT 3 -nvx|awk '{print $1}''
                  20966:20140321:192934.973 Run remote command [sudo /sbin/iptables -L INPUT 3 -nvx|awk '{print $1}'] Result [0] []...
                  I can copy&paste the command from the logs and get a response
                  Code:
                  su zabbix -s /bin/sh
                  sh-4.1$ sudo /sbin/iptables -L INPUT 3 -nvx | awk '{print $1}'
                  247722
                  Does anyone have an idea why Zabbix-Server does not get the same result?
                  System is a Centos 6.5 with selinux disabled

                  Thanks for any help

                  tobi

                  Comment

                  • tobster
                    Junior Member
                    • Mar 2014
                    • 5

                    #10
                    I did some more testing of this UserParameter. I added a second one which prints colon 2 with awk and not colon 1 as the other.
                    Code:
                    UserParameter=ipt.pkt.stat[*],sudo /sbin/iptables -L $1 $2 -nvx | awk '{print $$1}'
                    UserParameter=ipt.traffic.stat[*],sudo /sbin/iptables -L $1 $2 -nvx | awk '{print $$2}'
                    And the second one works properly!
                    Code:
                    zabbix_get -s 192.168.199.130 -k ipt.pkt.stat\[INPUT,3\]
                    
                    zabbix_get -s 192.168.199.130 -k ipt.traffic.stat\[INPUT,3\]
                    0
                    first I thought it's a problem with the parameter name, so I changed it but did not work. Then I changed the $$1 of the first parameter to $$2 and tested again. It works!
                    Code:
                    zabbix_get -s 192.168.199.130 -k ipt.pkt.stat\[INPUT,3\]
                    0
                    Then I tested the same on a debian wheezy server and it's the same behaviour

                    Comment

                    • knike
                      Junior Member
                      • Feb 2015
                      • 1

                      #11
                      I found a simple solution (my version zabbix 2.4).
                      For me, working triple dollar sign.
                      1. Test file.
                      # cat /tmp/iostat
                      0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0 0 c0t500003953818EC00d0
                      0.0 15.3 0.0 184.6 0.0 0.3 0.0 19.6 0 6 c0t500003953818EDB0d0
                      0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0 0 c0t500003953818EA28d0


                      2. Zabbix-agentd.conf:
                      UserParameter=custom.hdd[*],awk '/$1/{print $$$2}' /tmp/iostat

                      3. Check from Zabbix Server:
                      # zabbix_get -s 192.168.1.1 -k 'custom.hdd[c0t500003953818EDB0d0,11]'
                      c0t500003953818EDB0d0
                      # zabbix_get -s 192.168.1.1 -k 'custom.hdd[c0t500003953818EDB0d0,4]'
                      184.6

                      Comment

                      Working...