TomonoriSoejima / Tejun

notes related to working cases
5 stars 3 forks source link

how to get global ip using watcher #125

Open TomonoriSoejima opened 1 year ago

TomonoriSoejima commented 1 year ago
PUT _watcher/watch/ip_check
{
  "trigger": {
    "schedule": {
      "interval": "10m"
    }
  },
  "input": {
    "http": {
      "request": {
        "url": "https://api.ipify.org?format=json"
      }
    }
  },
  "condition": {
      "always": {}
  },
  "actions": {
    "log": {
      "logging": {
        "text": "Current public IP is {{ctx.payload.ip}}"
      }
    }
  }
}

POST _watcher/watch/ip_check/_execute

After checking the IP address, you may no longer need this watcher and it can be deleted like this. DELETE _watcher/watch/ip_check

TomonoriSoejima commented 1 year ago

Summary: The code describes two Elasticsearch Watcher API requests related to monitoring and logging the current public IP address of the executing system.

  1. Watcher Creation (PUT _watcher/watch/ip_check):

    • Purpose: This request creates (or updates) a watch named ip_check.
    • Trigger: The watch is set to trigger every 10 minutes.
    • Input:
      • An HTTP request is made to https://api.ipify.org?format=json, a service that returns the public IP of the requester in a JSON format.
    • Condition: The watch will always execute its actions since it uses the always condition.
    • Actions:

      • It has a single action (log) which logs the fetched public IP address. The logging message is "Current public IP is {{ctx.payload.ip}}", where {{ctx.payload.ip}} will be replaced by the actual IP address fetched from the IP service.

      Elasticsearch Watcher Documentation Elasticsearch HTTP Input Elasticsearch Watch Conditions Elasticsearch Logging Action

  2. Watch Execution (POST _watcher/watch/ip_check/_execute):

Note: Using services like ipify.org to determine the public IP is common, but always ensure you are adhering to any terms of service or rate limits of the external service.