This repository was archived by the owner on Jul 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp__tp_link_wa901nd
executable file
·159 lines (129 loc) · 3.68 KB
/
http__tp_link_wa901nd
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env ruby
if $0 =~ /^(?:|.*\/)http_([^_]+)_/
host = $1
end
abort "# Error: couldn't understand what I'm supposed to monitor." unless host
user = ENV['user'] || 'admin'
password = ENV['password'] || 'admin'
require 'resolv'
require 'digest/md5'
require 'base64'
require 'cgi'
require 'csv'
require 'net/http'
class DeviceStats
def initialize(host, user, password)
@host = Resolv.getaddress(host)
@auth_cookie = build_auth_cookie(user, password)
end
def wireless_channel
stats = extract_js_array(status_page, 'wlanPara')
return nil if stats.nil?
if stats[2] == "15"
stats[9].to_i
else
stats[2].to_i
end
end
def connected_clients
data = extract_js_array(wireless_stats_page, 'wlanHostPara')
return nil if data.nil?
data[0].to_i
end
def uptime
data = extract_js_array(status_page, 'statusPara')
return nil if data.nil?
data[4].to_f / 86400.0
end
private
def extract_js_array(page, name)
if page =~ %r{#{Regexp.escape(name)}\s+=\s+new\s+Array\((.+?)\)}m
CSV.parse_line($1.gsub(/\n/, ''))
else
nil
end
end
def status_page
@status_page ||= get_status_page.body
end
def wireless_stats_page
@wireless_stats_page ||= get_wireless_stats_page.body
end
def logout
return if @url_token.nil?
get_page(path_with_token('/userRpm/LogoutRpm.htm'))
@url_token = nil
end
def get_status_page
get_page(path_with_token('/userRpm/StatusRpm.htm'))
end
def get_wireless_stats_page
get_page(path_with_token('/userRpm/WlanStationRpm.htm'))
end
def path_with_token(path)
@url_token ||= get_url_token
"/#{@url_token}#{path}"
end
def get_url_token
get_page('/', false)
resp = get_page('/userRpm/LoginRpm.htm?Save=Save', false)
if resp.body =~ %r{http://#{Regexp.escape(@host)}/([A-Za-z0-9]+)/}
at_exit { logout }
return $1
else
return nil
end
end
def get_page(path, include_referrer = true)
Net::HTTP.start(@host, 80) do |http|
req = Net::HTTP::Get.new(path)
req['Cookie'] = @auth_cookie
if include_referrer
req['Referer'] = "http://#{@host}#{path_with_token('/userRpm/Index.htm')}"
end
http.request(req)
end
end
def build_auth_cookie(user, password)
hashed_password = Digest::MD5.hexdigest(password)
value = Base64.encode64("#{user}:#{hashed_password}").strip
'Authorization=Basic%20' + CGI.escape(value)
end
end
if (ARGV[0] == 'config')
puts "host_name #{host}" unless host == 'localhost'
puts "multigraph wireless_channel"
puts "graph_title Wireless Channel"
puts "graph_args --base 1000 -l 0"
puts "graph_vlabel channel"
puts "graph_category network"
puts "channel.label Channel"
puts "channel.type GAUGE"
puts "channel.min 0"
puts "channel.draw LINE1"
puts "multigraph connected_clients"
puts "graph_title Connected Clients"
puts "graph_args --base 1000 -l 0"
puts "graph_vlabel clients"
puts "graph_category network"
puts "clients.label Clients"
puts "clients.type GAUGE"
puts "clients.min 0"
puts "clients.draw LINE1"
puts "multigraph uptime"
puts "graph_title System Uptime"
puts "graph_args --base 1000 -l 0"
puts "graph_vlabel uptime in days"
puts "graph_category system"
puts "uptime.label uptime"
puts "uptime.type GAUGE"
puts "uptime.draw AREA"
exit 0
end
stats = DeviceStats.new(host, user, password)
puts "multigraph wireless_channel"
puts "channel.value #{stats.wireless_channel.nil? ? 'U' : stats.wireless_channel}"
puts "multigraph connected_clients"
puts "clients.value #{stats.connected_clients.nil? ? 'U' : stats.connected_clients}"
puts "multigraph uptime"
puts "uptime.value #{stats.uptime || 'U'}"