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.

336 lines
11 KiB

10 years ago
10 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. {% extends "front/base_docs.html" %}
  2. {% load compress static hc_extras %}
  3. {% block title %}Documentation - {% site_name %}{% endblock %}
  4. {% block description %}
  5. <meta name="description" content="Monitor any service that can make a HTTP request or send an email: cron jobs, Bash scripts, Python, Ruby, Node, PHP, JS, ...">
  6. {% endblock %}
  7. {% block keywords %}
  8. <meta name="keywords" content="healthchecks, crontab monitoring, python health check, bash health check, cron monitoring, cron tutorial, cron howto, api health check, open source">
  9. {% endblock %}
  10. {% block docs_content %}
  11. <h2>How {% site_name %} Works</h2>
  12. <p>
  13. Each check in <a href="{% url 'hc-index' %}">My Checks</a>
  14. page has a unique "ping" URL. Whenever you make a HTTP request to this URL,
  15. {% site_name %} records the request and updates the "Last Ping" value of
  16. the corresponding check.
  17. </p>
  18. <p>When a certain, configurable amount of time passes since last received ping,
  19. the check is considered "late". {% site_name %} then
  20. waits for additional time (configured with the "Grace Time" parameter) and,
  21. if still no ping, sends you an alert.</p>
  22. <p>As long as the monitored service sends pings on time, you receive no
  23. alerts. As soon as it fails to check in on time, you get notified.
  24. It is a simple idea.</p>
  25. <h2 class="rule">Executing a Ping</h2>
  26. <p>
  27. At the end of your batch job, add a bit of code to request
  28. your ping URL.
  29. </p>
  30. <ul>
  31. <li>HTTP and HTTPS protocols both work.
  32. Prefer HTTPS, but on old systems you may need to fall back to HTTP.</li>
  33. <li>Request method can be GET, POST or HEAD</li>
  34. <li>Both IPv4 and IPv6 work</li>
  35. <li>
  36. For HTTP POST requests, you can include additional diagnostic information
  37. for your own reference in the request body. If the request body looks
  38. like a UTF-8 string, {% site_name %} will log the first 10 kilobytes
  39. of the request body, so you can inspect it later.
  40. </li>
  41. </ul>
  42. <p>The response will have status code "200 OK" and response body will be a
  43. short and simple string "OK".</p>
  44. <h2 class="rule">Signalling a Failure</h2>
  45. <p>
  46. Append <code>/fail</code> to a ping URL and use it to actively signal a
  47. failure. Requesting the <code>/fail</code> URL will immediately mark the
  48. check as "down". You can use this feature to minimize the delay from
  49. your monitored service failing to you getting a notification.
  50. </p>
  51. <p>Below is a skeleton code example in Python which signals a failure when the
  52. work function returns an unexpected value or throws an exception:</p>
  53. {% include "front/snippets/python_requests_fail.html" %}
  54. <h2 class="rule">Examples</h2>
  55. <p>
  56. Jump to example:
  57. <a href="#crontab">Crontab</a>,
  58. <a href="#bash">Bash</a>,
  59. <a href="#python">Python</a>,
  60. <a href="#ruby">Ruby</a>,
  61. <a href="#node">Node</a>,
  62. <a href="#php">PHP</a>,
  63. <a href="#cs">C#</a>,
  64. <a href="#browser">Browser</a>,
  65. <a href="#powershell">PowerShell</a>,
  66. <a href="#email">Email</a>.
  67. </p>
  68. <a name="crontab"></a>
  69. <h3 class="docs-example">Crontab</h3>
  70. <p>
  71. When using cron, probably the easiest is to append a curl
  72. or wget call after your command. The scheduled time comes,
  73. and your command runs. If it completes successfully (exit code 0),
  74. curl or wget runs a HTTP GET call to the ping URL.
  75. </p>
  76. {% include "front/snippets/crontab.html" %}
  77. <p>With this simple modification, you monitor several failure
  78. scenarios:</p>
  79. <ul>
  80. <li>The whole machine has stopped working (power outage, janitor stumbles on wires, VPS provider problems, etc.) </li>
  81. <li>cron daemon is not running, or has invalid configuration</li>
  82. <li>cron does start your task, but the task exits with non-zero exit code</li>
  83. </ul>
  84. <p>Either way, when your task doesn't finish successfully, you will soon
  85. know about it.</p>
  86. <p>The extra options to curl are meant to suppress any output, unless it hits
  87. an error. This is to prevent cron from sending an email every time the
  88. task runs. Feel free to adjust the curl options to your liking.
  89. </p>
  90. <table class="table curl-opts">
  91. <tr>
  92. <th>&amp;&amp;</th>
  93. <td>Run curl only if <code>/home/user/backup.sh</code> succeeds</td>
  94. </tr>
  95. <tr>
  96. <th>
  97. -f, --fail
  98. </th>
  99. <td>Makes curl treat non-200 responses as errors</td>
  100. </tr>
  101. <tr>
  102. <th>-s, --silent</th>
  103. <td>Silent or quiet mode. Don't show progress meter or error messages.</td>
  104. </tr>
  105. <tr>
  106. <th>-S, --show-error</th>
  107. <td>When used with -s it makes curl show error message if it fails.</td>
  108. </tr>
  109. <tr>
  110. <th>--retry &lt;num&gt;</th>
  111. <td>
  112. If a transient error is returned when curl tries to perform a
  113. transfer, it will retry this number of times before giving up.
  114. Setting the number to 0 makes curl do no retries
  115. (which is the default). Transient error means either: a timeout,
  116. an FTP 4xx response code or an HTTP 5xx response code.
  117. </td>
  118. </tr>
  119. <tr>
  120. <th>&gt; /dev/null</th>
  121. <td>
  122. Redirect curl's stdout to /dev/null (error messages go to stderr,)
  123. </td>
  124. </tr>
  125. </table>
  126. <a name="bash"></a>
  127. <h3 class="docs-example">Bash or a shell script</h3>
  128. <p>Both <code>curl</code> and <code>wget</code> examples accomplish the same
  129. thing: they fire off a HTTP GET method.</p>
  130. <p>
  131. If using <code>curl</code>, make sure it is installed on your target system.
  132. Ubuntu, for example, does not have curl installed out of the box.
  133. </p>
  134. {% include "front/snippets/bash_curl.html" %}
  135. {% include "front/snippets/bash_wget.html" %}
  136. <a name="python"></a>
  137. <h3 class="docs-example">Python</h3>
  138. <p>
  139. If you are already using the
  140. <a href="http://docs.python-requests.org/en/master/">requests</a> library,
  141. it's convenient to also use it here:
  142. </p>
  143. {% include "front/snippets/python_requests.html" %}
  144. <p>
  145. Otherwise, you can use the <code>urllib</code> standard module.
  146. </p>
  147. {% include "front/snippets/python_urllib2.html" %}
  148. <p>
  149. You can include additional diagnostic information in the
  150. in the request body (for POST requests), or in the "User-Agent"
  151. request header:
  152. </p>
  153. {% include "front/snippets/python_requests_payload.html" %}
  154. <a name="ruby"></a>
  155. <h3 class="docs-example">Ruby</h3>
  156. {% include "front/snippets/ruby.html" %}
  157. <a name="node"></a>
  158. <h3 class="docs-example">Node</h3>
  159. {% include "front/snippets/node.html" %}
  160. <a name="php"></a>
  161. <h3 class="docs-example">PHP</h3>
  162. {% include "front/snippets/php.html" %}
  163. <a name="cs"></a>
  164. <h3 class="docs-example">C#</h3>
  165. {% include "front/snippets/cs.html" %}
  166. <a name="browser"></a>
  167. <h3>Browser</h3>
  168. <p>
  169. {% site_name %} includes <code>Access-Control-Allow-Origin:*</code>
  170. CORS header in its ping responses, so cross-domain AJAX requests
  171. should work.
  172. </p>
  173. {% include "front/snippets/browser.html" %}
  174. <a name="powershell"></a>
  175. <h3 class="docs-example">PowerShell</h3>
  176. <p>
  177. You can use <a href="https://msdn.microsoft.com/en-us/powershell/mt173057.aspx">PowerShell</a>
  178. and Windows Task Scheduler to automate various tasks on a Windows system.
  179. From within a PowerShell script it is also easy to ping {% site_name %}.
  180. </p>
  181. <p>Here is a simple PowerShell script that pings {% site_name %}.
  182. When scheduled to run with Task Scheduler, it will essentially
  183. just send regular "I'm alive" messages. You can of course extend it to
  184. do more things.</p>
  185. {% include "front/snippets/powershell.html" %}
  186. <p>Save the above to e.g. <code>C:\Scripts\healthchecks.ps1</code>. Then use
  187. the following command in a Scheduled Task to run the script:
  188. </p>
  189. <div class="highlight">
  190. <pre>powershell.exe -ExecutionPolicy bypass -File C:\Scripts\healthchecks.ps1</pre>
  191. </div>
  192. <p>In simple cases, you can also pass the script to PowerShell directly,
  193. using the "-command" argument:</p>
  194. {% include "front/snippets/powershell_inline.html" %}
  195. <a name="email"></a>
  196. <h3 class="docs-example">Email</h3>
  197. <p>
  198. As an alternative to HTTP/HTTPS requests,
  199. you can "ping" this check by sending an
  200. email message to <strong>{{ ping_email }}</strong>
  201. </p>
  202. <p>
  203. This is useful for end-to-end testing weekly email delivery.
  204. </p>
  205. <p>
  206. An example scenario: you have a cron job which runs weekly and
  207. sends weekly email reports to a list of e-mail addresses. You have already
  208. set up a check to get alerted when your cron job fails to run.
  209. But what you ultimately want to check is your emails <em>get sent and
  210. get delivered</em>.
  211. </p>
  212. <p>
  213. The solution: set up another check, and add its
  214. @hchk.io address to your list of recipient email addresses. Set its
  215. Period to 1 week. As long as your weekly email script runs correctly,
  216. the check will be regularly pinged and will stay up.
  217. </p>
  218. <h2 class="rule">When Alerts Are Sent</h2>
  219. <p>
  220. Each check has a configurable <strong>Period</strong> parameter, with the default value of one day.
  221. For periodic tasks, this is the expected time gap between two runs.
  222. </p>
  223. <p>
  224. Additionally, each check has a <strong>Grace</strong> parameter, with default value of one hour.
  225. You can use this parameter to account for run time variance of tasks.
  226. For example, if a backup task completes in 50 seconds one day, and
  227. completes in 60 seconds the following day, you might not want to get
  228. alerted because the backups are 10 seconds late.
  229. </p>
  230. <p>Each check can be in one of the following states:</p>
  231. <table class="table">
  232. <tr>
  233. <td>
  234. <span class="status icon-new"></span>
  235. </td>
  236. <td>
  237. <strong>New.</strong>
  238. A check that has been created, but has not received any pings yet.
  239. </td>
  240. </tr>
  241. <tr>
  242. <td>
  243. <span class="status icon-paused"></span>
  244. </td>
  245. <td>
  246. <strong>Monitoring Paused.</strong>
  247. You can resume monitoring of a paused check by pinging it.
  248. </td>
  249. </tr>
  250. <tr>
  251. <td>
  252. <span class="status icon-up"></span>
  253. </td>
  254. <td>
  255. <strong>Up.</strong>
  256. Time since last ping has not exceeded <strong>Period</strong>.
  257. </td>
  258. </tr>
  259. <tr>
  260. <td>
  261. <span class="status icon-grace"></span>
  262. </td>
  263. <td>
  264. <strong>Late.</strong>
  265. Time since last ping has exceeded <strong>Period</strong>,
  266. but has not yet exceeded <strong>Period</strong> + <strong>Grace</strong>.
  267. </td>
  268. </tr>
  269. <tr>
  270. <td>
  271. <span class="status icon-down"></span>
  272. </td>
  273. <td>
  274. <strong>Down.</strong>
  275. Time since last ping has exceeded <strong>Period</strong> + <strong>Grace</strong>.
  276. When check goes from "Late" to "Down", {% site_name %}
  277. sends you an alert.
  278. </td>
  279. </tr>
  280. </table>
  281. {% endblock %}
  282. {% block scripts %}
  283. {% compress js %}
  284. <script src="{% static 'js/jquery-2.1.4.min.js' %}"></script>
  285. <script src="{% static 'js/bootstrap.min.js' %}"></script>
  286. <script src="{% static 'js/clipboard.min.js' %}"></script>
  287. <script src="{% static 'js/snippet-copy.js' %}"></script>
  288. {% endcompress %}
  289. {% endblock %}