使用ansible检测磁盘速度慢

问题描述 投票:0回答:1

我们认为磁盘速度慢的阈值是读取和写入 3000 行的时间超过 0.10 秒,我们使用下面的播放并获得它所花费的实时时间。

   - name: Extracting last 3000 lines from logs on "{{ inventory_hostname }}"
     shell: "time tail -3000 {{ item }} > {{ item }}_last3klines"
     register: diskspeed
     loop: "{{ log }}"

   - include_tasks: "{{ playbook_dir }}/printdiskspeed.yml"
     loop: "{{ diskspeed.results }}"
     loop_control:
       loop_var: files 

cat printdiskspeed.yml

   - debug:
       msg: "{{ files.stderr_lines[1].split()[1] }}"

输出:

TASK [debug] ************************************************************************************************************************************************

Wednesday 05 April 2023  23:54:18 -0500 (0:00:00.056)       0:00:17.689 *******

ok: [host3] => {

    "msg": "0m0.03s"

}

TASK [debug] ************************************************************************************************************************************************

Wednesday 05 April 2023  23:54:18 -0500 (0:00:00.020)       0:00:17.709 *******

ok: [host3] => {

    "msg": "0m0.02s"

}

TASK [debug] ************************************************************************************************************************************************

Wednesday 05 April 2023  23:54:18 -0500 (0:00:00.019)       0:00:17.729 *******

ok: [host3] => {

    "msg": "0m0.02s"

}

TASK [debug] ************************************************************************************************************************************************

Wednesday 05 April 2023  23:54:18 -0500 (0:00:00.025)       0:00:17.754 *******

ok: [host1] => {

    "msg": "0m0.02s"

}

TASK [debug] ************************************************************************************************************************************************

Wednesday 05 April 2023  23:54:18 -0500 (0:00:00.024)       0:00:17.779 *******

ok: [host1] => {

    "msg": "0m0.01s"

}

 
TASK [debug] ************************************************************************************************************************************************

Wednesday 05 April 2023  23:54:18 -0500 (0:00:00.025)       0:00:17.804 *******

ok: [host1] => {

    "msg": "0m0.02s"

}

我需要关于如何比较和检查上面输出中的时间是否大于 0m0.10s 的帮助?

我试过下面但它错误

   - debug:
       msg: "DISK SLOW"
     when: "{{ files.stderr_lines[1].split()[1] }}" > "0m0.10s"

输出:

TASK [include_tasks] ****************************************************************************************************************************************

Wednesday 05 April 2023  23:36:24 -0500 (0:00:00.064)       0:00:18.779 *******

fatal: [host3]: FAILED! => {"reason": "We were unable to read either as JSON nor YAML, these are the errors we got from each:\nJSON: Expecting value: line 1 column 4 (char 3)\n\nSyntax Error while loading YAML.\n  did not find expected comment or line break\n\nThe error appears to be in '/web/playbooks/automation/printdiskspeed.yml': line 8, column 55, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n       msg: \"DISK SLOW\"\n     when: \"{{ files.stderr_lines[1].split()[1] }}\" > \"0m0.02s\"\n                                                      ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n"}

你能建议正确的语法/解决方案吗?

time ansible syntax runtime-error case
1个回答
1
投票

即使在我看来给定的方法不是解决用例的正确方法并且应该避免,对于所描述的问题可能有两个简单的快速和懒惰的解决方案。


在 Ansible 控制节点上使用后处理的一种不太可取的方法可能是将值视为一种“版本”。为此,您可以查看以下最小示例剧本。

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    expectedTime: "0m0.10s"
    measuredTime: "0m0.15s"

  tasks:

  - name: Show hint
    debug:
      msg: "SLOW"
    when: measuredTime is version(expectedTime, '>=')

类似的问答和进一步


在远程节点上使用预处理的更可取的方法可能是仅创建和收集感兴趣的结果和值。

---
- hosts: localhost
  become: true
  gather_facts: false

  tasks:

  - name: Extracting last lines from log
    shell: 'TIMEFORMAT=%R; time tail -3000 /var/log/messages > /tmp/messages'
    changed_when: false # because it is an reporting task
    register: realtime

  - debug:
      msg: "{{ realtime.stderr }}"

导致输出

TASK [Extracting last lines from log] *****
ok: [localhost]

TASK [debug] *****
ok: [localhost] =>
  msg: '0.012'

转换后仅为数值。

  - debug:
      msg: "FAST"
    when: realtime.stderr | float <= 0.010

归功于

© www.soinside.com 2019 - 2024. All rights reserved.