Last active
December 22, 2015 09:38
-
-
Save yacn/6453047 to your computer and use it in GitHub Desktop.
Useful helpers for sensu-cli
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 | |
# needs sensu-cli installed and configured | |
require 'pp' | |
def sensu_cli_list_cmd(sub) | |
"sensu-cli #{sub} list -f single" if %w[event client].include?(sub) | |
end | |
# column labels: | |
# clients: address , name , timestamp (last seen) | |
# events: check , client , flapping? , issued , occurrences , status | |
def clients; get('client'); end | |
def events; get('event'); end | |
def get(thing) | |
out = IO.popen(sensu_cli_list_cmd(thing)).readlines if %[event client].include?(thing) | |
out.pop # removes totals from end | |
out.map! {|thing| thing.split(/\s+/)} | |
build_hashes(out) | |
end | |
def build_hashes(output) | |
num_cols = output[0].size | |
col_titles = output.shift | |
outputs = [] | |
output.each do |entry| | |
h = {} | |
col_titles.each_with_index do |title, index| | |
if title == 'issued' or title == 'timestamp' | |
entry[index] = entry[index].to_i | |
end | |
unless title == 'issued' | |
h[title.to_sym] = entry[index] | |
else | |
h[:timestamp] = entry[index] | |
end | |
end | |
outputs << h | |
end | |
outputs | |
end | |
def time_filter(list, seconds) | |
filtered = list.select {|thing| thing[:timestamp] < seconds} | |
epoch_sort(filtered) | |
end | |
def epoch_sort(list) | |
list.sort {|a,b| a[:timestamp] <=> b[:timestamp]} | |
end | |
def last_seen_in_august | |
time_filter(clients, Time.new(2013, 8, 31, 23, 59, 59).to_i) | |
end | |
def last_seen_september_1 | |
time_filter(clients, Time.new(2013, 9, 1, 23, 59, 59).to_i) | |
end | |
def last_seen_today | |
time_filter(clients, Time.now.to_i) | |
end | |
def keepalives | |
events.select {|evnt| evnt[:check] == 'keepalive'} | |
end | |
def kev_subnet_keepalives | |
keepalives.select {|evnt| evnt[:client].include?('kev')} | |
end | |
def kev_subnet | |
clients.select {|client| client[:name].include?('kev')} | |
end | |
kev_subnet.each do |client| | |
cmd = "sensu-cli client delete #{client[:name]} > /dev/null" | |
#system(cmd) | |
puts cmd | |
end | |
#epoch_sort(clients).each do |client| | |
# node = client[:name] | |
# last_seen = Time.at(client[:timestamp]) | |
# puts "#{last_seen} : #{node}" | |
#end | |
#last_seen_today.each do |client| | |
# node = client[:name] | |
# last_seen = Time.at(client[:timestamp].to_i) | |
# puts "Last saw #{node} on #{last_seen}" | |
#end | |
#last_seen_september_1.each do |client| | |
# node = client[:name] | |
# last_seen = Time.at(client[:timestamp]) | |
# cmd = "sensu-cli client delete #{node} > /dev/null" | |
# puts "Deleting #{node}, last seen on #{last_seen}" | |
# #system(cmd) | |
#end | |
#epoch_sort(clients).each do |client| | |
# ip = client[:address] | |
# node = client[:name] | |
# last_seen = Time.at(client[:timestamp]) | |
# puts "#{node}\t#{ip}\t#{last_seen}" | |
#end | |
#last_seen_in_august.each do |client| | |
# node = client[:name] | |
# last_seen = Time.at(client[:timestamp]) | |
# puts "Last saw #{node} on #{last_seen}" | |
#end | |
#last_seen_in_august.each do |client| | |
# node = client[:name] | |
# last_seen = Time.at(client[:timestamp]) | |
# cmd = "sensu-cli client delete #{node} > /dev/null" | |
# puts "Deleting #{node}, last seen on #{last_seen}" | |
# system(cmd) | |
#end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment