在使用zabbix做Windows服务器监控的时候遇到一个比较棘手的问题,检测IIS站点状态。
普通情况下,只要用浏览器访问iis站点测试一下返回码是不是200即可判断状态,但是我这次遇到的是iis使用了主机头,也就是说iis上的站点使用的是同一个IP地址,同一个端口号。外部请求是根据域名来判断访问哪个站点,并且IIS前面还有负载均衡器,这样我就不能让zabbix也通过域名的方式测试IIS了,因为这样不能判断是不是所有的IIS都没有问题。
为此,研究了如何在本地使用命令行的方式获取iis站点的运行状态,做成一个批处理文件供zabbix分析如下:
@echo off C:\Windows\System32\inetsrv\appcmd.exe list site | find "Stopped" >> D:\zabbix\iis.txt C:\Windows\System32\inetsrv\appcmd.exe list apppool | find "Stopped" >> D:\zabbix\iis.txt for /f "tokens=1 delims=(" %%1 in (D:\zabbix\iis.txt)do (set x=%%1&& call set x=%%x:"=%% call echo %%x%%) cd.>D:\zabbix\iis.txt
脚本说明:
第一行:关闭echo输出。
第二行:获取所有站点的状态,查找状态为stopped的站点,将结果放在iis.txt文件里。
第三行:获取所有应用程序池的状态,查找状态为stopped的程序池,将结果放在iis.txt文件里。
第四行:以左括号为分隔符(delims指定分隔符),截取iis.txt里第一列的数据(tokens=1指定第一列),并去掉双引号(如果有双引号,zabbix使用微信告警的时候,引号后面的内容就没了)
第五行:将iis.txt文件清空。方便下次使用。
如果所有站点都正常,该脚本返回为空。
如果有站点异常,该脚本返回如下:
表示名字为:portal的站点停了,但是portal的程序池没有停
名字为:defaultapppool的程序池停了,但是站点没有停
第一列的site表示站点故障,apppool表示应用程序池故障。
zabbix里调用这个脚本即可实现对iis站点的监控,如何添加监控项这里就不讲了。触发器可以选择:最近获取到的字符串长度大于1即报警。原因为这个脚本只有在iis站点或者程序池有停止的时候,才会返回停止的值。
【附录】
asp.net获取本地IIS上绑定的网站的信息
我们使用ADSI来操作IIS的时候,需要提供他们的Path。比如默认本机80端口的默认站点的目录路径就是:IIS://localhost/w3svc/1/root
它的格式是:
IIS://ComputerName/Service/Website/Directory
ComputerName:即操作的服务器的名字,可以是名字也可以是IP,经常用的就是localhost
Service:即操作的服务器,IIS中有Web,也有FTP,还有SMTP这些服务,我们主要是操作IIS的Web功能,因此此处就是”W3SVC”,如果是FTP则应是”MSFTPSVC”
WebSite:一个IIS服务中可以包括很多的站点,这个就用于设置操作的站点。他的值是一个数字,默认是1,表示缺省站点,如果有其它,也是数字。
但需要注意的是,并不是自增。后面会有一个小程序获得这个值。
Directory:要操作的目录名称,一个站点一般顶层目录为”ROOT”,其它目录则是他的孩子(Child)。
以上资料摘自飞刀的文章,具体看:
http://aspcool.com/lanmu/browse1.asp?ID=914&bbsuser=csharp
http://aspcool.com/lanmu/browse1.asp?ID=915&bbsuser=csharp
由于上面的WebSite 并不是简单的自增。我们要知道某台机子上所有站点对应的值,可以通过下面的小程序获得这个值:
using System.DirectoryServices;
……..
DirectoryEntry root = newDirectoryEntry(“IIS://localhost/W3SVC”);
foreach(DirectoryEntry dir in root.Children)
{
if(dir.SchemaClassName == “IIsWebServer”)
{
string ww =dir.Properties["ServerComment"].Value.ToString();
Response.Write(string.Format(“IIS://localhost/W3SVC/{0}/ROOT/ {1}<br>”,dir.Name,ww));
}
}
当然,你想获得更多的属性值,可以通过 dir.Properties[] 去获得。
为了说明这里的 WebSite 并不是自增的,下面看我在我本机执行上面程序的结果。
IIS://localhost/W3SVC/1/ROOT/ Default WebSite
IIS://localhost/W3SVC/1307630583/ROOT/ MyWeb81
IIS://localhost/W3SVC/1307630584/ROOT/ MyWeb82
IIS://localhost/W3SVC/1307630585/ROOT/ MyWeb83
IIS://localhost/W3SVC/1307630586/ROOT/ MyWeb84
IIS://localhost/W3SVC/1307630587/ROOT/ MyWeb85
IIS://localhost/W3SVC/1307630683/ROOT/ MyWeb90
IIS://localhost/W3SVC/1307630684/ROOT/ MyWeb91
IIS://localhost/W3SVC/1307630685/ROOT/ MyWeb92
IIS://localhost/W3SVC/1307630686/ROOT/ MyWeb93
IIS://localhost/W3SVC/1758797915/ROOT/ ghj1976.net
IIS://localhost/W3SVC/2/ROOT/ MicrosoftSharePoint Administration
IIS://localhost/W3SVC/2546/ROOT/ 94
各个的值都不一样。所以在使用这个路径的时候,不要想当然的以为是简单的自增。
protected void Page_Load(object sender, EventArgs e)
{
DirectoryEntry directoryEntry = new DirectoryEntry(@"IIS://localhost/W3SVC");
IEnumerator ienum = directoryEntry.Children.GetEnumerator();
string HostInfo = "";
while (ienum.MoveNext())
{
DirectoryEntry entrypool = (DirectoryEntry)ienum.Current;
System.DirectoryServices.PropertyCollection ppC = (System.DirectoryServices.PropertyCollection)entrypool.Properties;
IDictionaryEnumerator idenum = ppC.GetEnumerator();
if (entrypool.SchemaClassName == "IIsWebServer")
{
string[] serverBind = ppC["ServerBindings"][0].ToString().Split(':');//获取网站绑定的IP,端口,主机头
string EnableDeDoc = ppC["EnableDefaultDoc"][0].ToString();
string DefaultDoc = ppC["DefaultDoc"][0].ToString();//默认文档
string MaxConnections = ppC["MaxConnections"][0].ToString();//iis连接数,-1为不限制
string ConnectionTimeout = ppC["ConnectionTimeout"][0].ToString();//连接超时时间
string MaxBandwidth = ppC["MaxBandwidth"][0].ToString();//最大绑定数
string ServerState = ppC["ServerState"][0].ToString();//运行状态 HostInfo += "站点描述:" + ppC["ServerComment"][0].ToString() + "<br>IP地址:" + serverBind[0].ToString() + "<br>TCP端口:" + serverBind[1].ToString() + "<br>主机头:" + serverBind[2].ToString() + "<br>";//获取IIS下所有站点名称
HostInfo += "启用默认文档:" + EnableDeDoc + "<br>";
HostInfo += "默认文档:" + DefaultDoc + "<br>";
HostInfo += "最大连接:" + MaxConnections + "<br>";
HostInfo += "连接超时:" + ConnectionTimeout + "<br>";
HostInfo += "最大绑定数:" + MaxBandwidth + "<br>";
HostInfo += "运行状态:" + ServerState + "<br><br>";
}
}
Response.Write(HostInfo);
Response.End();
}