#!/bin/bash

echo '<<<fsls>>>'

FSLS_STDIN=""
read -t 0 -N 0 && FSLS_STDIN=$(cat)

MOUNTED_CHECK_INTERVAL=120
UNMOUNTED_CHECK_INTERVAL=1440

test -f /etc/check_mk_agent.conf && . /etc/check_mk_agent.conf

# ziskanie mountov ktore budeme kontrolovat
# 1 lokalne zvazky
mounts=$( awk '$1 !~ /#/ && $3 !~ /swap/ && ($4 !~ /noauto/ || $3 ~ /gpfs/) {print $2}' /etc/fstab 2>/dev/null )
# 2 /auto/*
mounts=$mounts' '$( awk '$1 !~ /#/ && $0 !~ /^$/ {print "/auto/"$1}' /etc/auto.auto 2> /dev/null )
mounts=$mounts' '$( awk '$1 !~ /#/ && $0 !~ /^$/ {print "/auto/"$1}' /etc/auto.meta-nfs 2> /dev/null )
# 3 /afs/ics.muni.cz
[ -d "/afs/ics.muni.cz" ] && mounts="$mounts /afs/ics.muni.cz"

[ "$only_local_fsls" = true ] && mounts=""
# 4 lokalni cfg
[ -f "/etc/check_mk/fsls.cfg" ] && mounts="$mounts $(cat /etc/check_mk/fsls.cfg 2>/dev/null | sed 's/#.*$//g')"

still_running="no"
pid_fsls=$(cat /tmp/pid.fsls_$(echo $SSH_CLIENT | awk '{print $1}') 2>/dev/null)
if [ ${#pid_fsls} -gt 0 ] ; then
    ps ax | awk -v PID=$pid_fsls '{if($1==PID && $5=="/bin/bash")exit 1}' > /dev/null 2> /dev/null
    if [ $? -eq 1 ] ; then
        still_running="$pid_fsls"
    fi
fi

mounted="$(mount 2>/dev/null | awk '{print $3}')"

mounts_to_ls=""
for mount in $mounts ; do

    if [ "$still_running" != "no" ]; then
        echo "$mount waiting between checks; pid ${still_running} still exists"
        continue
    fi

    mount_disabled=$(echo "$FSLS_STDIN" | awk -v mount=$mount '{if (mount ~ $1 && $2 == "disabled") {print "yes"; exit}}')
    
    mountFile="${mount//\//\\\\}_$(echo $SSH_CLIENT | awk '{print $1}')"
    

    mount_message=" fs unmounted, last check: $(stat -c %y /tmp/output.ls_$mountFile 2>/dev/null)"
    is_mounted=0
    for i in $mounted ; do
        if [[ "$mount" == "$i" ]] ; then
           is_mounted=1
           mount_message=" fs mounted, last check: $(stat -c %y /tmp/output.ls_$mountFile 2>/dev/null)"
        fi
    done

    if  [[ "$mount" =~ "auto" ]] && [ -e /var/spool/pbs/mom_priv/flag_in_cloud ] ; then
        mount_message="in cloud;$mount_message"
    fi
    
    pid=$(cat /tmp/pid.ls_$mountFile 2>/dev/null)
    if [ ${#pid} -gt 0 ] ; then
        # jeden radek vypisu 'ps ax' vypada nejak takto: "31280 pts/4    S      0:00 /usr/bin/time -f time %e ls -f /afs/ics.muni.cz"
        ps ax | awk -v PID=$pid '{if($1==PID && $5=="/usr/bin/time" && $9=="ls")exit 1}' > /dev/null 2> /dev/null
        if [ $? -eq 1 ] ; then
            if [ "$mount_disabled" == "yes" ] ; then
                kill -9 ${pid}
                rm /tmp/pid.ls_$mountFile /tmp/output.ls_$mountFile 2>/dev/null
            else
                echo "$mount pid ${pid} still exists"
                continue    
           fi
        else
          if [ ! $is_mounted -eq 1 ] ; then 
		    echo "$mount time 0.0 $mount_message"
          elif [ "$mount_disabled" != "yes" ] ; then
            if [ `grep -c "." /tmp/output.ls_$mountFile` -eq 1 ] ; then 
                echo "$mount `cat /tmp/output.ls_$mountFile` $mount_message"
            else    
                echo "$mount `head -n -2 /tmp/output.ls_$mountFile | awk '{ printf "%s, ", $0 }'` $mount_message"
            fi
            #rm /tmp/pid.ls_$mountFile /tmp/output.ls_$mountFile 2>/dev/null
          fi
        fi
    else
        if [ "$mount_disabled" != "yes" ] ; then
            echo "$mount not checked yet $mount_message"
        fi
    fi

    if [ "$mount_disabled" == "yes" ] ; then
        echo "$mount time 0.00 disabled $mount_message"
        continue
    fi

    if [ $is_mounted -eq 1 ] && [[ $(find /tmp/output.ls_$mountFile -mmin -$MOUNTED_CHECK_INTERVAL 2>/dev/null | wc -l) -gt "0" ]] ; then
        continue
    fi

    if [ ! $is_mounted -eq 1 ] && [[ $(find /tmp/output.ls_$mountFile -mmin -$UNMOUNTED_CHECK_INTERVAL 2>/dev/null | wc -l) -gt "0" ]] ; then
        continue
    fi

    rm /tmp/pid.ls_$mountFile /tmp/output.ls_$mountFile 2>/dev/null
    mounts_to_ls+="$mount "
done

if [ "$still_running" != "no" ]; then
    exit 0
fi

for mount in $mounts_to_ls ; do
    mountFile="${mount//\//\\\\}_$(echo $SSH_CLIENT | awk '{print $1}')"
    /usr/bin/time -f 'time %e' ls -f "$mount" >/dev/null 2>/tmp/output.ls_$mountFile & disown 2>/dev/null
    echo "$!" > "/tmp/pid.ls_$mountFile"
    # mezi volanim ls musi byt kratka prestavka, viz #106213
    sleep 2
done & disown
echo "$!" > "/tmp/pid.fsls_$(echo $SSH_CLIENT | awk '{print $1}')"
