Windows系统硬盘剩余空间的监控
公司有几台Windows 2008服务器,经常忘记清理磁盘空间,就自己动手用VC#写了个小工具做了个磁盘监控的工具.放在计划任务里边执行.这样就可以自动每隔几个小时就自动检查系统中磁盘的剩余空间状况,如果超过设定的最低空间值,就向指定的邮件地址发送报警邮件.另外,为了方便监控,还提供日志查阅.
实现方法很简单,代码如下:
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 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 | using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net; using System.Net.Mail; using System.Xml; namespace DiskspaceMonitor { class DSM_Main { static void Main() { // 读取配置文件 XmlDocument myxml = new XmlDocument(); myxml.Load(Directory.GetCurrentDirectory() + "\\config.xml" ); XmlElement root = myxml.DocumentElement; // 从配置文件中获取接收者邮件地址,多个用逗号分隔 XmlNode nod = root.SelectSingleNode( "/Setting/MailReceiver/MailAddress" ); string lcReceiverMailAddress = nod.InnerText; // 从配置文件中获取报警阀值,单位是MB nod = root.SelectSingleNode( "/Setting/AlertThresholdSetting/Threshold" ); long lnAlertThreshold = System.Int32.Parse(nod.InnerText); bool lbSendMail = false ; string lcMessageText = "警告信息:" + "\r\n" + "\r\n" ; string lcDriverName = "" ; long lcDriverTotalFreeSpace = 0; string lcMachineName = System.Environment.MachineName; //IPHostEntry ipEntry = Dns.GetHostEntry(lcMachineName); IPHostEntry ipEntry = Dns.GetHostByName(lcMachineName); string lcMachineIPAddress = ipEntry.AddressList[0].ToString(); DriveInfo[] allDrivers = DriveInfo.GetDrives(); foreach (DriveInfo d in allDrivers) { if (d.IsReady == true && d.DriveType == System.IO.DriveType.Fixed) { lcDriverTotalFreeSpace = d.TotalFreeSpace / 1024 / 1024; lcDriverName = d.Name; if (lcDriverTotalFreeSpace <= lnAlertThreshold) { lbSendMail = true ; lcMessageText = lcMessageText + " 本地磁盘" + lcDriverName + " 的可用剩余空间只剩下 " + lcDriverTotalFreeSpace + " MB." ; lcMessageText = lcMessageText + "超过了预定义的警告值 " + lnAlertThreshold + " MB. 请马上清理,否则系统将会出现严重问题!" ; lcMessageText = lcMessageText + "\r\n" ; } } } if (lbSendMail == true ) { lcMessageText = lcMessageText + "\r\n" ; lcMessageText = lcMessageText + "此邮件为系统自动发送,请勿回复!谢谢~" + "\r\n" ; lcMessageText = lcMessageText + "--By MailRobot" + "\r\n" ; lcMessageText = lcMessageText + System.DateTime.Now + "\r\n" ; // 从配置文件中获取发送邮件账户名称和密码 nod = root.SelectSingleNode( "/Setting/SMTP/Account" ); string lcSMTPAccountName = nod.InnerText; nod = root.SelectSingleNode( "/Setting/SMTP/Password" ); string lcSMTPAccountPassword = nod.InnerText; System.Net.Mail.MailMessage mymail = new System.Net.Mail.MailMessage(); mymail.To.Add(lcReceiverMailAddress); mymail.From = new MailAddress( "robot@barhe.org" , "Barhe MailRobot" , System.Text.Encoding.UTF8); mymail.Subject = "报警!请检查电脑 " + lcMachineName + "(" + lcMachineIPAddress + ")" + " 磁盘分区的可用磁盘空间." ; mymail.SubjectEncoding = System.Text.Encoding.UTF8; mymail.Body = lcMessageText; mymail.BodyEncoding = System.Text.Encoding.UTF8; mymail.IsBodyHtml = false ; mymail.Priority = MailPriority.High; SmtpClient client = new SmtpClient(); client.Credentials = new System.Net.NetworkCredential(lcSMTPAccountName, lcSMTPAccountPassword); client.Host = "smtp.barhe.org" ; client.Send(mymail); LogManager.WriteLog(LogFile.DiskspaceMonitor, "经检查,计算机 [" + lcMachineName + "] 部分磁盘空间容量过低,已发送报警邮件." ); } else { LogManager.WriteLog(LogFile.DiskspaceMonitor, "经检查,计算机 [" + lcMachineName + "] 中各分区剩余容量正常." ); } return ; } } } public class LogManager { private static string logPath = string .Empty; public static string LogPath { get { logPath = System.Environment.CurrentDirectory; return logPath; } set { logPath = value; } } private static string logFielPrefix = string .Empty; public static string LogFielPrefix { get { return logFielPrefix; } set { logFielPrefix = value; } } public static void WriteLog( string logFile, string msg) { try { /* System.IO.StreamWriter sw = System.IO.File.AppendText( LogPath + LogFielPrefix + logFile + " " + DateTime.Now.ToString("yyyyMMdd") + ".Log" ); */ System.IO.StreamWriter sw = System.IO.File.AppendText( LogPath + "\\" + logFile + ".log " ); sw.WriteLine(DateTime.Now.ToString( "yyyy-MM-dd HH:mm:ss: " ) + msg); sw.Close(); } catch { } } public static void WriteLog(LogFile logFile, string msg) { WriteLog(logFile.ToString(), msg); } } public enum LogFile { DiskspaceMonitor, Trace, Warning, Error, SQL } |
这里边需要另外设置一个配置文件config.xml,内容如下:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 | <!--?xml version="1.0" encoding="utf-8"?--> < setting > # 报警接收Email地址 < mailreceiver > < mailaddress >me@barhe.org</ mailaddress > </ mailreceiver > # 发送邮件帐户配置 < smtp > # 这里填入认证用户名.一般自建服务器的,只需要填写邮件的前缀.第三方的企业邮箱(如QQ企业邮箱/网易企业邮箱/263企业邮箱等)则需要把邮箱名写全. < account >robot@barhe.org</ account > < password >mailpassword</ password > </ smtp > # 报警的阀值,单位是MB < alertthresholdsetting > < threshold >6144</ threshold > </ alertthresholdsetting > </ setting > |
然后将程序编译完,把它和配置文件放在一个文件夹里边,再另外写一个批处理,如下:
1 2 3 4 5 | @ echo off d: cd "Program Files\DiskspaceMonitor" DiskspaceMonitor.exe @ echo on |
然后再在计划任务里边调用这个批处理文件就可以了.
其实之前有试过直接在计划任务里边调用编译好的EXE文件,但怎么都不能成功.试了N次失败之后,就改用批处理来弄了…