Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. try -- try1
  2.    
  3.     -- set dbug to 1 to enable a bunch of extra logging
  4.     set dBug to 0
  5.     if dBug is 1 then log "1"
  6.    
  7.     set ctr to 0
  8.    
  9.     -- 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.
  10.     repeat 1000 times -- loop1
  11.        
  12.         set ctr to 0
  13.         log (current date)
  14.        
  15.         repeat 5 times -- loop2
  16.            
  17.             set ctr to ctr + 1
  18.            
  19.             if dBug is 1 then log " -----------------------> " & ctr
  20.            
  21.             tell application "System Events" -- tell1
  22.                
  23.                 if dBug is 1 then log "2"
  24.                
  25.                 if ctr = 1 then
  26.                     set P to contents of (do shell script "curl -X GET 'https://api.newrelic.com/v2/key_transactions/*****.xml'    -H 'X-Api-Key:*****' -i ")
  27.                     set appname to "Guaranteed Inventory Service API"
  28.                 end if
  29.                 if ctr = 2 then
  30.                     set P to contents of (do shell script "curl -X GET 'https://api.newrelic.com/v2/key_transactions/*****.xml'    -H 'X-Api-Key:*****' -i ")
  31.                     set appname to "LP Checkers"
  32.                 end if
  33.                 if ctr = 3 then
  34.                     set P to contents of (do shell script "curl -X GET 'https://api.newrelic.com/v2/applications/*****.xml' -H 'X-Api-Key:*****' -i")
  35.                     set appname to "Core WWW"
  36.                 end if
  37.                 if ctr = 4 then
  38.                     set P to contents of (do shell script "curl -X GET 'https://api.newrelic.com/v2/applications/*****.xml' -H 'X-Api-Key:*****' -i")
  39.                     set appname to "Account Services"
  40.                 end if
  41.                 if ctr = 5 then
  42.                     set P to contents of (do shell script "curl -X GET 'https://api.newrelic.com/v2/applications/*****.xml' -H 'X-Api-Key:*****' -i")
  43.                     set appname to "eCommerce Shopping Cart"
  44.                 end if
  45.                
  46.                 set numFound to 0
  47.                
  48.                 if dBug is 1 then log "3 ---------------------------"
  49.                
  50.                 set numSkip to 0
  51.                
  52.                 -- 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)
  53.                
  54.                 repeat with x from 1 to count of every paragraph of P -- repeat3
  55.                    
  56.                     set strItem to paragraph x of P
  57.                     if dBug is 1 then log numSkip & "> " & strItem
  58.                    
  59.                     -- 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)
  60.                     if "<application_summary>" is in strItem and ctr is not 1 then set numSkip to 1
  61.                     if "</application_summary>" is in strItem and ctr is not 1 then set numSkip to 0
  62.                    
  63.                     if numSkip is 0 then -- if1
  64.                        
  65.                        
  66.                         if "<apdex_score>" is in strItem and numSkip = 0 then -- if2
  67.                            
  68.                            
  69.                             set strItemx to offset of ">" in strItem
  70.                             set strItemy to offset of "</apdex_score>" in strItem
  71.                             set strItemx to strItemx + 3
  72.                             set strItemy to strItemy - 1
  73.                            
  74.                             -- 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
  75.                             set strItemz to characters strItemx thru strItemy of strItem
  76.                            
  77.                             set blinkLight to "unknown"
  78.                            
  79.                             -- 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)                        
  80.                             if strItemz starts with "1" then set blinkLight to "red"
  81.                             if strItemz starts with "2" then set blinkLight to "red"
  82.                             if strItemz starts with "3" then set blinkLight to "red"
  83.                             if strItemz starts with "4" then set blinkLight to "red"
  84.                             if strItemz starts with "5" then set blinkLight to "red"
  85.                             if strItemz starts with "6" then set blinkLight to "yellow"
  86.                             if strItemz starts with "7" then set blinkLight to "yellow"
  87.                             if strItemz starts with "8" then set blinkLight to "green"
  88.                             if strItemz starts with "9" then set blinkLight to "green"
  89.                             if strItemz starts with "0" then set blinkLight to "green"
  90.                            
  91.                             -- another hack to get us a presentable apdex score
  92.                             set strPrefix to "0."
  93.                             if strItemz starts with "0" then set strPrefix to "1."
  94.                             log strPrefix & strItemz & "*) ------------- " & appname & "  ------------- (*" & blinkLight & ""
  95.                            
  96.                             if dBug is 1 then log blinkLight
  97.                            
  98.                             try --try2
  99.                                
  100.                                 -- 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.                       
  101.                                 if blinkLight = "red" then do shell script "curl 'http://localhost:8934/blink1/fadeToRGB?rgb=%23FF0000'"
  102.                                 if blinkLight = "yellow" then do shell script "curl 'http://localhost:8934/blink1/fadeToRGB?rgb=%23999900&time=1'"
  103.                                 if blinkLight = "green" then do shell script "curl 'http://localhost:8934/blink1/fadeToRGB?rgb=%23009900&time=1'"
  104.                                 delay 5
  105.                                 do shell script "curl 'http://localhost:8934/blink1/fadeToRGB?rgb=%23000000&time=1'"
  106.                                
  107.                             end try -- try2
  108.                            
  109.                         end if -- if "<apdex_score>" is in strItem and numSkip = 0 then -- if2
  110.                        
  111.                     end if -- if numSkip is 0 then -- if1
  112.                    
  113.                 end repeat -- repeat with x from 1 to count of every paragraph of P -- repeat3
  114.                
  115.             end tell -- tell application "System Events" -- tell1
  116.            
  117.         end repeat -- repeat 5 times -- loop2
  118.        
  119.        
  120.         try -- try3
  121.             -- Flip the blink1 temporarily to blue as a cue that we'd completed the cycle
  122.             do shell script "curl 'http://localhost:8934/blink1/fadeToRGB?rgb=%23000099&time=1'"
  123.             delay 2
  124.         end try -- try3
  125.        
  126.        
  127.         log ""
  128.         log ""
  129.        
  130.        
  131.     end repeat -- repeat 1000 times -- loop1
  132.    
  133. end try -- try1