forked from aliyun/ros-templates
-
Notifications
You must be signed in to change notification settings - Fork 1
/
collect-application-log.yml
371 lines (362 loc) · 11.5 KB
/
collect-application-log.yml
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
ROSTemplateFormatVersion: '2015-09-01'
Description:
en: Application business log monitoring and alarming.
zh-cn: 应用业务日志监控与告警。
Parameters:
CommonName:
Type: String
Default: collect-app-log
SlsProjectName:
Type: String
Label:
en: Name of sls project
zh-cn: 日志项目的名称
Description:
en: >-
The name contains 3 to 36 characters. It must start and end with
a lowercase letter or number. The value can contain lowercase
letters, digits, and hyphens (-).
zh-cn: 长度为3~36个字符。必须以小写英文字母或数字开头和结尾。可包含小写英文字母、数字和短划线(-)。
AssociationProperty: AutoCompleteInput
AssociationPropertyMetadata:
Length: 5
Prefix: biz-log-test-
CharacterClasses:
- Class: lowercase
min: 1
ZoneId:
Type: String
AssociationProperty: 'ALIYUN::ECS::Instance::ZoneId'
AssociationPropertyMetadata:
AutoSelectFirst: true
Label:
en: Availability Zone
zh-cn: 可用区
InstanceType:
Type: String
AssociationProperty: 'ALIYUN::ECS::Instance::InstanceType'
AssociationPropertyMetadata:
SpotStrategy: SpotAsPriceGo
InstanceChargeType: PostPaid
SystemDiskCategory: cloud_essd
ZoneId: ${ZoneId}
Label:
en: Instance Type
zh-cn: 实例规格
Description:
zh-cn: 本方案会创建一个抢占式实例,模拟日志采集。
en: >-
This solution will create a spot instance and simulate log collection.
Default: ecs.c7.large
InstancePassword:
NoEcho: true
Type: String
Description:
en: >-
Server login password, Length 8-30, must contain three(Capital letters,
lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special
symbol in)
zh-cn: >-
服务器登录密码,长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/
中的特殊符号)
Label:
en: Instance Password
zh-cn: 实例密码
ConstraintDescription:
en: >-
Length 8-30, must contain three(Capital letters, lowercase letters,
numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in)
zh-cn: '长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;''<>,.?/ 中的特殊符号)'
AssociationProperty: 'ALIYUN::ECS::Instance::Password'
Default: null
Resources:
Vpc:
Type: 'ALIYUN::ECS::VPC'
Properties:
CidrBlock: 192.168.0.0/16
VpcName:
Fn::Sub: ${CommonName}-vpc
VSwitch:
Type: 'ALIYUN::ECS::VSwitch'
Properties:
VpcId:
Ref: Vpc
CidrBlock: 192.168.0.0/24
ZoneId:
Ref: ZoneId
VSwitchName:
Fn::Sub: ${CommonName}-vsw
SecurityGroup:
Type: 'ALIYUN::ECS::SecurityGroup'
Properties:
VpcId:
Ref: Vpc
SecurityGroupName:
Fn::Sub: ${CommonName}-sg
SecurityGroupIngress:
- PortRange: 22/22
SourceCidrIp: 0.0.0.0/0
IpProtocol: tcp
- PortRange: 443/443
SourceCidrIp: 0.0.0.0/0
IpProtocol: tcp
- PortRange: 80/80
SourceCidrIp: 0.0.0.0/0
IpProtocol: tcp
- PortRange: 3306/3306
SourceCidrIp: 0.0.0.0/0
IpProtocol: tcp
EcsInstance:
Type: 'ALIYUN::ECS::InstanceGroup'
Properties:
VpcId:
Ref: Vpc
ZoneId:
Ref: ZoneId
VSwitchId:
Ref: VSwitch
SecurityGroupId:
Ref: SecurityGroup
ImageId: aliyun_3_9_x64_20G_alibase_
InstanceName:
Fn::Sub: ${CommonName}-ecs
InstanceType:
Ref: InstanceType
SystemDiskCategory: cloud_essd
MaxAmount: 2
InternetMaxBandwidthOut: 100
SpotStrategy: SpotAsPriceGo
Password:
Ref: InstancePassword
GenerateLog:
Type: 'ALIYUN::ECS::RunCommand'
Properties:
InstanceIds:
Fn::GetAtt:
- EcsInstance
- InstanceIds
Type: RunShellScript
Sync: true
Timeout: 3600
CommandContent:
Fn::Sub: |-
#!/bin/bash
export PATH=/usr/local/bin:$PATH
cat << EOF > sls-monitor-test.py
# -*- coding: utf-8 -*-
import sys
import random
import time
import os
from argparse import ArgumentParser
from ipaddress import IPv4Address, IPv4Network
MIN_SLEEP_RANGE = 1000
MAX_SLEEP_RANGE = 5000
IPV4_PUBLIC_NETWORKS = [
IPv4Network('10.0.0.0/8'),
IPv4Network('172.16.0.0/12'),
IPv4Network('192.168.0.0/16'),
IPv4Network('127.0.0.0/8'),
IPv4Network('169.254.0.0/16')
]
def generate_public_ipv4_address():
while True:
ip_address = IPv4Address(random.randint(0, 2**32 - 1))
if not any(ip_address in network for network in IPV4_PUBLIC_NETWORKS):
return str(ip_address)
def parse_args():
parser = ArgumentParser()
parser.add_argument('--output-file', required=True, help='Output file path')
parser.add_argument('--min-sleep', type=int, default=MIN_SLEEP_RANGE,
help='Minimum sleep interval in milliseconds (default: %d)' % MIN_SLEEP_RANGE)
parser.add_argument('--max-sleep', type=int, default=MAX_SLEEP_RANGE,
help='Maximum sleep interval in milliseconds (default: %d)' % MAX_SLEEP_RANGE)
parser.add_argument('--failed-probability', type=float, default=0.1,
help='Probability of generating a "failed" entry (default: %.1f)' % 0.1)
parser.add_argument('--max-entries', type=int, default=sys.maxsize,
help='Maximum number of entries to output (default: no limit)')
return parser.parse_args()
def write_to_file(message, log_level, userid, code, client_ip):
with open(args.output_file, mode='a', encoding='utf-8') as f:
f.write(f'level: {log_level} userid: {userid} action: login code: {code} client_ip: {client_ip}\n')
def main():
global args
args = parse_args()
entry_count = 0
while entry_count < args.max_entries:
# Generate random user ID
userid = random.randint(10000, 10000000)
# Generate random IP address
client_ip = generate_public_ipv4_address()
# Determine whether to log a successful or failed event
if random.random() < args.failed_probability:
code = 'failed'
log_level = 'WARNING'
else:
code = 'success'
log_level = 'INFO'
message = None
write_to_file(message, log_level, userid, code, client_ip)
entry_count += 1
# Sleep for a random interval within the specified range
sleep_time = random.randint(args.min_sleep, args.max_sleep) / 1000.0
time.sleep(sleep_time)
if __name__ == '__main__':
main()
EOF
nohup python3 sls-monitor-test.py --output-file=/tmp/sls-monitor-test.log --max-sleep=2000 --min-sleep=500 --failed-probability=0.1 --max-entries=100000 > output.log 2>&1 &
AutoEnableSLS:
Type: ALIYUN::ROS::AutoEnableService
Properties:
ServiceName: SLS
SlsProject:
DependsOn: AutoEnableSLS
Type: ALIYUN::SLS::Project
Properties:
Name:
Ref: SlsProjectName
SlsLogStore:
Type: ALIYUN::SLS::Logstore
Properties:
LogstoreName: biz-log-test
ProjectName:
Ref: SlsProject
SlsIndex:
Type: ALIYUN::SLS::Index
Properties:
ProjectName:
Ref: SlsProject
FullTextIndex:
Enable: true
KeyIndices:
- Name: content
Type: text
LogstoreName:
Fn::GetAtt:
- SlsLogStore
- LogstoreName
InstallLogAgent:
Type: ALIYUN::OOS::Execution
Properties:
TemplateName: ACS-ECS-BulkyInstallLogAgent
SafetyCheck: Skip
Parameters:
regionId:
Ref: ALIYUN::Region
OOSAssumeRole: ''
targets:
ResourceIds:
Fn::GetAtt:
- EcsInstance
- InstanceIds
RegionId:
Ref: ALIYUN::Region
Type: ResourceIds
rateControl:
Mode: Concurrency
MaxErrors: 100%
Concurrency: 1
action: install
DependsOn:
- SlsIndex
SlsMachineGroup:
Type: ALIYUN::SLS::MachineGroup
Properties:
GroupName: sls-minitor-test
ProjectName:
Ref: SlsProject
MachineIdentifyType: ip
MachineList:
Fn::GetAtt:
- EcsInstance
- PrivateIps
DependsOn:
- InstallLogAgent
SlsLogtailConfig:
Type: ALIYUN::SLS::LogtailConfig
Properties:
ProjectName:
Ref: SlsProject
LogtailConfigName: sls-monitor-test
LogstoreName:
Fn::GetAtt:
- SlsLogStore
- LogstoreName
RawConfigData:
configName: sls-monitor-test
inputDetail:
adjustTimezone: false
delayAlarmBytes: 0
delaySkipBytes: 0
discardNonUtf8: false
discardUnmatch: true
dockerFile: false
enableRawLog: false
fileEncoding: utf8
filePattern: sls-monitor-test.log
filterKey: [ ]
filterRegex: [ ]
key:
- content
localStorage: true
logBeginRegex: .*
logPath: /tmp
logTimezone: ''
logType: common_reg_log
maxDepth: 0
maxSendRate: -1
mergeType: topic
preserve: true
preserveDepth: 0
priority: 0
regex: (.*)
sendRateExpire: 0
sensitive_keys: [ ]
tailExisted: false
timeFormat: ''
topicFormat: none
inputType: file
logSample: ''
outputDetail:
logstoreName:
Fn::GetAtt:
- SlsLogStore
- LogstoreName
outputType: LogService
ApplyConfigToMachineGroup:
Type: ALIYUN::SLS::ApplyConfigToMachineGroup
Properties:
ProjectName:
Ref: SlsProject
ConfigName:
Fn::GetAtt:
- SlsLogtailConfig
- LogtailConfigName
GroupName:
Fn::GetAtt:
- SlsMachineGroup
- GroupName
Outputs:
EcsLoginAddress:
Description:
zh-cn: ECS登录地址。
en: Ecs login address.
Value:
'Fn::Sub': https://ecs-workbench.aliyun.com/?from=EcsConsole&instanceType=ecs®ionId=${ALIYUN::Region}&instanceId=${EcsInstance}
LogQueryAddress:
Description:
zh-cn: 日志查询地址。
en: Log query address.
Value:
'Fn::Sub': https://sls.console.aliyun.com/lognext/project/${SlsProject}/logsearch/${SlsLogStore.LogstoreName}
Metadata:
'ALIYUN::ROS::Interface':
ParameterGroups:
- Parameters:
- InstanceType
- InstancePassword
- SlsProjectName
TemplateTags:
- acs:technical-solution:storage:应用业务日志监控与告警-tech_solu_118
Hidden:
- CommonName