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.

96 lines
2.9 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 an appropriate place in 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. # Sends a HTTP GET request with curl:
  9. curl -m 10 --retry 5 PING_URL
  10. # Silent version (no stdout/stderr output unless curl hits an error):
  11. curl -fsS -m 10 --retry 5 -o /dev/null PING_URL
  12. ```
  13. Here's what each curl parameter does:
  14. **-m <seconds>**
  15. : Maximum time in seconds that you allow the whole operation to take.
  16. **--retry <num>**
  17. : If a HTTP request fails, retry up to this many times. By default, curl
  18. uses an increasing delay between each retry (1s, 2s, 4s, 8s, ...).
  19. See also [--retry-delay](https://curl.haxx.se/docs/manpage.html#--retry-delay).
  20. **-f, --fail**
  21. : Makes curl treat non-200 responses as errors.
  22. **-s, --silent**
  23. : Silent or quiet mode. Hides the progress meter, but also
  24. hides error messages.
  25. **-S, --show-error**
  26. : Re-enables error messages when -s is used.
  27. **-o /dev/null**
  28. : Redirect curl's stdout to /dev/null (error messages still go to stderr).
  29. ## Signalling Failure from Shell Scripts
  30. You can append `/fail` or `/{exit-status}` to any ping URL and use the resulting URL
  31. to actively signal a failure. The exit status should be a 0-255 integer.
  32. SITE_NAME will interpret exit status 0 as success, and all non-zero values as failures.
  33. The following example runs `/usr/bin/certbot renew`, and uses the `$?` variable to
  34. look up its exit status:
  35. ```bash
  36. #!/bin/sh
  37. # Payload here:
  38. /usr/bin/certbot renew
  39. # Ping SITE_NAME
  40. curl -m 10 --retry 5 PING_URL/$?
  41. ```
  42. ## Logging Command Output
  43. When pinging with HTTP POST, you can put extra diagnostic information in request
  44. body. If the request body looks like a valid UTF-8 string, SITE_NAME
  45. will accept and store first 10KB of the request body.
  46. In the below example, certbot's output is captured and submitted via HTTP POST:
  47. ```bash
  48. #!/bin/sh
  49. m=$(/usr/bin/certbot renew 2>&1)
  50. curl -fsS -m 10 --retry 5 --data-raw "$m" PING_URL
  51. ```
  52. ## Auto-provisioning New Checks
  53. This example uses SITE_NAME [Management API](../api/) to create a check "on the fly"
  54. (if it does not already exist) and to retrieve its ping URL.
  55. Using this technique, you can write services that automatically
  56. register with SITE_NAME the first time they run.
  57. ```bash
  58. #!/bin/bash
  59. API_KEY=your-api-key-here
  60. # Check's parameters. This example uses system's hostname for check's name.
  61. PAYLOAD='{"name": "'`hostname`'", "timeout": 60, "grace": 60, "unique": ["name"]}'
  62. # Create the check if it does not exist.
  63. # Grab the ping_url from JSON response using the jq utility:
  64. URL=`curl -s SITE_ROOT/api/v1/checks/ -H "X-Api-Key: $API_KEY" -d "$PAYLOAD" | jq -r .ping_url`
  65. # Finally, send a ping:
  66. curl -m 10 --retry 5 $URL
  67. ```