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.

38 lines
2.6 KiB

  1. <h1>Signalling failures</h1>
  2. <p>Append <code>/fail</code> to a ping URL and use it to actively signal a failure.
  3. Requesting the <code>/fail</code> URL will immediately mark the check as "down".
  4. You can use this feature to minimize the delay from your monitored service failing
  5. to you getting a notification.</p>
  6. <h2>Shell Scripts</h2>
  7. <p>The below shell script sends sends either a "success" or "failure" ping depending on
  8. command's (certbot in this example) exit code:</p>
  9. <div class="highlight"><pre><span></span><code><span class="ch">#!/bin/sh</span>
  10. <span class="nv">url</span><span class="o">=</span>PING_URL
  11. /usr/bin/certbot renew
  12. <span class="k">if</span> <span class="o">[</span> <span class="nv">$?</span> -ne <span class="m">0</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span> <span class="nv">url</span><span class="o">=</span><span class="nv">$url</span>/fail<span class="p">;</span> <span class="k">fi</span>
  13. curl --retry <span class="m">3</span> <span class="nv">$url</span>
  14. </code></pre></div>
  15. <h2>Python</h2>
  16. <p>Below is a skeleton code example in Python which signals a failure when the
  17. work function returns an unexpected value or throws an exception:</p>
  18. <div class="highlight"><pre><span></span><code><span class="kn">import</span> <span class="nn">requests</span>
  19. <span class="n">URL</span> <span class="o">=</span> <span class="s2">&quot;PING_URL&quot;</span>
  20. <span class="k">def</span> <span class="nf">do_work</span><span class="p">():</span>
  21. <span class="c1"># Do your number crunching, backup dumping, newsletter sending work here.</span>
  22. <span class="c1"># Return a truthy value on success.</span>
  23. <span class="c1"># Return a falsy value or throw an exception on failure.</span>
  24. <span class="k">return</span> <span class="kc">True</span>
  25. <span class="n">success</span> <span class="o">=</span> <span class="kc">False</span>
  26. <span class="k">try</span><span class="p">:</span>
  27. <span class="n">success</span> <span class="o">=</span> <span class="n">do_work</span><span class="p">()</span>
  28. <span class="k">finally</span><span class="p">:</span>
  29. <span class="c1"># On success, requests PING_URL</span>
  30. <span class="c1"># On failure, requests PING_URL/fail</span>
  31. <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>
  32. </code></pre></div>