Module: Awsbix::AmazonWebServices

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

Instance Method Summary collapse

Instance Method Details

#aws_get_hostsObject

retrieve non-excluded and running EC2 hosts



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

def aws_get_hosts()
    @ec2_hosts = Array.new

    # get sg to ignore or process based on zbx_filter_model
    case self.get_conf('zbx_filter_model')
        when 'exclude'
            excluded_security_groups = self.get_conf('zbx_exclude_security_group')
        when 'include'
            included_security_groups = self.get_conf('zbx_include_security_group')
    end

    self.ec2_connect()
    # loop through all hosts, across all regions in config
    self.aws_get_regions().each do |region|
        self.debug_print("info: processing #{region}")
        AWS.memoize do
            @ec2.regions[region].instances.each do | inst |
                if inst.status.match(/running/) then
                    inst.security_groups.each do | sg |
                        case self.get_conf('zbx_filter_model')
                            when 'exclude'
                                # do not process if sg is excluded
                                unless excluded_security_groups.include?(sg.name) then
                                    # do not push if already present
                                    unless @ec2_hosts.include?(inst) then
                                        @ec2_hosts.push(inst)
                                    end
                                end
                            when 'include'
                                # process if sg is included
                                if included_security_groups.include?(sg.name) then
                                    # do not push if already present
                                    unless @ec2_hosts.include?(inst) then
                                        @ec2_hosts.push(inst)
                                    end
                                end
                        end
                    end
                end
            end
        end
    end
    return @ec2_hosts
end

#aws_get_regionsObject



23
24
25
26
# File 'lib/awsbix/aws.rb', line 23

def aws_get_regions()
    # return an array of all regions in config
    self.get_conf('aws_regions')
end

#ec2_connectObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/awsbix/aws.rb', line 28

def ec2_connect()
    # credentials -> IAM role -> Raise exception
    if self.get_conf('aws_access_key') and self.get_conf('aws_secret_key') then
        AWS.config(
            :access_key_id => self.get_conf('aws_access_key'),
            :secret_access_key => self.get_conf('aws_secret_key')
        )
        @ec2 = AWS::EC2.new()
    else
        # try IAM role
        begin
            @ec2 = AWS::EC2.new()
        rescue
            raise ErrorAWSAuthentication
        end
    end
end