Pages

Thursday, May 31, 2012

Server Monitoring - Disk Space Batch File

Weeks ago we began down a road, experimenting with the idea that you could monitor servers with Salesforce.  I did mislead you; Salesforce isn't doing the monitoring, but will be responsible for creating notifications (through workflow) and give additional metrics for future systems reporting.

We've already created an object called "Alerts" to place information into (records that could cause sys admins night sweats and tremors).  That object has two fields, basically who (the node) triggered the alert and what (the conditions) the alert is for.

The next step is to create and schedule a batch. The batch below will take two parameters - the drive letter that you want to monitor and the low disk space threshold in MBs (i.e., "Disk_Space_Alert.bat C 20480" would monitor the C:\ and create an alert when the amount of disk space was 20480MBs/20GBs or less)
@ECHO OFF
setlocal

REM - This issues the DIR command on the disk letter provided, pipes it
REM - into FINDSTR, looks for the line where "free" is located, and places that
REM - line into a temp file
REM - Then it converts that line into tokens and assigns the number of free bytes
REM - to the FREE_BYTES variable
dir %1: 2>&1 | findstr free > temp

for /f "tokens=3,4*" %%f in ( temp ) do (
     set FREE_BYTES=%%f
)

REM - This removes commas and decimal points from FREE_BYTES

set FREE_BYTES=%FREE_BYTES:.=%
set FREE_BYTES=%FREE_BYTES:,=%

REM - The following line takes the number of free bytes and uses Powershell
REM - to calculate the number of MBs and assigned as a string variable
REM - This was done due to batch files numbers being limited to 32-bits of precision


FOR /F %%B IN ('powershell %FREE_BYTES%/1024/1024') DO (
    SET FREE_M_BYTES=%%B
)

REM - Next the decimals are removed and assigned to an integer variable

FOR /F "tokens=1* delims=,." %%B IN ("%FREE_M_BYTES%") DO (
    SET /A FREE_M_BYTES_INT = %%B
)

REM - Here we assign the threshold limit parameter to a variable

set /A LIMIT=%2

REM - Next, if the number of free MBs is less or equal to the limit
REM - a CSV is created. First the headers, then the alert fields
REM - After the file is created, process.bat is called to initiate the insert
REM - into Salesforce. Don't forget to install Data Loader, change your path
REM - and see my next post about the config file


IF %FREE_M_BYTES_INT% LEQ %LIMIT% (
    echo RESOURCE, DETAILS > alert.csv
    echo %COMPUTERNAME%, Low Space on %1 - %FREE_M_BYTES_INT% MB remaining >> alert.csv
    C:Program Files (x86)salesforce.comData Loaderbinprocess.bat ../conf Disk_Space_Alert
)

So in a nutshell:
  1. Create the batch file
  2. Schedule the batch file
  3. When scheduling, you'll need to specify the disk letter and the MB limit (i.e., "Disk_Space_Alert.bat C 20480")
  4. The program will launch, check the amount of disk space on the specified drive
  5. The batch will then convert bytes to MBs and compare it to the limit specified
  6. If the limit is met or exceeded, a CSV is created and formatted with information about the issue
  7. Finally, the batch calls another batch file that initiates a Data Loader insert
In the next post, I'll discuss using the SF Data Loader from the command line and how my config file is set up.

Where'd He Go?

Hope you aren't feeling too neglected with my blogging absence!  I've been going through various life changes and preparing for the road ahead. 

Since my last post, I've peacefully and respectfully parted ways with my previous employer and excited to announce that I will be providing Salesforce administration and development consulting services as I further develop my own company on the force.com platform.

If you're interested in hiring, bouncing ideas off of, having a cup of coffee with, hitting the gym with, or any other activity in general where you want to discuss Salesforce projects with me, feel free to shoot me an e-mail at kirk@salesforcery.com.

There should be plenty of updates in the future weeks.  Summer '12 is out now with some great features and I'm already one post into a series on server monitoring (talk about a cliff-hanger).  Thanks to all the visitors that I've picked up on this blog over the past year; it's only going to get better!

...and seriously, it doesn't cost anything to email me, so here's that link again:  kirk@salesforcery.com.

-Kirk