Grafana/Telegraf/Ruby/RVM:telegraf 用户无法找到 ruby​​ 路径

Posted

技术标签:

【中文标题】Grafana/Telegraf/Ruby/RVM:telegraf 用户无法找到 ruby​​ 路径【英文标题】:Grafana/Telegraf/Ruby/RVM : telegraf user isn't able to find ruby path 【发布时间】:2019-04-18 16:35:07 【问题描述】:

默认情况下,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 这样的用户来说,一切运行良好 - 成功

不幸的是,以电报用户身份运行的电报服务不起作用,返回 "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 用户身份打开 shell 也可以 - 成功

[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

这可能与登录与非登录外壳有关吗?如何克服这一点? 我们可以在更广泛的范围内处理这个问题。如何使用没有 shell 的用户在 RVM 多用户安装中运行 ruby​​ 脚本?

【问题讨论】:

【参考方案1】:

这里的技巧是在我们希望运行的命令前面加上/bin/bash --login。这样会打开一个登录 shell 并调用初始化脚本。

【讨论】:

以上是关于Grafana/Telegraf/Ruby/RVM:telegraf 用户无法找到 ruby​​ 路径的主要内容,如果未能解决你的问题,请参考以下文章