简体   繁体   English

如何安排人偶自定义事实每X小时执行一次?

[英]How to schedule puppet custom facts to execute every X hours?

I have a custom puppet fact (written in ruby) which executes a simple bash script. 我有一个自定义的木偶事实(用红宝石编写),它执行一个简单的bash脚本。 The script checks all files in the file system for world-readable permissions and returns results accordingly. 该脚本检查文件系统中的所有文件是否具有世界可读的权限,并相应地返回结果。 The puppet fact checks the output of the script and returns "fail" if the script output is not null. 木偶事实检查脚本的输出,如果脚本输出不为空,则返回“失败”。

The problem is that the script is resource intensive and I don't want it to be executed every 30 minutes (client' puppet.conf: runinternval=1800). 问题在于该脚本占用大量资源,我不希望每30分钟执行一次(客户端的puppet.conf:runinternval = 1800)。

I've tried using the "schedule" resource but because of the way puppet works, it only affects the class I'm using to evaluate the fact's output. 我尝试使用“计划”资源,但是由于人偶的工作方式,它只会影响我用来评估事实输出的类。 Puppet facts are evaluated on each puppet run, regardless of anything else. 每次其他人偶运行时都会评估人偶事实,无论其他情况如何。 (thus making them available for resources) (因此使它们可用于资源)

I've also tried to move the code that executes the bash script out from the fact and into the puppet class but it appears you cannot evaluate the output of a bash script (using "exec" type) and store it in a variable (again, because of the way puppet works). 我还尝试将执行bash脚本的代码从事实移到puppet类中,但是看来您无法评估bash脚本的输出(使用“ exec”类型)并将其存储在变量中(再次) ,因为木偶的工作方式)。

I am now reduced to try and apply some kind of scheduling mechanism using Ruby language, and implement it in the fact. 现在,我被简化为尝试使用Ruby语言应用某种调度机制,并实现它。 I've read a bit on PStore and right now this seems like a good direction. 我已经在PStore上阅读了一些,现在看来这是一个很好的方向。

Does anyone know how I can make a ruby script execute only during night? 有谁知道我怎样才能让红宝石脚本只在晚上执行? I cannot use crontab because the code will run by puppet. 我不能使用crontab,因为代码将由puppet运行。

Code snippet from the puppet class: 木偶类的代码片段:

if $::myfact == 'fail' {
  notify { "warning blah blah...":
    loglevel => err,
    schedule => 'night',
   }
} 

Code snippet from the puppet fact: 人偶事实的代码片段:

 require 'facter'
 Facter.add(:myfact) do
   setcode do
     if File.exists? '/usr/local/puppet/scripts/myscript.sh'
       result = Facter::Util::Resolution.exec("/usr/local/puppet/scripts/myscript.sh")
       if result != ''
         puts "Result of myfact is: \n #{result}"
       'fail'
     end
   end
 end

end 结束

Your custom fact can memorialize its result in a file somewhere on the system. 您的自定义事实可以将其结果记录在系统中某个文件中。 When it is evaluated, it can compare the timestamp on that file to the current system time, and either read back the value or compute it freshly, as appropriate. 评估后,它可以将该文件上的时间戳与当前系统时间进行比较,并根据需要读回该值或重新进行计算。 That also allows you to defer updates (for instance, by touch ing the file) or to manually request re-evaluation (by deleting the file). 这还允许您推迟更新(例如,通过touch文件)或手动请求重新评估(通过删除文件)。

Consider, however, whether it makes sense to decouple the costly evaluation from fact gathering. 但是,请考虑将昂贵的评估与事实收集脱钩是否有意义。 Even if Facter only occasionally has to re-evaluate the fact from scratch, Facter -- and therefore Puppet -- will take a long time to perform those runs. 即使Facter仅偶尔需要从头开始重新评估事实,Facter以及Puppet仍然需要很长时间才能执行这些运行。 You could consider instead using a scheduled job to perform the evaluation at whatever intervals you like, and have the custom fact always rely on cached results from the latest of those evaluations. 您可以考虑使用调度的作业以您希望的任何时间间隔执行评估,并使自定义事实始终依赖于最新评估中的缓存结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM