forked from 9652040795/aws-policies
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaws-ec2-asg-cloudwatch-agent
More file actions
176 lines (140 loc) · 5.21 KB
/
Copy pathaws-ec2-asg-cloudwatch-agent
File metadata and controls
176 lines (140 loc) · 5.21 KB
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
#!/bin/bash
# Purpose: AmazonCloudWatch Agent Automated Installation
# OS: AmazonLinux
# Os: Ubuntu/Other linux distro: Note for Ubuntu Install CWAgent from mentioned below official AWS Doc
# https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/install-CloudWatch-Agent-commandline-fleet.html
############################################################
# Note: arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy # Attach this policy to ROLE + YOUR REGION us-east-1
############################################################
yum install -y amazon-cloudwatch-agent
INSTANCE_ID=$(cat /sys/devices/virtual/dmi/id/board_asset_tag)
cat << EOF > /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.toml
[agent]
collection_jitter = "0s"
debug = false
flush_interval = "1s"
flush_jitter = "0s"
hostname = ""
interval = "60s"
logfile = "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log"
logtarget = "lumberjack"
metric_batch_size = 1000
metric_buffer_limit = 10000
omit_hostname = false
precision = ""
quiet = false
round_interval = false
[inputs]
[[inputs.disk]]
fieldpass = ["used_percent"]
tagexclude = ["mode"]
[inputs.disk.tags]
metricPath = "metrics"
[[inputs.logfile]]
destination = "cloudwatchlogs"
file_state_folder = "/opt/aws/amazon-cloudwatch-agent/logs/state"
[[inputs.logfile.file_config]]
file_path = "/var/log/cfn-hup.log"
from_beginning = true
log_group_name = "EC2-CloudWatchLogs"
log_stream_name = "$INSTANCE_ID/cfn-hup.log"
pipe = false
timezone = "UTC"
[[inputs.logfile.file_config]]
file_path = "/var/log/cfn-init.log"
from_beginning = true
log_group_name = "EC2-CloudWatchLogs"
log_stream_name = "$INSTANCE_ID/cfn-init.log"
pipe = false
timezone = "UTC"
[[inputs.logfile.file_config]]
file_path = "/var/log/cfn-init-cmd.log"
from_beginning = true
log_group_name = "EC2-CloudWatchLogs"
log_stream_name = "$INSTANCE_ID/cfn-init-cmd.log"
pipe = false
timezone = "UTC"
[[inputs.logfile.file_config]]
file_path = "/var/log/cloud-init.log"
from_beginning = true
log_group_name = "EC2-CloudWatchLogs"
log_stream_name = "$INSTANCE_ID/cloud-init.log"
pipe = false
timezone = "UTC"
[[inputs.logfile.file_config]]
file_path = "/var/log/cloud-init-output.log"
from_beginning = true
log_group_name = "EC2-CloudWatchLogs"
log_stream_name = "$INSTANCE_ID/cloud-init-output.log"
pipe = false
timezone = "UTC"
[[inputs.logfile.file_config]]
file_path = "/var/log/cfn-wire.log"
from_beginning = true
log_group_name = "EC2-CloudWatchLogs"
log_stream_name = "$INSTANCE_ID/cfn-wire.log"
pipe = false
timezone = "UTC"
[inputs.logfile.tags]
metricPath = "logs"
[[inputs.mem]]
fieldpass = ["used_percent"]
[inputs.mem.tags]
metricPath = "metrics"
[outputs]
[[outputs.cloudwatch]]
force_flush_interval = "60s"
namespace = "CWAgent"
region = "us-east-1"
tagexclude = ["host", "metricPath"]
[outputs.cloudwatch.tagpass]
metricPath = ["metrics"]
[[outputs.cloudwatchlogs]]
force_flush_interval = "5s"
log_stream_name = "$INSTANCE_ID"
region = "us-east-1"
tagexclude = ["metricPath"]
[outputs.cloudwatchlogs.tagpass]
metricPath = ["logs"]
[processors]
[[processors.ec2tagger]]
ec2_metadata_tags = ["InstanceId"]
refresh_interval_seconds = "0s"
[processors.ec2tagger.tagpass]
metricPath = ["metrics"]
EOF
LINE_OLD='/opt/aws/amazon-cloudwatch-agent/bin/start-amazon-cloudwatch-agent'
LINE_NEW='/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent -config /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.toml'
sed -i "s%$LINE_OLD%$LINE_NEW%g" /etc/systemd/system/amazon-cloudwatch-agent.service
systemctl daemon-reload
systemctl start amazon-cloudwatch-agent
systemctl enable amazon-cloudwatch-agent
# End
# In case of Auto-Scaling Group Memory Use the mentioned below & Put the Complete Script in Launch Configuration USERDATA
echo '
{
"agent": {
"metrics_collection_interval": 60
},
"metrics": {
"namespace": "ASG_Memory",
"append_dimensions": {
"AutoScalingGroupName": "${aws:AutoScalingGroupName}",
"InstanceId": "${aws:InstanceId}"
},
"aggregation_dimensions" : [["AutoScalingGroupName"]],
"metrics_collected": {
"mem": {
"measurement": [
{"name": "mem_used_percent", "rename": "MemoryUtilization", "unit": "Percent"}
],
"metrics_collection_interval": 60
}
}
}
}' > /opt/aws/amazon-cloudwatch-agent/bin/config.json
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json -s
systemctl daemon-reload
systemctl start amazon-cloudwatch-agent
systemctl enable amazon-cloudwatch-agent
## End