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.





                                         

Sunday, May 12, 2013

SQL –Ignore and remove undesirable characters using Sql. statement.


I wrote a portal for our customers.
Using this portal they can follow-up their orders status.
Our data base contain our item number and our customer
item number.
Usually when our customer want information about specific
product they use their item code.

One of our customers has not consistently codes for his items.
His items look as follow :
 Y-55121 Y 55122 Y55131 55141Y,55141 Y ,55145-Y.

Actually he add Y to his item code to indicate who is the
supplier of this item.
He added Y to his code because our company name start with Y.

This customer want to query our database by using his item code.
i.e. the number without addition 55141,55121 …
for instance.

How to make this search ?
It is imposible to perfom search for every combination because
we have many combinations and also it is very hard to perfom
search from item to item

For example :
Our search is: From 5513 To 5514 and our database contains the following Items:
 Y55134 ,Y-55136 ,Y 55133 ,55131Y ,55135 Y,55132-Y
All these items should appear in our search.

The best way is to ignore the 'Y' and '-' and perform search by
numbers only.

How to do it?

1:- I will replace the 'Y' by *blank using replace function.
2:- Replace '-' by *blank using replace function.
3:- Remove blanks from the field in section 2.
4:- Perfom serach by Item number.

Actually we can make it using one SQL statement as follow



For SQL400
Select * from MyTable where
trim(replace(replace(CusItno,'Y',' '),'-',' '))>='5133' and
trim(replace(replace(CusItno,'Y',' '),'-',' '))<='5134'


For SQL server
Select * from MyTable where
ltrim(rtrim(replace(replace(CusItno,'Y',' '),'-',' ')))>='5133' and
ltrim(rtrim(replace(replace(CusItno,'Y',' '),'-',' ')))<='5134'

PS: In SQL400 we can use trim function to remove blanks from right and left
However there is no such function in SQL Server that’s why we
replace it with 2 functions ltrim that remove blanks from left and
rtrim that remove blanks from right