This repository has been archived by the owner on May 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuppet_
executable file
·150 lines (137 loc) · 4.61 KB
/
puppet_
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env ruby
# USAGE
# This plugin is named puppet_ it should be symlinked into /etc/munin/plugins as
# either puppet_node or puppet_total(alternatively it can be puppet_nodes or
# puppet_totals).
# In your /etc/munin/plugin-conf.d/puppet_ set the following:
# [puppet_*]
# user root
#
# There are two enviroment variables that can be passed to it:
# puppet_logs - This should be a string that defines the puppet log file
# (by default it is "/var/log/syslog")
# num_minutes - Only useful when calling the plugin as puppet_node, see below
#
# puppet_node
# Running this plugin as puppet_node will report the total amount of compiled
# catalogs/configs and their average compile time over the last 5 and 30 minutes.
# These values can be altered by using the enviroment variable num_minutes.
#
# num_minutes should be a string of any amount of integers separated by a
# comma(,). Example: "5,30,60" will report the above metrics for the last
# 5, 30 and 60 minutes.
#
# puppet_total
# Running this plugin as puppet_total will report the total number of known nodes
# and the total number of unique nodes that had their catalog compiled in the
# last 24 hours.
#
# COPYRIGHT
# Copyright Pieter Lexis - Kumina B.V. ([email protected])
# This script is licensed under the GNU GPL version 3 or higher
#
# [1] - http://www.puppetlabs.com/puppet/introduction/
#%# family=auto
#%# capabilities=autoconf
require 'time'
def parselogs(since, count_unique=false) # This function parses the puppet_log
# Assuming puppetmaster logs to syslog and it does not logrotate more than
# once every 24 hours.
# TODO write better syslog handling code
# pseudo-code:
# logfiles =["#{logfile}"]
# logfile.{1,{2..X}.gz}.each do |file|
# top_line = [FIRST LINE OF file]
# logfiles.add("#{logfile}")
# if Time.parse(top_line[0..14] > since
# [BREAK FROM each]
# end
logfile = ENV["puppet_logs"] || "/var/log/syslog"
logfiles = ["#{logfile}", "#{logfile}.1"]
# setup variables
# regexp = a regex to be able to extract hostnames and compilation times
regexp = ".* for (.*) in (.*) seconds"
count = 0
if count_unique
hosts = Array.new
else
total = 0
end
# The actual parsing of the logs
logfiles.each do |logfile|
# We only want lines related to configuration compiles
File.open(logfile).grep(/Compiled configuration|Compiled catalog/).each do |line|
# get the puppet_total values
if count_unique and Time.parse(line[0..14]) > since and line =~ /#{regexp}/
unless hosts.include?($1)
hosts << $1
count += 1
end
# get the puppet_node values
elsif not count_unique and Time.parse(line[0..14]) > since and line =~ /#{regexp}/
total += $2.to_f
count += 1
end
end
end
if count_unique
return count
else
return count, total
end
end
def count_total # Count the total number of known nodes
return Dir.entries('/var/lib/puppet/yaml/facts/').size-2
end
# Get the current time, it's used to calculate the since times
t = Time.now
# Do something different based on the name by which this script it invoked
case $0
when /puppet_nodes?$/
num_minutes = ENV["num_minutes"] || "5,30"
num_minutes = num_minutes.split(/,/)
case ARGV[0]
when "config"
puts "graph_title Puppet nodeconfigs"
puts "graph_vlabel clients or number of seconds"
num_minutes.each do |num_minute|
puts "last#{num_minute}m_count.label Config compilations in the last #{num_minute} minutes"
puts "last#{num_minute}m_compile_avg.label Avg compile time for the last #{num_minute} minutes"
end
puts "graph_category puppet"
when "autoconf"
puts "yes"
else # we were called with no or a non-sensical parameter
num_minutes.each do |num_minute|
count, total = parselogs(t - (60 * num_minute.to_i))
puts "last#{num_minute}m_count.value #{count}"
puts "last#{num_minute}m_compile_avg.value #{(total / count).to_s[0..3]}" unless count == 0
end
end
when /puppet_totals?$/
case ARGV[0]
when "config"
puts "graph_title Puppet total nodes"
puts "graph_vlabel clients"
puts "known_clients.label Total number of known clients"
puts "last24h_unique_count.label Unique clients in the last 24 hours"
puts "graph_category puppet"
when "autoconf"
if Process.euid == 0
puts "yes"
else
puts "no"
end
else
t24h = t - (60 * 60 * 24)
puts "known_clients.value #{count_total()}"
puts "last24h_unique_count.value #{parselogs(t24h, true)}"
end
else # scriptname makes no sense
if ARGV[0] == "autoconf"
puts "no"
end
end
# vim: shiftwidth=2:
# vim: smarttab:
# vim: expandtab: