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 / 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 / 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 / _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 / 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
@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 / health-blink.sh
Last active April 3, 2021 18:42
Healthchecks.io + blink(1)
@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 / 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 / MapSpeedTest.java
Created March 4, 2020 06:22
Benchmark of map implementation access speeds
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
@dimo414
dimo414 / description.md
Created March 4, 2020 06:06
St. Petersburg Paradox Simulation

The St. Petersburg paradox, in a nutshell, is: why would you refuse to play a game with an unlimited expected payout? Shouldn't you be willing to pay any finite price to play, since your expected return is infinite?

This simulation explores what the Stanford Encyclopedia refers to as the "Average Win" explanation, that:

Expected value is in effect average payback in the long run.... [I]f you're going to play St. Petersburg with a substantial fee per game, you're likely to have to play a very very long time before you come out with a positive net payoff. Ordinary practical considerations thus apply... You may have to play a very long time before winning once. But worse: in a series of losses, the amount needed for the next bet rises. How much bankroll would you need to guarantee success...? Whatever bankroll you take into the casino, there's a chance that this will not be enough.

The