You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.1 KiB

  1. # Shell Scripts
  2. You can easily add SITE_NAME monitoring to a shell script. All you
  3. have to do is make a HTTP request at the end of the script.
  4. [curl](https://curl.haxx.se/docs/manpage.html) and
  5. [wget](https://www.gnu.org/software/wget/manual/wget.html)
  6. are two common command line HTTP clients you can use.
  7. ```bash
  8. # Sending a HTTP GET request with curl:
  9. curl --retry 3 PING_URL
  10. # Silent version (no stdout/stderr output unless curl hits an error):
  11. curl -fsS --retry 3 PING_URL
  12. # Sending a HTTP GET request with wget:
  13. wget PING_URL -O /dev/null
  14. ```
  15. ## Signalling Failure from Shell Scripts
  16. You can append `/fail` to any ping URL and use the resulting URL to actively
  17. signal a failure. The below example:
  18. * runs `/usr/bin/certbot renew`
  19. * if the certbot command is successful (exit code 0), send HTTP GET to `PING_URL`
  20. * otherwise, send HTTP GET to `PING_URL/fail`
  21. ```bash
  22. #!/bin/sh
  23. # Payload here:
  24. /usr/bin/certbot renew
  25. # Ping SITE_NAME
  26. curl --retry 3 "PING_URL$([ $? -ne 0 ] && echo -n /fail)"
  27. ```
  28. ## Logging Command Output
  29. When pinging with HTTP POST, you can put extra diagnostic information in request
  30. body. If the request body looks like a valid UTF-8 string, SITE_NAME
  31. will accept and store first 10KB of the request body.
  32. In the below example, certbot's output is captured and submitted via HTTP POST:
  33. ```bash
  34. #!/bin/sh
  35. m=$(/usr/bin/certbot renew 2>&1)
  36. curl -fsS --retry 3 -X POST --data-raw "$m" PING_URL
  37. ```
  38. ## Auto-provisioning New Checks
  39. This example uses SITE_NAME [Management API](../api/) to create a check "on the fly"
  40. (if it does not already exist) and to retrieve its ping URL.
  41. Using this technique, you can write services that automatically
  42. register with SITE_NAME the first time they run.
  43. ```bash
  44. #!/bin/bash
  45. API_KEY=your-api-key-here
  46. # Check's parameters. This example uses system's hostname for check's name.
  47. PAYLOAD='{"name": "'`hostname`'", "timeout": 60, "grace": 60, "unique": ["name"]}'
  48. # Create the check if it does not exist.
  49. # Grab the ping_url from JSON response using the jq utility:
  50. URL=`curl -s SITE_ROOT/api/v1/checks/ -H "X-Api-Key: $API_KEY" -d "$PAYLOAD" | jq -r .ping_url`
  51. # Finally, send a ping:
  52. curl --retry 3 $URL
  53. ```