-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.rb
118 lines (100 loc) · 2.39 KB
/
app.rb
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
#!/usr/bin/env ruby
# vim: set et sts=2 sw=2 ts=2 fdm=marker ft=ruby :
# author: TAKANO Mitsuhiro a.k.a. @takano32
#
require 'sinatra'
require 'pit'
require 'json'
require 'sinatra/cross_origin'
configure do
enable :cross_origin
end
require_relative "./location/#{ENV["EVENTNAME"] || "default"}"
$location = @location
require 'drb/drb'
uri = 'druby://localhost:8282'
DRb.start_service
$zabbix = DRbObject.new_with_uri(uri)
get '/' do
redirect '/v1/version'
end
get '/v1/version' do
version = '1.1.0'
end
get '/v1/associations' do
redirect '/v1/associations/all'
end
get '/v1/associations/:location' do
location = params[:location]
redirect "/v1/associations/#{location}/both"
end
get '/v1/associations/:location/:band' do
location = params[:location]
band = params[:band]
case location
when 'all'
when /#{@ap_name_pattern}/
when *$location.keys.map(&:to_s)
else
halt 404
end
case band
when /2(_|\.)?4[Gg][Hh][Zz]/
b = '2.4GHz'
when /5(_|\.)?0[Gg][Hh][Zz]/
b = '5GHz'
when 'both'
b = 'both'
else
halt 404
end
response.headers['Access-Control-Allow-Origin'] = '*'
content_type :json
# {'associations' => dummy_associations(location, b)}.to_json
{'associations' => associations(location, b)}.to_json
end
get '/v1/traffics' do
redirect '/v1/traffics/all'
end
get '/v1/traffics/:host' do
host = params[:host]
redirect "/v1/traffics/#{host}/all"
end
get '/v1/traffics/:host/:interface' do
host = params[:host]
interface = params[:interface]
redirect "/v1/traffics/#{host}/#{interface}/both"
end
get '/v1/traffics/:host/:interface/:direction' do
response.headers['Access-Control-Allow-Origin'] = '*'
content_type :json
{'traffics' => traffics()}.to_json
end
error 404 do
'404 endpoint not found.'
end
def associations(location, band)
associations = $zabbix.get_associations
result = 0
locations = $location[:all]
unless $location.keys.include? location.to_sym
halt 404 if associations[location.to_s].nil?
locations = [location.to_sym]
else
locations = $location[location.to_sym]
end
locations.each do |ap|
ap = ap.to_s
next unless associations.has_key? ap
if band == 'both' or band == '2.4GHz' then
result += associations[ap]['2_4GHz']
end
if band == 'both' or band == '5GHz' then
result += associations[ap]['5GHz']
end
end
result
end
def traffics()
$zabbix.get_traffics
end