Skip to content

Instantly share code, notes, and snippets.

View dimo414's full-sized avatar
🦀
Playing with Rust

Michael Diamond dimo414

🦀
Playing with Rust
View GitHub Profile
@dimo414
dimo414 / heartbeat.sh
Last active March 27, 2024 20:21
Basic Server Heartbeat Script
#!/bin/bash
# Source: https://gist.github.com/dimo414/10d6f162fb6d72f517a041d28d92314f
#
# This is a basic heartbeat monitoring script suitible for adding to
# a machine's crontab to receive alerts (via https://healthchecks.io)
# when the machine is unhealthy.
#
# I use it to keep an eye on my headless Raspberry Pi, but it should
# work for most Linux machines.
#
@dimo414
dimo414 / README.md
Last active June 5, 2023 06:53
Raspberry Pi Camera + mjpg-streamer systemd setup

This article includes setup instructions for the Raspberry Pi camera, but doesn't capture setting it up as a service (even though a few lines up it sets up OctoPrint as a service), and instead provides a pair of relatively-complex scripts to be run at startup via /etc/rc.local.

Setup

  • copy webcam.sh to the mjpg-streamer directory (or wherever you'd like, just update the MJPG_DIR variable)
  • copy pi-webcam.service to /etc/systemd/system/ (requires sudo), ensure ExecStart points to the correct path
@dimo414
dimo414 / _README.md
Last active November 28, 2022 07:25
Bash array expansion patterns for use with -u

Expanding Bash arrays safely with set -u

Prior to Bash 4.4 set -u treated empty arrays as "unset", and terminates the process. There are a number of possible workarounds using array parameter expansion, however almost all of them fail in certain Bash versions.

This gist is a supplement to this StackOverflow post.

@dimo414
dimo414 / getopts.sh
Last active March 14, 2022 12:25
getopts function helper
#!/bin/bash
#
# Helper utility to simplify using Bash's getopts utility, and make it usable with functions.
#
# Example usage:
# foo() {
# local _usage=... # optional usage string
# eval "$(parse_opts 'ab:f:v')" # provide a standard getopts optstring
# echo "f is $f" # opts are now local variables
# if (( a )); then # check boolean flags with (( ... ))
@dimo414
dimo414 / classes-template.class.php
Created November 16, 2014 20:42
Example code for creating a flexible PHP template with shared header and footer behavior.
<?php
/*
Enables a PHP page to abstract out the site design (the template) from
the page design (the content).
Generally, a page will have a common header and footer (sidebars and the
like would live in one or the other). Following this pattern, a new page
can be created (after including the necessary files of course)) like so:
@dimo414
dimo414 / default_detect.py
Created March 5, 2020 05:00
Detecting whether an argparse argument was set explicitly or not
class DefaultCapturingNamespace(argparse.Namespace):
"""An argparse Namespace that tracks the number of times the given argument
has been set. In theory this can allow you to distinguish between values
set by argparse (e.g. default values) and those specified by the user.
"""
def __init__(self, **kwargs):
type_name = type(self).__name__
argparse.Namespace.__setattr__(self, '_%s__original_values' % type_name, {})
argparse.Namespace.__setattr__(self, '_%s__set_counts' % type_name, {})
@dimo414
dimo414 / health-blink.sh
Last active April 3, 2021 18:42
Healthchecks.io + blink(1)
@dimo414
dimo414 / health-plex.sh
Last active April 3, 2021 18:42
Plex Healthcheck
#!/bin/bash
# Checks that a Plex server is up. For devices on a local network your
# router may have created [hostname].local or [hostname].lan DNS entries;
# otherwise you can pass an IP address or ensure your server is remotely
# accessible (https://support.plex.tv/articles/200931138/).
#
# See also
# https://old.reddit.com/r/PleX/comments/7qolre/what_do_others_do_to_monitor_plex_health/
# https://support.plex.tv/articles/201638786-plex-media-server-url-commands/
#
@dimo414
dimo414 / GCChurn.java
Created March 26, 2013 04:34
GC Churn Java application, intended to cause high amounts of garbage collection for benchmarking
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Runnable which causes a heavy yet stable amount of GC; useful for
* benchmarking applications under load.
*
@dimo414
dimo414 / build.md
Last active April 30, 2020 07:04
Run Slack Dogebot on Raspberry Pi

Build the Luit/dogebot binary (I prefer to do so in a Docker container) for ARM:

$ docker run -v /tmp:/mnt/tmp -it debian:testing
docker$ apt-get update && apt-get install -y git golang
# Ensure build dependencies are available locally
docker$ go get github.com/Luit/dogebot/src/dogebot
docker$ git clone https://github.com/Luit/dogebot
docker$ cd dogebot/src/dogebot
# Build a Raspberry Pi binary