Skip to content

Instantly share code, notes, and snippets.

@barmstrong
Created October 21, 2011 01:03
Show Gist options
  • Save barmstrong/1302842 to your computer and use it in GitHub Desktop.
Save barmstrong/1302842 to your computer and use it in GitHub Desktop.
Ruby script to connect to New Relic and turn on our flame lamp!
#!/usr/bin/env ruby
## as seen on http://nerds.airbnb.com/monitoring-your-serverswith-fire
THRESHOLD = 1000 # milliseconds
flame_on = false
def log ms, msg
puts "#{Time.now.to_s} \t #{ms}ms \t #{msg}"
end
while true
begin
response = `curl --silent --header "x-api-key:YOUR_API_KEY" https://rpm.newrelic.com/accounts/7358/applications/2237/threshold_values.xml`
line = response.split("\n").select{|l| l.include?("Response Time")}.first
ms = /metric_value="(\d+)"/.match(line)[1].to_i
if ms > THRESHOLD and !flame_on
log ms, "turning lamp on!"
`python usbnetpower8800.py on`
flame_on = true
elsif ms < THRESHOLD and flame_on
log ms, "turning lamp off"
`python usbnetpower8800.py off`
flame_on = false
end
# p ms
sleep 10
rescue Exception => e
puts "#{e.message} \n#{e.backtrace}"
sleep 60
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment