try -- try1
-- set dbug to 1 to enable a bunch of extra logging
set dBug to 0
if dBug is 1 then log "1"
set ctr to 0
-- we'll do the entire process 1000 times and stop. Right now it's taking roughly 30 seconds to check all five servers, so that would cover 8 hours. That'll never happen at work since I'm carrying my laptop to so many meetings.
repeat 1000 times -- loop1
set ctr to 0
log (current date)
repeat 5 times -- loop2
set ctr to ctr + 1
if dBug is 1 then log " -----------------------> " & ctr
tell application "System Events" -- tell1
if dBug is 1 then log "2"
if ctr = 1 then
set P to contents of (do shell script "curl -X GET 'https://api.newrelic.com/v2/key_transactions/*****.xml' -H 'X-Api-Key:*****' -i ")
set appname to "Guaranteed Inventory Service API"
end if
if ctr = 2 then
set P to contents of (do shell script "curl -X GET 'https://api.newrelic.com/v2/key_transactions/*****.xml' -H 'X-Api-Key:*****' -i ")
set appname to "LP Checkers"
end if
if ctr = 3 then
set P to contents of (do shell script "curl -X GET 'https://api.newrelic.com/v2/applications/*****.xml' -H 'X-Api-Key:*****' -i")
set appname to "Core WWW"
end if
if ctr = 4 then
set P to contents of (do shell script "curl -X GET 'https://api.newrelic.com/v2/applications/*****.xml' -H 'X-Api-Key:*****' -i")
set appname to "Account Services"
end if
if ctr = 5 then
set P to contents of (do shell script "curl -X GET 'https://api.newrelic.com/v2/applications/*****.xml' -H 'X-Api-Key:*****' -i")
set appname to "eCommerce Shopping Cart"
end if
set numFound to 0
if dBug is 1 then log "3 ---------------------------"
set numSkip to 0
-- we'll parse the XML file line-by-line (I could have used AppleScript's built-in XML parser, but this was down and dirty)
repeat with x from 1 to count of every paragraph of P -- repeat3
set strItem to paragraph x of P
if dBug is 1 then log numSkip & "> " & strItem
-- for applications, NewRelic reports two different apdex_scores - for our case, we want the browser_summary and not the application_summary (if we had properly parsed the XML file, this wouldn't be a problem)
if "<application_summary>" is in strItem and ctr is not 1 then set numSkip to 1
if "</application_summary>" is in strItem and ctr is not 1 then set numSkip to 0
if numSkip is 0 then -- if1
if "<apdex_score>" is in strItem and numSkip = 0 then -- if2
set strItemx to offset of ">" in strItem
set strItemy to offset of "</apdex_score>" in strItem
set strItemx to strItemx + 3
set strItemy to strItemy - 1
-- for "<apdex_score>0.45</apdex>" we're going to extract "45" - this is a horrible hack but Google failed me at translating a decimal from a string into a number
set strItemz to characters strItemx thru strItemy of strItem
set blinkLight to "unknown"
-- I could clean this up. I left it separate for each one in case I wanted to make multiple variations of red (maybe call a pre-defined pattern with mutiple blinks or something depending on severity)
if strItemz starts with "1" then set blinkLight to "red"
if strItemz starts with "2" then set blinkLight to "red"
if strItemz starts with "3" then set blinkLight to "red"
if strItemz starts with "4" then set blinkLight to "red"
if strItemz starts with "5" then set blinkLight to "red"
if strItemz starts with "6" then set blinkLight to "yellow"
if strItemz starts with "7" then set blinkLight to "yellow"
if strItemz starts with "8" then set blinkLight to "green"
if strItemz starts with "9" then set blinkLight to "green"
if strItemz starts with "0" then set blinkLight to "green"
-- another hack to get us a presentable apdex score
set strPrefix to "0."
if strItemz starts with "0" then set strPrefix to "1."
log strPrefix & strItemz & "*) ------------- " & appname & " ------------- (*" & blinkLight & ""
if dBug is 1 then log blinkLight
try --try2
-- Attempt to light the blink1 and keep it lit for 5 seconds and then gentle fade to black. (gentle fades to light yellow and light green, full red has no fade.
if blinkLight = "red" then do shell script "curl 'http://localhost:8934/blink1/fadeToRGB?rgb=%23FF0000'"
if blinkLight = "yellow" then do shell script "curl 'http://localhost:8934/blink1/fadeToRGB?rgb=%23999900&time=1'"
if blinkLight = "green" then do shell script "curl 'http://localhost:8934/blink1/fadeToRGB?rgb=%23009900&time=1'"
delay 5
do shell script "curl 'http://localhost:8934/blink1/fadeToRGB?rgb=%23000000&time=1'"
end try -- try2
end if -- if "<apdex_score>" is in strItem and numSkip = 0 then -- if2
end if -- if numSkip is 0 then -- if1
end repeat -- repeat with x from 1 to count of every paragraph of P -- repeat3
end tell -- tell application "System Events" -- tell1
end repeat -- repeat 5 times -- loop2
try -- try3
-- Flip the blink1 temporarily to blue as a cue that we'd completed the cycle
do shell script "curl 'http://localhost:8934/blink1/fadeToRGB?rgb=%23000099&time=1'"
delay 2
end try -- try3
log ""
log ""
end repeat -- repeat 1000 times -- loop1
end try -- try1