Skip to content

Instantly share code, notes, and snippets.

@peteoleary
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save peteoleary/67b32bb1f3c020f93287 to your computer and use it in GitHub Desktop.

Select an option

Save peteoleary/67b32bb1f3c020f93287 to your computer and use it in GitHub Desktop.
A simple Ruby command line tool which takes a Heroku log tail as STDIN and tries to detect an increase in swap space on a particular dyno
#!/usr/bin/env ruby
require 'time'
LINE_REGEXP = /(\d{4})-(\d{2})-(\d{2})T(\d{2})\:(\d{2})\:(\d{2})\.(\d{6})\+(\d{2})\:(\d{2})\s(.+?)\[(.+?)\.(.+?)\]\:(.+)/
MAX_MEMORY = ARGV[0].to_f
APP = ARGV[1].to_s
printf "Max Memory is #{MAX_MEMORY}\n"
printf "App is #{APP}\n"
while true do
f = open("|heroku logs --app #{APP}")
lines = f.read()
f.close
lines.each_line do |e|
# TODO: use named groups here so that references to matches aren't as fragile
matches = LINE_REGEXP.match(e)
if matches
timestamp = "#{matches[1]}-#{matches[2]}-#{matches[3]} #{matches[4]}:#{matches[5]}:#{matches[6]}"
if (matches[13].include?("R14"))
command = "heroku ps:restart #{matches[11]}.#{matches[12]} --app #{APP}"
system("#{command}")
end
end
end
sleep(5)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment