Skip to content

Instantly share code, notes, and snippets.

@benyanke
Created November 24, 2020 16:22
Show Gist options
  • Save benyanke/a18bd02036b7fac0f352e1d58c231a70 to your computer and use it in GitHub Desktop.
Save benyanke/a18bd02036b7fac0f352e1d58c231a70 to your computer and use it in GitHub Desktop.
Custom text exporters for prometheus
#!/bin/bash
# Reports on all filesystems declared in fstab, and if they are mounted or not - to detect unmounted filesystems
# NOTE: exporter info to stdout, human readable info to stderr
# in normal operation, pipe stdout to the datafile and stderr to null or syslog
# TODO : add a second error metric so that errors in this script are reported
tmpfile="$(mktemp)"
finalOut="$(mktemp)"
echo "# HELP node_filesystem_mounts_fstab_mounted Indicates if a filesystem in fstab is currently mounted" > "$finalOut"
echo "# TYPE node_filesystem_mounts_fstab_mounted gauge" >> "$finalOut"
# Get a list of all significant (non-comment/dupe) lines
cat /etc/fstab | grep -vE "^#" | uniq | sort > "$tmpfile"
# Loop through the lines
while read -r line
do
blockDevice="$(echo "$line" | awk '{print $1}')"
mountPoint="$(echo "$line" | awk '{print $2}')"
type="$(echo "$line" | awk '{print $3}')"
options="$(echo "$line" | awk '{for(i=4;i<=NF;i++){out=out" "$i}; print out}')"
# skip things which are not valid mounts
if [[ "$blockDevice" == "" || "$mountPoint" == "" ]] ; then
echo "Skipping the following line as invalid. Note, it's probably a blank line." >>/dev/stderr
echo "$line" >>/dev/stderr
echo "" >>/dev/stderr
continue
fi
echo "Found filesystem line to parse" >>/dev/stderr
echo "BLOCK: $blockDevice" >>/dev/stderr
echo "MOUNT: $mountPoint ">>/dev/stderr
echo "TYPE: $type" >>/dev/stderr
echo "OPTIONS: $options" >>/dev/stderr
echo "" >>/dev/stderr
# Check if mounted
# More strict, breaks with uuids
#mount | grep -E "^$blockDevice on $mountPoint type $type" &> /dev/null
# ignores uuids
mount | grep -E "^.* $mountPoint type $type" &> /dev/null
if [[ "$?" == "0" ]] ; then
isMounted="1"
else
isMounted="0"
fi
echo "node_filesystem_mounts_fstab_mounted{device=\"$blockDevice\",mountpoint=\"$mountPoint\",type=\"$type\"} $isMounted" >> "$finalOut"
#printf '%s\n' "$line"
done < <(cat $tmpfile)
cat $finalOut
rm $tmpfile
rm $finalOut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment