check file exist and age

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
User avatar
ruffsense
Posts: 140
Joined: Thu Apr 11, 2013 12:40 am

check file exist and age

Post 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
I don't insult, I diagnose.
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: check file exist and age

Post 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.
Former Nagios Employee
User avatar
ruffsense
Posts: 140
Joined: Thu Apr 11, 2013 12:40 am

Re: check file exist and age

Post 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
I don't insult, I diagnose.
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: check file exist and age

Post 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.
Former Nagios Employee
User avatar
ruffsense
Posts: 140
Joined: Thu Apr 11, 2013 12:40 am

Re: check file exist and age

Post by ruffsense »

i forgot to mention not locally. I forgot the -H
I don't insult, I diagnose.
User avatar
ruffsense
Posts: 140
Joined: Thu Apr 11, 2013 12:40 am

Re: check file exist and age

Post 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.
I don't insult, I diagnose.
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: check file exist and age

Post 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.
Former Nagios Employee
User avatar
ruffsense
Posts: 140
Joined: Thu Apr 11, 2013 12:40 am

Re: check file exist and age

Post by ruffsense »

Filesys::SmbClient

Is not working with centos 7
I don't insult, I diagnose.
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: check file exist and age

Post by rkennedy »

You're probably just missing the package.

Try yum install samba-client
Former Nagios Employee
tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

Re: check file exist and age

Post 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?
Former Nagios employee
Locked