Skip to content

Instantly share code, notes, and snippets.

@buty4649
Last active December 21, 2022 03:49
Show Gist options
  • Save buty4649/40cbeaffef6e49ad1ef035728538e7db to your computer and use it in GitHub Desktop.
Save buty4649/40cbeaffef6e49ad1ef035728538e7db to your computer and use it in GitHub Desktop.
Mackerel APIを使ってサーバインベントリ収集を行うスクリプト
#!/usr/bin/env ruby
# MIT License
#
# Copyright (c) 2022 buty4649
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'mackerel-client'
end
require 'mackerel/client'
require 'time'
mackerel_api_key = ENV.fetch('MACKEREL_APIKEY')
client = Mackerel::Client.new(mackerel_api_key:)
puts [
'Server Name', 'vCPUs', 'RAM(GB)',
'CPU Utilization Peak (%)', 'RAM Utilization Peak (%)',
'Operating System', 'Provisioned Storage (GB)',
].join(",")
client.get_hosts.each do |host|
h = host.to_h
cpu_count = h.dig('meta', 'cpu')&.count || 0
memory_total_kb = h.dig('meta', 'memory', 'total')
next if cpu_count.zero? || memory_total_kb.nil?
name = host.name
memory_total = memory_total_kb.delete_suffix('KB').to_i * 1024.0
memory_total_gb = (memory_total_kb.delete_suffix('KB').to_i / 1024.0 / 1024.0).round
id = host.id
storage_total = h.dig('meta', 'block_device').sum do |(_, meta)|
# vendorがあればちゃんとディスクっぽい
next unless meta.key?('vendor') || meta.key?('size')
meta['size'].to_i
end
storage_total_gb = (storage_total / 1024.0 / 1024.0).round
prev_month = Time.now.to_date.prev_month.strftime("%Y-%m-01 00:00:00")
fetch_from = Time.parse(prev_month).to_i.to_s
current_month = Time.now.to_date.strftime("%Y-%m-01 00:00:00")
fetch_to = Time.parse(current_month).to_i.to_s
cpu_idle_metrics = client.get_host_metrics(id, 'cpu.idle.percentage', fetch_from, fetch_to)
next if cpu_idle_metrics.empty?
ram_used_metrics = client.get_host_metrics(id, 'memory.used', fetch_from, fetch_to)
next if cpu_idle_metrics.empty?
cpu_util_peek = (cpu_count * 100 - cpu_idle_metrics.min_by{|m| m['value']}['value']).round(2)
memory_util_peek = (ram_used_metrics.max_by{|m| m['value']}['value'] / memory_total * 100.0).round(2 )
platform_name = h.dig('meta', 'kernel', 'platform_name')
platform_version = h.dig('meta', 'kernel', 'platform_version')
os = "#{platform_name} #{platform_version}"
result = [name, cpu_count, memory_total_gb, cpu_util_peek, memory_util_peek, os, storage_total_gb]
puts result.join(",")
rescue
# 404 Not Found対策
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment