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.

307 lines
10 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 staticfiles 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>Summary</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>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>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. <a name="crontab"></a>
  55. <h3>Crontab</h3>
  56. <p>
  57. When using cron, probably the easiest is to append a curl
  58. or wget call after your command. The scheduled time comes,
  59. and your command runs. If it completes successfully (exit code 0),
  60. curl or wget runs a HTTP GET call to the ping URL.
  61. </p>
  62. {% include "front/snippets/crontab.html" %}
  63. <p>With this simple modification, you monitor several failure
  64. scenarios:</p>
  65. <ul>
  66. <li>The whole machine has stopped working (power outage, janitor stumbles on wires, VPS provider problems, etc.) </li>
  67. <li>cron daemon is not running, or has invalid configuration</li>
  68. <li>cron does start your task, but the task exits with non-zero exit code</li>
  69. </ul>
  70. <p>Either way, when your task doesn't finish successfully, you will soon
  71. know about it.</p>
  72. <p>The extra options to curl are meant to suppress any output, unless it hits
  73. an error. This is to prevent cron from sending an email every time the
  74. task runs. Feel free to adjust the curl options to your liking.
  75. </p>
  76. <table class="table curl-opts">
  77. <tr>
  78. <th>&amp;&amp;</th>
  79. <td>Run curl only if <code>/home/user/backup.sh</code> succeeds</td>
  80. </tr>
  81. <tr>
  82. <th>
  83. -f, --fail
  84. </th>
  85. <td>Makes curl treat non-200 responses as errors</td>
  86. </tr>
  87. <tr>
  88. <th>-s, --silent</th>
  89. <td>Silent or quiet mode. Don't show progress meter or error messages.</td>
  90. </tr>
  91. <tr>
  92. <th>-S, --show-error</th>
  93. <td>When used with -s it makes curl show error message if it fails.</td>
  94. </tr>
  95. <tr>
  96. <th>--retry &lt;num&gt;</th>
  97. <td>
  98. If a transient error is returned when curl tries to perform a
  99. transfer, it will retry this number of times before giving up.
  100. Setting the number to 0 makes curl do no retries
  101. (which is the default). Transient error means either: a timeout,
  102. an FTP 4xx response code or an HTTP 5xx response code.
  103. </td>
  104. </tr>
  105. <tr>
  106. <th>&gt; /dev/null</th>
  107. <td>
  108. Redirect curl's stdout to /dev/null (error messages go to stderr,)
  109. </td>
  110. </tr>
  111. </table>
  112. <a name="bash"></a>
  113. <h3>Bash or a shell script</h3>
  114. <p>Both <code>curl</code> and <code>wget</code> examples accomplish the same
  115. thing: they fire off a HTTP GET method.</p>
  116. <p>
  117. If using <code>curl</code>, make sure it is installed on your target system.
  118. Ubuntu, for example, does not have curl installed out of the box.
  119. </p>
  120. {% include "front/snippets/bash_curl.html" %}
  121. {% include "front/snippets/bash_wget.html" %}
  122. <a name="python"></a>
  123. <h3>Python</h3>
  124. <p>
  125. If you are already using the
  126. <a href="http://docs.python-requests.org/en/master/">requests</a> library,
  127. it's convenient to also use it here:
  128. </p>
  129. {% include "front/snippets/python_requests.html" %}
  130. <p>
  131. Otherwise, you can use the <code>urllib</code> standard module.
  132. </p>
  133. {% include "front/snippets/python_urllib2.html" %}
  134. <a name="ruby"></a>
  135. <h3>Ruby</h3>
  136. {% include "front/snippets/ruby.html" %}
  137. <a name="node"></a>
  138. <h3>Node</h3>
  139. {% include "front/snippets/node.html" %}
  140. <a name="php"></a>
  141. <h3>PHP</h3>
  142. {% include "front/snippets/php.html" %}
  143. <a name="browser"></a>
  144. <h3>Browser</h3>
  145. <p>
  146. {% site_name %} includes <code>Access-Control-Allow-Origin:*</code>
  147. CORS header in its ping responses, so cross-domain AJAX requests
  148. should work.
  149. </p>
  150. {% include "front/snippets/browser.html" %}
  151. <a name="powershell"></a>
  152. <h3>PowerShell</h3>
  153. <p>
  154. You can use <a href="https://msdn.microsoft.com/en-us/powershell/mt173057.aspx">PowerShell</a>
  155. and Windows Task Scheduler to automate various tasks on a Windows system.
  156. From within a PowerShell script it is also easy to ping {% site_name %}.
  157. </p>
  158. <p>Here is a simple PowerShell script that pings {% site_name %}.
  159. When scheduled to run with Task Scheduler, it will essentially
  160. just send regular "I'm alive" messages. You can of course extend it to
  161. do more things.</p>
  162. {% include "front/snippets/powershell.html" %}
  163. <p>Save the above to e.g. <code>C:\Scripts\healthchecks.ps1</code>. Then use
  164. the following command in a Scheduled Task to run the script:
  165. </p>
  166. <div class="highlight">
  167. <pre>powershell.exe -ExecutionPolicy bypass -File C:\Scripts\healthchecks.ps1</pre>
  168. </div>
  169. <p>In simple cases, you can also pass the script to PowerShell directly,
  170. using the "-command" argument:</p>
  171. {% include "front/snippets/powershell_inline.html" %}
  172. <a name="email"></a>
  173. <h3>Email</h3>
  174. <p>
  175. As an alternative to HTTP/HTTPS requests,
  176. you can "ping" this check by sending an
  177. email message to <strong>{{ ping_email }}</strong>
  178. </p>
  179. <p>
  180. This is useful for end-to-end testing weekly email delivery.
  181. </p>
  182. <p>
  183. An example scenario: you have a cron job which runs weekly and
  184. sends weekly email reports to a list of e-mail addresses. You have already
  185. set up a check to get alerted when your cron job fails to run.
  186. But what you ultimately want to check is your emails <em>get sent and
  187. get delivered</em>.
  188. </p>
  189. <p>
  190. The solution: set up another check, and add its
  191. @hchk.io address to your list of recipient email addresses. Set its
  192. Period to 1 week. As long as your weekly email script runs correctly,
  193. the check will be regularly pinged and will stay up.
  194. </p>
  195. <h2>When Alerts Are Sent</h2>
  196. <p>
  197. Each check has a configurable <strong>Period</strong> parameter, with the default value of one day.
  198. For periodic tasks, this is the expected time gap between two runs.
  199. </p>
  200. <p>
  201. Additionally, each check has a <strong>Grace</strong> parameter, with default value of one hour.
  202. You can use this parameter to account for run time variance of tasks.
  203. For example, if a backup task completes in 50 seconds one day, and
  204. completes in 60 seconds the following day, you might not want to get
  205. alerted because the backups are 10 seconds late.
  206. </p>
  207. <p>Each check can be in one of the following states:</p>
  208. <table class="table">
  209. <tr>
  210. <td>
  211. <span class="status icon-new"></span>
  212. </td>
  213. <td>
  214. <strong>New.</strong>
  215. A check that has been created, but has not received any pings yet.
  216. </td>
  217. </tr>
  218. <tr>
  219. <td>
  220. <span class="status icon-paused"></span>
  221. </td>
  222. <td>
  223. <strong>Monitoring Paused.</strong>
  224. You can resume monitoring of a paused check by pinging it.
  225. </td>
  226. </tr>
  227. <tr>
  228. <td>
  229. <span class="status icon-up"></span>
  230. </td>
  231. <td>
  232. <strong>Up.</strong>
  233. Time since last ping has not exceeded <strong>Period</strong>.
  234. </td>
  235. </tr>
  236. <tr>
  237. <td>
  238. <span class="status icon-grace"></span>
  239. </td>
  240. <td>
  241. <strong>Late.</strong>
  242. Time since last ping has exceeded <strong>Period</strong>,
  243. but has not yet exceeded <strong>Period</strong> + <strong>Grace</strong>.
  244. </td>
  245. </tr>
  246. <tr>
  247. <td>
  248. <span class="status icon-down"></span>
  249. </td>
  250. <td>
  251. <strong>Down.</strong>
  252. Time since last ping has exceeded <strong>Period</strong> + <strong>Grace</strong>.
  253. When check goes from "Late" to "Down", {% site_name %}
  254. sends you an alert.
  255. </td>
  256. </tr>
  257. </table>
  258. {% endblock %}
  259. {% block scripts %}
  260. {% compress js %}
  261. <script src="{% static 'js/jquery-2.1.4.min.js' %}"></script>
  262. <script src="{% static 'js/bootstrap.min.js' %}"></script>
  263. <script src="{% static 'js/clipboard.min.js' %}"></script>
  264. <script src="{% static 'js/snippet-copy.js' %}"></script>
  265. {% endcompress %}
  266. {% endblock %}