最近發現公司運行的web網站應用程序池會突然停止,做的負載均衡(路由負載)又無法監測到IIS應用程序池的情況,就想着通過某一種監控方式監測IIS應用程序池的情況,如果處關閉狀態則立刻重新啟動.所說的IIS應用程序池監控也只能算是偽監控,不喜勿噴.
運行: winform + windows計划任務
1.首先獲取IIS版本號

/// <summary> /// 獲取當前計算機相關信息 /// </summary> public static class AcquireComputerInfo { /// <summary> /// 獲取當前IIS版本號 /// </summary> /// <param name="domainname"></param> /// <returns></returns> public static string GetIssVersionByDri(string domainname = "") { //try //{ // if (string.IsNullOrEmpty(domainname)) // { // //如果為空 則默認為本地機器 // domainname = "LOCALHOST"; // } // DirectoryEntry getEntity = new DirectoryEntry("IIS://" + domainname + "/W3SVC/INFO"); // string Versions = getEntity.Properties["MajorIISVersionNumber"].Value.ToString(); // return Versions; //} //catch (Exception se) //{ // //說明一點:IIS5.0中沒有(int)entry.Properties["MajorIISVersionNumber"].Value;屬性,將拋出異常 證明版本為 5.0 // //MessageBox.Show("獲取ISS的版本是發生異常信息:" + se.Message); // return "5.0"; //} //RegistryKey表示 Windows 注冊表中的項級節點.此類是注冊表封裝 string issversion = string.Empty; RegistryKey getkey = Registry.LocalMachine.OpenSubKey("software\\microsoft\\inetstp"); if (getkey != null) { issversion = Convert.ToInt32(getkey.GetValue("majorversion", -1)).ToString(); //MessageBox.Show(issversion.ToString()); } return issversion; } /// <summary> /// 獲取當前計算機信息 例如:計算機名:aaa,IP:10.10.10.10 /// </summary> /// <returns></returns> public static string GetComputerInfo() { var computerNmae = System.Net.Dns.GetHostName();//獲取當前計算機名稱 var computerIp = new System.Net.IPAddress(System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList[0].Address).ToString();//獲取局域網IP return string.Format("PC_Name: {0},IP: {1}",computerNmae,computerIp); } }
2.查看當前iis上所有的應用程序池的
/// <summary>
/// 檢測當前線程池是否正常開啟 /// </summary> public void MonitoringISSAppPool() { // string method_Recycle = "Recycle"; //Start開啟 Recycle回收 Stop 停止 string method_Start = "Start"; var i = 0; DirectoryEntry appPool = new DirectoryEntry("IIS://localhost/W3SVC/AppPools"); try { foreach (DirectoryEntry item in appPool.Children) { string AppPoolCommand = item.Properties["ManagedPipelineMode"].Value.ToString(); string ManagedRuntimeVersion = item.Properties["ManagedRuntimeVersion"].Value.ToString();//,net版本號 string AppPoolState = item.Properties["AppPoolState"].Value.ToString();//當前狀態 if (AppPoolState != "2") { DirectoryEntry findPool = appPool.Children.Find(item.Name, "IIsApplicationPool"); findPool.Invoke(method_Start, null); appPool.CommitChanges(); appPool.Close(); var IIsMsg = string.Format("[{0}],名稱:[{1}],NET版本:{2},標識:{3},當前狀態:[{4}],時間:{5}\n", PC_Info, item.Name, ManagedRuntimeVersion, item.SchemaClassName, "已啟動", DateTime.Now.ToString()); ClsLogHelper.m_CreateWorkLogTxt(item.Name, IIsMsg); } } //lblCount_text.Text = i.ToString(); } catch (Exception ex) { ClsLogHelper.m_CreateErrorLogTxt("MonitoringISSAppPool", string.Format("{0},時間:{1},啟動失敗", AcquireComputerInfo.GetComputerInfo(), DateTime.Now), ex.Message);//日志記錄錯誤信息 ClsLogHelper.Email("MonitoringISSAppPool", string.Format("{0},時間:{1},啟動失敗", AcquireComputerInfo.GetComputerInfo(), DateTime.Now)+" "+ ex.Message);//郵件發送錯誤信息 } }
3.那么剩下就是windows計划任務的工作了,簡單配置一下設置觸發器時間為每隔1秒中執行一次
到此一個IIS應用程序池偽監控就已經完成,當然在啟動應用程序池時,啟動失敗時還需要記錄詳細的錯誤信息,以及將發生錯誤的服務器名稱和IP通過郵件的方式發送給管理員,管理員可以在第一時間知道具體那台服務器IIS出現問題.
日志記錄:
通過日志記錄我們可以發現在第二條記錄中Audi這個應用程序池已經被正常啟動.
當然這種小工具只能臨時解決IIS應用程序池意外停止的問題,IIS應用程序池意外停止可能有很多原因導致,例如:內存溢出或者代碼邏輯問題,要想根本解決就只能安排程序員檢查代碼了.
不喜勿噴,如果有什么更好的意見或者想法歡迎提出寶貴意見哦....