Skip to content

Instantly share code, notes, and snippets.

View cuu508's full-sized avatar

Pēteris Caune cuu508

View GitHub Profile
@cuu508
cuu508 / linemode.py
Created December 9, 2021 12:55
Experimental {% linemode %} and {% line %} template tags
class LineModeNode(Node):
def __init__(self, nodelist):
self.nodelist = nodelist
def render(self, context):
context["__lines__"] = []
self.nodelist.render(context)
return "\n".join(context["__lines__"])
# crontab -l
5,17,32,47 0-23 * * * logger -t experiment4 `date -Iseconds`
# journalctl --since "2021-03-28" -t experiment4
-- Logs begin at Sat 2021-03-27 00:00:14 GMT, end at Sun 2021-03-28 18:47:01 IST
Mar 28 00:05:01 debian experiment4[1594]: 2021-03-28T00:05:01+00:00
Mar 28 00:17:01 debian experiment4[1605]: 2021-03-28T00:17:01+00:00
Mar 28 00:32:01 debian experiment4[1613]: 2021-03-28T00:32:01+00:00
Mar 28 00:47:01 debian experiment4[1621]: 2021-03-28T00:47:01+00:00
Mar 28 02:00:01 debian experiment4[1629]: 2021-03-28T02:00:01+01:00
@cuu508
cuu508 / Caddyfile
Created June 4, 2021 09:20
Caddyfile which pings hc-ping.com every 5 minutes
:8081 {
reverse_proxy /secret/ hc-ping.com {
health_uri /8bc796e2-bbd5-4956-8e36-5f7f836b1696
health_interval 5m
health_headers {
Host hc-ping.com
}
}
respond "Hello world"
@cuu508
cuu508 / coverage_but_not_quite.py
Created April 15, 2021 11:10
Detect which lines can be removed from a .py file without breaking tests
import subprocess
path = "hc/api/views.py"
test_cmd = "python manage.py test --keepdb --parallel=8 -v 0"
original = open(path, "r").read()
report = open("report-%s.txt" % path.replace("/", "."), "w")
lines = original.split("\n")
for idx in range(0, len(lines)):
@cuu508
cuu508 / checkloadavg.sh
Last active September 26, 2018 08:06
Report load average to Healthchecks.io. If it's above the configured threshold, use the "/fail" endpoint
# Run this from cron every minute:
# * * * * * checkloadavg.sh
CODE=ca75b1df-4f4e-4711-8dc0-f21eeb4b0dac
AVG=$(cat /proc/loadavg | cut -d " " -f 1)
THRESHOLD=0.5
if (( $(echo "$AVG > $THRESHOLD" | bc -l) )); then
curl --retry 3 -d "Load average: $AVG" -X POST https://hc-ping.com/$CODE/fail
else
class IncludeNode(Node):
context_key = '__include_context'
# ... constructor etc. ...
def render(self, context):
try:
# ... template gets rendered here ...
except Exception as e:
if context.template.engine.debug:
<!-- Template emails/report-body-text.html -->
Hello,
This is a monthly report sent by healthchecks.io.
{% include "emails/summary-text.html %}
Cheers,
The healthchecks.io Team
def switch(tag, project_dir):
# Supervisor
supervisor_conf_path = "/etc/supervisor/conf.d/hc_%s.conf" % tag
upload_template("supervisor/hc.conf.tmpl",
supervisor_conf_path,
context=locals(),
backup=False,
use_sudo=True)
upload_template("supervisor/hc_sendalerts.conf.tmpl",
location ~ ^/(\w\w\w\w\w\w\w\w-\w\w\w\w-\w\w\w\w-\w\w\w\w-\w\w\w\w\w\w\w\w\w\w\w\w)/?$ {
add_header Content-Type text/plain;
postgres_pass database;
postgres_output value;
postgres_escape $ip $remote_addr;
postgres_escape $agent =$http_user_agent;
postgres_escape $body =$request_body;
def deploy():
""" Checks out code, prepares venv, runs management commands,
updates supervisor and nginx configuration. """
now = datetime.datetime.today()
now_string = now.strftime("%Y%m%d-%H%M%S")
project_dir = "/home/hc/webapps/hc-%s" % now_string
venv_dir = os.path.join(project_dir, "venv")
svn_url = "https://github.com/healthchecks/healthchecks/trunk"