Module: Awsbix::API

Included in:
Awsbix
Defined in:
lib/awsbix/api.rb

Instance Method Summary collapse

Instance Method Details

#zbx_connectObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/awsbix/api.rb', line 23

def zbx_connect()
    # if an http auth credential pair are set in the config use them
    if self.get_conf('zbx_http_user') and self.get_conf('zbx_http_password') and self.get_conf('zbx_username') and self.get_conf('zbx_password') and self.get_conf('zbx_url') then
        @zbx = ZabbixApi.connect(
            :url            => self.get_conf('zbx_url'),
            :user           => self.get_conf('zbx_username'),
            :password       => self.get_conf('zbx_password'),
            :http_user      => self.get_conf('zbx_http_user'),
            :http_password  => self.get_conf('zbx_http_password')
        )
    # fall back to no http auth
    elsif self.get_conf('zbx_username') and self.get_conf('zbx_password') and self.get_conf('zbx_url') then
        @zbx = ZabbixApi.connect(
            :url            => self.get_conf('zbx_url'),
            :user           => self.get_conf('zbx_username'),
            :password       => self.get_conf('zbx_password')
        )
    else
    	raise ErrorZabbixAuthentication
    end
end

#zbx_connected?Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
# File 'lib/awsbix/api.rb', line 45

def zbx_connected?()
    if @zbx.client.auth.kind_of?(String) then
        # assume we are connected and authenticated
        return true
    else
        return false
    end
end

#zbx_create_group(group) ⇒ Object



86
87
88
89
90
91
# File 'lib/awsbix/api.rb', line 86

def zbx_create_group(group)
    unless self.zbx_group_exists?(group) then
        # create group
        @zbx.hostgroups.create(:name => group)
    end
end

#zbx_create_host(hostname, group, port, ip, dns, useip, templates) ⇒ Object



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
# File 'lib/awsbix/api.rb', line 93

def zbx_create_host(hostname,group,port,ip,dns,useip,templates)
    unless self.zbx_host_exists?(hostname) then
        template_ids = Array.new()
        if templates then
            self.debug_print("debug: adding templates #{templates}")
        elsif templates = self.get_conf('zbx_default_templates') then
            self.debug_print("debug: using default templates (#{templates})")
        else
            self.debug_print("debug: no templates set")
        end

        templates.each do | tpl |
            template_ids.push({'templateid' => @zbx.templates.get_id(:host => tpl)})
        end

        # create host
        self.debug_print("debug: creating #{hostname}")
        @zbx.hosts.create(
            :host => hostname,
            :interfaces => [
                {
                    :type => 1,
                    :main => 1,
                    :ip => ip,
                    :dns => dns,
                    :port => port,
                    :useip => useip
                }
            ],
            :groups => [ :groupid => @zbx.hostgroups.get_id(:name => group) ],
            :templates => template_ids 
        )
    end
end

#zbx_disable_host(hostname) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/awsbix/api.rb', line 145

def zbx_disable_host(hostname)
    # append hostname suffix if one is configured
    if self.get_conf('aws_dns_suffix') then
        self.debug_print("debug: appending #{self.get_conf('aws_dns_suffix')} to #{hostname}")
        hostname = hostname + self.get_conf('aws_dns_suffix')
    end

    @zbx.hosts.update(
        :hostid => @zbx.hosts.get_id(:host => hostname),
        :status => 1
    )
end

#zbx_enable_host(hostname) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/awsbix/api.rb', line 132

def zbx_enable_host(hostname)
    # append hostname suffix if one is configured
    if self.get_conf('aws_dns_suffix') then
        self.debug_print("debug: appending #{self.get_conf('aws_dns_suffix')} to #{hostname}")
        hostname = hostname + self.get_conf('aws_dns_suffix')
    end

    @zbx.hosts.update(
        :hostid => @zbx.hosts.get_id(:host => hostname),
        :status => 0
    )
end

#zbx_get_all_hostsObject



128
129
130
# File 'lib/awsbix/api.rb', line 128

def zbx_get_all_hosts()
    @zbx_hosts = @zbx.hosts.all
end

#zbx_group_exists?(group) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/awsbix/api.rb', line 70

def zbx_group_exists?(group)
    self.debug_print("debug: checking #{group} exists")

    if self.zbx_connected?() then
        if @zbx.hostgroups.get_id(:name => group) then
            self.debug_print("debug: #{group} exists")
            return true
        else
            self.debug_print("debug: #{group} not found")
            return false
        end
    else
        self.debug_print("debug: please connect to the server")
    end
end

#zbx_host_exists?(hostname) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/awsbix/api.rb', line 54

def zbx_host_exists?(hostname)
    self.debug_print("debug: processing #{hostname}")

    if self.zbx_connected?() then
        if @zbx.hosts.get_id(:host => hostname) then
            self.debug_print("debug: #{hostname} exists")
            return true
        else
            self.debug_print("debug: #{hostname} not found")
            return false
        end
    else
        self.debug_print("debug: please connect to the server")
    end
end

#zbx_process_host(options = {}) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/awsbix/api.rb', line 158

def zbx_process_host(options = {})
    unless options[:hostname] and options[:group] and options[:port] and options[:ip] and options[:dns] and options[:useip] then
        puts "error: not enough parameters"
        exit
    end
        
    # append hostname suffix if one is configured
    if self.get_conf('aws_dns_suffix') then
        self.debug_print("debug: appending #{self.get_conf('aws_dns_suffix')} to #{options[:hostname]}")
        hostname = options[:hostname] + self.get_conf('aws_dns_suffix')
        dns = options[:dns] + self.get_conf('aws_dns_suffix')
    else
        hostname = options[:hostname]
        dns = options[:dns]
    end

    self.debug_print("debug: processing #{hostname}")

    # create group
    self.zbx_create_group(options[:group])

    # create host
    self.zbx_create_host(hostname,options[:group],options[:port],options[:ip],dns,options[:useip],options[:templates])
end