Last active
September 14, 2016 19:36
-
-
Save acidprime/56cf8f297bd5ea876a3147c608cc3d5d to your computer and use it in GitHub Desktop.
Check the current license count against puppetdb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would do
for robustness.