Skip to content

Instantly share code, notes, and snippets.

@weirdbricks
Created June 15, 2014 22:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save weirdbricks/3e0d0e3428f3d683ccfa to your computer and use it in GitHub Desktop.
Save weirdbricks/3e0d0e3428f3d683ccfa to your computer and use it in GitHub Desktop.
ansible playbook to add memory metrics for cloudwatch - RedHat family only
- hosts: all
vars:
ec2_access_key: "AKIA-----------A"
ec2_secret_key: "i-------------/--------A"
remote_user: ec2-user
sudo: yes
tasks:
- name: "make sure package perl-switch is installed for the monitoring scripts"
yum: name=perl-Switch state=present
when: ansible_os_family == "RedHat"
- name: "make sure package perl-Sys-Syslog is installed for the monitoring scripts"
yum: name=perl-Sys-Syslog state=present
when: ansible_os_family == "RedHat"
- name: "make sure package perl-LWP-Protocol-https is installed for the monitoring scripts"
yum: name=perl-LWP-Protocol-https state=present
when: ansible_os_family == "RedHat"
- name: download scripts
get_url: url=http://ec2-downloads.s3.amazonaws.com/cloudwatch-samples/CloudWatchMonitoringScripts-v1.1.0.zip dest=/tmp/CloudWatchMonitoringScripts.zip
- name: unzip the scripts
unarchive: src=/tmp/CloudWatchMonitoringScripts.zip dest=/tmp/
- name: delete archive
file: path=/tmp/CloudWatchMonitoringScripts.zip state=absent
- name: set Access key in credentials file
replace: dest=/tmp/aws-scripts-mon/awscreds.template regexp='AWSAccessKeyId=' replace='AWSAccessKeyId={{ ec2_access_key }}' backup=yes
- name: set Secret key in credentials file
replace: dest=/tmp/aws-scripts-mon/awscreds.template regexp='AWSSecretKey=' replace='AWSSecretKey={{ ec2_secret_key }}' backup=yes
- name: move directory out of /tmp
command: mv /tmp/aws-scripts-mon/ /root/ creates=/root/aws-scripts-mon/
- name: add command to cron
lineinfile: dest=/etc/crontab insertafter=EOF line="* * * * * root /root/aws-scripts-mon/mon-put-instance-data.pl --mem-util --mem-used --mem-avail --aws-credential-file=/root/aws-scripts-mon/awscreds.template"
@mkatkade
Copy link

mkatkade commented May 8, 2017

Thanks for the Script. I have faced some errors and I have tried to solve them using below script.
Note: Syntax of command may differ by version to version in Ansible Playbook.

  • hosts: all
    vars:
    ec2_access_key: "AKIA......................."
    ec2_secret_key: "b..........................................................H"
    remote_user: ec2-user
    become: yes
    tasks:
    • name: "make sure package perl-switch is installed for the monitoring scripts"
      yum: name=perl-Switch state=present
      when: ansible_distribution == "RedHat"
    • name: "make sure package perl-Sys-Syslog is installed for the monitoring scripts"
      yum: name=perl-Sys-Syslog state=present
      when: ansible_distribution == "RedHat"
    • name: "make sure package perl-LWP-Protocol-https is installed for the monitoring scripts"
      yum: name=perl-LWP-Protocol-https state=present
      when: ansible_distribution == "RedHat"
    • name: download scripts
      get_url: url=http://ec2-downloads.s3.amazonaws.com/cloudwatch-samples/CloudWatchMonitoringScripts-v1.1.0.zip dest=/tmp/CloudWatchMonitoringScripts.zip
    • yum: name=unzip state=present
    • name: unzip the scripts
      unarchive:
      src: /tmp/CloudWatchMonitoringScripts.zip
      dest: /tmp/
      remote_src: True
    • name: delete archive
      file: path=/tmp/CloudWatchMonitoringScripts.zip state=absent
    • name: set Access key in credentials file
      replace: dest=/tmp/aws-scripts-mon/awscreds.template regexp='AWSAccessKeyId=' replace='AWSAccessKeyId={{ ec2_access_key }}' backup=yes
    • name: set Secret key in credentials file
      replace: dest=/tmp/aws-scripts-mon/awscreds.template regexp='AWSSecretKey=' replace='AWSSecretKey={{ ec2_secret_key }}' backup=yes
    • name: move directory out of /tmp
      command: mv /tmp/aws-scripts-mon/ /root/ creates=/root/aws-scripts-mon/
    • name: "add command to cron"
      lineinfile: dest=/etc/crontab insertafter=EOF line="* * * * * root /root/aws-scripts-mon/mon-put-instance-data.pl --mem-util --mem-used --mem-avail --disk-path=/dev/xvda2 --disk-space-util --disk-space-used --disk-space-avail --aws-credential-file=/root/aws-scripts-mon/awscreds.template"

@dminca
Copy link

dminca commented Jun 27, 2017

@mkatkade ever heard of syntax highlighting 🙃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment