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.

79 lines
4.9 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. <h1>Shell Scripts</h1>
  2. <p>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. <a href="https://curl.haxx.se/docs/manpage.html">curl</a> and
  5. <a href="https://www.gnu.org/software/wget/manual/wget.html">wget</a>
  6. are two common command line HTTP clients you can use.</p>
  7. <div class="bash highlight"><pre><span></span><code><span class="c1"># Sends a HTTP GET request with curl:</span>
  8. curl -m <span class="m">10</span> --retry <span class="m">5</span> PING_URL
  9. <span class="c1"># Silent version (no stdout/stderr output unless curl hits an error):</span>
  10. curl -fsS -m <span class="m">10</span> --retry <span class="m">5</span> -o /dev/null PING_URL
  11. </code></pre></div>
  12. <p>Here's what each curl parameter does:</p>
  13. <dl>
  14. <dt><strong>-m &lt;seconds&gt;</strong></dt>
  15. <dd>Maximum time in seconds that you allow the whole operation to take.</dd>
  16. <dt><strong>--retry &lt;num&gt;</strong></dt>
  17. <dd>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 <a href="https://curl.haxx.se/docs/manpage.html#--retry-delay">--retry-delay</a>.</dd>
  20. <dt><strong>-f, --fail</strong></dt>
  21. <dd>Makes curl treat non-200 responses as errors.</dd>
  22. <dt><strong>-s, --silent</strong></dt>
  23. <dd>Silent or quiet mode. Hides the progress meter, but also
  24. hides error messages.</dd>
  25. <dt><strong>-S, --show-error</strong></dt>
  26. <dd>Re-enables error messages when -s is used.</dd>
  27. <dt><strong>-o /dev/null</strong></dt>
  28. <dd>Redirect curl's stdout to /dev/null (error messages still go to stderr).</dd>
  29. </dl>
  30. <h2>Signalling Failure from Shell Scripts</h2>
  31. <p>You can append <code>/fail</code> to any ping URL and use the resulting URL to actively
  32. signal a failure. The following example:</p>
  33. <ul>
  34. <li>runs <code>/usr/bin/certbot renew</code></li>
  35. <li>if the certbot command is successful (exit code 0), sends HTTP GET to <code>PING_URL</code></li>
  36. <li>otherwise, sends HTTP GET to <code>PING_URL/fail</code></li>
  37. </ul>
  38. <div class="bash highlight"><pre><span></span><code><span class="ch">#!/bin/sh</span>
  39. <span class="c1"># Payload here:</span>
  40. /usr/bin/certbot renew
  41. <span class="c1"># Ping SITE_NAME</span>
  42. curl -m <span class="m">10</span> --retry <span class="m">5</span> <span class="s2">&quot;PING_URL</span><span class="k">$(</span><span class="o">[</span> <span class="nv">$?</span> -ne <span class="m">0</span> <span class="o">]</span> <span class="o">&amp;&amp;</span> <span class="nb">echo</span> -n /fail<span class="k">)</span><span class="s2">&quot;</span>
  43. </code></pre></div>
  44. <h2>Logging Command Output</h2>
  45. <p>When pinging with HTTP POST, you can put extra diagnostic information in request
  46. body. If the request body looks like a valid UTF-8 string, SITE_NAME
  47. will accept and store first 10KB of the request body.</p>
  48. <p>In the below example, certbot's output is captured and submitted via HTTP POST:</p>
  49. <div class="bash highlight"><pre><span></span><code><span class="ch">#!/bin/sh</span>
  50. <span class="nv">m</span><span class="o">=</span><span class="k">$(</span>/usr/bin/certbot renew <span class="m">2</span>&gt;<span class="p">&amp;</span><span class="m">1</span><span class="k">)</span>
  51. curl -fsS -m <span class="m">10</span> --retry <span class="m">5</span> --data-raw <span class="s2">&quot;</span><span class="nv">$m</span><span class="s2">&quot;</span> PING_URL
  52. </code></pre></div>
  53. <h2>Auto-provisioning New Checks</h2>
  54. <p>This example uses SITE_NAME <a href="../api/">Management API</a> to create a check "on the fly"
  55. (if it does not already exist) and to retrieve its ping URL.
  56. Using this technique, you can write services that automatically
  57. register with SITE_NAME the first time they run.</p>
  58. <div class="bash highlight"><pre><span></span><code><span class="ch">#!/bin/bash</span>
  59. <span class="nv">API_KEY</span><span class="o">=</span>your-api-key-here
  60. <span class="c1"># Check&#39;s parameters. This example uses system&#39;s hostname for check&#39;s name.</span>
  61. <span class="nv">PAYLOAD</span><span class="o">=</span><span class="s1">&#39;{&quot;name&quot;: &quot;&#39;</span><span class="sb">`</span>hostname<span class="sb">`</span><span class="s1">&#39;&quot;, &quot;timeout&quot;: 60, &quot;grace&quot;: 60, &quot;unique&quot;: [&quot;name&quot;]}&#39;</span>
  62. <span class="c1"># Create the check if it does not exist.</span>
  63. <span class="c1"># Grab the ping_url from JSON response using the jq utility:</span>
  64. <span class="nv">URL</span><span class="o">=</span><span class="sb">`</span>curl -s SITE_ROOT/api/v1/checks/ -H <span class="s2">&quot;X-Api-Key: </span><span class="nv">$API_KEY</span><span class="s2">&quot;</span> -d <span class="s2">&quot;</span><span class="nv">$PAYLOAD</span><span class="s2">&quot;</span> <span class="p">|</span> jq -r .ping_url<span class="sb">`</span>
  65. <span class="c1"># Finally, send a ping:</span>
  66. curl -m <span class="m">10</span> --retry <span class="m">5</span> <span class="nv">$URL</span>
  67. </code></pre></div>