#!/bin/bash

#######
## Description: Simple plugin to check if a container's filesystem is read-only.
## Author: Sebastian Klee
## E-Mail: s.klee@timmehosting.de
## Version: 1.0
## Date: 09.02.2022
#######


# The command determines the filesystem to be checked and assigns it to the variable "file_system".
file_system=$(df -h | grep -E '/$' | awk '{print $1}')

# A filtered Output of /proc/mounts. The lines found by the filtering are counted and passed to the variable "output_ro_mounts".
output_ro_mounts=$(grep "$file_system" /proc/mounts | awk '{print $4}' | grep -c -E "^ro|,ro,|,ro$")

# If the value of the variable is bigger then 0, we know the filesystem is read-only. In this case we exit the script with the exit code 2, which results to a critical in our monitoring.
if [ "$output_ro_mounts" -gt 0 ]; then
  echo "Dateisystem ist Read-Only!"
  grep "$file_system" /proc/mounts
  exit 2
fi

echo "Alles ok!"
exit 0

