Skip to content

Instantly share code, notes, and snippets.

@tiago-cruz-movile
Created February 19, 2016 18:56
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tiago-cruz-movile/00d1eeeb4352b04132bf to your computer and use it in GitHub Desktop.
How Movile Works: Plugin movile_uptime.rb
#
# Author:: Tiago Cruz (<tiago.cruz@movile.com>)
# Copyright:: Copyright (c) 2015 Tiago Cruz
# License:: GNU General Public License, version 2.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##
# This plugin makes search and sort the output by 'uptime'
#
# Usage examples:
# knife movile uptime chef_environment:production
# knife movile uptime platform:xenserver -l 15
# knife movile uptime dc:miami
##
require 'chef/knife'
module Movile
class MovileUptime < Chef::Knife
deps do
require 'chef/search/query'
end
banner "knife movile uptime QUERY (options)"
option :limit,
:short => "-l INT",
:long => "--limit INT",
:description => "The number of rows to return",
:default => 30,
:proc => lambda { |i| i.to_i }
option :debug,
:short => "-d",
:long => "--debug",
:description => "Enables debug",
:default => false,
:proc => lambda { |val| true }
# start
def run
all_nodes = []
count = 0
limit = config[:limit] # 30 default
debug = config[:debug]
q = Chef::Search::Query.new
query = @name_args[0] || "chef_environment:production" # search default
ui.warn "Searching for '#{query}' with #{limit} results ..."
q.search(:node, query) do |node|
all_nodes << node
end
# header
header = "%-2s %-20s %-20s %-13s %-15s %-15s" % ["ID", "Name", "IP Address", "Uptime", "Virtualization", "Platform OS"]
puts "#{ui.color(header, :cyan)}"
#
## BOF ORDER by uptime
#
all_nodes.sort! do |n1, n2|
# get first field if exists
if n1.has_key?("uptime")
uptime_split_1 = n1.uptime.split(" ")
else
uptime_split_1 = [0]
end
if n2.has_key?("uptime")
uptime_split_2 = n2.uptime.split(" ")
else
uptime_split_2 = [0]
end
uptime_n1_0 = uptime_split_1.first
uptime_n2_0 = uptime_split_2.first
uptime_n1_1 = uptime_split_1[1]
uptime_n2_1 = uptime_split_2[1]
# order by days, ignore hours and minutes
if uptime_n1_1 == "days" or uptime_n1_1 == "day"
updays_n1 = uptime_n1_0
else
updays_n1 = 0
end
if uptime_n2_1 == "days" or uptime_n2_1 == "day"
updays_n2 = uptime_n2_0
else
updays_n2 = 0
end
# if both nodes have the uptime property
if n2.has_key?("uptime") && n1.has_key?("uptime")
# then we do the comparison and sort between using integer
(updays_n2.to_i or 0) <=> (updays_n1.to_i or 0)
elsif n1.has_key?("uptime") # if only the n1 node has de property
# n1 should come before than n2
-1
else # only node 2 has the property
# n2 should come before n1
1
end
end
#
## EOF ORDER by uptime
#
all_nodes.each do |node|
count += 1
if node.has_key?("ec2")
ip = node['ec2']['public_ipv4']
else
ip = node['ipaddress']
end
hostname = node['hostname']
# x days or hours to display
if node.has_key?("uptime")
uptime_split = node['uptime'].split(" ")
updays = uptime_split[0] + " " + uptime_split[1]
else
updays = 0
end
# virtual of physical
if node['virtualization'] && node['virtualization']['role']
system = node['virtualization']['system']
role = node['virtualization']['role']
virt = "#{system}:#{role}"
else
virt = "physical"
end
platform = node['platform'] + " " + node['platform_version']
# limit output
if limit >= count
output = "%02d %-20s %-20s %-13s %-15s %-15s" % [ "#{count}","#{hostname}","#{ip}","#{updays}","#{virt}","#{platform}" ]
ui.msg(output)
else
break
end
# Debug
puts node if debug
end
ui.msg "Total #{all_nodes.size} nodes found"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment