# Use watcher with topbeat

**URL:** <https://discuss.elastic.co/t/use-watcher-with-topbeat/52874>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [June 15, 2016, 2:22pm UTC](https://discuss.elastic.co/t/use-watcher-with-topbeat/52874 "2016-06-15T14:22:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kennedy\_Kan1](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Kennedy\_Kan1](https://discuss.elastic.co/u/Kennedy_Kan1)\
**Post date:** [June 15, 2016, 2:22pm UTC](https://discuss.elastic.co/t/use-watcher-with-topbeat/52874/1 "2016-06-15T14:22:18Z")

</div>

I have been using topbeat to collect and monitor disk usage data. I have made a barchart in kibana for the top 5 servers with most disk usage percentage on fs.used\_p.  
How can I use watcher to alert when the percentage reach a certain number?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [June 15, 2016, 3:45pm UTC](https://discuss.elastic.co/t/use-watcher-with-topbeat/52874/2 "2016-06-15T15:45:37Z")

</div>

Hey,

thats not too hard. The important part is to get the query right - and this depends what you want to use. I took this as an example:

```auto
GET topbeat-*/filesystem/_search
{
  "size": 0, 
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "@timestamp": {
              "gte": "now-1m",
              "lte": "now"
            }
          }
        },
        {
          "range" : {
            "fs.used_p" : {
              "gte" : 0.4
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "by_host": {
      "terms": {
        "field": "beat.hostname",
        "size": 10
      },
      "aggs": {
        "by_fs": {
          "terms": {
            "field": "fs.device_name",
            "size": 10
          },
          "aggs": {
            "max": {
              "max": {
                "field": "fs.used_p"
              }
            }
          }
        }
      }
    }
  }
}

```

which returns data like this

```json
{
  "took": 10,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 10,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "by_host": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "orca",
          "doc_count": 10,
          "by_fs": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "/dev/disk1",
                "doc_count": 5,
                "max": {
                  "value": 0.49000000953674316
                }
              },
              {
                "key": "devfs",
                "doc_count": 5,
                "max": {
                  "value": 1
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

As you can see I searched for data on my notebook with disks that are more than 40% full, aggregate by host and disk and return the max value of the disk usage.

You can easily combine this into a fully fledged watch

```json
PUT _watcher/watch/free_space
{
  "metadata": {
    "used_percent": 0.4
  },
  "trigger": {
    "schedule": {
      "interval": "5m"
    }
  },
  "input": {
    "search": {
      "request": {
        "indices": [
          "<topbeat-{now/d}>"
        ],
        "types": [
          "filesystem"
        ],
        "body": {
          "size": 0,
          "query": {
            "bool": {
              "filter": [
                {
                  "range": {
                    "@timestamp": {
                      "gte": "now-1m",
                      "lte": "now"
                    }
                  }
                },
                {
                  "range": {
                    "fs.used_p": {
                      "gte": 0.4
                    }
                  }
                }
              ]
            }
          },
          "aggs": {
            "by_host": {
              "terms": {
                "field": "beat.hostname",
                "size": 100
              },
              "aggs": {
                "by_fs": {
                  "terms": {
                    "field": "fs.device_name"
                  },
                  "aggs": {
                    "max": {
                      "max": {
                        "field": "fs.used_p"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "condition": {
    "compare": {
      "ctx.payload.hits.total": {
        "gte": 5
      }
    }
  },
  "actions": {
    "email_alert": {
      "throttle_period": "15m",
      "email": {
        "to": "user@example.org",
        "subject": "Watcher {{ctx.watch_id}} at {{ctx.trigger.triggered_time}}: Filesystem Usage Alert for some hosts",
        "body": "{{#ctx.payload.aggregations.by_host.buckets}}Host {{key}} with {{#by_fs.buckets}}[{{key}}/{{max.value}}]{{/by_fs.buckets}}\n{{/ctx.payload.aggregations.by_host.buckets}}"
      }
    }
  }
}

```

If you check out the email you are receiving you will see that it is pretty raw and you should invest some time, to maybe do a transform to create better values to output.

But this is the basic idea...

Hope this helps...

--Alex

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 1:44pm UTC](https://discuss.elastic.co/t/use-watcher-with-topbeat/52874/3 "2017-07-06T13:44:59Z")

</div>


