From 26c6a0d934e36ffb26f770e740e9294d172a7d84 Mon Sep 17 00:00:00 2001 From: pks Date: Sun, 21 Mar 2021 11:31:02 +0100 Subject: init --- README | 2 + hosts.txt.example | 1 + up-www.rb | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ up.rb | 65 ++++++++++++++++++++++++++++ 4 files changed, 192 insertions(+) create mode 100644 README create mode 100644 hosts.txt.example create mode 100644 up-www.rb create mode 100644 up.rb diff --git a/README b/README new file mode 100644 index 0000000..6169803 --- /dev/null +++ b/README @@ -0,0 +1,2 @@ +# TODO +- check specific websites diff --git a/hosts.txt.example b/hosts.txt.example new file mode 100644 index 0000000..2574169 --- /dev/null +++ b/hosts.txt.example @@ -0,0 +1 @@ +google.de http:80/tcp diff --git a/up-www.rb b/up-www.rb new file mode 100644 index 0000000..049f897 --- /dev/null +++ b/up-www.rb @@ -0,0 +1,124 @@ +#!/usr/bin/env ruby + +require "sinatra" +require "zipf" +require "./up" + +configure do + hosts = {} + ReadFile.new("./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 diff --git a/up.rb b/up.rb new file mode 100644 index 0000000..d32804e --- /dev/null +++ b/up.rb @@ -0,0 +1,65 @@ +#!/usr/bin/env ruby + +require "net/ping" +require "socket" + +class Host + def initialize s + # example: 'nash ssh:2001 mqtt:1883' + parts = s.split + @hostname = parts.first + @services_and_ports = {} + parts.slice(1, parts.size).each { |part| + service, port_and_protocol = part.split ":" + port, protocol = port_and_protocol.split "/" + @services_and_ports["#{service}/#{protocol}"] = [port.to_i, protocol] + } + end + + def hostname + return @hostname + end + + def up? hostname + ping = Net::Ping::External.new @hostname + ping.ping? + end + + def service_up? service, port, protocol + if protocol == "tcp" + begin + sock = TCPSocket.new @hostname, port + sock.close + return true + rescue + return false + end + elsif protocol == "udp" + system "nc -u -z -w 1 #{@hostname} #{port+1} &>/dev/null" + return $?.success? + else + STDERR.write "unknown protocol '#{protocol}'\n" + end + end + + def check + ret = {} + ret["up"] = up? @hostname + return ret if not ret["up"] + @services_and_ports.each_key { |k| + ret[k] = service_up? k, @services_and_ports[k][0], @services_and_ports[k][1] + } + return ret + end +end + +def main + while line = STDIN.gets + h = Host.new line + puts h.check.to_s + end +end + +if __FILE__ == $0 + main +end -- cgit v1.2.3