Filestat as bash script

hello everyone,

im searching for a bash script kind of local check that gives same output like mk_filestats.py
does someone wrote something like this? i will be grateful for sharing it

thank you.

hi ymez,

I think we wrote the following for an AIX host where no python was available, feel free to change the name of the config file. No guarantee on it:

so far it only covers the file count feature, not oldest/newest/sizes etc but you can include more functions when parsing through each file in the while loop at the bottom of the script

example Config file: /etc/check_mk/sva_filestats.cfg

_SVA_FILESTAT_NAME[0]="OMPlus Queue Files"
_SVA_FILESTAT_PATH[0]="/usr/lpplus/queues"
_SVA_FILESTAT_NAME[1]="OMPlus Report Files"
_SVA_FILESTAT_PATH[1]="/usr/lpplus/reports"

plugin $MK_LIBDIR/plugins/sva_filestats

#!/opt/freeware/bin/bash
 
conf_file="sva_filestats.cfg"
echo "<<<filestats:sep(0)>>>"
  
if ! [ -r "${MK_CONFDIR}/${conf_file}" ] ; then
echo "Error - No config available... \"${MK_CONFDIR}/${conf_file}\""
else
source "${MK_CONFDIR}/${conf_file}"
fi
 
echo ${!_SVA_FILESTAT_NAME[@]} | tr ' ' '\n' | while read _ARRAY_INDEX ; do
  filecount=$(ls -1 ${_SVA_FILESTAT_PATH[${_ARRAY_INDEX}]} | wc -l)
  echo "[[[count_only ${_SVA_FILESTAT_NAME[${_ARRAY_INDEX}]}]]]"
  echo "{'count': $filecount, 'type': 'summary'}"
done

output is mk_filestat compatible, but doesn’t cover all the use cases mk_filestat has. Feel free to extend it but share if you can :wink:

1 Like

thank you i will try it.
i allready wrote something like fileage but it is to slow… it take to much when looping through many folders.
i still did not try it in prod env. it still need some changes. feel free to add and correct.

#!/bin/bash

Alter=1000
GroesseINbytes=20
folders=(

        "/root/TESTINFO"
        "/root/TESTINFO2"
        "/root/FILEINFO"
)

for folder in "${folders[@]}"
do
                unset files
                files+=$(find $folder -type f )


                        for file in ${files[@]}
                                do

                                        filesizeBYTES=$(stat -c %s $file)
                                        fileageINepoch=$(stat -c %Y $file)
                                        filemtime=$(stat -c %Y newfile)
                                        currtime=$(date +%s)
                                        fileageINseconds=$(( (currtime - filemtime)))

                                                if  [[ $fileageINseconds -le $Alter  ]] || [[ $filesizeinBYTES -le $GroesseINbytes ]]
                                                then

                                                        status=2
                                                        statustext="$file is $fileageINseconds seconds old and $filesizeBYTES Bytes"
                                                        echo  "$status File age - $statustext"
                                                fi

                                done
done

output should be like :

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed. Contact an admin if you think this should be re-opened.