Skip to content

Instantly share code, notes, and snippets.

@mharsch
mharsch / prep.sh
Last active December 29, 2023 13:30
wipe and prep drives for replotting
#!/bin/bash
# Script for wiping old plot directories and prepping drives for new plots
# Pause between steps in case something goes wrong (Ctrl-C to bail out)
RANGE=sdb[g-j]
DEVS=/dev/$RANGE
DEVS_SCHED=/sys/block/${RANGE}/queue/scheduler
PARTS=/dev/${RANGE}1
OLD_DIRS=/plots/${RANGE}1
@mharsch
mharsch / gist:ce4ec394a3a058b7d0d3d25f52595f13
Created December 18, 2023 19:05
enable pc speaker control via beep when logged in remotely (Ubuntu 22.04)
This will save you from going down a rather deep documentation rabbit hole:
1.) enable pcspkr driver by removing blacklist entry for pcspkr in /etc/modprobe.d/blacklist.conf
2.) create user group 'beep' for controlling permissions to speaker
sudo addgroup --system beep
3.) Add your user account to the new beep group (logout and log back in after this step)
sudo usermod -aG beep $USER
4.) change default permissions on speaker device to allow members of group 'beep' to access speaker.
Comment out previous entry and add the following line to /usr/lib/udev/rules.d/70-pcspkr-beep.rules
@mharsch
mharsch / kasa_grafana.md
Created September 21, 2023 15:12
use kasa smart plug with prometheus / grafana

Uses this container to scrape power data from the reverse-engineered kasa API running on the smart plugs (e.g. HS100).

Docker compose example

compose.yml

version: '3.8'
@mharsch
mharsch / gist:e942fc0f0092f69ea5904a727542340f
Last active June 16, 2023 21:44
Configure systemd watchdog on raspberry pi to reboot if it can't ping a certain host

Most online instructions for Linux watchdog refer to the 'watchdog' package which is best described here

The watchdog configuration described here uses systemd, and the two methods are mutually exclusive, so if you've previously setup the above watchdog, you must first uninstall it.

sudo apt remove watchdog

Our goal is to add a systemd service that continuously pings a known host (or IP address)

/**
* V. Hunter Adams
* DDS of sine wave on MCP4822 DAC w/ ISR
*
* Modified example code from Raspberry Pi
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*
GPIO 8 (pico pin 11) MCP4725 SDA
@mharsch
mharsch / alpha4_sim_results.txt
Created April 22, 2023 17:40
alpha4 GPU harvesting simulate results for RTX 3080 Ti, 3070, 3060
RTX 3080 Ti System:
===================
OS="Ubuntu 22.04.2 LTS 5.15.0-70-generic"
SystemVendor/Model="Gigabyte Technology Co., Ltd. B550 AORUS MASTER"
CPU="1x AMD Ryzen 9 5950X 16-Core Processor (C:16|T:32)"
GPU="NVIDIA GeForce RTX 3080 Ti"
GPUSKU="Ampere"
GPUArch="N/A"
DriverVersion="530.30.02"
PCIeLink="PCIe 4x16"
@mharsch
mharsch / mt_viki_remote
Created February 18, 2023 18:32
Arduino replaces MT-VIKI KVM remote module for changing / polling selected KVM input
#include <math.h>
// connected to 'D+' pin on mini USB
const int input_pin = 11;
// connected to 'D-' pin on mini USB
const int output_pin = 12;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
@mharsch
mharsch / hostname_watchdog.md
Last active June 16, 2023 21:37
setup linux to reboot if the host loses access to hostname (not IP)

Deprecated

I no longer recommend the below method. For my updated recommendation, see this gist

Setup Linux to reboot if it loses access to a certain host by hostname (not IP)

The linux watchdog service can be configured to run certain 'liveness' tests periodically, and then take some action (such as reboot) if a test fails (and doesn't recover) for some period of time.

There is a built-in test for pinging an IP address (i.e. 'ping' directive), but you may instead need to test access to a hostname

@mharsch
mharsch / gist:fc04ef0d236ebb66965e1a0d0232ece9
Last active May 13, 2022 18:09
user watchdog script for flooding system log errors
If you have a failure mode that can be diagnosed by a high rate of messages flooding the system log,
you can use the script below in conjunction with the watchdog service to detect the condition
and reboot if it persists. This is a pretty big hammer, but potentially better than just rebooting on
loss of network or some other arbitrary liveness test.
add a 'test-binary' line to /etc/watchdog.conf pointing to this script (will be treated as V0 test script
with no corresponding repair script). Also, set 'interval' to something like 20 seconds (at least greater than
the sample period in the script).
You can convince yourself it's working (once the watchdog service has been restarted with the above config)
@mharsch
mharsch / gist:b1108372982a1921235563ee5f603807
Last active December 30, 2023 16:38
run a service on a schedule (e.g. off-peak electricity rates)
We want to run a long-running process during certain hours, days-of-the-week, etc.
We want the service to start on a schedule, and be stopped on a corresponding schedule.
Example: Residential electric rates are based on Time of Use (TOU) such that Weekdays
from 1p to 7p are considered 'Peak Hours' when rates are much higher than all other times.
Our long running process uses significant electricity, so we want to stop running it
once we reach the Peak Hours period, and resume running once the Peak period is over.
1.) We create a systemd service file for the long running process (foo.service)
2.) We create a systemd timer unit associated with the service (foo.timer). This timer handles