【问题标题】:privlage issues when attempting to run the passenger-status command from within a rubyscript尝试从 ruby​​ 脚本中运行乘客状态命令时的权限问题
【发布时间】:2012-04-29 08:47:12
【问题描述】:

我用 ruby​​ 编写了一个 collectd 插件,用于检查乘客状态并报告各种指标。当我测试我的脚本时一切正常,但是当我尝试通过 collectd 运行我的脚本时,它会失败并显示以下消息。

“错误:您无权查询此 Phusion Passenger 实例的状态。请使用 'sudo' 重试。”

然后我更改了我的 ruby​​ 脚本以使用 sudo 命令来获取乘客状态,这导致了

"exec plugin: exec_read_one: error = sudo: 抱歉,你必须有一个 tty 才能运行 sudo"

然后我尝试让 collectd 以 root 身份运行脚本,但我得到了以下结果

“执行插件:胆小拒绝以root身份执行程序。”

我不确定我还能尝试什么。由 root 以外的用户使用时失败的命令是 passenger-status

这是脚本

#!/usr/bin/env ruby 需要'getoptlong'

# The name of the collectd plugin, something like apache, memory, mysql, interface, ... PLUGIN_NAME = '乘客状态'

定义用法 puts("#{$0} -h [-i ]") 出口 结束

# Main 开始 # 同步标准输出,以便正确刷新到collectd。 $stdout.sync = true

# 解析命令行选项 主机名 = 无 采样间隔 = 20 # 秒,默认值 opts = GetoptLong.new( ['--hostid', '-h', GetoptLong::REQUIRED_ARGUMENT], [ '--sampling-interval', '-i', GetoptLong::OPTIONAL_ARGUMENT ] ) opts.each 做 |opt, arg| 案例选择 当'--hostid' 主机名 = arg 当'--采样间隔' 采样间隔 = arg.to_i 结尾 结尾 使用 if !hostname

# 收集循环 虽然真的做 start_run = Time.now.to_i next_run = start_run + sampling_interval

# collectd data and print the values
data = `passenger-status`
max = data.match(/max (.*)/).to_s.split.last
count = data.match(/count (.*)/).to_s.split.last
active = data.match(/active (.*)/).to_s.split.last
inactive = data.match(/inactive (.*)/).to_s.split.last
waiting = data.match(/Waiting on global queue: ([\d]+)/).to_s.split.last
puts("PUTVAL #{hostname}/#{PLUGIN_NAME}/gauge-max_allowed_connections #{start_run}:#{max}")
puts("PUTVAL #{hostname}/#{PLUGIN_NAME}/gauge-thread_count #{start_run}:#{count}")
puts("PUTVAL #{hostname}/#{PLUGIN_NAME}/gauge-threads_active #{start_run}:#{active}")
puts("PUTVAL #{hostname}/#{PLUGIN_NAME}/gauge-threads_inactive #{start_run}:#{inactive}")
puts("PUTVAL #{hostname}/#{PLUGIN_NAME}/gauge-waiting_in_queue #{start_run}:#{waiting}")

# sleep to make the interval
while((time_left = (next_run - Time.now.to_i)) > 0) do
  sleep(time_left)
end

结束 结束

【问题讨论】:

  • 你能告诉我们你试过的代码吗?这会有所帮助。
  • 我已将整个脚本添加到我的 OP 中

标签: ruby centos passenger collectd


【解决方案1】:

查看phusion_passenger/admin_tools/server_instance.rb 文件,我能够确定乘客正在使用哪个文件来确定用户是否能够运行乘客状态命令。

filename = "#{@generation_path}/passenger-status-password.txt" password = File.open(filename, "rb") do |f| f.read end rescue Errno::EACCES raise RoleDeniedError

它试图读取的文件是 passenger-status-password.txt 这个文件位于 CentOS 5.7 上乘客版本 3.0.9 的 /tmp/passenger.1.0.19198/generation-1/目录中。我将文件修改为 644,这解决了这个问题。如果有人想在没有 sudo 的情况下运行乘客状态命令,此解决方案也适用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-06-09
    • 2013-01-21
    相关资源
    最近更新 更多