Windows系统硬盘剩余空间的监控
公司有几台Windows 2008服务器,经常忘记清理磁盘空间,就自己动手用VC#写了个小工具做了个磁盘监控的工具.放在计划任务里边执行.这样就可以自动每隔几个小时就自动检查系统中磁盘的剩余空间状况,如果超过设定的最低空间值,就向指定的邮件地址发送报警邮件.另外,为了方便监控,还提供日志查阅.
实现方法很简单,代码如下:
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,内容如下:
# 报警接收Email地址 # 发送邮件帐户配置 me@barhe.org # 这里填入认证用户名.一般自建服务器的,只需要填写邮件的前缀.第三方的企业邮箱(如QQ企业邮箱/网易企业邮箱/263企业邮箱等)则需要把邮箱名写全. # 报警的阀值,单位是MBrobot@barhe.org mailpassword 6144
然后将程序编译完,把它和配置文件放在一个文件夹里边,再另外写一个批处理,如下:
@echo off d: cd "Program Files\DiskspaceMonitor" DiskspaceMonitor.exe @echo on
然后再在计划任务里边调用这个批处理文件就可以了.
其实之前有试过直接在计划任务里边调用编译好的EXE文件,但怎么都不能成功.试了N次失败之后,就改用批处理来弄了…