Il seguente batch lo utilizzo per controllare periodicamente il file di log generato dal server di posta hMailServer, residente ed in esecuzione su di un sistema server. Il log viene costamente aggiornato dal server in tempo reale.

Essendo fondamentale il sistema di messaggistica elettronica, effettuo controlli sui log giornalmente. Tramite il seguente batch, opportunamente lanciato in orari prestabiliti tramite le operazioni pianificate di Windows, riesco ad avere un certo tipo di controllo.

Il batch nell’ordine effettua:

  1. Calcolo del giorno corrente
  2. Calcolo del giorno precedente con verifica se siamo ad inizio mese
  3. Definizione dei file di log da ispezionare
  4. Ricerca di opportune stringe quali Authentication failed, Message could not be delivered, Recipient unknown (memorizzate una sotto l’altra all’interno del file hmail_stringhe.txt) su entrambi i file
  5. Generazione di output su console

[cc lang=”text”]@ECHO OFF
:BEGIN
CLS
TITLE Controllo Log hMailServer
echo +———————————–+
echo ! Controllo Log hMailServer !
echo +———————————–+
ECHO ! Controlla il file del log di ieri !
ECHO ! e di oggi da possibili errori !
ECHO +———————————–+
ECHO.
SET GIORNO=%date:~0,2%
SET MESE=%date:~3,2%
SET ANNO=%date:~-4%
IF %GIORNO% == 01 GOTO CALCOLO_IERI
SET /A GIORNO=%GIORNO%-1
IF %GIORNO% LSS 10 SET GIORNO=0%GIORNO%
GOTO CONTROLLO

:CALCOLO_IERI
SET /A MESE=%MESE%-1
IF %MESE% LSS 10 SET MESE=0%MESE%
IF %MESE% == 01 SET GIORNO=31
IF %MESE% == 02 SET GIORNO=28
IF %MESE% == 03 SET GIORNO=31
IF %MESE% == 04 SET GIORNO=30
IF %MESE% == 05 SET GIORNO=31
IF %MESE% == 06 SET GIORNO=30
IF %MESE% == 07 SET GIORNO=31
IF %MESE% == 08 SET GIORNO=31
IF %MESE% == 09 SET GIORNO=30
IF %MESE% == 10 SET GIORNO=31
IF %MESE% == 11 SET GIORNO=30
IF %MESE% == 00 SET GIORNO=31
IF %MESE% == 00 SET MESE=12
IF %MESE% == 12 SET /A ANNO=%ANNO%-1

:CONTROLLO
SET HMAIL_LOG=hmailserver_%date:~-4%-%date:~3,2%-%date:~0,2%.log
SET HMAIL_LOG_IERI=hmailserver_%ANNO%-%MESE%-%GIORNO%.log
IF EXIST \\ip.address\hMailserverLogs\%HMAIL_LOG_IERI% echo Checking file %HMAIL_LOG_IERI%
findstr /G:\\MyComputer\batch\hmail_stringhe.txt \\ip.address\hMailserverLogs\%HMAIL_LOG_IERI%
IF NOT EXIST \\ip.address\hMailserverLogs\%HMAIL_LOG_IERI% echo Missing file %HMAIL_LOG_IERI%
ECHO.
IF EXIST \\ip.address\hMailserverLogs\%HMAIL_LOG% echo Checking file %HMAIL_LOG%
IF NOT EXIST \\ip.address\hMailserverLogs\%HMAIL_LOG% echo Missing file %HMAIL_LOG%
findstr /G:\\MyComputer\batch\hmail_stringhe.txt \\ip.address\hMailserverLogs\%HMAIL_LOG%
ECHO.

REM ECHO IERI = %GIORNO%/%MESE%/%ANNO%

:END
SET HMAIL_LOG=
SET GIORNO=
SET MESE=
SET ANNO=
pause[/cc]

Il batch fa uso dei classici comandi condizionali quali if, if exist (ed il suo opposto if not exist) per dirigere correttamente il flusso iterativo senza incappare in errori di FILE NOT FOUND.

Viene fatto uso anche del comando findstr che effettua la ricerca di stringhe all’interno di file. In particolare viene usata l’opzione /G: seguita dal file contenente le parole chiavi da ricercare.

[adrotate banner=”1″]