具有响应时间的uri模块 [英] uri module with response times

查看:60
本文介绍了具有响应时间的uri模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得Ansible网站的响应时间,大致如下 URI模块,但它似乎不支持响应时间.

I'd like to have the response times from sites in Ansible, something along the lines like this, but in Ansible. I'm using the URI module but it seems it does not support response times.

我不喜欢在时间配置文件中使用回调插件,因为我在单个任务中声明了多个url.

I do not like to use the callback plugin with the time profile, because I state multiple url's in a single task.

我看到Ansible没有返回我需要的值,这是某人已经完成的事情吗?

I see Ansible does not return the values I require, is this something someone has already done?

推荐答案

因此,我创建了整个剧本超出此请求.该剧本本身包括:

So, I've created a whole playbook out of this request. The playbook itself includes:

  • 检查URL是否提供状态码200
  • 包括从主机到服务器的反应时间
  • 失败时发送Slack消息
  • 将日志发送到Elasticsearch服务器

可以设置一个cronjob来让剧本每x秒运行一次.

One could set a cronjob to let the playbook run each x seconds.

要回答此特定问题:

 - hosts: localhost
   vars:
     sites:
       - https://google.com

    - name: verify if all urls works properly
      uri:
        url: "{{ item }}"
        timeout: 10
      with_items: "{{ sites }}"
      register: health_check
      ignore_errors: yes

    # From the docs: The time in sec it took from the start until
    # the first byte was just about to be transferred. This includes
    # time_pretransfer and also the time the server needed to calculate the result.
    - name: response times of sites via cURL via the `starttransfer` method
      shell: 'curl {{ item }} -s -o /dev/null -w  "%{time_starttransfer}\n"'
      register: curl_time
      ignore_errors: True
      args:
        warn: false
      with_items:
        - "{{ sites }}"

    - name: write url and status code to file
      lineinfile:
        line: "{{ start_time }}, {{ item.url }}, {{ item.status }}"
        insertafter: EOF
        dest: "{{ file_path }}"
      with_items: "{{ health_check.results }}"

    - name: append response times to the file
      lineinfile:
        path: "{{ file_path }}"
        backrefs: yes
        regexp: "^(.*{{ start_time }}, .*{{ item.item | replace ('https://', '') | replace ('http://', '')   }}.*)"
        line: '\1, {{ item.stdout }}'
      with_items: "{{ curl_time.results }}"

这篇关于具有响应时间的uri模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆