Last active
April 26, 2021 01:15
-
-
Save naavveenn/a9e0f102501c2fbe24f7cc49d5b94910 to your computer and use it in GitHub Desktop.
Cloudwatch agent installation: Make sure to attach a cloudwatch role to your ec2 instance. amazon-cloudwatch-agent.json file should be created before hand (on your local machine or from where you are executing your ansible playbook), other wise cw_agent will not start. Below is the example of amazon-cloudwatch-agent.json.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"agent": { | |
"metrics_collection_interval": 60, | |
"logfile": "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log" | |
}, | |
"metrics": { | |
"metrics_collected": { | |
"cpu": { | |
"resources": [ | |
"*" | |
], | |
"measurement": [ | |
{"name": "cpu_usage_idle", "rename": "CPU_USAGE_IDLE", "unit": "Percent"}, | |
{"name": "cpu_usage_iowait", "rename": "CPU_USAGE_IOWAIT", "unit": "Percent"}, | |
{"name": "cpu_time_idle", "rename": "CPU_TIME_IDLE", "unit": "Percent"}, | |
{"name": "cpu_time_iowait", "rename": "CPU_TIME_IOWAIT", "unit": "Percent"} | |
] | |
}, | |
"disk": { | |
"resources": [ | |
"/" | |
], | |
"measurement": [ | |
{"name": "disk_free", "rename": "DISK_FREE", "unit": "Gigabytes"}, | |
{"name": "disk_inodes_free", "rename": "DISK_INODES_FREE", "unit": "Count"}, | |
{"name": "disk_inodes_total", "rename": "DISK_INODES_TOTAL", "unit": "Count"}, | |
{"name": "disk_inodes_used", "rename": "DISK_INODES_USED", "unit": "Count"} | |
] | |
}, | |
"diskio": { | |
"resources": [ | |
"*" | |
], | |
"measurement": [ | |
{"name": "diskio_iops_in_progress", "rename": "DISKIO_IOPS_IN_PROGRESS", "unit": "Megabytes"}, | |
{"name": "diskio_read_time", "rename": "DISKIO_READ_TIME", "unit": "Megabytes"}, | |
{"name": "diskio_write_time", "rename": "DISKIO_WRITE_TIME", "unit": "Megabytes"} | |
] | |
}, | |
"mem": { | |
"measurement": [ | |
{"name": "mem_free", "rename": "MEM_FREE", "unit": "Megabytes"}, | |
{"name": "mem_total", "rename": "MEM_TOTAL", "unit": "Megabytes"}, | |
{"name": "mem_used", "rename": "MEM_USED", "unit": "Megabytes"} | |
] | |
}, | |
"net": { | |
"resources": [ | |
"eth0" | |
], | |
"measurement": [ | |
{"name": "net_bytes_recv", "rename": "NET_BYTES_RECV", "unit": "Bytes"}, | |
{"name": "net_bytes_sent", "rename": "NET_BYTES_SENT", "unit": "Bytes"} | |
] | |
}, | |
"netstat": { | |
"measurement": [ | |
{"name": "netstat_tcp_listen", "rename": "NETSTAT_TCP_LISTEN", "unit": "Count"}, | |
{"name": "netstat_tcp_syn_sent", "rename": "NETSTAT_TCP_SYN_SENT", "unit": "Count"}, | |
{"name": "netstat_tcp_established", "rename": "NETSTAT_TCP_ESTABLISHED", "unit": "Count"} | |
] | |
}, | |
"processes": { | |
"measurement": [ | |
{"name": "processes_blocked", "rename": "PROCESSES_BLOCKED", "unit": "Count"}, | |
{"name": "processes_running", "rename": "PROCESSES_RUNNING", "unit": "Count"}, | |
{"name": "processes_zombies", "rename": "PROCESSES_ZOMBIES", "unit": "Count"} | |
] | |
} | |
}, | |
"append_dimensions": { | |
"InstanceId": "${aws:InstanceId}", | |
"InstanceType": "${aws:InstanceType}" | |
}, | |
"aggregation_dimensions" : [["InstanceId", "InstanceType"]] | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
###Cloudwatch role should be attached to the ec2 instance### | |
- hosts: dd ###servers on which you need to run the cw_agent | |
become: yes | |
remote_user: root | |
gather_facts: true | |
tasks: | |
- name: Check if Cloudwatch Agent is Installed Already | |
shell: /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status | |
register: init_status_result | |
ignore_errors: yes | |
- debug: | |
var: init_status_result | |
- name: Create Directory for Downloading Cloudwatch Agent .zip | |
file: | |
path: /opt/aws/amazon-cloudwatch-zip | |
state: directory | |
owner: root | |
group: root | |
mode: 0755 | |
recurse: no | |
when: init_status_result.failed == true | |
- name: Download Latest Version of Amazon Cloudwatch Agent | |
get_url: | |
url: "https://s3.amazonaws.com/amazoncloudwatch-agent/linux/amd64/latest/AmazonCloudWatchAgent.zip" | |
dest: /opt/aws/amazon-cloudwatch-zip/AmazonCloudWatchAgent.zip | |
mode: 0755 | |
when: init_status_result.failed == true | |
- name: Unzip Cloudwatch Download File | |
unarchive: | |
remote_src: yes | |
src: /opt/aws/amazon-cloudwatch-zip/AmazonCloudWatchAgent.zip | |
dest: /opt/aws/amazon-cloudwatch-zip | |
when: init_status_result.failed == true | |
- name: Execute the Installation Script | |
shell: /opt/aws/amazon-cloudwatch-zip/install.sh | |
args: | |
chdir: /opt/aws/amazon-cloudwatch-zip | |
ignore_errors: yes | |
when: init_status_result.failed == true | |
- name: Transfer Cloudwatch Configuration File | |
copy: | |
src: /etc/ansible/roles/amazon-cloudwatch-agent.json | |
dest: /opt/aws/amazon-cloudwatch-agent/etc | |
owner: root | |
group: root | |
mode: 0755 | |
when: init_status_result.failed == true | |
- name: Adding CW agent start command in cron | |
cron: | |
name: "Start the agent at reboot" | |
special_time: reboot | |
job: "/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json -s >> /var/log/cloudwatch.log" | |
when: init_status_result.failed == true | |
- name: Start Amazon Cloudwatch Agent | |
shell: /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json -s | |
- name: Validate if the agent is running or not | |
shell: /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status | |
register: out | |
- debug: | |
msg: "{{ out }}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment