#!/usr/bin/env ruby $LOAD_PATH << File.join(File.dirname(__FILE__), *%w[.. lib]) load "scout_realtime.rb" # bootstraps everything, including all other requires require "dante" # ... except dante, which is required here because it's only relevant to running via command line require "fileutils" # ... and fileutils, which we use for setting up the home directory # exit if Ruby version 1.9 on Mac if RUBY_VERSION =~ /^1\.9/ && ServerMetrics::SystemInfo.os.include?("darwin") puts "Scout realtime doesn't work with Ruby 1.9.x on Mac. If you are using RVM, try switching to Ruby 2.0 (preferred), or 1.8.7" exit end # by default, scout_realtime puts its pid and logfile in ~/.scout/ home_dir = File.join( (File.expand_path("~") rescue "/"), ".scout" ) FileUtils.mkdir_p(home_dir) # ensure home directory exists pid_path = File.join(home_dir, "scout_realtime.pid") log_path = File.join(home_dir, "scout_realtime.log") # parse the command cmd = ARGV.first command = nil command = :start if cmd.nil? || cmd =~/^\-/ command = cmd.downcase.to_sym if cmd && %w(start stop help).include?(cmd.downcase) # parse the options opts = Trollop::options do banner <<-EOS Stream realtime server metrics to your browser! To start the daemon: scout_realtime start OR JUST scout_realtime To stop it: scout_realtime stop To view in your browser, do ONE of the following: A) create an SSH tunnel: ssh -NL 5555:localhost:5555 user@ip_or_hostname (run that command on your own computer, not the server) then point your browser to: http://localhost:5555 --- OR --- B) open a port in your firewall: sudo iptables -A INPUT -p tcp --dport 5555 -j ACCEPT (run that command on your server) then point your browser to: http://your-ip-or-hostname:5555 * FYI, the log file is: #{log_path} by default * See http://scoutapp.github.io/scout_realtime for more info Advanced options: EOS opt :port, "point your web browser to this port to view realtime metrics.", :default => 5555, :short => "-p" opt :foreground, "run in the foreground, i.e., don't daemonize the process. Useful for debugging.", :default => false, :short => "-f" opt :log_path, "full path for a log file", :default=>log_path opt :pid_path, "full path for a PID file", :default=>pid_path #opt :quiet, "don't print anything to console on start or stop", :default=>false end # if these were left as defaults, they'll be unchanged from the initial assignment above log_path = opts[:log_path] pid_path = opts[:pid_path] startup_message=< !opts[:foreground] to Dante::Runner instead if opts[:foreground] puts " ** Initializing. cntl-c to stop. Logging to STDOUT **" Scout::Realtime::Main.instance(:port=>opts[:port]).go_sinatra puts startup_message else if command == :start if Scout::Realtime.port_occupied?(opts[:port]) puts "Local port #{opts[:port]} doesn't appear to be available - try another port." exit end Dante::Runner.new('scout_realtime').execute(:daemonize => !opts[:foreground], :pid_path => pid_path, :log_path => log_path, :port=>opts[:port]) do |options| Scout::Realtime::Main.instance(:port=>options[:port]).go_sinatra end puts startup_message puts '* "scout_realtime stop" to stop the daemon' # Fork another process to run briefly & check that the primary daemon stayed running pid = IO.read(pid_path).chomp.to_i fork do sleep 2 is_still_running = begin Process.getpgid( pid ) true rescue Errno::ESRCH false end if !is_still_running puts "\n## oh-oh, the daemon crashed. See log at #{log_path} ##\n" end end elsif command == :stop Dante::Runner.new('scout_realtime').execute(:kill => true, :pid_path => pid_path) elsif command == :help puts "to get help: scout_realtime --help " end end