#!/usr/bin/env ruby require "sinatra" require "zipf" $:.unshift File.dirname(__FILE__) require "up" if ENV['APP_ENV'] == :production set :port, 80 set :bind, "0.0.0.0" else set :port, 8080 set :bind, "127.0.0.1" end configure do hosts = {} ReadFile.new("#{File.dirname(__FILE__)}/hosts.txt").readlines.each { |line| hosts[line.split[0]] = Host.new line } puts "> loaded #{hosts.keys.size} hosts" set :hosts, hosts end get '/host/:host' do hostname = params[:host] if settings.hosts.include? hostname check = settings.hosts[hostname].check if not check["up"] "off #{check.to_s}" elsif check.values.all?{|v|v} "ok #{check.to_s}" elsif check.select{|k,_|k.end_with?"/tcp"}.values.all?{|v|v} \ and not check.select{|k,_|k.end_with?"/udp"}.values.all?{|v|v} "maybe-ok #{check.to_s}" else "not-ok #{check.to_s}" end else "unknown" end end get '/host/raw/:host' do hostname = params[:host] if settings.hosts.include? hostname "#{settings.hosts[hostname].check}" else "" end end get '/all-hosts' do "#{settings.hosts.keys.join ","}" end get '/check-all-hosts' do out = "" out end get '/' do out =<

EOS out end