1

在使用 ec2 动态清单通过 ansible 设置 AWS CloudWatch 警报时,我发现了一个非常具体的问题。

我已成功设置aws-script-mon来监控我的机器上磁盘和 RAM 的使用情况。

我还设法使用ansible ec2_metric_alarm模块设置 RAM 使用警报。

我目前面临的问题是在设置磁盘使用警报时,Filesystem需要维度参数,但在ec2 动态库存变量中未返回。

我的一些机器将文件系统设置为/dev/xvda1,而其他机器则设置为:/dev/disk/by-uuid/abcd123-def4-....

我目前的“解决方案”如下:

- name: "Disk > 60% (filesystem by fixed uuid)"
  ec2_metric_alarm:
    state: present
    name: "{{ ec2_tag_Name }}-Disk"
    region: "{{ ec2_region }}"
    dimensions:
      InstanceId: '{{ ec2_id }}'
      MountPath: "/"
      Filesystem: '/dev/disk/by-uuid/abcd123-def4-...'
    namespace: "System/Linux"
    metric: DiskSpaceUtilization
    statistic: Average
    comparison: ">="
    threshold: 60.0
    unit: Percent
    period: 300
    evaluation_periods: 1
    description: Triggered when Disk utilization is more than 60% for 5 minutes
    alarm_actions: ['arn:aws:sns:us-west-2:1234567890:slack']
  when: ec2_tag_Name in ['srv1', 'srv2']

- name: "Disk > 60% (filesystem /dev/xvda1)"
  ec2_metric_alarm:
    state: present
    name: "{{ ec2_tag_Name }}-Disk"
    region: "{{ ec2_region }}"
    dimensions:
      InstanceId: '{{ ec2_id }}'
      MountPath: "/"
      Filesystem: '/dev/xvda1'
    namespace: "System/Linux"
    metric: DiskSpaceUtilization
    statistic: Average
    comparison: ">="
    threshold: 60.0
    unit: Percent
    period: 300
    evaluation_periods: 1
    description: Triggered when Disk utilization is more than 60% for 5 minutes
    alarm_actions: ['arn:aws:sns:us-west-2:1234567890:slack']
  when: ec2_tag_Name not in ['srv1', 'srv2']

这两个任务之间的唯一区别是Filesystem维度和when条件(innot in)。

有什么方法可以获取Filesystem价值,以便我可以像说的那样舒适地使用它们ec2_id?我最担心的是,在创建新机器并根据该值处理机器列表时,我必须观察文件系统值。

4

1 回答 1

1

我找不到这个问题的好解决方案,最后编写了一个 bash 脚本来生成一个包含 UUID 变量的 YAML 文件。

  1. 使用脚本模块在远程机器上运行脚本: - script: ../files/get_disk_uuid.sh > /disk_uuid.yml
  2. 从远程获取创建的文件
  3. 使用 include_vars_module 从文件中导入变量。YAML 语法要求变量名中的连字符替换为下划线。磁盘标签“cloudimg-rootfs”变为“cloudimg_rootfs”。未标记的磁盘使用变量名称:'disk0、disk1、disk2...'

脚本并不理想。有一天我想写一个模块来完成同样的任务。

#! /bin/bash
# get_disk_uuid.sh

echo '---'
disk_num=0
for entry in `blkid -o export -s LABEL -s UUID`; do
    if [[ $entry == LABEL* ]];
    then
   label=${entry: 6}
    elif [[ $entry == UUID* ]];
    then
       uuid=${entry: 5}
    fi
    if [ $label ] && [ $uuid ]; then
        printf $label: | tr - _
        printf ' '$uuid'\n'
        label=''
        uuid=''
    elif [ $uuid ]; then
        printf disk$disk_num:
        printf ' '$uuid'\n'
        label=''
        uuid=''
fi
done
于 2016-04-05T18:46:29.470 回答