|
|
@ -1,34 +1,49 @@ |
|
|
|
<h1>Shell Scripts</h1> |
|
|
|
<p>You can easily add SITE_NAME monitoring to a shell script. All you |
|
|
|
have to do is make a HTTP request at the end of the script. |
|
|
|
have to do is make a HTTP request at an appropriate place in the script. |
|
|
|
<a href="https://curl.haxx.se/docs/manpage.html">curl</a> and |
|
|
|
<a href="https://www.gnu.org/software/wget/manual/wget.html">wget</a> |
|
|
|
are two common command line HTTP clients you can use.</p> |
|
|
|
<div class="highlight"><pre><span></span><code><span class="c1"># Sending a HTTP GET request with curl:</span> |
|
|
|
curl --retry <span class="m">3</span> PING_URL |
|
|
|
<div class="highlight"><pre><span></span><code><span class="c1"># Sends a HTTP GET request with curl:</span> |
|
|
|
curl -m <span class="m">10</span> --retry <span class="m">5</span> PING_URL |
|
|
|
|
|
|
|
<span class="c1"># Silent version (no stdout/stderr output unless curl hits an error):</span> |
|
|
|
curl -fsS --retry <span class="m">3</span> PING_URL |
|
|
|
|
|
|
|
<span class="c1"># Sending a HTTP GET request with wget:</span> |
|
|
|
wget PING_URL -O /dev/null |
|
|
|
curl -fsS -m <span class="m">10</span> --retry <span class="m">5</span> -o /dev/null PING_URL |
|
|
|
</code></pre></div> |
|
|
|
|
|
|
|
|
|
|
|
<p>Here's what each curl parameter does:</p> |
|
|
|
<dl> |
|
|
|
<dt><strong>-m <seconds></strong></dt> |
|
|
|
<dd>Maximum time in seconds that you allow the whole operation to take.</dd> |
|
|
|
<dt><strong>--retry <num></strong></dt> |
|
|
|
<dd>If a HTTP request fails, retry up to this many times. By default, curl |
|
|
|
uses an increasing delay between each retry (1s, 2s, 4s, 8s, ...). |
|
|
|
See also <a href="https://curl.haxx.se/docs/manpage.html#--retry-delay">--retry-delay</a>.</dd> |
|
|
|
<dt><strong>-f, --fail</strong></dt> |
|
|
|
<dd>Makes curl treat non-200 responses as errors.</dd> |
|
|
|
<dt><strong>-s, --silent</strong></dt> |
|
|
|
<dd>Silent or quiet mode. Hides the progress meter, but also |
|
|
|
hides error messages.</dd> |
|
|
|
<dt><strong>-S, --show-error</strong></dt> |
|
|
|
<dd>Re-enables error messages when -s is used.</dd> |
|
|
|
<dt><strong>-o /dev/null</strong></dt> |
|
|
|
<dd>Redirect curl's stdout to /dev/null (error messages still go to stderr).</dd> |
|
|
|
</dl> |
|
|
|
<h2>Signalling Failure from Shell Scripts</h2> |
|
|
|
<p>You can append <code>/fail</code> to any ping URL and use the resulting URL to actively |
|
|
|
signal a failure. The below example:</p> |
|
|
|
signal a failure. The following example:</p> |
|
|
|
<ul> |
|
|
|
<li>runs <code>/usr/bin/certbot renew</code></li> |
|
|
|
<li>if the certbot command is successful (exit code 0), send HTTP GET to <code>PING_URL</code></li> |
|
|
|
<li>otherwise, send HTTP GET to <code>PING_URL/fail</code></li> |
|
|
|
<li>if the certbot command is successful (exit code 0), sends HTTP GET to <code>PING_URL</code></li> |
|
|
|
<li>otherwise, sends HTTP GET to <code>PING_URL/fail</code></li> |
|
|
|
</ul> |
|
|
|
<div class="highlight"><pre><span></span><code><span class="ch">#!/bin/sh</span> |
|
|
|
|
|
|
|
<span class="c1"># Payload here:</span> |
|
|
|
/usr/bin/certbot renew |
|
|
|
<span class="c1"># Ping SITE_NAME</span> |
|
|
|
curl --retry <span class="m">3</span> <span class="s2">"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">&&</span> <span class="nb">echo</span> -n /fail<span class="k">)</span><span class="s2">"</span> |
|
|
|
curl -m <span class="m">10</span> --retry <span class="m">5</span> <span class="s2">"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">&&</span> <span class="nb">echo</span> -n /fail<span class="k">)</span><span class="s2">"</span> |
|
|
|
</code></pre></div> |
|
|
|
|
|
|
|
|
|
|
@ -40,7 +55,7 @@ will accept and store first 10KB of the request body.</p> |
|
|
|
<div class="highlight"><pre><span></span><code><span class="ch">#!/bin/sh</span> |
|
|
|
|
|
|
|
<span class="nv">m</span><span class="o">=</span><span class="k">$(</span>/usr/bin/certbot renew <span class="m">2</span>><span class="p">&</span><span class="m">1</span><span class="k">)</span> |
|
|
|
curl -fsS --retry <span class="m">3</span> --data-raw <span class="s2">"</span><span class="nv">$m</span><span class="s2">"</span> PING_URL |
|
|
|
curl -fsS -m <span class="m">10</span> --retry <span class="m">5</span> --data-raw <span class="s2">"</span><span class="nv">$m</span><span class="s2">"</span> PING_URL |
|
|
|
</code></pre></div> |
|
|
|
|
|
|
|
|
|
|
@ -61,5 +76,5 @@ register with SITE_NAME the first time they run.</p> |
|
|
|
<span class="nv">URL</span><span class="o">=</span><span class="sb">`</span>curl -s SITE_ROOT/api/v1/checks/ -H <span class="s2">"X-Api-Key: </span><span class="nv">$API_KEY</span><span class="s2">"</span> -d <span class="s2">"</span><span class="nv">$PAYLOAD</span><span class="s2">"</span> <span class="p">|</span> jq -r .ping_url<span class="sb">`</span> |
|
|
|
|
|
|
|
<span class="c1"># Finally, send a ping:</span> |
|
|
|
curl --retry <span class="m">3</span> <span class="nv">$URL</span> |
|
|
|
curl -m <span class="m">10</span> --retry <span class="m">5</span> <span class="nv">$URL</span> |
|
|
|
</code></pre></div> |