Sunday, May 26, 2013

How to check server disk space and mail warning when disk space is low

In this article I will explain how to write a program that check the disk space
If the disk space is less than specific percentage the program will send an email
to specific key users.
This program will send an email in case that disk "C:\" has free disk
space less than 10% ,of course you can modify
the program so it will get the drive , percent ,email list from set up file.

Lets start to explain how the program work

1:- We have to import Net mail by using the following command

Imports System.Net.Mail

2:- Declare the following varibles outside the procedures and function so allways we can access them

      2.1: FreeSize the total free size in disk in bytes.
      2.2: TotalSize total disk size in bytes.
      2.3: perln free disk space prcent * 100
             By dividing this number by 100 I will get
             precent with just two digits after point.
      2.4 per the precent of disk space

Dim FreeSize, TotalSize, perln As Long
Dim per As Double

3:- We will write function to check the disk space

Private function ChkDsk() As Boolean
'******* Declare var info varibale
Dim cdrive As System.IO.DriveInfo
'******* init our varibale to check disk c
Dim cdrive = My.Computer.FileSystem.GetDriveInfo("C:\")
'******* Get free disk space in bytes
FreeSize = cdrive.TotalFreeSpace
'******* Get total disk space
TotalSize = cdrive.TotalSize
'******* Get free disk space pecent * 100
perln = (UsedSize / TotalSize) * 10000
'******* Divide prcent by 100 to get free disk space
'******* In format ##.##%
per = perln / 100
******** Return true if free disk space less than 10%
If per < 10 then Return True else Return False
End

=======================================================
4:- Write a function that send mail to key users


This function receive 2 parameters
       - Mail from (email address of the "sender")
       - Mai to (users that will receive email addresses separated by ";")
For example user1@yourcompany.com ; user2@yourcompany.com

Our function should look like this

Private function  SndWarnMsg(ByVal efromas As String, ByVal eto As _
String) As Boolean

Dim Mail As New MailMessage
Dim S, MailS As String
Dim Ar

Dim I as integer

Try
'********* Our mail message subject
Mail.Subject = "Low disk space in Server"
'********* Split to mail address
ar = Split(eto, ";")
'********* Add to mail address
For i = 0 To UBound(ar)
     MailS = Trim(ar(i))

     Mail.To.Add(MailS)

Next

'********* Our mail sender
Mail.From = New MailAddress(efromas)
'********* This varibale contains our mail message

S = "This is an automatic message from  Server _
because of low disk space" + vbCrLf

S =S + "The free disk percentage is : " & per & "%"  &vbCrLf

'********* Our mail message

Mail.Body = S

'********* SMTP server info/Account info/Port info
Dim SMTP As New SmtpClient(Your Mail Server/or Mail server IP)
SMTP.EnableSsl = False

SMTP.Credentials = New System.Net.NetworkCredential(sender mail user , mail pwd)
SMTP.Port = 25 'Mail port uusually 25
'********* Send mail
SMTP.Send(Mail)
Return true

Catch ex As Exception
    return false  '****** return false In case that error ocur
End Try

End function

5:- Put all togther
     In page load event we will run ChkDsk function if it return true then we will send message
     by using SndWarnMsg
     the next step to stop this program by using end statment.
     Actually i will add this program to the Scheduled Tasks to check the disk every day

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


Dim sts as boolean
if ChkDsk=true then
    sts=SndWarnMsg("youremail","email1;email2;email3......")
if sts=true then end Else msgbox("Error ocur while sending mail")
  End Sub =================================================================================== ===================================================================================

  Actually you can change the program to use data file that contains program setting It is recommended to 
  get the following variables fro setting file
               1:-Disk to checked "C:\ , D:\ ,E:\..................."
               2:-Disk space percentage to start warn user that there is a problem (10%,9%........)
               3:-Mail sender
               4:-Receivers mail address separated by ";"
               5:-Error message text
Finally add this program to the Scheduled Tasks to check the disk every day or every
some hours.





                                         

No comments:

Post a Comment