-
Notifications
You must be signed in to change notification settings - Fork 34
/
dev-nodes
executable file
·67 lines (58 loc) · 2.42 KB
/
dev-nodes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
set -e
err=0
conf_file="$HOME"/.dev-nodes.monitor.list
[[ -n "$DBUS_SESSION_BUS_ADDRESS" ]] || \
export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/"$(id -u)"/bus
usage() {
local bin=${0##*/}
[[ $1 -eq 0 ]] || exec 1>&2
echo "Usage: $bin [config-file]"
echo
echo "Reads config-file with a list of device checks to run,"
echo " sends desktop notifications for any detected issues/mismatches."
echo "Default configuration file path: $conf_file"
echo "D-Bus socket for notifications: $DBUS_SESSION_BUS_ADDRESS"
exit ${1:-1}; }
[[ "$1" != -h && "$1" != --help && "$1" -le 1 ]] || usage 0
conf_file=${1:-$conf_file}
check_dev() {
dev=$1 act=$2; shift 2
case "$act" in
plug|missing)
[[ ! "$dev" =~ ^[A-Z]+= ]] || dev=$(findfs "$dev" 2>/dev/null ||:)
[[ ! -e "$dev" ]] || return 0 ;;
unplug|remove)
[[ ! "$dev" =~ ^[A-Z]+= ]] || dev=$(findfs "$dev" 2>/dev/null ||:)
[[ -e "$dev" ]] || return 0 ;;
net-cut) ip -br link show "$dev" >/dev/null 2>&1 || return 0 ;;
net-connect) ip -br link show "$dev" >/dev/null 2>&1 && return 0 ||: ;;
fido2-pins-[0-9]*)
[[ -e "$dev" ]] || return 0
re=$'(^|\n)[\r ]*pin retries: ([0-9]+)[\r ]*(\n|$)'
st=$(fido2-token -I "$dev" || echo 'pin retries: 0')
[[ "$st" =~ $re ]] && att_left=${BASH_REMATCH[2]} || att_left=0
[[ "$att_left" -lt "${act#fido2-pins-}" ]] || return 0 ;;
yk-piv-pins-[0-9]*=[0-9]*)
[[ -e "$dev" ]] || return 0
[[ "$act" =~ ^yk-piv-pins-([0-9]+)=([0-9]+)$ ]] && {
serial=${BASH_REMATCH[1]} att_chk=${BASH_REMATCH[2]}; } \
|| { serial=000 att_chk=999; }
re=$'(^|\n)[\r ]*PIN tries remaining: +([0-9]+)/'
st=$(ykman -d "$serial" piv info || echo 'PIN tries remaining: 0/0')
[[ "$st" =~ $re ]] && att_left=${BASH_REMATCH[2]} || att_left=0
[[ "$att_left" -lt "$att_chk" ]] || return 0 ;;
*) echo >&2 "ERROR: Unrecognized action '$dev $act ...'"; err=1; return
esac
read b2 junk < <(b2sum -l32 <<< "$dev"); u=cron-notify-"$b2"
systemctl -q --user is-active "$u" && return ||:
systemd-run --user --no-block -qG -u "$u" -- \
notify-send -w -u critical -t 0 -i circuit \
"[dev-check] <span color=\"red\" text_transform=\"uppercase\">$act</span>: $@" \
"Detected dev issue <big>[$act]</big>: <b><big>$@</big></b>"`
`"\nMachine: $HOSTNAME // Date-time: $(printf '%(%F %R)T' -1)"; }
while read dev act name; do
case "$dev" in ''|\#*) continue; esac
check_dev "$dev" "$act" "$name"
done < "$HOME"/.dev-nodes.monitor.list
exit $err