Ad Widget

Collapse

PATCH: Lua Scripts in Zabbix

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nelsonab
    Senior Member
    Zabbix Certified SpecialistZabbix Certified Professional
    • Sep 2006
    • 1233

    #1

    PATCH: Lua Scripts in Zabbix

    ** Documentation and latest version can be found at: http://trac.red-tux.net/wiki/lua **


    This patch is for Zabbix 1.8.3, I will work on a 1.8.4 version soon.

    This patch requires no DB changes.

    This patch will require a recompile and a reinstall of your php frontend. (same as an upgrade)

    You will need to have the lua development libraries installed for this to work.

    To install patch
    1) Untar zabbix 1.8.3 into a fresh directory
    1a) Download patch (see link above)
    2) In the new 1.8.3 directory run the following command: patch -p0 < lua-zabbix-1.8.3.patch (if that does not work try patch -p1 < lua-zabbix-1.8.3.patch)
    3) Run "configure" with the following option --with-lua
    4) compile and install
    5) copy frontend directory as you would a normal install/upgrade


    So what does this patch do? It allows you to create your own Lua scripts and run them as a normal item. Right now there are only two functions available in the lua script engine, getitem(itemid) (returns val,clock in that order) and zabbix_log(level,message).

    Configuring is pretty straight forward as a new item item is added called "Lua Script" (type 16 for those interested). The key just needs to be in the format of lua[unique identifier]. Also it is important that your lua script return a value using the "return" statement.

    In addition you can also set up your own lua library. Just point the zabbix_server.conf variable to where you want to load your library for example:
    LuaScriptLibrary=/etc/zabbix/lua/zabbix.lua

    There is also a configuration option to start more Lua Pollers using
    StartLUAPollers, the default value is 1.

    In the following example I have created a lua library with the following:
    Code:
    function sum(...)
      total=0
      for i=1,#arg do
        total = total + get_item(arg[i])
      end
      return total
    end
    The main goal of this patch is to allow for people to do simple scripting within Zabbix. Imagine if you needed to create a standard deviation function for various items, how would you implement that now? Via an external script which would either query the API or the database directly. Also looking long term I would also like to tie this into triggers, thus allowing for the writing of more complex triggers using Lua. Imagine conditional triggers where the value of one item determines the sensitivity or values a trigger will activate on for other items.

    If you have suggestions for the next patch release let me know.
    Attached Files
    Last edited by nelsonab; 13-09-2011, 09:03. Reason: made note of latest version
    RHCE, author of zbxapi
    Ansible, the missing piece (Zabconf 2017): https://www.youtube.com/watch?v=R5T9NidjjDE
    Zabbix and SNMP on Linux (Zabconf 2015): https://www.youtube.com/watch?v=98PEHpLFVHM

  • nelsonab
    Senior Member
    Zabbix Certified SpecialistZabbix Certified Professional
    • Sep 2006
    • 1233

    #2
    Attached to this message is the latest version of the Lua Patch for Zabbix.

    To install this patch:
    1) Download a fresh Zabbix-1.8.3 and untar
    2) Download patch
    3) cd to new Zabbix-1.8.3 directory and run the following command
    patch -p1 < (path_to_patch)1.8.3-p2-lua.patch
    4) run the bootstrap.sh (bash bootstrap.sh), this will regenerate the appropriate autoconf scripts and headers
    5) configure with the --with-lua option.

    Overall things are similar to the above. In the config file there are now the following options:

    StartLUAPollers - (default 1) The number of Lua pollers to fork
    LuaScriptLibrary - The location where the lua script library cab be found
    LuaCheckPermissions - (0 = off -> default, 1 = on) Turns on permissions checking for database related Lua commands
    LuaDefaltUser - (default: guest) The default user to use should the user information not be provided

    The following commands are available:
    zabbix_log(LogLevel,Message) - displays a message in the zabbix_server log with the prefix of lua_zabbix_log
    get_last(itemid) - returns val,clock in that order
    now() - returns the current time in seconds since epoch

    The available log levels are:
    LOG_LEVEL_EMPTY
    LOG_LEVEL_CRIT
    LOG_LEVEL_ERR
    LOG_LEVEL_WARNING
    LOG_LEVEL_DEBUG

    When creating a new Lua item the Alias of the user used to create the object is written to the item's properties in the database. When get_last is called the alias used to create the item is checked to see if they have read permission to the item. If they do not an error is returned and the item becomes "Not Supported". No permissions has the lowest priority (So far I'm not 100% sure what the order or priority is for Zabbix, though I think no permissions should be highest followed by read only then read/write).

    More information can also be found here:


    If you have any questions or comments let me know!

    Enjoy!
    Attached Files
    Last edited by nelsonab; 20-01-2011, 00:23.
    RHCE, author of zbxapi
    Ansible, the missing piece (Zabconf 2017): https://www.youtube.com/watch?v=R5T9NidjjDE
    Zabbix and SNMP on Linux (Zabconf 2015): https://www.youtube.com/watch?v=98PEHpLFVHM

    Comment

    • qix
      Senior Member
      Zabbix Certified SpecialistZabbix Certified Professional
      • Oct 2006
      • 423

      #3
      Good job on this!
      This is a feature zabbix needs in my opinion.
      This way we can use small scripts from within Zabbix so we don't need shell access to the server to setup external scripts.

      I hope this will eventually mean a merge with the main Zabbix source tree
      With kind regards,

      Raymond

      Comment

      • nelsonab
        Senior Member
        Zabbix Certified SpecialistZabbix Certified Professional
        • Sep 2006
        • 1233

        #4
        Attached is another version of the patch.

        Again this is for 1.8.3, for installation instructions see my previous post.

        This version adds script timeouts. There is also a new config file option LuaTimeout. The default timeout is 15 seconds, the value can be set from 0 to 60 seconds. When a script times, an error is returned and the item becomes "Not Supported".
        Attached Files
        RHCE, author of zbxapi
        Ansible, the missing piece (Zabconf 2017): https://www.youtube.com/watch?v=R5T9NidjjDE
        Zabbix and SNMP on Linux (Zabconf 2015): https://www.youtube.com/watch?v=98PEHpLFVHM

        Comment

        • qix
          Senior Member
          Zabbix Certified SpecialistZabbix Certified Professional
          • Oct 2006
          • 423

          #5
          Timeouts are a good idea. However, I'm not sure about the 'fail once, become unsupported'. Maybe it should be timeout 3 times in row, then become unsupported? I can imagine a script taking a while because of unforeseen external influences
          With kind regards,

          Raymond

          Comment

          • nelsonab
            Senior Member
            Zabbix Certified SpecialistZabbix Certified Professional
            • Sep 2006
            • 1233

            #6
            Unfortunately there's no easy way around that. Zabbix automatically sets something as "Not Supported" the moment there is an error, this is true of all items. I could dive into "fixing" this but that would be a lot of work and I'd like to keep my focus on this in the hopes that one day it may gain inclusion. :-)
            RHCE, author of zbxapi
            Ansible, the missing piece (Zabconf 2017): https://www.youtube.com/watch?v=R5T9NidjjDE
            Zabbix and SNMP on Linux (Zabconf 2015): https://www.youtube.com/watch?v=98PEHpLFVHM

            Comment

            • qix
              Senior Member
              Zabbix Certified SpecialistZabbix Certified Professional
              • Oct 2006
              • 423

              #7
              Fair enough
              With kind regards,

              Raymond

              Comment

              • nelsonab
                Senior Member
                Zabbix Certified SpecialistZabbix Certified Professional
                • Sep 2006
                • 1233

                #8
                Attached is a patch for Zabbix 1.8.4 There are no new features in this patch.

                To install see post #2
                Attached Files
                RHCE, author of zbxapi
                Ansible, the missing piece (Zabconf 2017): https://www.youtube.com/watch?v=R5T9NidjjDE
                Zabbix and SNMP on Linux (Zabconf 2015): https://www.youtube.com/watch?v=98PEHpLFVHM

                Comment

                • richlv
                  Senior Member
                  Zabbix Certified Trainer
                  Zabbix Certified SpecialistZabbix Certified Professional
                  • Oct 2005
                  • 3112

                  #9
                  note that unsupported items are retried every 10 minutes anyway - see administration -> general -> general or somewhere in that dropdown to change that
                  Zabbix 3.0 Network Monitoring book

                  Comment

                  • clopez
                    Junior Member
                    • Feb 2011
                    • 8

                    #10
                    Hello,

                    I am testing your patch on Zabbix 1.8.4 but I am not sure how to pass the name of an item to the function get_last.
                    • I have a host with an Zabbix trapper item.
                      • The KEY of this item is "TEMP"
                      • The itemid (on SQL database) of this item is 22182


                    • On the same host I am trying to create a "Lua Script" Item with key "lua[TEMP]"
                      • If I use the following code for this Lua Script:

                        Code:
                        value = get_last(22182)
                        return (value)
                        • It works as expected and I see in the Zabbix debug log that it does the following SQL query
                          Code:
                           Query [txnlev:0] [select clock,value from history_uint where itemid=22182 order by clock desc limit 1]
                      • But If I put the Item name instead of the itemid it don't works:

                        Code:
                        value = get_last(TEMP)
                        return (value)
                        • It is not able to translate the key of the item (TEMP) to the itemid (22182).


                          Code:
                          Query [txnlev:0] [select clock,value from history_uint where itemid=0 order by clock desc limit 1]

                    What I am missing? Thanks!

                    Comment

                    • nelsonab
                      Senior Member
                      Zabbix Certified SpecialistZabbix Certified Professional
                      • Sep 2006
                      • 1233

                      #11
                      You are not missing anything. :-) Right now get_last cannot accept anything other than numeric item ids.

                      Let me see what I can do with regards to writing a lookup routine which can allow you to look up what the itemid is for a host:item combination. You can then take that and feed it to the get_last function. Also I should add some type checking to ensure get_last receives a numeric value.

                      Thanks for the feedback.
                      RHCE, author of zbxapi
                      Ansible, the missing piece (Zabconf 2017): https://www.youtube.com/watch?v=R5T9NidjjDE
                      Zabbix and SNMP on Linux (Zabconf 2015): https://www.youtube.com/watch?v=98PEHpLFVHM

                      Comment

                      • clopez
                        Junior Member
                        • Feb 2011
                        • 8

                        #12
                        Within the item type "Calculated" I simply can use last(TEMP) and Zabbix is able to translate it. It would be very nice that the "lua script" type was able to translate/understand the item keys as the "calculated" item type does.

                        I guess it is using the function evaluate_LAST() defined in src/libs/zbxserver/evalfunc.c

                        Thanks for the quick reply!!

                        Comment

                        • clopez
                          Junior Member
                          • Feb 2011
                          • 8

                          #13
                          Perhaps a way of implementing this is defining a new function in src/libs/zbxlua/zbxlua.c like get_itemid(key,host) that would return the itemid of that host:key. The parameter host would be optional and will default to the current host were the "lua script" is defined. What do you think about this?

                          Thanks again!

                          Comment

                          • clopez
                            Junior Member
                            • Feb 2011
                            • 8

                            #14
                            Hello,

                            I have notified that when you return a non defined variable or a nil zabbix-server crashes giving a segmentation fault:

                            For example. The following script
                            Code:
                            return (nondefined)
                            will cause a fatal error on the zabbix-server

                            Also if you try to return a nil this will happen

                            Code:
                            return (nil)

                            Comment

                            • clopez
                              Junior Member
                              • Feb 2011
                              • 8

                              #15
                              However at first sight it only seems to happen on the return statement. If you try to do an operation with an undefined variable
                              Code:
                              defined = nondefined + 2
                              return (defined)
                              It don't crashes the server, and I can see on the logs:

                              Code:
                              Lua error: [string "defined = nondefined + 2..."]:1: attempt to perform arithmetic on global 'nondefined' (a nil value)
                              0.000399 seconds to execute Lua script itemid:22273  script:(null)
                              Item [55AA-1:lua[TEMP]] error: [string "defined = nondefined + 2..."]:1: attempt to perform arithmetic on global 'nondefined' (a nil value)

                              Comment

                              Working...