Ad Widget

Collapse

UserParameter execution environment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MurgaNikolay
    Junior Member
    • May 2014
    • 21

    #1

    UserParameter execution environment

    Hi all.

    I have ruby script and ruby installed via rvm.

    In my user agent I have something like this:

    Code:
    UserParameter=test.parameter,cd /home/zabbix/scripts && ./statistic.rb
    In statistic.rb:
    Code:
    #!/usr/bin/env ruby
    puts '1'
    It's simplified example. Real script is biggest.

    If I run using ssh console - all is ok, I have '1'
    But in UserParameter is not worked. Problem in the environment.

    I've try get environment variables:

    Code:
    UserParameter=test.env,export
    Code:
    ~$ zabbix_get -s localhost -k "test.env"
    export LANG='en_GB.UTF-8'
    export PATH='/home/ubuntu/.rvm/gems/ruby-2.0.0-p0/bin:/home/ubuntu/.rvm/gems/ruby-2.0.0-p0@global/bin:/home/ubuntu/.rvm/rubies/ruby-2.0.0-p0/bin:/home/ubuntu/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/sbin:/sbin:/usr/sbin:/sbin'
    export PWD='/'
    export TERM='xterm-256color'
    I don't have right HOME variable for zabbix user and I have a rubbish a '/home/ubuntu/.rvm/gems/ruby-2.0.0-p0/bin'. It's PATH from ubuntu user!!!
    For load RVM I need zabbix user environment variables and logged shell.
    I try run a command using `bash -l -c`, but result is not better.

    Code:
    UserParameter=test.env,bash -l -c 'export'
    Code:
    ~$ zabbix_get -s localhost -k "test.env"
    declare -x LANG="en_GB.UTF-8"
    declare -x OLDPWD
    declare -x PATH="/home/ubuntu/.rvm/gems/ruby-2.0.0-p0/bin:/home/ubuntu/.rvm/gems/ruby-2.0.0-p0@global/bin:/home/ubuntu/.rvm/rubies/ruby-2.0.0-p0/bin:/home/ubuntu/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/sbin:/sbin:/usr/sbin:/sbin"
    declare -x PWD="/"
    declare -x SHLVL="1"
    declare -x TERM="xterm-256color"
    And only if change commad to:
    Code:
    UserParameter=test.env,HOME=~zabbix bash -l -c 'export'
    I got less work result:

    Code:
    declare -x HOME="/home/zabbix"
    declare -x LANG="en_GB.UTF-8"
    declare -x OLDPWD
    declare -x PATH="/home/ubuntu/.rvm/gems/ruby-2.0.0-p0/bin:/home/ubuntu/.rvm/gems/ruby-2.0.0-p0@global/bin:/home/ubuntu/.rvm/rubies/ruby-2.0.0-p0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/sbin:/sbin:/usr/sbin:/sbin:/home/zabbix/.rvm/bin"
    declare -x PWD="/"
    declare -x SHLVL="1"
    declare -x TERM="xterm-256color"
    declare -x _system_arch="x86_64"
    declare -x _system_name="Ubuntu"
    declare -x _system_type="Linux"
    declare -x _system_version="12.04"
    declare -x rvm_alias_expanded
    declare -x rvm_bin_flag
    declare -x rvm_bin_path="/home/zabbix/.rvm/bin"
    declare -x rvm_configure_env
    declare -x rvm_configure_flags
    declare -x rvm_delete_flag="0"
    declare -x rvm_docs_type
    declare -x rvm_file_name
    declare -x rvm_gemstone_package_file
    declare -x rvm_gemstone_url
    declare -x rvm_make_flags
    declare -x rvm_niceness
    declare -x rvm_nightly_flag
    declare -x rvm_only_path_flag
    declare -x rvm_path="/home/zabbix/.rvm"
    declare -x rvm_prefix="/home/zabbix"
    declare -x rvm_proxy
    declare -x rvm_quiet_flag
    declare -x rvm_ruby_alias
    declare -x rvm_ruby_bits
    declare -x rvm_ruby_file
    declare -x rvm_ruby_make
    declare -x rvm_ruby_make_install
    declare -x rvm_ruby_mode
    declare -x rvm_ruby_string="system"
    declare -x rvm_script_name
    declare -x rvm_sdk
    declare -x rvm_silent_flag
    declare -x rvm_sticky_flag
    declare -x rvm_system_flag
    declare -x rvm_use_flag
    declare -x rvm_user_flag
    declare -x rvm_version="1.25.25 (master)"
    declare -x rvm_wrapper_name
    But I think it's bad temporal solution.
    Same results if I use system.run[].

    I have many scripts that should run in the user's environment zabbix.
    Perhaps there is a more elegant solution for load user environment?
    Thanks.

    PS: Zabbix env from console.
    Code:
    ~$ export
    declare -x HISTCONTROL="ignorespace"
    declare -x HOME="/home/zabbix"
    declare -x LANG="en_GB.UTF-8"
    declare -x LANGUAGE="en_GB:en"
    declare -x LC_ALL="en_GB.UTF-8"
    declare -x LC_CTYPE="en_US.UTF-8"
    declare -x LOGNAME="zabbix"
    declare -x MAIL="/var/mail/zabbix"
    declare -x MC_SID="9922"
    declare -x MC_TMPDIR="/tmp/mc-zabbix"
    declare -x OLDPWD="/home/zabbix/scripts"
    declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/zabbix/.rvm/bin:/home/zabbix/.rvm/bin"
    declare -x PWD="/home/zabbix/scripts"
    declare -x SHELL="/bin/bash"
    declare -x SHLVL="2"
    declare -x TERM="xterm-256color"
    declare -x USER="zabbix"
  • Colttt
    Senior Member
    Zabbix Certified Specialist
    • Mar 2009
    • 878

    #2
    which bash has zabbix?

    can you run this script as zabbix-user?
    Debian-User

    Sorry for my bad english

    Comment

    • MurgaNikolay
      Junior Member
      • May 2014
      • 21

      #3
      Yes, I can run this script as zabbix and all is ok.
      This script wrote on ruby, and I want run it on rvm.

      Now I have something like this and this worked:
      Code:
      UserParameter=test.env,HOME=~zabbix bash -l -c 'my command'
      So I think this question can be closed.
      Last edited by MurgaNikolay; 11-05-2014, 11:22.

      Comment

      • markkrj
        Junior Member
        • Jun 2017
        • 2

        #4
        /etc/default/zabbix-agent

        DISCLAIMER: I'm posting this to a few old threads because google got me there and it might help other people.

        If you wanna set environment variables for your UserParameters or any other things that is run by the zabbix-agent, place them in /etc/default/zabbix-agent. The default systemd's zabbix-agent service unit has an parameter for sourcing it (EnvironmentFile=-/etc/default/zabbix-agent). The file doesn't exist by default, hence the minus (-) before the path (in systemd, an minus in front of the file avoid error if the files doesn't exist).

        If you're still using SysVinit (/etc/init.d scripts), then take a look at zabbix-agent's script (/etc/init.d/zabbix-agent) search for the following lines:
        Code:
        [ -r /etc/default/zabbix-agent ]
        if it's not there, add it.

        After that, restart the service. Profit.

        PS: You can try your zabbix-agent environment this way:
        add this to your agent's config:
        Code:
        UserParameter=test.env,export
        Restart your zabbix-agent and run
        Code:
        zabbix_get -s localhost -k test.env

        Comment

        Working...