最近发现公司运行的web网站应用程序池会突然停止,做的负载均衡(路由负载)又无法监测到IIS应用程序池的情况,就想着通过某一种监控方式监测IIS应用程序池的情况,如果处关闭状态则立刻重新启动.所说的IIS应用程序池监控也只能算是伪监控,不喜勿喷.
运行:winform + windows计划任务
1.首先获取IIS版本号
///<summary>///获取当前计算机相关信息///</summary>publicstaticclassAcquireComputerInfo
{///<summary>///获取当前IIS版本号///</summary>///<param name="domainname"></param>///<returns></returns>publicstaticstringGetIssVersionByDri(stringdomainname ="")
{//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注册表中的项级节点.此类是注册表封装stringissversion =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());}returnissversion;
}///<summary>///获取当前计算机信息 例如:计算机名:aaa,ip:10.10.10.10///</summary>///<returns></returns>publicstaticstringGetComputerInfo()
{varcomputerNmae = System.Net.Dns.GetHostName();//获取当前计算机名称varcomputerIp =newSystem.Net.IPAddress(System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList[0].Address).ToString();//获取局域网IPreturnstring.Format("PC_Name: {0},IP: {1}",computerNmae,computerIp);
}
}
获取当前计算机相关信息
2.查看当前iis上所有的应用程序池的
/// <summary>
///检测当前线程池是否正常开启///</summary>publicvoidMonitoringISSAppPool()
{//string method_Recycle = "Recycle";//Start开启 Recycle回收 Stop 停止stringmethod_Start ="Start";vari =0;
DirectoryEntry appPool=newDirectoryEntry("IIS://localhost/W3SVC/AppPools");try{foreach(DirectoryEntry iteminappPool.Children)
{stringAppPoolCommand = item.Properties["ManagedPipelineMode"].Value.ToString();stringManagedRuntimeVersion = item.Properties["ManagedRuntimeVersion"].Value.ToString();//,net版本号stringAppPoolState = 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();varIIsMsg =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应用程序池意外停止可能有很多原因导致,例如:内存溢出或者代码逻辑问题,要想根本解决就只能安排程序员检查代码了.
不喜勿喷,如果有什么更好的意见或者想法欢迎提出宝贵意见哦....