Skip to content

Instantly share code, notes, and snippets.

@stefanandres
Last active August 29, 2015 14:09
Show Gist options
  • Save stefanandres/fa9d9e8d05d9a93004af to your computer and use it in GitHub Desktop.
Save stefanandres/fa9d9e8d05d9a93004af to your computer and use it in GitHub Desktop.
root@controller /var/lib/puppet/state $ cat check_puppet
#!/usr/bin/env ruby
# 2014, s.andres@syseleven.de
#
# Parse last_run_report.yaml and check what puppet's status was on the last run
# print changes/failure to stdout and return nagios usable exit codes
#
require 'yaml'
require 'puppet'
require 'optparse'
options = OpenStruct.new
options.report_file = '/var/lib/puppet/state/last_run_report.yaml'
options.warning = 3600
options.critical = 7200
OptionParser.new do |opts|
opts.banner = "Usage: check_puppet_agent [options]"
opts.on('-w [seconds]', OptionParser::DecimalInteger, "warning threshold (default #{options.warning} seconds)") { |v| options.warning = v }
opts.on('-c [seconds]', OptionParser::DecimalInteger, "critical threshold (default #{options.critical} seconds)") { |v| options.critical = v }
opts.on('-f [filename]', "path to report file (default: #{options.report_file})") { |v| options.report_file = v }
end.parse!
if ! File.file?(options.report_file)
puts "CRITICAL - file '#{options.report_file}' does not exist"
exit 2
end
fileage = (Time.now - File.stat(options.report_file).mtime).to_i
if fileage > options.critical
puts "CRITICAL - #{options.report_file} is #{fileage}s old (threshold: #{options.critical})"
exit 2
elsif fileage > options.warning
puts "WARNING - #{options.report_file} is #{fileage}s old (threshold: #{options.warning})"
exit 1
end
begin
report = YAML.load_file(options.report_file)
rescue => detail
puts "Cannot parse #{options.report_file}"
puts detail.backtrace
puts detail
exit 2
end
status = report.status
ret = []
report.logs.each do |key|
if key.tags[0] == 'err'
ret << key.message
end
if key.tags[0] == 'notice'
if key.message.include? "Would have triggered 'refresh' from"
next
elsif key.message.include? 'Finished catalog run in'
next
end
if key.source == 'Puppet'
next
end
ret << "#{key.source}: #{key.message}"
end
end
# changed, unchanged (noop), failed
if status == 'changed'
puts "OK - #{ret.length} changes were done:"
puts ret
exit 0
elsif status == 'unchanged'
# might mean nothing changed or pending changes
if ret.length > 0
puts "WARNING - #{ret.length} pending changes:"
puts ret
exit 1
else
puts 'OK - 0 changes'
exit 0
end
elsif status == 'failed'
puts "CRITICAL - error on run:"
puts ret
exit 2
end
#puts status
#puts ret
root@controller /var/lib/puppet/state $ ./check_puppet
OK - 7 changes were done:
/Stage[main]/Sys11stack::Profile::Cinder::Volume/Sys11stack::Profile::Cinder::Volume::Cinder_type_create[rbd1]/Cinder::Type[rbd1]/Cinder::Type_set[rbd1]/Exec[cinder type-key rbd1 set volume_backend_name=rbd1]/returns: executed successfully
/Stage[main]/Sys11stack::Profile::Cinder::Volume/Sys11stack::Profile::Cinder::Volume::Cinder_type_create[iscsi1]/Cinder::Type[iscsi1]/Cinder::Type_set[iscsi1]/Exec[cinder type-key iscsi1 set volume_backend_name=iscsi1]/returns: executed successfully
/Stage[main]/Ceilometer::Keystone::Auth/Keystone_user_role[ceilometer@services]/roles: roles changed ['ResellerAdmin', 'admin'] to 'admin ResellerAdmin'
/Stage[main]/Neutron::Server::Notifications/Nova_admin_tenant_id_setter[nova_admin_tenant_id]/ensure: created
/Stage[main]/Contrail::Profile::Provision_linklocal/Exec[provision linklocal]/returns: executed successfully
/Stage[main]/Contrail::Profile::Provision_vrouter/Exec[provision_vrouter compute]/returns: executed successfully
/Stage[main]/Ceilometer::Api/Service[ceilometer-api]: Triggered 'refresh' from 1 events
root@controller /var/lib/puppet/state $ ./check_puppet
WARNING - 6 pending changes:
/Stage[main]/Sys11stack::Profile::Cinder::Volume/Sys11stack::Profile::Cinder::Volume::Cinder_type_create[rbd1]/Cinder::Type[rbd1]/Cinder::Type_set[rbd1]/Exec[cinder type-key rbd1 set volume_backend_name=rbd1]/returns: current_value notrun, should be 0 (noop)
/Stage[main]/Sys11stack::Profile::Cinder::Volume/Sys11stack::Profile::Cinder::Volume::Cinder_type_create[iscsi1]/Cinder::Type[iscsi1]/Cinder::Type_set[iscsi1]/Exec[cinder type-key iscsi1 set volume_backend_name=iscsi1]/returns: current_value notrun, should be 0 (noop)
/Stage[main]/Ceilometer::Keystone::Auth/Keystone_user_role[ceilometer@services]/roles: current_value ["ResellerAdmin", "admin"], should be admin ResellerAdmin (noop)
/Stage[main]/Neutron::Server::Notifications/Nova_admin_tenant_id_setter[nova_admin_tenant_id]/ensure: current_value absent, should be present (noop)
/Stage[main]/Contrail::Profile::Provision_linklocal/Exec[provision linklocal]/returns: current_value notrun, should be 0 (noop)
/Stage[main]/Contrail::Profile::Provision_vrouter/Exec[provision_vrouter compute]/returns: current_value notrun, should be 0 (noop)
root@controller /var/lib/puppet/state $ ./check_puppet
CRITICAL - error on run:
Could not retrieve catalog from remote server: Error 400 on SERVER: Syntax error at 'include' at /opt/openstack-puppet/.../manifests/profile/nova/api.pp:8 on node controller.local
Could not retrieve catalog; skipping run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment