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.

45 lines
2.7 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. <h1>Signaling failures</h1>
  2. <p>You can actively signal a failure to SITE_NAME by slightly changing the
  3. ping URL: append either <code>/fail</code> or <code>/{exit-status}</code> to your normal ping URL.
  4. The exit status should be a 0-255 integer. SITE_NAME will interpret
  5. exit status 0 as success and all non-zero values as failures.</p>
  6. <p>Examples:</p>
  7. <div class="bash highlight"><pre><span></span><code><span class="c1"># Reports failure by appending the /fail suffix:</span>
  8. curl --retry <span class="m">3</span> PING_URL/fail
  9. <span class="c1"># Reports failure by appending a non-zero exit status:</span>
  10. curl --retry <span class="m">3</span> PING_URL/1
  11. </code></pre></div>
  12. <p>By actively signaling failures to SITE_NAME, you can minimize the delay from your
  13. monitored service encountering a problem to you getting notified about it.</p>
  14. <h2>Shell Scripts</h2>
  15. <p>The below shell script appends <code>$?</code> (a special variable which contains the
  16. exit status of the last executed command) to the ping URL:</p>
  17. <div class="bash highlight"><pre><span></span><code><span class="ch">#!/bin/sh</span>
  18. /usr/bin/certbot renew
  19. curl --retry <span class="m">3</span> PING_URL/<span class="nv">$?</span>
  20. </code></pre></div>
  21. <h2>Python</h2>
  22. <p>Below is a skeleton code example in Python which signals a failure when the
  23. work function returns an unexpected value or throws an exception:</p>
  24. <div class="python highlight"><pre><span></span><code><span class="kn">import</span> <span class="nn">requests</span>
  25. <span class="n">URL</span> <span class="o">=</span> <span class="s2">&quot;PING_URL&quot;</span>
  26. <span class="k">def</span> <span class="nf">do_work</span><span class="p">():</span>
  27. <span class="c1"># Do your number crunching, backup dumping, newsletter sending work here.</span>
  28. <span class="c1"># Return a truthy value on success.</span>
  29. <span class="c1"># Return a falsy value or throw an exception on failure.</span>
  30. <span class="k">return</span> <span class="kc">True</span>
  31. <span class="n">success</span> <span class="o">=</span> <span class="kc">False</span>
  32. <span class="k">try</span><span class="p">:</span>
  33. <span class="n">success</span> <span class="o">=</span> <span class="n">do_work</span><span class="p">()</span>
  34. <span class="k">finally</span><span class="p">:</span>
  35. <span class="c1"># On success, requests PING_URL</span>
  36. <span class="c1"># On failure, requests PING_URL/fail</span>
  37. <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">URL</span> <span class="k">if</span> <span class="n">success</span> <span class="k">else</span> <span class="n">URL</span> <span class="o">+</span> <span class="s2">&quot;/fail&quot;</span><span class="p">)</span>
  38. </code></pre></div>