Last active
December 19, 2015 18:39
-
-
Save jvanderhoof/6000146 to your computer and use it in GitHub Desktop.
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
namespace :deploy do | |
def format_column(str, length) | |
(length - str.length).times do | |
str += ' ' | |
end | |
str | |
end | |
def table_display(table_hsh, columns) | |
rows = [columns] | |
max_values = columns.map{|i| i.length } | |
table_hsh.each_pair do |key, row_arr| | |
row_arr.each do |row_hsh| | |
row = [key] | |
columns.each_with_index do |column, index| | |
row << row_hsh[column] if row_hsh.has_key?(column) | |
end | |
row.each_with_index do |col, index| | |
max_values[index] = col.to_s.length if col.to_s.length > max_values[index] | |
end | |
rows << row | |
end | |
end | |
rows = rows.map do |row| | |
formatted_row = [] | |
row.each_with_index do |col, index| | |
formatted_row << format_column(col, max_values[index]) | |
end | |
formatted_row | |
end | |
first = rows.delete_at(0) | |
puts '' | |
rows.sort{|x,y| x[0] <=> y[0]}.insert(0, first).each {|row| puts row.join(' | ')} | |
puts '' | |
end | |
def extract_from_console(command, headers, remove_indexes=[]) | |
report = {} | |
run("#{command}; true") do |channel, stream, data| | |
if stream == :out | |
data.split(/\n/).each do |raw_row| | |
row = raw_row.split(' ') | |
remove_indexes.each {|i| row.delete_at(i)} | |
row[headers.count - 1] = row[headers.count - 1..row.length-1].join(' ') if headers.count < row.count # join the rest of the array in the last column | |
report[channel[:server].to_s] ||= [] | |
report[channel[:server].to_s] << Hash[*headers.zip(row).flatten] | |
end | |
end | |
end | |
report | |
end | |
def extract_from_console_and_display(command, headers, remove_indexes=[]) | |
table_display(extract_from_console(command, headers, remove_indexes), ['Server'] + headers) | |
end | |
namespace :servers do | |
desc "clear logs" | |
task :clear_logs, :roles => :jobs do | |
run "rm #{shared_path}/log/*" | |
end | |
desc "Display free memory" | |
task :memory_usage, :roles => :jobs do | |
headers = "Total Used Free Shared Buffers Cached".split | |
extract_from_console_and_display("free -m | grep Mem", headers, [0]) | |
end | |
desc "Display swap usage" | |
task :swap_usage, :roles => :jobs do | |
headers = "total used free".split | |
extract_from_console_and_display("free -m | grep Swap", headers, [0]) | |
end | |
desc "Display current disk usage" | |
task :disk_space, :roles => :jobs do | |
headers = ["Total Size","Used","Available","% Uses"] | |
extract_from_console_and_display("df -h | grep ^/dev/", headers, [5, 0]) | |
end | |
desc "Display all forever job instances currently running" | |
task :running_forever_jobs, :roles => :jobs do | |
headers = ["User", 'PID', '% CPU', '% Memory', 'VSZ', 'RSS', 'TTY', 'Stat', 'Start', 'Time', 'Command'] | |
extract_from_console_and_display("ps faux | grep 'Forever' | grep -v grep", headers) | |
end | |
desc "Display all delayed_job instances currently running" | |
task :running_delayed_jobs, :roles => :jobs do | |
headers = ["User", 'PID', '% CPU', '% Memory', 'VSZ', 'RSS', 'TTY', 'Stat', 'Start', 'Time', 'Command'] | |
extract_from_console_and_display("ps faux | grep 'delayed_job' | grep -v grep", headers) | |
end | |
task :kill_all_dj_jobs, :roles => :jobs do | |
run("kill -9 `ps -ef | grep delayed_job | grep -v grep | awk '{print $2}'`; true") | |
end | |
task :delayed_jobs_log, :roles => :jobs do | |
trap("INT") { puts 'Interupted'; exit 0; } | |
run "tail -f #{shared_path}/log/delayed_job.log" do |channel, stream, data| | |
if stream == :out | |
data.split(/\n/).each do |row| | |
puts "#{channel[:host]}: #{row}" | |
end | |
end | |
end | |
end | |
task :missing_agent_errors, :roles => :jobs do | |
puts (extract_from_console("tail -n 1000 #{shared_path}/log/red_file_updates.log | grep missing", [''])).to_yaml | |
end | |
task :dj_errors, :roles => :jobs do | |
puts (extract_from_console("tail -n 1000 #{shared_path}/log/cdn_exception.log", [''])).to_yaml | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment