summaryrefslogtreecommitdiff
path: root/up-www.rb
blob: 049f897d9b2cd7ac24920c771418ceb09a56f388 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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 = "<html><head></head><body><ul>"
  settings.hosts.each_value { |host|
    out += "  <li>#{host.hostname}: #{host.check}</li>"
  }
  out += "</ul></body></html>"
  out
end

get '/' do
  out =<<EOS
  <head>
  <style type="text/css">
    .box { border:2px dashed black;
      float:left;
      padding:1em;
      margin:.5em;
      background:#eee;
      cursor:pointer; }
  </style>
  <script type="text/javascript">
function update_host(host)
{
  request = new XMLHttpRequest();
  request.__host = host
  request.onload = function (e) {
    status = this.responseText.split(" ")[0];
    response = this.responseText;
    if (status == "ok") {
      document.getElementById(this.__host).style.background = "green";
      document.getElementById(this.__host).style.border = "2px solid green";
      document.getElementById(this.__host).style.color = "white";
    } else if (status == "off") {
      document.getElementById(this.__host).style.background = "black";
      document.getElementById(this.__host).style.border = "2px solid white";
      document.getElementById(this.__host).style.color = "white";
    } else if (status == "maybe-ok") {
      document.getElementById(this.__host).style.background = "yellow";
      document.getElementById(this.__host).style.border = "2px dashed white";
    } else {
      document.getElementById(this.__host).style.background = "red";
      document.getElementById(this.__host).style.border = "2px dashed white";
    }
    document.getElementById(this.__host).__full_status = response;
    document.getElementById(this.__host).onclick = function () {
      document.getElementById("info").innerHTML = this.__full_status;
    }
  }
  request.open("GET", "/host/" + host);
  request.send();
}

request = new XMLHttpRequest();
request.onload = function (e) {
  hosts = this.responseText.split(",");
  for (hostname of hosts) {
    new_node = document.createElement("div");
    new_node.id = hostname;
    new_node.className = "box";
    new_text_node = document.createTextNode(hostname);
    new_node.appendChild(new_text_node);
    document.getElementById("boxes").appendChild(new_node);
    update_host(hostname);
  }
}
request.open("GET", "/all-hosts", true);
request.send();
  </script>
  </head>
  <body>
    <div id="boxes"></div>
    <p style="clear:left" id="info"></p>
  </body>
</html>
EOS
  out
end