Ruby 厨师长Ohai插件无限循环,资源泄漏和脱壳风险最小化

Ruby 厨师长Ohai插件无限循环,资源泄漏和脱壳风险最小化,ruby,chef-infra,Ruby,Chef Infra,通常,Ohai插件会定期运行以收集一些主机参数,一些插件通常会添加到公司的所有节点。这可能对资源使用和Ohai如何处理这一点很敏感。我这里有两个问题 第一个问题是,如果我意外地放入无限循环,会发生什么?Ohai/ruby是否有一些最大堆大小或内存限制 第二个问题是关于在Ohai的壳牌公司。是否可以减少超时?你知道更多的保护措施以防万一吗 我现在只使用特殊的ruby超时: require 'timeout' begin status = Timeout::timeout(600) {

通常,Ohai插件会定期运行以收集一些主机参数,一些插件通常会添加到公司的所有节点。这可能对资源使用和Ohai如何处理这一点很敏感。我这里有两个问题

第一个问题是,如果我意外地放入无限循环,会发生什么?Ohai/ruby是否有一些最大堆大小或内存限制

第二个问题是关于在Ohai的壳牌公司。是否可以减少超时?你知道更多的保护措施以防万一吗

我现在只使用特殊的ruby超时:

require 'timeout'
begin
  status = Timeout::timeout(600) {
    # all code here
  }
rescue Timeout::Error
  puts 'timeout'
end

如果ohai挂起,chef客户端运行将不会启动/成功。。您应该在某种监视中注意到这一点

关于超时部分:搜索源代码显示:

def shell_out(cmd,**选项)
#除非调用方指定30秒后超时
选项[:超时]| |=30
so=Mixlib::ShellOut.new(cmd,options)
因此,您应该能够根据自己的喜好设置超时(本例中为2秒):

so=shell\u out(“/bin/your command”,:timeout=>2)
关于第三个分问题

你知道更多的保护措施以防万一吗


你越来越胖了。尝试解决出现的问题,停止工程设计。

仅为了完整性,Chef不防范损坏或恶意Ohai插件。如果您在Ohai插件中加入
sleep 1 while true
,它将永远快乐地坐在那里。

似乎我已经找到了解决方案,可以在CPU、磁盘空间使用、磁盘空间I/O、长时间运行超时和堆大小内存限制方面限制Redhat Linux的Ohai资源。因此,您不会影响其他主机的组件。在理想的情况下,您可以编写优化和正确的代码,但内存泄漏是一个全球性问题,可能会发生,所以我认为需要保护,特别是当您将Ohai插件加载到hunders或tousands生产服务器时

CPU- 如果我是对的,Ohai插件的cpu优先级最低(-19?)。请确认这一点,如果你知道。所以Ohai插件在CPU方面不会影响您的生产应用程序

磁盘空间- Ohai插件应该写入节点属性

为意外的长期运行提供保护-

    require 'timeout'
    begin
      status = Timeout::timeout(600) {
        # Ohai plugin code is here
      }
    rescue Timeout::Error
      puts 'timeout'
    end
require "thread"
# This thread is memory watcher. It works separately and does exit if heap size reached.
# rss is checked including childs but excluding shared memory.
# This could be ok for Ohai plugin. I'm assuming memory is not shared.
# Exit - if heap size limit reached (10 000 KB) or any unexpected scenario happened.
Thread.start {
  loop do
    sleep 1
    current_memory_rss = `ps ax -o pid,rss | grep -E "^[[:space:]]*#{$$}"`.strip.split.map(&:to_i)[1].to_i

    if current_memory_rss != nil && current_memory_rss > 0 && $$ != nil && $$.to_i > 0
      exit if current_memory_rss > 10_000
    else
      exit
    end
  end
}

# Your Ohai code begins here
# For testing, any code can be included to make memory growing constantly as infinite loop:
loop do
  puts `ps ax -o pid,rss | grep -E "^[[:space:]]*#{$$}"`.strip.split.map(&:to_i)[1].to_s + ' KB'
end
为外壳意外长期运行提供保护:

so = shell_out("/bin/your-command", :timeout => 30)
内存(RAM)堆大小限制-

    require 'timeout'
    begin
      status = Timeout::timeout(600) {
        # Ohai plugin code is here
      }
    rescue Timeout::Error
      puts 'timeout'
    end
require "thread"
# This thread is memory watcher. It works separately and does exit if heap size reached.
# rss is checked including childs but excluding shared memory.
# This could be ok for Ohai plugin. I'm assuming memory is not shared.
# Exit - if heap size limit reached (10 000 KB) or any unexpected scenario happened.
Thread.start {
  loop do
    sleep 1
    current_memory_rss = `ps ax -o pid,rss | grep -E "^[[:space:]]*#{$$}"`.strip.split.map(&:to_i)[1].to_i

    if current_memory_rss != nil && current_memory_rss > 0 && $$ != nil && $$.to_i > 0
      exit if current_memory_rss > 10_000
    else
      exit
    end
  end
}

# Your Ohai code begins here
# For testing, any code can be included to make memory growing constantly as infinite loop:
loop do
  puts `ps ax -o pid,rss | grep -E "^[[:space:]]*#{$$}"`.strip.split.map(&:to_i)[1].to_s + ' KB'
end
请让我知道你是否有更好的解决方案,但它似乎是有效的

磁盘I/O读取大量使用-
timeout在这里应该有帮助,但建议避免使用类似于
find
和类似的命令

这与什么有关?我添加了它只是因为节点属性在json中,它们不是;-)或取决于你在什么地方看到它的方式谢谢。我见过一些Ohai问题,当厨师长客户受到影响时,Ohai回滚并不是那么简单。插件文件存储在本地。所以我想在这里讨论一下,如果您知道如何限制插件,使其不影响其他主机组件。我知道这有点过于工程化,但有一些限制,比如Java heap size max或其他一些限制,那就太好了。我明白,从这个角度来看,这真的很有意义:)不确定Ruby中是否有“heap”这样的东西。。