Created
June 20, 2018 08:50
-
-
Save mahemoff/4041a04b7409b44d9ef89153d0449f0c to your computer and use it in GitHub Desktop.
Report monthly Linode cost
This file contains 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
#!/usr/bin/env ruby | |
require 'byebug' | |
require 'linode' | |
require 'awesome_print' | |
# setup API access - key is in file | |
open('/etc/linode.conf').read =~ /api_key: (.+)/ | |
key=$1 | |
lin = Linode.new(api_key: key).linode | |
# get generic plans and prices | |
plans = {} | |
lin.avail.linodeplans.each { |plan| | |
plans[plan.planid] = plan | |
} | |
ap plans | |
# get our plans and prices | |
nodes = {} | |
lin.list.each { |node| | |
group = node[:lpm_displaygroup] | |
nodes[group] ||= [] | |
nodes[group] << node | |
} | |
# report costs | |
puts "Monthly Linode costs by server group:" | |
total_cost = 0 | |
nodes.each_pair { |group, nodes| | |
ap nodes | |
cost = nodes.map { |n| n.planid }.map { |id| id; plans[id].price }.inject(:+).round | |
total_cost += cost | |
puts "#{group} $#{cost}" | |
} | |
puts "\nTOTAL: $#{total_cost}/month" | |
puts "\nThis report was automatically generated" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment