Class: LogStash::Inputs::Multirds
- Inherits:
-
Base
- Object
- Base
- LogStash::Inputs::Multirds
- Includes:
- PluginMixins::AwsConfig::V2
- Defined in:
- lib/logstash/inputs/multirds.rb
Defined Under Namespace
Modules: SinceDB
Instance Method Summary collapse
- #acquire_lock(db, table, id, lock_owner, expire_time: 10) ⇒ Object
- #ensure_lock_table(db, table) ⇒ Object
- #filename2datetime(name) ⇒ Object
- #get_logfile_list(rds, instance_pattern, logfile_pattern) ⇒ Object
- #get_logfile_record(db, id, tablename) ⇒ Object
- #register ⇒ Object
- #run(queue) ⇒ Object
- #set_logfile_record(db, id, tablename, key, value) ⇒ Object
- #stop ⇒ Object
Instance Method Details
#acquire_lock(db, table, id, lock_owner, expire_time: 10) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/logstash/inputs/multirds.rb', line 66 def acquire_lock(db, table, id, lock_owner, expire_time: 10) begin db.update_item({ key: { id: id }, table_name: table, update_expression: "SET lock_owner = :lock_owner, expires = :expires", expression_attribute_values: { ':lock_owner' => lock_owner, ':expires' => Time.now.utc.to_i + expire_time }, return_values: "UPDATED_NEW", condition_expression: "attribute_not_exists(lock_owner) OR lock_owner = :lock_owner OR expires < :expires" }) rescue => e @logger.error "logstash-input-multirds acquire_lock exception\n #{e}" end end |
#ensure_lock_table(db, table) ⇒ Object
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 |
# File 'lib/logstash/inputs/multirds.rb', line 25 def ensure_lock_table(db, table) begin tables = db.list_tables({ }) return true if tables.to_h[:table_names].to_a.include?(table) # TODO: there is a potential race condition here where a table could come back in list_tables but not be in ACTIVE state we should check this better result = db.create_table({ table_name: table, key_schema: [ { attribute_name: 'id', key_type: 'HASH' } ], attribute_definitions: [ { attribute_name: 'id', attribute_type: 'S' } ], provisioned_throughput: { read_capacity_units: 10, write_capacity_units: 10 } }) # wait here for the table to be ready (1..10).each do |i| sleep i rsp = db.describe_table({ table_name: table }) return true if rsp.to_h[:table][:table_status] == 'ACTIVE' end rescue => e @logger.error "logstash-input-multirds ensure_lock_table exception\n #{e}" return false end return false end |
#filename2datetime(name) ⇒ Object
189 190 191 192 |
# File 'lib/logstash/inputs/multirds.rb', line 189 def filename2datetime(name) parts = name.match /(\d{4})-(\d{2})-(\d{2})-(\d{2})$/ Time.utc parts[1], parts[2], parts[3], parts[4] end |
#get_logfile_list(rds, instance_pattern, logfile_pattern) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/logstash/inputs/multirds.rb', line 85 def get_logfile_list(rds, instance_pattern, logfile_pattern) log_files = [] begin dbs = rds.describe_db_instances dbs.to_h[:db_instances].each do |db| next unless db[:db_instance_identifier] =~ /#{instance_pattern}/ logs = rds.describe_db_log_files({ db_instance_identifier: db[:db_instance_identifier] }) logs.to_h[:describe_db_log_files].each do |log| next unless log[:log_file_name] =~ /#{logfile_pattern}/ log[:db_instance_identifier] = db[:db_instance_identifier] log_files.push(log) end end rescue => e @logger.error "logstash-input-multirds get_logfile_list instance_pattern: #{instance_pattern} logfile_pattern:#{logfile_pattern} exception \n#{e}" end log_files end |
#get_logfile_record(db, id, tablename) ⇒ Object
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/logstash/inputs/multirds.rb', line 106 def get_logfile_record(db, id, tablename) res = db.get_item({ key: { id: id }, table_name: tablename }) extra_fields = {'marker' => '0:0'} extra_fields.merge(res.item) end |
#register ⇒ Object
131 132 133 134 135 136 137 138 139 |
# File 'lib/logstash/inputs/multirds.rb', line 131 def register @client_id = "#{Socket.gethostname}:#{java.util::UUID.randomUUID.to_s}" unless @client_id @logger.info "Registering multi-rds input", :instance_name_pattern => @instance_name_pattern, :log_file_name_pattern => @log_file_name_pattern, :group_name => @group_name, :region => @region, :client_id => @client_id @db = Aws::DynamoDB::Client.new @rds = Aws::RDS::Client.new @ready = ensure_lock_table @db, @group_name end |
#run(queue) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/logstash/inputs/multirds.rb', line 141 def run(queue) if !@ready @logger.warn "multi-rds dynamodb lock table not ready, unable to proceed" return false end @thread = Thread.current Stud.interval(@polling_frequency) do logs = get_logfile_list @rds, @instance_name_pattern, @log_file_name_pattern logs.each do |log| id = "#{log[:db_instance_identifier]}:#{log[:log_file_name]}" lock = acquire_lock @db, @group_name, id, @client_id next unless lock # we won't do anything with the data unless we get a lock on the file rec = get_logfile_record @db, id, @group_name next unless rec['marker'].split(':')[1].to_i < log[:size].to_i # No new data in the log file so just continue # start reading log data at the marker more = true marker = rec[:marker] while more do rsp = @rds.download_db_log_file_portion( db_instance_identifier: log[:db_instance_identifier], log_file_name: log[:log_file_name], marker: rec[:marker], ) rsp[:log_file_data].lines.each do |line| @codec.decode(line) do |event| decorate event event.set "rds_instance", log[:db_instance_identifier] event.set "log_file", log[:log_file_name] queue << event end end more = rsp[:additional_data_pending] marker = rsp[:marker] end # set the marker back in the lock table set_logfile_record @db, id, @group_name, 'marker', marker end end end |
#set_logfile_record(db, id, tablename, key, value) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/logstash/inputs/multirds.rb', line 116 def set_logfile_record(db, id, tablename, key, value) db.update_item({ key: { id: id }, table_name: tablename, update_expression: "SET #{key} = :v", expression_attribute_values: { ':v' => value }, return_values: "UPDATED_NEW" }) end |
#stop ⇒ Object
185 186 187 |
# File 'lib/logstash/inputs/multirds.rb', line 185 def stop Stud.stop! @thread end |