-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdaemon
More file actions
executable file
·123 lines (113 loc) · 2.91 KB
/
daemon
File metadata and controls
executable file
·123 lines (113 loc) · 2.91 KB
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
#!/usr/bin/env ruby
# author: @takano32
#
require 'pit'
require 'logger'
require 'thread' # for Mutex
require 'zabbixapi'
require 'json'
class Zabbix
def initialize(user, password, url_prefix, apname_prefix="AP-")
@logger = Logger.new(STDOUT)
@logger.level = Logger::INFO
@mutex = Mutex.new
@api = ZabbixApi.connect(
:url => "#{url_prefix}/api_jsonrpc.php",
:user => user,
:password => password
)
@associations = {}
@traffics = {}
@apname_prefix = apname_prefix
@hosts = {}
@logger.info 'Zabbix::init: get hosts...'
begin
hosts = @api.hosts.get(:output => 'extend')
rescue RuntimeError, SocketError => e
p e
end
@logger.info 'Zabbix::init: get hosts done.'
hosts.each do |host|
@hosts[host['hostid']] = host['name']
end
end
def update
@logger.info 'Zabbix::update: get items...'
begin
items = @api.items.get(:output => 'extend')
rescue RuntimeError, SocketError => e
p e
sleep 15
retry
end
@logger.info 'Zabbix::update: get items done.'
# AP Associations count
items.each do |item|
next unless item['name'] =~ /Ap If No Of Users/
next unless item['key_'] =~ /(#{@apname_prefix}[0-9]{3})/
ap = $1
next unless item['name'] =~ /((2\.4|5) GHz)/
band = $1.sub('.', '_').gsub(" ", "")
@associations[ap] = {} if @associations[ap].nil?
@associations[ap][band] = item['lastvalue'].to_i
end
# Traffic flow
items.each do |item|
next unless host_name = @hosts[item['hostid']]
next unless item['name'] =~ /(Incoming|Outgoing) .*traffic on/
direction = $1
case item['key_']
when /if(In|Out)Octets\[(.*)\]/
target = $2
when /net.if.(in|out)\[(.*)\]/
target = $2
else
next
end
@traffics["#{host_name}-#{target}-#{direction}"] = item['lastvalue'].to_i
end
end
def start
@thread = Thread.start do |t|
while true do
@mutex.synchronize do
@logger.debug 'Updating ...'
update
@logger.debug 'Update done.'
end
sleep 15
end
end
end
def stop
@thread.join
end
def get_associations
@mutex.synchronize do
@logger.info 'Zabbix::get_associations'
return @associations
end
end
def get_traffics
@mutex.synchronize do
@logger.info 'Zabbix::get_traffics'
return @traffics
end
end
end
if __FILE__ == $0 then
config = Pit.get('conbu-api-daemon',
:require => {
user: 'zabbix username',
password: 'zabbix password',
url_prefix: 'prefix of zabbix URL',
apname_prefix: "prefix of AP name ('AP-' + 001)"
})
zabbix = Zabbix.new(config[:user], config[:password], config[:url_prefix], config[:apname_prefix])
zabbix.start
require 'drb/drb'
uri = 'druby://0.0.0.0:8282'
DRb.start_service(uri, zabbix)
zabbix.stop
Drb.thread.join
end