Skip to content

Instantly share code, notes, and snippets.

@nijave
nijave / pyiostat.py
Last active May 12, 2021 21:20
Rudimentary iostat in Python to demo sysfs block device stats
# pyiostat
import os
import time
from pprint import pprint
# https://www.kernel.org/doc/html/latest/block/stat.html
stat_labels_extended = [
# reads
@nijave
nijave / gigabyte-x470-gaming-7-sensors.sh
Last active April 7, 2024 10:27
Setup sensors for Gigabyte X470 Gaming 7 w/ Fedora
#!/bin/sh
# The chip info can be found via sensors-detect in lm_sensors
# Driver `to-be-written':
# * ISA bus, address 0xa40
# Chip `ITE IT8686E Super IO Sensors' (confidence: 9)
# Driver `it87':
# * ISA bus, address 0xa60
# Chip `ITE IT8792E Super IO Sensors' (confidence: 9)
@nijave
nijave / python_apache_logs.py
Last active May 4, 2021 02:08 — forked from sumeetpareek/python_apache_logs.py
A very simple Apache access log parser in Python
#!/usr/bin/env python3
import json
import re
import sys
parts = [
r'(?P<host>\S+)', # host %h
r'\S+', # indent %l (unused)
r'(?P<user>\S+)', # user %u
@nijave
nijave / s3_access_log_summary.py
Last active April 30, 2021 17:25
Shows a summary of information from AWS S3 Access logs assuming they're in the same directory
#!/usr/bin/env python3
import os
import re
# Reference https://stackoverflow.com/questions/7961316/regex-to-split-columns-of-an-amazon-s3-bucket-log
parse = re.compile(r"([^\s\"\[\]]+|\[[^\]\[]+\]|\"[^\"]+\")\s+")
logs = (
l.strip()
@nijave
nijave / logging.py
Last active April 13, 2021 02:31
Python logger setup (with alternate format)
import logging
import os
log = logging.getLogger(__file__ if __name__ == "__main__" else __name__)
logging.basicConfig(
level=getattr(logging, os.environ.get("LOG_LEVEL", "INFO").upper()),
format="%(asctime)s [%(levelname)s]: %(message)s",
datefmt="%Y-%m-%dT%H:%M:%S%z",
)
@nijave
nijave / dkim-rotate.py
Created April 1, 2021 01:02
A simple opendkim key rotator
#!/usr/bin/env python3
# Note: many os-related methods in python3.5 don't accept
# pathlib.Path and require manual conversion to string, first
import os
import random
import re
import shutil
import subprocess
@nijave
nijave / sas_devices.py
Last active December 7, 2021 11:33
Convert sas2ircu output to json for easier use in other tools
#!/usr/bin/env python3
"""
Usage:
./sas_devices.py - json output
./sas_devices.py --summary - human output with locations and serial
Warning: sas2ircu invocations have occassionally caused kernel
panics on previous FreeBSD installations
Related https://redmine.ixsystems.com/issues/14859
@nijave
nijave / verify.sh
Created February 14, 2021 04:17
OpenSSL Decrypt with x509 Public Key
encrypted_file=some_file
cert=certificate.pem
openssl x509 -pubkey -noout -in $cert -out certificate.key
openssl rsautl -pubin -inkey certificate.key -in some_file
@nijave
nijave / json_parse.sh
Last active December 22, 2020 01:56
Parallel parse json files (Cloudtrail Logs)
#1/bin/sh
# --line-buffer to prevent json from accidentally getting split in the middle
# -n 128 cat-ing large quantities of files is faster since the programs aren't re-invoked as often (too high and you risk not using all the threads in the final batches)
# -P 125% slightly over subscribe the CPU to lessen the chance of wasting cycles if something becomes blocked
# -q -m 4M make a small file buffer to smooth out I/O (don't make it too large as a single cat process could starve the other threads)
# -rc for single line per json object and "raw" output
# final jq is just to format and colorize the results
# zcat can be used to read json.gz files. This examples read off zfs compressing with zstd
@nijave
nijave / jupyter.sh
Created October 31, 2020 16:10
Compile code required for Jupyter on FreeBSD (jupyter on opnsense)
# Set up a jail
iocage create -r 12.1-RELEASE -n wheelhouse
iocage set dhcp=on bpf=on vnet=on wheelhouse
iocage start wheelhouse
# Build some wheels inside the jail
iocage console wheelhouse
env ASSUME_ALWAYS_YES=yes pkg update
pkg install -y python38 libzmq4 bash