Page 1 of 2

Looking for a file check script

Posted: Tue Mar 31, 2015 10:09 am
by greyclear
Having a hard time wraping my head around some of the file checking scripts I have found for example I seen this http://exchange.nagios.org/directory/Pl ... le/details and I want to basically check one particular file for its age. If the time stamp date becomes two hours or older it means the report isn't running. In short I am looking for something that can monitor crystal reports. If there are no changes to files I can only assume the report didnt run

Re: Looking for a file check script

Posted: Tue Mar 31, 2015 11:56 am
by rhassing
In NET-SNMP it is easy to create options in the snmpd.conf file which will execute scripts.

This way it is easy to execute scripts from a remote server without the need to log in, or have another daemon running.

On the host, where the script should be executed, runs the snmp daemon that comes with the NET-SNMP package. In de file snmpd.conf we add the following line:

Code: Select all

exec filedate /usr/local/bin/check_file-date.sh
exec filesize /usr/local/bin/check_file-size.sh
On this host we also create these scripts:

check_file-date.sh:

Code: Select all

#!/bin/bash
ls -l /etc/hosts | awk '{print $6,$7,$8}'
exit 0
and

/usr/local/bin/check_file-size.sh

Code: Select all

 #!/bin/bash
ls -l /etc/hosts | awk '{print $5}'
exit 0
The oid which can be used to execute the scripts is: .1.3.6.1.4.1.2021.8

The next command can be executed on any remote host (which has the rights):

Code: Select all

snmpwalk -v1 -c public 192.168.1.1 .1.3.6.1.4.1.2021.8
The output would be something like this:

Code: Select all

UCD-SNMP-MIB::extIndex.1 = INTEGER: 1
UCD-SNMP-MIB::extIndex.2 = INTEGER: 2
UCD-SNMP-MIB::extNames.1 = STRING: filedate
UCD-SNMP-MIB::extNames.2 = STRING: filesize
UCD-SNMP-MIB::extCommand.1 = STRING: /usr/local/bin/check_file-date.sh 
UCD-SNMP-MIB::extCommand.2 = STRING: /usr/local/bin/check_file-size.sh 
UCD-SNMP-MIB::extResult.1 = INTEGER: 0
UCD-SNMP-MIB::extResult.2 = INTEGER: 0
UCD-SNMP-MIB::extOutput.1 = STRING: nov 12 18:52
UCD-SNMP-MIB::extOutput.2 = STRING: 27851
UCD-SNMP-MIB::extErrFix.1 = INTEGER: 0
UCD-SNMP-MIB::extErrFix.2 = INTEGER: 0
UCD-SNMP-MIB::extErrFixCmd.1 = STRING: 
UCD-SNMP-MIB::extErrFixCmd.2 = STRING:
Now you see there is an extra oid available which will just give the outcome of the script.

Code: Select all

snmpwalk -v1 -c public 192.168.1.1 UCD-SNMP-MIB::extOutput.1
UCD-SNMP-MIB::extOutput.2 = STRING: 27851
In Nagios, you could use the check_snmp plugin:

Code: Select all

check_snmp -H 192.168.1.1 -o UCD-SNMP-MIB::extOutput.2 -s 27851
SNMP OK - 27851 | UCD-SNMP-MIB::extOutput.2=27851

Re: Looking for a file check script

Posted: Tue Mar 31, 2015 1:06 pm
by greyclear
The file on said system will be windows.

Re: Looking for a file check script

Posted: Tue Mar 31, 2015 1:47 pm
by jdalrymple
What in particular about the posted script are you having trouble with?

Code: Select all

C:\Users\jdalrymple\Downloads>cscript.exe check_MostRecentFile.vbs c:\users\jdalrymple 20 30 100
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

critical: jdalrymple on D.lnk is 11823 minutes old.
It's not very robust, but does what is advertised. Do you need to check a specific file? this one tests whole directories.

Re: Looking for a file check script

Posted: Tue Mar 31, 2015 2:19 pm
by greyclear
Specific file but I suppose I could move it to a folder of its own. How do I setup the command for this so that it executes cscript on the server?

Re: Looking for a file check script

Posted: Tue Mar 31, 2015 2:35 pm
by jdalrymple
Do you have nsclient++ installed on the server already? If not you'll want it.

http://sites.box293.com/nagios/guides/n ... dows/0-4-x

Add the following line to the [/settings/external scripts/scripts] section of nsclient.ini

Code: Select all

check_fileage=cscript.exe //T:30 //NoLogo scripts\check_MostRecentFile.vbs  <DIR> <WARNAGE> <CRITICALAGE> <MINFILESIZEBYTES>
then you'll have to add a service to your nagios config that does something like:

Code: Select all

$USER1$/check_nrpe -H $HOSTADDRESS$ -c check_fileage
That's a pretty dumbed down version of the process. Let us know if you need more detail.

Re: Looking for a file check script

Posted: Tue Mar 31, 2015 3:13 pm
by greyclear
Yep its installed. Just to test, if I am doing it right ./check_nrpe -H crep7 -c check_MostRecentFile -a C:\Sharepoint Reports\running\Reportrun.pdf 1440
No handler for command: check_mostrecentfile

Code: Select all

# If you want to fill this file with all avalible options run the following command:
#   nscp settings --generate --add-defaults --load-all
# If you want to activate a module and bring in all its options use:
#   nscp settings --activate-module <MODULE NAME> --add-defaults
# For details run: nscp settings --help


; Undocumented section
[/modules]

; CheckDisk - CheckDisk can check various file and disk related things. The current version has commands to check Size of hard drives and directories.
CheckDisk = 1

; Event log Checker. - Check for errors and warnings in the event log. This is only supported through NRPE so if you plan to use only NSClient this wont help you at all.
CheckEventLog = 1

; Check External Scripts - A simple wrapper to run external scripts and batch files.
CheckExternalScripts = 1

; Helper function - Various helper function to extend other checks. This is also only supported through NRPE.
CheckHelpers = 1

; Check NSCP - Checkes the state of the agent
CheckNSCP = 1

; CheckSystem - Various system related checks, such as CPU load, process state, service state memory usage and PDH counters.
CheckSystem = 1

; CheckWMI - CheckWMI can check various file and disk related things. The current version has commands to check Size of hard drives and directories.
CheckWMI = 1

; NRPE server - A simple server that listens for incoming NRPE connection and handles them.
NRPEServer = 1

; NSCAClient - Passive check support over NSCA.
NSCAClient = 1

; NSClient server - A simple server that listens for incoming NSClient (check_nt) connection and handles them. Although NRPE is the preferred method NSClient is fully supported and can be used for simplicity or for compatibility.
NSClientServer = 1


; Undocumented section
[/settings/default]

; ALLOWED HOSTS - A comaseparated list of allowed hosts. You can use netmasks (/ syntax) or * to create ranges.
allowed hosts = 10.2.2.180

[/settings/NRPE/server]
allow arguments=1

; A list of aliases available. An alias is an internal command that has been "wrapped" (to add arguments). Be careful so you don't create loops (ie check_loop=check_a, check_a=check_loop)
[/settings/external scripts/alias]

check_fileage=cscript.exe //T:30 //NoLogo scripts\check_MostRecentFile.vbs  C:\Sharepoint Reports\running\Reportrun.pdf 20 30 9000

; alias_cpu - Alias for alias_cpu. To configure this item add a section called: /settings/external scripts/alias/alias_cpu
alias_cpu = checkCPU warn=80 crit=90 time=5m time=1m time=30s

; alias_cpu_ex - Alias for alias_cpu_ex. To configure this item add a section called: /settings/external scripts/alias/alias_cpu_ex
alias_cpu_ex = checkCPU warn=$ARG1$ crit=$ARG2$ time=5m time=1m time=30s

; alias_disk - Alias for alias_disk. To configure this item add a section called: /settings/external scripts/alias/alias_disk
alias_disk = CheckDriveSize MinWarn=10% MinCrit=5% CheckAll FilterType=FIXED

; alias_disk_loose - Alias for alias_disk_loose. To configure this item add a section called: /settings/external scripts/alias/alias_disk_loose
alias_disk_loose = CheckDriveSize MinWarn=10% MinCrit=5% CheckAll FilterType=FIXED ignore-unreadable

; alias_event_log - Alias for alias_event_log. To configure this item add a section called: /settings/external scripts/alias/alias_event_log
alias_event_log = CheckEventLog file=application file=system MaxWarn=1 MaxCrit=1 "filter=generated gt -2d AND severity NOT IN ('success', 'informational') AND source != 'SideBySide'" truncate=800 unique descriptions "syntax=%severity%: %source%: %message% (%count%)"

; alias_file_age - Alias for alias_file_age. To configure this item add a section called: /settings/external scripts/alias/alias_file_age
alias_file_age = checkFile2 filter=out "file=$ARG1$" filter-written=>1d MaxWarn=1 MaxCrit=1 "syntax=%filename% %write%"

; alias_file_size - Alias for alias_file_size. To configure this item add a section called: /settings/external scripts/alias/alias_file_size
alias_file_size = CheckFiles "filter=size > $ARG2$" "path=$ARG1$" MaxWarn=1 MaxCrit=1 "syntax=%filename% %size%" max-dir-depth=10

; alias_mem - Alias for alias_mem. To configure this item add a section called: /settings/external scripts/alias/alias_mem
alias_mem = checkMem MaxWarn=80% MaxCrit=90% ShowAll=long type=physical type=virtual type=paged type=page

; alias_process - Alias for alias_process. To configure this item add a section called: /settings/external scripts/alias/alias_process
alias_process = checkProcState "$ARG1$=started"

; alias_process_count - Alias for alias_process_count. To configure this item add a section called: /settings/external scripts/alias/alias_process_count
alias_process_count = checkProcState MaxWarnCount=$ARG2$ MaxCritCount=$ARG3$ "$ARG1$=started"

; alias_process_hung - Alias for alias_process_hung. To configure this item add a section called: /settings/external scripts/alias/alias_process_hung
alias_process_hung = checkProcState MaxWarnCount=1 MaxCritCount=1 "$ARG1$=hung"

; alias_process_stopped - Alias for alias_process_stopped. To configure this item add a section called: /settings/external scripts/alias/alias_process_stopped
alias_process_stopped = checkProcState "$ARG1$=stopped"

; alias_sched_all - Alias for alias_sched_all. To configure this item add a section called: /settings/external scripts/alias/alias_sched_all
alias_sched_all = CheckTaskSched "filter=exit_code ne 0" "syntax=%title%: %exit_code%" warn=>0

; alias_sched_long - Alias for alias_sched_long. To configure this item add a section called: /settings/external scripts/alias/alias_sched_long
alias_sched_long = CheckTaskSched "filter=status = 'running' AND most_recent_run_time < -$ARG1$" "syntax=%title% (%most_recent_run_time%)" warn=>0

; alias_sched_task - Alias for alias_sched_task. To configure this item add a section called: /settings/external scripts/alias/alias_sched_task
alias_sched_task = CheckTaskSched "filter=title eq '$ARG1$' AND exit_code ne 0" "syntax=%title% (%most_recent_run_time%)" warn=>0

; alias_service - Alias for alias_service. To configure this item add a section called: /settings/external scripts/alias/alias_service
alias_service = checkServiceState CheckAll

; alias_service_ex - Alias for alias_service_ex. To configure this item add a section called: /settings/external scripts/alias/alias_service_ex
alias_service_ex = checkServiceState CheckAll "exclude=Net Driver HPZ12" "exclude=Pml Driver HPZ12" exclude=stisvc

; alias_up - Alias for alias_up. To configure this item add a section called: /settings/external scripts/alias/alias_up
alias_up = checkUpTime MinWarn=1d MinWarn=1h

; alias_updates - Alias for alias_updates. To configure this item add a section called: /settings/external scripts/alias/alias_updates
alias_updates = check_updates -warning 0 -critical 0

; alias_volumes - Alias for alias_volumes. To configure this item add a section called: /settings/external scripts/alias/alias_volumes
alias_volumes = CheckDriveSize MinWarn=10% MinCrit=5% CheckAll=volumes FilterType=FIXED

; alias_volumes_loose - Alias for alias_volumes_loose. To configure this item add a section called: /settings/external scripts/alias/alias_volumes_loose
alias_volumes_loose = CheckDriveSize MinWarn=10% MinCrit=5% CheckAll=volumes FilterType=FIXED ignore-unreadable 

; default - Alias for default. To configure this item add a section called: /settings/external scripts/alias/default
default = 

Re: Looking for a file check script

Posted: Tue Mar 31, 2015 3:55 pm
by jdalrymple
[/settings/external scripts/scripts] section ... you may have to add it.

You may have issues with those arguments too, did you try them from the command line? I didn't dig into the VBS, but it seemed to expect 4 ARGs

Re: Looking for a file check script

Posted: Tue Mar 31, 2015 10:32 pm
by Box293
I hate to distract from the great solutions already provided, however NSClient++ has this functionality built in, no need for extra scripts.

From my website http://sites.box293.com/nagios/guides/c ... es-folders
Make Sure Files Exists That Were Modified In The Last Two Hours
Check a folder to make sure files exist that were modified in the past two hours. Generate a critical alert if no files matching this criteria are found.

NSClient++ 0.3.9 & 0.4.1

Code: Select all

Command:
check_nrpe -H 192.168.142.137 -c CheckFiles -a path='D:\\Important Stuff' pattern=*.* 'filter=written > -2h' 'master-syntax={%total% Files Found}' MinCrit=0

Output when files matching criteria are found:
{1 Files Found}|'found files'=1;0;0

Output when NO files matching criteria are found:
{0 Files Found}, found files: 0 < critical|'found files'=0;0;0
Note: If it is just a specific file you are after, I'm pretty sure you use path= and drop the pattern=

Code: Select all

check_nrpe -H 192.168.142.137 -c CheckFiles -a path='D:\\Important Stuff\\Important File.txt' 'filter=written > -2h' 'master-syntax={%total% Files Found}' MinCrit=0

Re: Looking for a file check script

Posted: Wed Apr 01, 2015 7:23 am
by greyclear
jdalrymple wrote:[/settings/external scripts/scripts] section ... you may have to add it.

You may have issues with those arguments too, did you try them from the command line? I didn't dig into the VBS, but it seemed to expect 4 ARGs
Ill look into this and see.