Grafana / Telegraf / Ruby / RVM:telegraf用户无法找到红宝石路径

时间:2018-11-15 14:33:46

标签: ruby rvm grafana telegraf

默认情况下,telegraf服务在用户 telegraf:telegraf

下运行
[esantos@localhost telegraf]$ systemctl status telegraf
● telegraf.service - The plugin-driven server agent for reporting metrics into InfluxDB
   Loaded: loaded (/usr/lib/systemd/system/telegraf.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2018-11-15 06:01:30 EST; 3h 16min ago
     Docs: https://github.com/influxdata/telegraf
 Main PID: 114362 (telegraf)
   CGroup: /system.slice/telegraf.service
           └─114362 /usr/bin/telegraf -config /etc/telegraf/telegraf.conf -config-directory /etc/telegraf/telegraf.d

Nov 15 06:01:30 localhost.localdomain systemd[1]: Started The plugin-driven server agent for reporting metrics into InfluxDB.
Nov 15 06:01:30 localhost.localdomain systemd[1]: Starting The plugin-driven server agent for reporting metrics into InfluxDB...

telegraf用户没有外壳

[esantos@localhost telegraf]$ cat /etc/passwd | grep telegraf
telegraf:x:996:994::/etc/telegraf:/bin/false

现在出现问题了……配置告诉telegraf运行一个ruby脚本,它将返回一个JSON对象

[esantos@localhost telegraf]$ cat /etc/telegraf/telegraf.d/groove.conf
[[inputs.exec]]
  commands = ["ruby /etc/telegraf/pm_collector.rb"]

对于esantos这样的用户,一切运行良好- 成功

不幸的是不适用于以telegraf用户身份运行的telegraf服务,返回 "ruby": executable file not found in $PATH" - FAIL

[esantos@localhost telegraf]$ sudo -u telegraf telegraf --config /etc/telegraf/telegraf.d/groove.conf --test
2018-11-15T14:19:08Z E! Error in plugin [inputs.exec]: exec: exec: "ruby": executable file not found in $PATH for command 'ruby /etc/telegraf/pm_collector.rb'

以telegraf用户身份打开外壳也可以- 成功

[esantos@localhost telegraf]$ sudo -u telegraf /bin/bash
bash-4.2$ cat .bashrc
source /usr/local/rvm/scripts/rvm
bash-4.2$ cat .bash_profile
source /usr/local/rvm/scripts/rvm
bash-4.2$
bash-4.2$ rvm -v
rvm 1.29.4 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]
bash-4.2$ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
bash-4.2$ gem -v
2.7.8
bash-4.2$ echo $PATH
/usr/local/rvm/gems/ruby-2.5.1/bin:/usr/local/rvm/gems/ruby-2.5.1@global/bin:/usr/local/rvm/rubies/ruby-2.5.1/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/rvm/bin
bash-4.2$ gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 2.7.8
  - RUBY VERSION: 2.5.1 (2018-03-29 patchlevel 57) [x86_64-linux]
  - INSTALLATION DIRECTORY: /usr/local/rvm/gems/ruby-2.5.1
  - USER INSTALLATION DIRECTORY: /etc/telegraf/.gem/ruby/2.5.0
  - RUBY EXECUTABLE: /usr/local/rvm/rubies/ruby-2.5.1/bin/ruby
  - EXECUTABLE DIRECTORY: /usr/local/rvm/gems/ruby-2.5.1/bin
  - SPEC CACHE DIRECTORY: /etc/telegraf/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /usr/local/rvm/rubies/ruby-2.5.1/etc
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /usr/local/rvm/gems/ruby-2.5.1
     - /usr/local/rvm/gems/ruby-2.5.1@global
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /usr/local/rvm/gems/ruby-2.5.1/bin
     - /usr/local/rvm/gems/ruby-2.5.1@global/bin
     - /usr/local/rvm/rubies/ruby-2.5.1/bin
     - /sbin
     - /bin
     - /usr/sbin
     - /usr/bin
     - /usr/local/rvm/bin

这与登录vs非登录shell有关吗?如何克服这个? 我们可以在更大范围内解决这个问题。如何使用无外壳用户在RVM多用户安装中运行ruby脚本?

1 个答案:

答案 0 :(得分:0)

这里的技巧是在/bin/bash --login之前加上我们要运行的命令。这样就取消了登录shell并调用了初始化脚本。