Skip to content

Instantly share code, notes, and snippets.

@acidprime
Last active September 14, 2016 19:36
Show Gist options
  • Save acidprime/56cf8f297bd5ea876a3147c608cc3d5d to your computer and use it in GitHub Desktop.
Save acidprime/56cf8f297bd5ea876a3147c608cc3d5d to your computer and use it in GitHub Desktop.
Check the current license count against puppetdb
#!/opt/puppetlabs/puppet/bin/ruby
require 'puppet'
require 'puppet/util/puppetdb'
require 'uri'
require 'json'
require 'yaml'
# Have puppet parse its config so we can call its settings
Puppet.initialize_settings
# Connect to the first puppetdb in the HA array
uri = URI(Puppet::Util::Puppetdb.config.server_urls.first)
server = uri.host
port = uri.port
# All active nodes ( basically the default )
query = ["and", ["=", ["node","active"], true]]
headers = {'Accept' => 'application/json'}
connection = Puppet::Network::HttpPool.http_instance(server,port,true)
body = connection.get("/pdb/query/v4/nodes?query=#{query.to_json}",headers).body
begin
response = JSON.load(body)
rescue
response = body
end
license_key = YAML.load_file('/etc/puppetlabs/license.key') rescue {}
if response.is_a?(Array)
output = {
:total_node_count => response.count,
:licensed_node_count => license_key['nodes'],
:unlicensed_node_count => license_key['nodes'].to_i - response.count,
}
puts output.to_json
else
puts response
end
@binford2k
Copy link

I would do

license_key = YAML.load_file('/etc/puppetlabs/license.key') rescue {}

for robustness.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment