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

def zbx_connect()
    if 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)


35
36
37
38
39
40
41
42
# File 'lib/awsbix/api.rb', line 35

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



76
77
78
79
80
81
# File 'lib/awsbix/api.rb', line 76

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



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

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_group_exists?(group) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/awsbix/api.rb', line 60

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)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/awsbix/api.rb', line 44

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



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/awsbix/api.rb', line 118

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