 { argv } = require 'optimist'
 
 koding = require './bongo'
+
+nsca = require 'koding-nsca-wrapper'
 
 parseServiceKey = require 'koding-service-key-parser'
 

      console.error 'Negative service count!'

koding.connect ->
  koding.monitorPresence
    join: (serviceKey) ->
      incService serviceKey, 1

    return hostname
  ).filter Boolean

  res.set "Content-Type", "text/json"

  if query.all?
    res.send services.map (hostname) -> "#{ protocol }//#{ hostname }"

  else unless services.length
    # FAILURE! send an alert to opsview.
    nsca.sendStatus 'Services presence', 2, 'Service loadbalancing failure detected!'
    # Fail-over to the value hard-coded into the config.
    res.send "\"#{ KONFIG.client.runtimeOptions.broker.sockJS }\""

  else
    i = genericServices.seq++ % services.length
    res.send "\"#{ protocol }//#{ services[i] }\""
