Skip to content

Instantly share code, notes, and snippets.

@mahemoff
Created June 20, 2018 08:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mahemoff/4041a04b7409b44d9ef89153d0449f0c to your computer and use it in GitHub Desktop.
Save mahemoff/4041a04b7409b44d9ef89153d0449f0c to your computer and use it in GitHub Desktop.
Report monthly Linode cost
#!/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