利用 Chef 进行服务器监控的可落地方案
一 总体思路与架构
二 快速落地步骤
三 关键配置与示例
Ohai.plugin(:CpuUsage) do
provides 'cpu_usage'
collect_data do
cpu_usage = `top -bn1 | grep 'load average' | awk '{printf("%.2f", $(NF-2))}'`.strip
node.normal['cpu_usage'] = cpu_usage
end
end
ohai 'load_custom_plugins' do
plugins ['cpu_usage']
action :nothing
end.run_action(:run)
log "CPU Usage: #{node['cpu_usage']}%" do
level :info
end
data_collector.server_url "https://automate.example.com/data-collector/v0/"
data_collector.token "your-secure-token-here"
data_collector.mode :both
data_collector.raise_on_failure false
data_collector.output_locations(
files: ["/var/log/chef/collector.log"],
urls: ["https://webhook.example.com/events"]
)
chef-client --test --config /path/to/client.rb,在 /var/log/chef/collector.log 可见 Run Start/Run End 事件。execute 'cleanup_old_logs' do
command 'find /var/log -type f -name "*.log" -mtime +7 -delete'
only_if 'df --output=pcent / | tail -1 | tr -d "% " | awk "$1 > 80"'
end
cookbook 'newrelic', '~> 最新稳定版本'recipe[newrelic::default]chef-client 自动部署与注册。四 最佳实践与注意事项
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。