check existence of folders and output to nagios ( OK/ Critical)

Hello,

I am looking for a batch file which verifies a list of directories (for example w,x,y,z folders), and in case some folder is absent, the output will be "Critical" and the absent folder/s, or in case that is not absent any, the output will be "OK".

Thanks
zsaboAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

delyan_valchevCommented:
Try something like:
@echo off
for %%i in (folder1 folder2 folder3) do (
	if exist %%i (
		echo %%i OK
		) else (
		echo %%i Critical
	     )
)
	

Open in new window

0
delyan_valchevCommented:
In case you don't care about each individual folder but rather output Critical if at least one is missing try the next script.
Hope it helps!
Delyan
@echo off
set output=OK
for %%i in (folder1 folder2 folder3) do if not exist %%i set output=Critical
echo %output%
	

Open in new window

0
zsaboAuthor Commented:
Hello Delyan,

Is it possible that the output be in rows?
0
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

t0t0Commented:
delyan_valchev
Unfortunately, your code does not truly validate the existence of folders. let's say you have a FILE named 'folder1' and two FOLDERS named 'folder2' and 'folder3'. Your batch file would list all three of them as FOLDERS!!! Quite clearly, this is wrong!

delyan_valchev
Unfortunately, your code does not work correctly either for the same reason as above.
0
t0t0Commented:
zsabo
Niether of the two previous solutions work correctly. I have commented why in my previous post.

The following code is the correct method for testing for the existence of folders:

@echo off
for %%a in (folder1 folder2 folder3) do (
   if exist %%a\ (
      echo %%a OK
   ) else (
      echo %%a Critical
   )
)
0
delyan_valchevCommented:
@t0t0
Thanks for the special case handling fix.

@zsabo
What do you mean by "output be in rows"?
0
zsaboAuthor Commented:
Hello Delyan,

I'm refering that output would be nice in a row. For example, instead of:

folder1
folder2
folder2

this way:

folder1 folder2 folder3...
0
t0t0Commented:
zsabo
That's not 'rows', that's columns.

@echo off
setlocal enabledelayedexpansion
set output=

for %%a in (folder1 folder2 folder3) do (
   if exist %%a\ (
      set output=!output! OK
   ) else (
      set output=!output! Critical
   )
)
echo %output%
0
t0t0Commented:
Or you could have something like this:

@echo off
setlocal enabledelayedexpansion
set output=

for %%a in (folder1 folder2 folder3) do (
   if exist %%a\ (
      set output=!output! %%a OK
   ) else (
      set output=!output! %%a Critical
   )
)
echo %output%
0
t0t0Commented:
How many folders do you anticipate testing the existence for?

I ask because if it's more than a few it would be easier to list the names of the folders in a separate text file which are then read by the batch file itself. This makes good sense as you can amend the text file without having to change the batch file code.
0
t0t0Commented:
Above, I showed you code which produce these outputs:

(comment ID: 24479224)

    folder1 Critcal
    folder2 OK
    folder3 OK

(comment ID: 24491832)

    folder1 Critical folder2 OK folder3 OK


(comment ID: 24491823)

    Critical OK OK

How about (where folder1 does not exist) just this (see the following code):

    folder2 folder3


@echo off
setlocal enabledelayedexpansion
set output=
for %%a in (folder1 folder2 folder3) do (
   if exist %%a\ set output=!output! %%a
)
echo %output%
0
t0t0Commented:
zsabo

(Magyar?)

Could we please have some feedback so that we can provide you with further assistance if need be.
0
zsaboAuthor Commented:
Hello t0t0,

I would like to monitor approximately 8 directories.

The result that I need is of the type:

OK

Critical FolderX FolderY
0
zsaboAuthor Commented:
OK --> All the directories exist.

Critical -->The directories do not exist or only some.
0
delyan_valchevCommented:
Here's the script with the requested output. In case some folders don't exist it also outputs the ones that do exist.
@echo off
setlocal EnableDelayedExpansion
set OK=OK
set Critical=Critical
for %%i in (folder1 folder2 folder3) do (
        if exist %%i\ (
                set OK=!OK! %%i
                ) else (
                set Critical=!Critical! %%i
             )
)
 
if "%Critical%"=="Critical" (
	rem All folders exist
	echo OK
	) else (
	rem Some folders exist
	echo %OK%
	echo %Critical%
)

Open in new window

0
zsaboAuthor Commented:
Hello delyan,

The output of the script  always show OK. In my test there isn't any folder to test , but output shows always OK:

>test_folder.cmd

OK
Critical E:\j
0
delyan_valchevCommented:
Normally when some or none of the folders exist it shows which are OK and which are not. In your case OK line is empty, meaning no folder is OK. If you want just the missing folders list, delete the line:
echo %OK%
(the third from the bottom)
0
t0t0Commented:
Please try the following and give some feedback as to how close we're arriving at a solution. (Included are my sample set of folders - but you can change these of course).

@echo off
setlocal enabledelayedexpansion
set ok=
set critical=

for %%a in (folder1 folder2 folder3 desktop sendto folder4 windows "my documents") do (
   if exist %%a\ (
      set ok=!ok! %%~a
   ) else (
      set critical=!critical! %%~a
   )
)

if defined ok echo OK %ok%
if defined critical echo Critical %critical%
0
t0t0Commented:
Please review the code in my previous comment BUT also review the current code.

The code below simply outputs either "Critical" or "OK" depending if some of the folders do not exist:

@echo off
set critical=
for %%a in (folder1 folder2 folder3 folder4) do (
   if not exist %%a\ set critical=1
)
if defined critical (echo Critical) else (echo OK)
0
t0t0Commented:
Please check the following:

@echo off
setlocal enabledelayedexpansion
set ok=
set critical=

for %%a in (folder1 folder2 folder3 desktop sendto folder4 windows "my documents") do (
   if exist %%a\ (
      echo %%~a - OK
      set ok=ok
   ) else (
      echo %%~a - Critical
      set critical=critical
   )
)
if defined ok echo Some folders were present
if defined critical echo Some folders were not present
0
t0t0Commented:
do you require further assistance or do you wish to close this question?
0
t0t0Commented:
Here's another way of assugning folders to variables and reading their names in a FOR loop. You can customise the settings for 'folder[1]...folder[8]'. Adding more folders is easy.



@echo off
setlocal enabledelayedexpansion
set ok=
set critical=

set folder[1]=c:\temp
set folder[2]=c:\documents and settings\paul
set folder[3]=c:\documents and settings\john
set folder[4]=c:\application a
set folder[5]=c:\application b
set folder[6]=c:\folderA
set folder[7]=c:\folderB
set folder[8]=c:\folderC

for /l %%a in (1,1,8)  do (
   if exist !folder[%%a]!\ (
      echo !folder[%%a]! - OK
      set ok=ok
   ) else (
      echo !folder[%%a]! - Critical
      set critical=critical
   )
)
if defined ok echo Some folders were present
if defined critical echo Some folders were not present
0
t0t0Commented:
zsabo

Please confirm whether the above solution solved your problem

Please close this long-overdue question.

Thank you.
0
t0t0Commented:
@echo off
if "%1"=="" exit /b 1

set ok=
set critical=

:loop
   if exist "%1\" (
      if not defined ok (
         set ok=%1
      ) else (
         set ok=%ok% %1
      )
   ) else (
      if not defined critical (
         set critical=%1
      ) else (
         set critical=%critical% %1
      )
   )
   shift
if not "%1"=="" goto loop

if defined ok echo ok %ok%
if defined critical echo critical %critical%
0
t0t0Commented:
Oops! I forgot to add....

call the above batch file with foldernames as paramters, like this for example:

   CONFIRM c:\temp c:\c:\tempfolder1 c:\temp\folder2 etc...

where 'confirm.bat' is the name of the batch file.
 
0
t0t0Commented:
qlemo

i believe my last code works a charm... how come I don't get points?
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Paul,

See http:#a24507316:

The result that I need is of the type:

OK

Critical FolderX FolderY
You provided batch variations which always report the folders which are ok, besides zsabo stated twice which output format he wants. He might have given you partitial or all points, but that is something a Cleanup Volunteer is not able to do (and that's true for varying the Grade, too). Strictly seen, the question is NOT answered.
0
t0t0Commented:
qlemo

After looking at it now, it's simpler then I first thought.

I was wondering if it's worth asking the asker if he'll accept the code below and perhaps giving him time to reply again.




zsabo

use the batch file by passing folders as command line options like this:

   test_folder.cmd folder1 folder2 folder3 etc...


@echo off
if "%1"=="" exit /b 1

set critical=

:loop
   if not exist "%~1\" (
      if not defined critical (
         set critical=%1
      ) else (
         set critical=%critical% %1
      )
   )
   shift
if not "%~1"=="" goto loop

if not defined critical (
   echo OK
) else (
   echo Critical %critical%
)



0
t0t0Commented:
Sorry, a slight correction with: 'if "%1"=="" exit /b 1'





qlemo

After looking at it now, it's simpler then I first thought.

I was wondering if it's worth asking the asker if he'll accept the code below and perhaps giving him time to reply again.




zsabo

use the batch file by passing folders as command line options like this:

   test_folder.cmd folder1 folder2 folder3 etc...


@echo off
if "%~1"=="" exit /b 1

set critical=

:loop
   if not exist "%~1\" (
      if not defined critical (
         set critical=%1
      ) else (
         set critical=%critical% %1
      )
   )
   shift
if not "%~1"=="" goto loop

if not defined critical (
   echo OK
) else (
   echo Critical %critical%
)

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Looking at the response rates and account stats of zsabo, there will be no answer. Last action recorded was of June 2009, so there is nothing to expect.

However, the code you provided last is correct, and would fulfill the requirements. Regarding the absense of the OP, I recommend to accept it.
0
t0t0Commented:
Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Network Management

From novice to tech pro — start learning today.