Daily IT Matters, this is the place where I post my daily findings on IT.

Thursday, October 30, 2008

Monitoring your windows servers with email alerts from the commandline

This is something that you will have to face allways.
You must monitor all servers that are under your command this way ensuring that servers keep on running happily.

For this I use the builtin perfmon and I use the opensource application SendEmail [you can download it here]
SendEmail is just an exe you don’t have to install it, just put in a folder lets say [ D:\SendEmail ]
I use a complete seperate machine to monitor my network this way I keep the load off from monitoring and this ensures the most accurate data.

In this example I want to monitor the free space on the c:\ drive of a remote server called “Fileserver”
On the machine from which you want to monitor start perfmon just hit [winkey + r and type perfmon] this is the quickest way.

image

 

You will see this screen:

image

In the left hand side expand performance logs and alerts.

image

Right click alerts and choose [New Alert Settings]

 

image

 

give your alert a meaningfull name

image

And Hit [OK]

Fill in the comment field with a meaningfull comment :-) and hit the [Add] button to add a counter.

image

The radiobutton Select counters from computer, select the performance object ‘LogicalDisk’ and Select the instance C: then click “Add”  and then “close”


image

You should now see this screen: set it like shown it will trigger an Alert when the c drive is under 500 MegaBytes. and this is sampled every hour.

 image

Now select the “Action” tab
image  
You see that I placed a checkmark at “Log an entry in the application event log”
And that I placed a checkmark at “Run this program”
I always want important events in the windows eventlog so that is why I checkmarked “Log an entry in the application event log”
With “run this program”  I let a batch run in this batchfile I fire SendEmail.

We will create this batchfile now:
First we need to find out what syntax SendEmail understands.

Open an commande prompt [winkey + r and then type cmd] and go to the folder containing the SendEmail executable.
Mine is set on drive x: I did create a network mapping for it.

so in the command prompt on x: I type SendEmail and that gives me the following options.


Synopsis: sendEmail -f ADDRESS [options]

Required:
-f ADDRESS from (sender) email address
* At least one recipient required via -t, -cc, or -bcc
* Message body required via -m, STDIN, or -o message-file=FILE

Common:
-t ADDRESS [ADDR ...] to email address(es)
-u SUBJECT message subject
-m MESSAGE message body
-s SERVER[:PORT] smtp mail relay, default is localhost:25

Optional:
-a FILE [FILE ...] file attachment(s)
-cc ADDRESS [ADDR ...] cc email address(es)
-bcc ADDRESS [ADDR ...] bcc email address(es)
-xu USERNAME username for SMTP authentication
-xp PASSWORD password for SMTP authentication

Paranormal:
-b BINDADDR[:PORT] local host bind address
-l LOGFILE log to the specified file
-v verbosity, use multiple times for greater effect
-q be quiet (i.e. no STDOUT output)
-o NAME=VALUE advanced options, for details try: --help misc
-o message-file=FILE -o message-format=raw
-o message-header=HEADER -o message-charset=CHARSET
-o reply-to=ADDRESS -o timeout=SECONDS
-o username=USERNAME -o password=PASSWORD
-o tls= -o fqdn=FQDN

Help:
--help the helpful overview you're reading now
--help addressing explain addressing and related options
--help message explain message body input and related options
--help networking explain -s, -b, etc
--help output explain logging and other output options
--help misc explain -o options, TLS, SMTP auth, and more




This is how my batchscript looks like



x:

SendEmail -f alert@mydomain.org -t ict@mydomain.org -u alert %1 -m "Alert %1 !" -s mymailserver


exit



In the firstline I go to X: this is where my SendEmail folder is.

In the secondline I actually fireup SendEmail.


-f this is the from address


-t this is the to address


-u this the subject line: You’ll notice the %1 in %1 the batchscripts reads the first argument that is given when starting  the alert I’ll explain this later. 
-m is the message body.


-s is the mailserver




The script is now ready save it where it can be found easily.

(I store all my running scripts in a production environment in folder called active scripts, decommisioned scripts I store in a folder called inactive scripts)




Now I’ll explain the %1 this is an variable that stores the argument that was given when starting the batchscript.

On the Action Tab in you’ll see a button called “command line arguments” click it now you will see this screen



 



image



The checkbox “Single Argument String” means that the script is called with 1 argument so you can only use %1 because you only have a single argument.

If you deselect it the check arguments are all in different variables %1 will be the name of the Alert %2 will be the date and time %3 measured value you’ll get the idea.





I want all the data in 1 string and I want all arguments.





Now hit “OK”



Now when the alert is triggered we get an email with all the data we need.




I hope this helps you in monitoring your servers.

No comments:

Google