Welcome, guest | Sign In | My Account | Store | Cart

Check available drive space. Email report if space usage exceeds 90%.

Tcl, 216 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
###########################################################
# Check available drive space. 
# Email alert if space usage exceeds 90%.
###########################################################

puts "\n executing [info script]\n"

# make script drive independent.

set drive [lindex [file split [info nameofexecutable]] 0 ] 

puts "\n proclib = $drive/scripts/TCL/proclib"

########################################################  
# Source utility procs.
########################################################  

source [ file join  $drive scripts/TCL/proclib/checkFile_proc.tcl ]
source [ file join  $drive scripts/TCL/proclib/smtp_proc.tcl ]
source [ file join  $drive scripts/TCL/proclib/reportHeader_proc.tcl ]
source [ file join  $drive scripts/TCL/proclib/printColumns.tcl ]
source [ file join  $drive scripts/TCL/proclib/convertToMb.tcl ]

########################################################  
# Source packages. 
########################################################  

package require twapi
package require Tclx
package require math::fuzzy
package require textutil

######################################
# Proc - check memory usage. 
######################################
proc driveSpace {} {

   puts "\n driveSpace \n"

   # calculate drive size and free space.  

   set drives [ concat [ twapi::get_logical_drives -type fixed ] \
                       [ twapi::get_logical_drives -type remote ]
	      ]

   foreach d $drives {

      array set a    [ twapi::get_drive_info $d -size -freespace ] 

      set spaceUsed  [ expr { (double ( $a(-size) - $a(-freespace) )/ $a(-size) ) } ] 

      set usedPerc   [ expr { [math::fuzzy::troundn $spaceUsed 2] *100 } ]

      set driveSize  [ concat [ toMB $a(-size) ]MB ]

      set driveSpace [ concat [ toMB $a(-freespace) ]MB ]

      lappend driveInfo [ list $d $driveSize $driveSpace $usedPerc ]

   }


#   if space usage is > 90 send error report.

   foreach e $driveInfo {

       if { [ lindex $e 3 ] > 90 } { 

	  set alert true 

       } else {

	  set alert false 

       }

   }

   if { [ string is true $alert ] } {
   
      writeReport $driveInfo
      
   }
   
}

######################################
# Proc - write report
######################################
proc writeReport { driveInfo } {
  
   global reportFile
   global reportFileId

   set header       "$::env(COMPUTERNAME) - Check Drive Space Usage"

   reportHeader $reportFileId $header $reportFile

   set s "*****************************************"
   set t1 [ format "%-15s %s" " " $s ]
   set s "The End"
   set t2 [ format "%-35s    %s" " " $s ]
   set t3 "$::env(COMPUTERNAME) - Space utilization." 

   puts $reportFileId \n$t3\n

   set width 12 
   set space " " 

   # format column headers in 12 character columns justified center. 

   set x [ list Drive  Size  FreeSpace  UsedPerc ] 

   foreach e $x {

      set c [ textutil::adjust $e -length $width -justify center -full true ]

      lappend lineH $c

   }

   set formatString "%-15s %s\n"
   
   puts $reportFileId [ format $formatString $space [ join $lineH ] ]
   
   set formatString "%-15s [ string repeat {%s } 4 ]" 
   
   # format detail line in 12 character columns with drive letter justified center and numerics justified right.

   foreach e $driveInfo {

       set driveLetter [ lindex $e 0 ]

       set c [ textutil::adjust $driveLetter -length $width -justify center -full true ]
        
       lappend line1 $c

       set y [ lrange $e 1 end ]

       foreach e $y {

          set c [ textutil::adjust $e -length $width -justify right ]

	  lappend line1 $c

       }

      set formatString "%-15s %s"

      puts $reportFileId [ format $formatString $space [ join $line1 ] ]

      unset line1
   }

   emailReport 

   ftruncate -fileid $reportFileId 0
}
###########################################
# Email Report 
###########################################

proc emailReport {} {

   global reportFile
   global reportFileId

   flush $reportFileId  

   set computerName $::env(COMPUTERNAME)

   set subject "$computerName - DriveSpace Alert."   

   sendSimpleMessage you@yourmail.com $subject $reportFile

}
######################################
# Control Section.
######################################

######################################
# Set Variables
######################################

set processId [ twapi::get_current_process_id ]

set eventId   [ twapi::eventlog_open -write ]

set data "DRIVESPACE STARTING"

twapi::eventlog_write $eventId 1 -type information -loguser -data $data

twapi::eventlog_close $eventId

set reportFile   [ file join $drive reports/notify/driveSpace.txt ]

#########################################
# Check if files exist.
#########################################

checkFile [file dirname $reportFile]

######################################
# Open output files.
######################################

set reportFileId  [open $reportFile w]

################################### 
# Global variables.
################################### 

global reportFile
global reportFileId

driveSpace
Created by Patrick Finnegan on Sun, 23 Jan 2005 (MIT)
Tcl recipes (162)
Patrick Finnegan's recipes (56)

Required Modules

  • (none specified)

Other Information and Tasks