-
Notifications
You must be signed in to change notification settings - Fork 1
/
out_blob.rb
344 lines (235 loc) · 6.54 KB
/
out_blob.rb
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/ruby -w
require 'fluent/output'
require 'fluent/env'
require 'fluent/config/error'
require 'azure/storage/blob'
require 'openssl'
require 'securerandom'
require 'rbconfig'
require 'json'
require 'json/ext'
require 'fluent/plugin/buffer'
require 'time'
module Fluent::Plugin
class Out_Blob< Fluent::Plugin::Output
#to register the custom plugin as a new valid fluentd plugin
Fluent::Plugin.register_output('blob',self)
#config parameters
desc 'resource ID'
config_param :resourceId, :string
desc 'deployment ID'
config_param :DeploymentId, :string
desc 'host'
config_param :Host, :string
desc 'the timestamp to use for records sent to blobs'
config_param :use_source_timestamp, :bool, :default => true
desc 'the field name for the event emit time stamp'
config_param :emit_timestamp_name, :string, :default => "FluentdIngestTimestamp"
desc 'storage account name'
config_param :account_name, :string
desc 'SAS token for storage account'
config_param :sas_token, :string
desc 'name of container'
config_param :container, :string
desc 'identity hash'
config_param :identity_hash, :string
#check if the container already exists
def check_container(blob_client)
begin
if(blob_client.get_container_properties(@container))
return true
end
rescue
return false
end
end
def check_blob(blob_client,blob_name)
begin
if(blob_client.get_blob_properties(@container,blob_name))
return true
end
rescue
return false
end
end
#get blob name for the instant
def get_blob_name
name=""
name+="resourceId=" + @resourceId + "/"
name+="i=" + @identity_hash + "/"
time=Time.now
name+="y=" + time.strftime("%Y").to_s + "/"
name+="m=" + time.strftime("%m").to_s + "/"
name+="d=" + time.strftime("%d").to_s + "/"
name+="h=" + time.strftime("%H").to_s + "/"
name+="m=" + "00" + "/"
name+="PT1H.json"
end
def get_last_blob(blob_client)
nextMarker = nil
last_name=""
last=""
loop do
blobs = blob_client.list_blobs(@container, { marker: nextMarker })
blobs.each do |blob|
last=last_name
last_name= blob.name
end
nextMarker = blobs.continuation_token
break unless nextMarker && !nextMarker.empty?
end
return last
end
#splits a property value pair
def split_msg(msg)
property,value=msg.split("=>")
content=" " + property + " : " + value
return content
end
def get_operation(tag)
system,operationName,category,level = tag.split(".")
return operationName
end
#formats the syslog record received by the output plugin
def format_syslog_msg(msg)
cont=""
i=1
while i<10;
first,rest=msg.split(", ",2)
if i!=2
cont+=split_msg(first) + ",\n"
end
msg=rest
i+=1
end
first,rest=msg.split("}",2)
cont+=split_msg(first) + ",\n"
return cont
end
#formats the file record received by the output plugin
def format_file_msg(msg)
cont=""
first,second=msg.split(", ")
cont+=split_msg(first) + ",\n"
cont+=split_msg(second) + ",\n"
return cont
end
#initializes the plugin
def initialize
super
#for blob
$stdout.sync = true
@created_blob=false
end
def formatted_to_msgpack_binary
return true
end
#to specify the format in which record is received
def format (tag,time,record)
if use_source_timestamp
[tag,time,record].to_msgpack
else
[tag, record].to_msgpack
end
end
def start
super
end
def shutdown
super
end
#formatting the blob content before the record
def pre_text
content=""
if @created_blob == true
content+="{\"records\":[\n"
@created_blob=false
else
content+=",\n"
end
time= Time.now.strftime('%Y-%m-%dT%H:%M:%SZ').to_s
content+="{ \"time\" : \"" + time + "\",\n"
content+=" \"resourceId\" : \"" + @resourceId + "\",\n"
content+=" \"properties\" : {\n"
return content
end
#formatting the blob content after the record
def post_text
content=" \"DeploymentId\" : \"" + @DeploymentId + "\",\n"
content+=" \"Host\" : \"" + @Host + "\"\n"
content+=" },\n"
return content
end
#formatting the tag
def tag_text(tag)
system,operationName,category,level = tag.split(".")
if(operationName=="file")
category="Unknown"
level="Unknown"
end
content=" \"category\" : \"" + category + "\",\n"
content+=" \"level\" : \"" + level + "\",\n"
content+=" \"operationName\" : \""
sysOperation=system+operationName
if sysOperation == "systemsyslog"
content+="LinuxSyslogEvent"
else
content+= "Unknown"
end
content+="\"\n}"
return content
end
#format the blob
def handle_record(record,tag)
content=pre_text
operation=get_operation(tag)
msg=record.to_s
#check if it is file or syslog
if operation == "syslog"
first,rest=msg.split("{",2)
content+=format_syslog_msg(rest)
else
content+=format_file_msg(msg)
end
content+=post_text
content+=tag_text(tag)
return content
end
#for asynchronous buffered output mode
def try_write(chunk)
#blob client to access the blobs in the storage account using SAS token
@blob_client = Azure::Storage::Blob::BlobService.create(storage_account_name:@account_name, storage_sas_token: @sas_token)
if(!check_container(@blob_client))
container = @blob_client.create_container(@container,options={})
@blob_client.set_container_acl(@container, "container")
end
blob_name=get_blob_name
#create an append blob if one does not already exist
if (!check_blob(@blob_client,blob_name))
@blob_client.create_append_blob(@container,blob_name)
@created_blob=true
@last_blob=get_last_blob(@blob_client)
if (@last_blob!="")
@blob_client.append_blob_block(@container, @last_blob, "\n]}")
end
end
chunk_id = chunk.unique_id
content=""
if use_source_timestamp
chunk.msgpack_each { |(tag, time, record)|
record[emit_timestamp_name] = Time.now.strftime('%Y-%m-%dT%H:%M:%SZ')
content+=handle_record(record,tag)
}
else
chunk.msgpack_each {|(tag, record)|
record[emit_timestamp_name] = Time.now.strftime('%Y-%m-%dT%H:%M:%SZ')
content+=handle_record(record,tag)
}
end
@log.flush
commit_write(chunk_id)
#append to an existing blob
@blob_client.append_blob_block(@container, blob_name, content)
end#method try_write
end # class Out_Blob
end #module Fluent::Plugin