Page 1 of 1

check file exist and age

Posted: Fri Feb 03, 2017 10:27 am
by ruffsense
How to check if file exist if the folder name is chnaging every day. The check also needs ssh to logon.(username and password)
2017-02-01
2017-02-02

Re: check file exist and age

Posted: Fri Feb 03, 2017 11:04 am
by rkennedy
Take a look at the plugins available here which may have one that works for you - https://exchange.nagios.org/index.php?o ... k_file_age

You may need to add in your own scripting to represent what the current date is for the dynamic changing folder name.

Re: check file exist and age

Posted: Fri Feb 03, 2017 11:28 am
by ruffsense
Non of them has a ssh login.

im searching for something like this?

./check_file_age -D \\directory\folder -u username -p password -f \%date%\* -c FILEMAXTIME

-u USERNAME -p PASSWORD -f WORKGROUP -D SMBSHARE -f FILEPATH -c FILEMAXTIME

Re: check file exist and age

Posted: Fri Feb 03, 2017 11:31 am
by rkennedy
You would need to find one that you can run in conjunction with check_by_ssh if none of them have the support for it, or write your own.

Once you're using check_by_ssh then you'll be able to execute a script locally.

Re: check file exist and age

Posted: Fri Feb 03, 2017 5:06 pm
by ruffsense
i forgot to mention not locally. I forgot the -H

Re: check file exist and age

Posted: Fri Feb 03, 2017 5:47 pm
by ruffsense

Code: Select all

#!/bin/bash

function usage {
  echo -en "$EMSG"
  echo -e "Usage:\n">&2
  echo -e "`basename $0` -u USERNAME -p PASSWORD -W WORKGROUP -s SMBSHARE -f FILEPATH -c FILEMAXTIME -w FILESIZEMIN [-d]\n">&2
  exit 3
}

#Process command line parameters
OPTERR=1
while getopts ":u:p:W:s:f:c:w:d" FLAG;do
  #echo "-$FLAG $OPTARG"
  [[ $OPTARG == -* ]] && echo "Invalid option: -$OPTARG">&2 && usage;
  case $FLAG in
    u)  USER=$OPTARG;;
    p)  PASS=$OPTARG;;
    W)  WORK=$OPTARG;;
    s)  SHER=$OPTARG;;
    f)  FILE=$OPTARG;;
    c)  FMXT=$OPTARG;;
    w)  FSZM=$OPTARG;;
    d)  DEBUG='true';;
    :)  echo "Option -$OPTARG requires an argument.">&2;usage;;
   \?)  echo "Invalid option: -$OPTARG">&2;usage;;
  esac
done
#Ensure required parameters are present
EMSG="Y U No enter all data?!?\n"
[ "$*" ]&&[ "$USER" ]&&[ "$PASS" ]&&[ "$WORK" ]&&[ "$SHER" ]&&[ $FILE ]&&[ "$FMXT" ]&&[ $FSZM ]||usage

#Make a samba call
LISTING=`smbclient $SHER $PASS \
-U $USER -W $WORK 2> /dev/null<< _END_
dir $FILE
_END_`

TNOW=`date +%s`
CRIT="yes"
WARN="no"
NEWC=0

#Parse resluts
FILES=`echo -e "$LISTING" | awk '$0~/^  [^.]/&&$(NF-6)!="D"'`
FILEC=`echo "$FILES"|wc -l`
IFS=$'\n';for LINE in $FILES;do
  FILE=`echo $LINE|awk '{print $1}'`
  SIZE=`echo $LINE|awk '{print $(NF-5)}'`
  TIME=$(date +%s -d "`echo $LINE|awk '{print $(NF-4),$(NF-3),$(NF-2),$(NF-1),$NF}'`")
  TDIFs=$(($TNOW-$TIME))
  TDIFm=$(($TDIFs/60))
  [ -n "$DEBUG" ] && echo $FILE - ${SIZE} bytes, ${TDIFm} min >&2
  [[ $TDIFm -lt $FMXT ]] && $((NEWC++)) && CRIT='no'
  [[ $SIZE -lt $FSZM ]] && WARN='yes'
done

[ $CRIT = "yes" ] && echo "No files found newer than $FMXT minutes!" && exit 2
[ $WARN = "yes" ] && echo "Files are smaller then $FSZM bytes!" && exit 1
echo OK - Found $FILEC files above $FSZM bytes and $NEWC newer than $FMXT minutes.
I need to fix this script. I don't need the workgroup flag and in the -f flag i need to add date +%Y_%m_%d.
Can anyone help me out. Im not that good in .sh scripts.

Re: check file exist and age

Posted: Mon Feb 06, 2017 10:33 am
by rkennedy
This will help walk you through adding the date - https://www.cyberciti.biz/faq/unix-linu ... -filename/

As this is a third party plugin, we're not able to provide direct support from it. The above link should help you out, though.

Re: check file exist and age

Posted: Mon Feb 06, 2017 3:45 pm
by ruffsense
Filesys::SmbClient

Is not working with centos 7

Re: check file exist and age

Posted: Mon Feb 06, 2017 4:13 pm
by rkennedy
You're probably just missing the package.

Try yum install samba-client

Re: check file exist and age

Posted: Wed Mar 01, 2017 2:18 pm
by tmcdonald
Just checking in since we have not heard from you in a while. Did @rkennedy's post clear things up or has the issue otherwise been resolved?