Enable dynamic reading of puppet report, independent of operating system and puppet version

This commit is contained in:
Martin Gebhardt 2022-02-27 17:49:45 +01:00
parent c909c07561
commit 452f3f2f4f
No known key found for this signature in database
GPG Key ID: 5472B866EA6CD3DD
1 changed files with 121 additions and 28 deletions

View File

@ -1,44 +1,137 @@
#!/usr/bin/env ruby
@dummyvar = <<-'=cut'
=head2 Description
# This plugin reports the duration of the most recent puppet agent run.
# It requires read access to the puppet logfile (defaults to /var/log/messages).
#
# CONFIGURATION
#
# [puppet*]
# env.puppet_logfile /var/log/message
# env.puppet_logformat "^%b %d"
#
# The logfile is where the puppet agent is expected to log its run time statistics.
# The format is the format of the date syslog writes to the file, which may vary
# according to locale and configuration.
puppet_runtime - Munin plugin that reports the duration of puppet runs.
# reports how long the puppet agent took to apply the catalog
def get_runtime
logfile = ENV['puppet_logfile'] || '/var/log/messages'
t = Time.now
dateformat = ENV['puppet_logformat'] || '^%b %d'
today = t.strftime(dateformat)
File.open(logfile).grep(/#{today}/).grep(/Finished catalog run in/).reverse_each do |line|
if line =~ /in (.*) seconds/
puts "runtime.value #{Regexp.last_match(1)}"
exit 0
end
end
=head2 Compatibility
This Plugin is tested with the following configurations:
Debian: {
version => 11 {
Puppet: {
version => 5.5.22,
version => 7.14.0,
}
}
}
Redhat: {
version => 8 {
Puppet: {
version => 6.4.0,
version => 6.19.1,
version => 7.9.2,
}
}
}
Fedora: {
version => 35 {
Puppet: {
version => 7.12.1,
version => 7.14.0,
}
}
}
=head2 Configuration
The following configuration is mandatory. It must be ensured that
the plugin is executed by the root user.
$ cat /etc/munin/plugin-conf.d/puppet.conf
[puppet_runtime]
user root
Further configurations are not necessary. If it does not work,
then run "puppet config print lastrunreport" and make sure that
the specified file is readable.
$ puppet config print lastrunreport
/opt/puppetlabs/puppet/cache/state/last_run_report.yaml
To check the function, perform the following:
$ munin-run puppet_runtime
An example Output:
$ munin-run puppet_runtime
total.value 6.952583373
fact_generation.value 4.8415459030075
catalog_application.value 1.060418801032938
plugin_sync.value 0.6275155210169032
The duration of the Puppet run is determined from the Puppet report.
This information can be found in the file last_run_report.yaml.
The location of the file is determined via the Puppet client, for example:
$ puppet config print lastrunreport
/opt/puppetlabs/puppet/cache/state/last_run_report.yaml
=head2 Tips & Tricks
Since you want to integrate the runtime of a puppet run into Munin,
you will most likely use puppet as well. Here you can find an
example for your puppet configuration:
file {'/etc/munin/plugin-conf.d/puppet.conf':
source => 'puppet:///modules/munin/puppet.conf',
notify => Service['munin-node'],
}
file {'/etc/munin/plugins/puppet_runtime':
ensure => 'link',
target => '/usr/share/munin/plugins/puppet_runtime',
}
=head3 Known issues
The only known issue that can occur is that puppet cannot be found.
This is because it is not known to the environment used. This issue
can occur when using the packages from the puppetlabs repo.
In this case, a simple link is enough to fix this issue:
file {'/usr/bin/puppet':
ensure => 'link',
target => '/opt/puppetlabs/bin/puppet',
}
=cut
require 'yaml'
report = YAML.load(File.read(`puppet config print lastrunfile`.strip))
time_report = report["time"]
if time_report.is_a?(Hash)
total, fact_generation, catalog_application, plugin_sync = time_report.values_at(
"total", "fact_generation", "catalog_application", "plugin_sync"
)
puts "total.value #{total}" if total
puts "fact_generation.value #{fact_generation}" if fact_generation
puts "catalog_application.value #{catalog_application}" if catalog_application
puts "plugin_sync.value #{plugin_sync}" if plugin_sync
end
case ARGV[0]
when 'config'
puts 'graph_category other'
puts 'graph_category system'
puts 'graph_args --base 1000 -l 0'
puts 'graph_scale no'
puts 'graph_title puppet catalog run time'
puts 'graph_title Puppet agent run time'
puts 'graph_vlabel Seconds'
puts 'runtime.label Catalog application time'
puts 'total.label Puppet agent runtime'
puts 'fact_generation.label Fact generation runtime'
puts 'catalog_application.label Catalog application runtime'
puts 'plugin_sync.label Plugin sync runtime'
exit 0
when 'autoconf'
puts 'yes'
exit 0
else
get_runtime
total
fact_generation
catalog_application
plugin_sync
end