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.

66 lines
4.6 KiB

4 years ago
4 years ago
  1. <h1>Attaching Logs</h1>
  2. <p>SITE_NAME ping endpoints accept HTTP HEAD, GET and POST request methods.</p>
  3. <p>When using HTTP POST, <strong>you can include arbitrary payload in the request body</strong>.
  4. If the request body looks like a UTF-8 string, SITE_NAME will log the
  5. first 10 kilobytes (10 000 bytes) of the request body, so you can inspect it later.</p>
  6. <h2>Logging Command Output</h2>
  7. <p>In this example, we run <code>certbot renew</code>, capture its output, and submit
  8. the captured output to SITE_NAME:</p>
  9. <div class="bash highlight"><pre><span></span><code><span class="ch">#!/bin/sh</span>
  10. <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>
  11. 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
  12. </code></pre></div>
  13. <h2>In Combination with the <code>/fail</code> Endpoint</h2>
  14. <p>We can extend the previous example and signal either success or failure
  15. depending on the exit code:</p>
  16. <div class="bash highlight"><pre><span></span><code><span class="ch">#!/bin/sh</span>
  17. <span class="nv">url</span><span class="o">=</span>PING_URL
  18. <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>
  19. <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>
  20. 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> <span class="nv">$url</span>
  21. </code></pre></div>
  22. <p>The above script can be packaged in a single line. The one-line
  23. version sacrifices some readability, but it can be used directly in crontab,
  24. without using a wrapper script:</p>
  25. <div class="bash highlight"><pre><span></span><code><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><span class="p">;</span> curl -fsS --data-raw <span class="s2">&quot;</span><span class="nv">$m</span><span class="s2">&quot;</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>
  26. </code></pre></div>
  27. <h2>Using Runitor</h2>
  28. <p><a href="https://github.com/bdd/runitor">Runitor</a> is a third party utility that runs the
  29. supplied command, captures its output and and reports to SITE_NAME.
  30. It also measures the execution time, and retries HTTP requests on transient errors.
  31. Best of all, the syntax is simple and clean:</p>
  32. <div class="bash highlight"><pre><span></span><code>runitor -uuid your-uuid-here -- /usr/bin/certbot renew
  33. </code></pre></div>
  34. <h2>Handling More Than 10KB of Logs</h2>
  35. <p>While SITE_NAME can store a small amount of logs in a pinch, it is not specifically
  36. designed for that. If you run into the issue of logs getting cut off, consider
  37. the following options:</p>
  38. <ul>
  39. <li>See if the logs can be made less verbose. For example, if you have a batch job
  40. that outputs a line of text per item processed, perhaps it can output a short
  41. summary with the totals instead.</li>
  42. <li>If the important content is usually at the end, submit the <strong>last 10KB</strong> instead
  43. of the first. Here is an example that submits the last 10KB of <code>dmesg</code> output:</li>
  44. </ul>
  45. <div class="bash highlight"><pre><span></span><code><span class="ch">#!/bin/sh</span>
  46. <span class="nv">m</span><span class="o">=</span><span class="k">$(</span>dmesg <span class="p">|</span> tail --bytes<span class="o">=</span><span class="m">10000</span><span class="k">)</span>
  47. 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
  48. </code></pre></div>
  49. <ul>
  50. <li>Finally, if for your use case it is critical to capture the entire log output,
  51. consider using a dedicated log aggregation service for capturing the logs.</li>
  52. </ul>