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.

64 lines
4.0 KiB

  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 the end of 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="highlight"><pre><span></span><code><span class="c1"># Sending a HTTP GET request with curl:</span>
  8. curl --retry <span class="m">3</span> PING_URL
  9. <span class="c1"># Silent version (no stdout/stderr output unless curl hits an error):</span>
  10. curl -fsS --retry <span class="m">3</span> PING_URL
  11. <span class="c1"># Sending a HTTP GET request with wget:</span>
  12. wget PING_URL -O /dev/null
  13. </code></pre></div>
  14. <h2>Signalling Failure from Shell Scripts</h2>
  15. <p>You can append <code>/fail</code> to any ping URL and use the resulting URL to actively
  16. signal a failure. The below example:</p>
  17. <ul>
  18. <li>runs <code>/usr/bin/certbot renew</code></li>
  19. <li>if the certbot command is successful (exit code 0), send HTTP GET to <code>PING_URL</code></li>
  20. <li>otherwise, send HTTP GET to <code>PING_URL/fail</code></li>
  21. </ul>
  22. <div class="highlight"><pre><span></span><code><span class="ch">#!/bin/sh</span>
  23. <span class="c1"># Payload here:</span>
  24. /usr/bin/certbot renew
  25. <span class="c1"># Ping SITE_NAME</span>
  26. curl --retry <span class="m">3</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>
  27. </code></pre></div>
  28. <h2>Logging Command Output</h2>
  29. <p>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.</p>
  32. <p>In the below example, certbot's output is captured and submitted via HTTP POST:</p>
  33. <div class="highlight"><pre><span></span><code><span class="ch">#!/bin/sh</span>
  34. <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>
  35. curl -fsS --retry <span class="m">3</span> -X POST --data-raw <span class="s2">&quot;</span><span class="nv">$m</span><span class="s2">&quot;</span> PING_URL
  36. </code></pre></div>
  37. <h2>Auto-provisioning New Checks</h2>
  38. <p>This example uses SITE_NAME <a href="../api/">Management API</a> to create a check "on the fly"
  39. (if it does not already exist) and to retrieve its ping URL.
  40. Using this technique, you can write services that automatically
  41. register with SITE_NAME the first time they run.</p>
  42. <div class="highlight"><pre><span></span><code><span class="ch">#!/bin/bash</span>
  43. <span class="nv">API_KEY</span><span class="o">=</span>your-api-key-here
  44. <span class="c1"># Check&#39;s parameters. This example uses system&#39;s hostname for check&#39;s name.</span>
  45. <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>
  46. <span class="c1"># Create the check if it does not exist.</span>
  47. <span class="c1"># Grab the ping_url from JSON response using the jq utility:</span>
  48. <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>
  49. <span class="c1"># Finally, send a ping:</span>
  50. curl --retry <span class="m">3</span> <span class="nv">$URL</span>
  51. </code></pre></div>