prometheus related updates
[distro-setup] / brc
diff --git a/brc b/brc
index 470c143f87a160b931ec323aeaff1840fbcc705f..97f22e1ca96ecbdb65bdabb69aea925305850f1c 100644 (file)
--- a/brc
+++ b/brc
@@ -224,6 +224,7 @@ export PROFILE_TASKS_TASK_OUTPUT_LIMIT=100
 # i for insensitive. the rest from
 # X means dont remove the current screenworth of output upon exit
 # R means to show colors n things
+# a useful flag is -F aka --quit-if-one-screen
 export LESS=RXij12
 export SYSTEMD_LESS=$LESS
 
@@ -260,6 +261,8 @@ if [[ $SOE ]]; then
 fi
 
 
+
+
 mysrc() {
   local path dir file
   path=$1
@@ -276,6 +279,16 @@ mysrc() {
 mysrc /a/bin/small-misc-bash/ll-function
 mysrc /a/bin/distro-functions/src/package-manager-abstractions
 
+# things to remember:
+# ALT-C - cd into the selected directory
+# CTRL-T - Paste the selected file path into the command line
+#
+# good guide to some of its basic features is the readme file
+# https://github.com/junegunn/fzf
+
+# if [[ -s /usr/share/doc/fzf/examples/key-bindings.bash ]]; then
+#   source /usr/share/doc/fzf/examples/key-bindings.bash
+# fi
 
 # * functions
 
@@ -435,7 +448,7 @@ cl() {
   # we have ~33 buttons as of this writing, so lets
   # prune down the history every once in a while.
   if (( start > 500 )); then
-    tac ~/.cdirs | awk '!seen[$0]++' | head -n 200 | sponge ~/.cdirs
+    tac ~/.cdirs | awk '!seen[$0]++' | head -n 200 | tac | sponge ~/.cdirs || [[ $? == 141 ]]
   fi
 
   for (( j=$start; j >= 0; j-- )); do
@@ -458,7 +471,7 @@ cl() {
   fi
   read -r -N 1 input
   if [[ $input != $'\n' ]]; then
-    c ${buttondirs[$input]}
+    c "${buttondirs[$input]}"
   fi
 }
 # back list
@@ -703,7 +716,7 @@ bwm() {
 }
 
 
-# for running in a fai rescue
+# for running in a fai rescue. iank specific.
 kdrescue() {
   d=vgata-Samsung_SSD_850_EVO_2TB_S2RLNX0J502123D
   for f in $d vgata-Samsung_SSD_870_QVO_8TB_S5VUNG0N900656V; do
@@ -836,6 +849,10 @@ cat-new-files() {
 
 }
 
+chownme() {
+  s chown -R $USER:$USER "$@"
+}
+
 # shellcheck disable=SC2032
 chown() {
   # makes it so chown -R symlink affects the symlink and its target.
@@ -856,6 +873,12 @@ cim() {
 d() { builtin bg "$@"; }
 ccomp bg d
 
+# f would be more natural, but i already am using it for something
+z() { builtin fg "$@"; }
+ccomp fg z
+
+x() { builtin kill %%; }
+
 dc() {
   diff --strip-trailing-cr -w "$@"   # diff content
 }
@@ -869,6 +892,26 @@ despace() {
   done
 }
 
+# df progress
+# usage: dfp MOUNTPOINT [SECOND_INTERVAL]
+# SECOND_INTERVAL defaults to 90
+dfp() {
+  # mp = mountpoint
+  local a b mp interval
+  mp=$1
+  interval=${2:-90}
+  if [[ ! $mp ]]; then
+    echo "dfp: error, missing 1st arg" >&2
+    return 1
+  fi
+  while true; do
+    a=$(df --output=used $mp | tail -n1)
+    sleep $interval
+    b=$(df --output=used $mp | tail -n1)
+    printf "used mib: %'d  mib/min: %s\n" $(( b /1000 )) $(( (b-a) / (interval * 1000 / 60 ) ))
+  done
+}
+
 # get ipv4 ip from HOST. or if it is already a number, return that
 hostip() {
   local host="$1"
@@ -916,13 +959,18 @@ digdiff() {
   diff -u /tmp/digdiff <(digsort $s2 "$@")
 }
 
+# date in a format i like reading
 dt() {
   date "+%A, %B %d, %r" "$@"
 }
 dtr() {
   date -R "$@"
 }
-ccomp date dt dtr
+# date with all digits in a format i like
+dtd() {
+  date +%F_%T% "$@"
+}
+ccomp date dt dtr dtd
 
 dus() { # du, sorted, default arg of
   du -sh ${@:-*} | sort -h
@@ -942,7 +990,8 @@ ea() {
     printf "%s" "${arg}" |& hexdump -C
   done
 }
-# echo vars. print var including escapes, etc
+
+# echo variables. print var including escapes, etc, like xxd for variable
 ev() {
   if (( ! $# )); then
     echo no args
@@ -977,6 +1026,9 @@ etail2() {
 ccomp tail etail etail2
 
 
+showkeys() {
+  ssh "$@" cat .ssh/authorized_keys{,2}
+}
 
 
 # print exim old pids
@@ -1063,6 +1115,20 @@ econfdev() {
   update-exim4.conf -d /tmp/edev/etc/exim4 -o /tmp/edev/e.conf
 }
 
+# exim grep in
+# show important information about incoming mail in the exim log
+egrin() {
+  sed -rn '/testignore|jtuttle|eximbackup/!s/^[^ ]+ ([^ ]+) [^ ]+ [^ ]+ <= ([^ ]+).*T="(.*)" from (<[^ ]+> .*$)/\1 \4\n \3/p' <${1:-/var/log/exim4/mainlog}
+}
+
+# 2nd line is message-id:
+egrinid() {
+  sed -rn '/testignore|jtuttle|eximbackup/!s/^[^ ]+ ([^ ]+) [^ ]+ [^ ]+ <= ([^ ]+).* id=([^ ]+) T="(.*)" from (<[^ ]+> .*$)/\1 \5\n \3\n \4/p' <${1:-/var/log/exim4/mainlog}
+}
+etailin() {
+  tail -F /var/log/exim4/mainlog | sed -rn '/testignore|jtuttle|eximbackup/!s/^[^ ]+ ([^ ]+) [^ ]+ [^ ]+ <= ([^ ]+).*T="(.*)" from (<[^ ]+> .*$)/\1 \4\n \3/p'
+}
+
 
 
 
@@ -1081,12 +1147,74 @@ faf() { # find all files. use -L to follow symlinks
        -o -name .undo-tree-history -prune \) -type f 2>/dev/null
 }
 
-# full path without resolving symlinks
+# usage ffconcat FILES_TO_CONCAT OUTPUT_FILE
+ffconcat() {
+  local tmpf
+  tmpf=$(mktemp)
+  printf "file '%s'\n" "$1" >$tmpf
+  while (( $# > 1 )); do
+    shift
+    printf "file '%s'\n" "$1" >>$tmpf
+  done
+  # https://trac.ffmpeg.org/wiki/Concatenate
+  ffmpeg -f concat -safe 0 -i $tmpf -c copy "$1"
+  rm $tmpf
+}
+ffremux() {
+  local tmpf tmpd
+  if (( $# == 0 )); then
+    echo ffremux error expected args >&2
+    return 1
+  fi
+  tmpd=$(mktemp -d)
+  for f; do
+    tmpf=$tmpd/"${f##*/}"
+    ffmpeg -i "$f" -c:v copy -c:a copy $tmpf
+    cat $tmpf >"$f"
+  done
+  rm -r $tmpd
+}
+
+
+
+# absolute path of file/dir without resolving symlinks.
+#
+# This is what realpath -s does in most cases, but sometimes it
+# actually resolves symlinks, at least when they are in /.
+#
+# Note, if run on a dir, if the final component is relative, it won't
+# resolve that. Use the below fpd for that.
+#
+# note: we could make a variation of this which
+# assigns to a variable name using eval, so that we don't have to do
+# x=$(fp somepath), which might save subshell overhead and look nice,
+# but I'm not going to bother.
 fp() {
-  local dir base
-  base="${1##*/}"
-  dir="${1%$base}"
-  printf "%s/%s\n" $(cd $dir; pwd) "$base"
+  local initial_oldpwd initial_pwd dir base
+  initial_oldpwd="$OLDPWD"
+  initial_pwd="$PWD"
+  if [[ $1 == */* ]]; then
+    dir="${1%/*}"
+    base="/${1##*/}"
+    # CDPATH because having it set will cause cd to possibly print output
+    CDPATH= cd "$dir"
+    printf "%s%s\n" "$PWD" "$base"
+    CDPATH= cd "$initial_pwd"
+    OLDPWD="$initial_oldpwd"
+  else
+    printf "%s/%s\n" "$PWD" "$1"
+  fi
+}
+# full path of directory without resolving symlinks
+fpd() {
+  local initial_oldpwd initial_pwd dir
+  initial_oldpwd="$OLDPWD"
+  initial_pwd="$PWD"
+  dir="$1"
+  CDPATH= cd "$dir"
+  printf "%s%s\n" "$PWD" "$base"
+  cd "$initial_pwd"
+  OLDPWD="$initial_oldpwd"
 }
 
 
@@ -1507,6 +1635,15 @@ ccomp mkdir mkc
 mkct() {
   mkc $(mktemp -d)
 }
+# mkdir the last arg, cp the rest into it
+mkcp() {
+  mkdir -p "${@: -1}"
+  cp "${@:1:$#-1}" "${@: -1}"
+}
+mkmv() {
+  mkdir -p "${@: -1}"
+  mv "${@:1:$#-1}" "${@: -1}"
+}
 
 mkt() { # mkdir and touch file
   local path="$1"
@@ -2260,6 +2397,13 @@ pson() {
   fi
 }
 
+# prometheus node curl
+pnodecurl() {
+  local host
+  host=${1:-127.0.0.1}
+  s curl --cert-type PEM --cert /etc/prometheus/ssl/prometheus_cert.pem --key /etc/prometheus/ssl/prometheus_key.pem --cacert /etc/prometheus/ssl/prom_node_cert.pem --resolve prom_node:9100:$host -v https://prom_node:9100/metrics
+}
+
 tx() { # toggle set -x, and the prompt so it doesnt spam
   if [[ $- == *x* ]]; then
     set +x
@@ -2362,7 +2506,7 @@ s/^\Wcapability: (.*)/\1/;Ta;h;b
 # and changing directory, so we don't have any open
 # directories or files that could cause problems when
 # remounting.
-z() {
+zr() {
   local tmp
   tmp=$(type -p "$1")
   if [[ $tmp ]]; then
@@ -2436,6 +2580,7 @@ spark()
   _spark_echo
 }
 
+pdfwc() { local f; for f; do echo "$f" $(pdfinfo "$f" | awk '/^Pages:/ {print $2}'); done }
 
 # * misc stuff
 
@@ -2522,6 +2667,10 @@ if [[ $- == *i* ]]; then
     if [[ $MAIL_HOST && $MAIL_HOST != "$HOSTNAME" ]]; then
       ps_char="@ $ps_char"
     fi
+    jobs_char=
+    if [[ $(jobs -p) ]]; then
+      jobs_char='j\j '
+    fi
     # We could test if sudo is active with sudo -nv
     # but then we get an email and log of lots of failed sudo commands.
     # We could turn those off, but seems better not to.
@@ -2531,7 +2680,7 @@ if [[ $- == *i* ]]; then
     if [[ ! $HISTFILE ]]; then
       ps_char="NOHIST $ps_char"
     fi
-    PS1="${PS1%"${PS1#*[wW]}"} $psudo\[$ps_color\]$ps_char\[$term_nocolor\] "
+    PS1="${PS1%"${PS1#*[wW]}"} $jobs_char$psudo\[$ps_color\]$ps_char\[$term_nocolor\] "
 
     # set titlebar. instead, using more advanced
     # titelbar below
@@ -2547,18 +2696,22 @@ if [[ $- == *i* ]]; then
     _title_escape="\033]0;"
   fi
 
+  # make the titlebar be the last command and the current directory.
   settitle () {
-    # this makes it so we show the current command if
-    # one is running, otherwise, show nothing
 
-    if [[ $1 == prompt-command ]]; then
+
+    # These are some checks to help ensure we dont set the title at
+    # times that the debug trap is running other than the case we
+    # want. Some of them might not be needed.
+    if (( ${#FUNCNAME[@]} != 1 || ${#BASH_ARGC[@]} != 2 || $BASH_SUBSHELL != 0 )); then
       return 0
     fi
-    if (( ${#BASH_ARGC[@]} == 1 && BASH_SUBSHELL == 0 )); then
-      echo -ne "$_title_escape ${PWD/#$HOME/~} "
-      printf "%s" "$*"
-      echo -ne "\007"
+    if [[ $1 == prompt-command ]]; then
+      return 0
     fi
+    echo -ne "$_title_escape ${PWD/#$HOME/~} "
+    printf "%s" "$*"
+    echo -ne "\007"
   }
 
   # note, this wont work: