Skip to content

Instantly share code, notes, and snippets.

@giuliocalzolari
Forked from naavveenn/Cloudwatch Agent Install
Last active April 29, 2024 03:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save giuliocalzolari/c8a03d312dac060fe6b244674befe0b1 to your computer and use it in GitHub Desktop.
Save giuliocalzolari/c8a03d312dac060fe6b244674befe0b1 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.
---
###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 }}"
{
"agent": {
"metrics_collection_interval": 300,
"logfile": "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log"
},
"logs": {
"force_flush_interval": 15,
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "/var/log/auth.log",
"log_group_name": "{hostname}",
"log_stream_name": "{instance_id}",
"timezone": "Local"
},
{
"file_path": "/var/log/syslog",
"log_group_name": "syslog",
"log_stream_name": "{instance_id}",
"timezone": "Local"
}
]
}
}
},
"metrics": {
"metrics_collected": {
"disk": {
"metrics_collection_interval": 600,
"resources": [
"/"
],
"measurement": [
{"name": "disk_free", "rename": "DISK_FREE", "unit": "Gigabytes"}
]
},
"mem": {
"metrics_collection_interval": 300,
"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": {
"metrics_collection_interval": 600,
"resources": [
"*"
],
"measurement": [
{"name": "net_bytes_recv", "rename": "NET_BYTES_RECV", "unit": "Bytes"},
{"name": "net_bytes_sent", "rename": "NET_BYTES_SENT", "unit": "Bytes"}
]
},
"netstat": {
"metrics_collection_interval": 1800,
"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": {
"metrics_collection_interval": 1800,
"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": {
"AutoScalingGroupName": "${!aws:AutoScalingGroupName}",
"ImageId": "${!aws:ImageId}",
"InstanceId": "${aws:InstanceId}",
"InstanceType": "${!aws:InstanceType}"
},
"aggregation_dimensions" : [
["AutoScalingGroupName"],
["InstanceId", "InstanceType"],
[]
]
}
}
{
"logs": {
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "c:\\ProgramData\\Amazon\\CloudWatchAgent\\Logs\\amazon-cloudwatch-agent.log",
"log_group_name": "amazon-cloudwatch-agent.log",
"log_stream_name": "{local_hostname}"
}
]
},
"windows_events": {
"collect_list": [
{
"event_format": "xml",
"event_levels": [
"WARNING",
"ERROR",
"CRITICAL"
],
"event_name": "System",
"log_group_name": "windows/System",
"log_stream_name": "{local_hostname}"
},
{
"event_format": "xml",
"event_levels": [
"WARNING",
"ERROR",
"CRITICAL"
],
"event_name": "Security",
"log_group_name": "windows/Security",
"log_stream_name": "{local_hostname}"
},
{
"event_format": "xml",
"event_levels": [
"WARNING",
"ERROR",
"CRITICAL"
],
"event_name": "Application",
"log_group_name": "windows/Application",
"log_stream_name": "{local_hostname}"
}
]
}
}
},
"metrics": {
"append_dimensions": {
"AutoScalingGroupName": "${aws:AutoScalingGroupName}",
"ImageId": "${aws:ImageId}",
"InstanceId": "${aws:InstanceId}",
"InstanceType": "${aws:InstanceType}"
},
"metrics_collected": {
"LogicalDisk": {
"measurement": [
"% Free Space"
],
"metrics_collection_interval": 60,
"resources": [
"*"
]
},
"Memory": {
"measurement": [
"% Committed Bytes In Use"
],
"metrics_collection_interval": 60
},
"Paging File": {
"measurement": [
"% Usage"
],
"metrics_collection_interval": 60,
"resources": [
"*"
]
},
"PhysicalDisk": {
"measurement": [
"% Disk Time"
],
"metrics_collection_interval": 60,
"resources": [
"*"
]
},
"Processor": {
"measurement": [
"% User Time",
"% Idle Time",
"% Interrupt Time"
],
"metrics_collection_interval": 60,
"resources": [
"_Total"
]
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment