用C#的控制台程序监控apache网站是否正常
2012-01-16 16:33
696 查看
自己随便写的,没用在真实环境上。仅供参考。
添加一个应用
选中 System.ServiceProcess
下面是源代码
希望大家给出意见。
自己感觉不足的地方,一是超时没有判断。二是对apache的假死还没想到合适的办法。
添加一个应用
选中 System.ServiceProcess
下面是源代码
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.ServiceProcess; using System.Threading; namespace frey { class Program { static void Main(string[] args) { string pageUrl = "http://localhost/"; bool istrue = false; while (true) { HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(pageUrl); HttpWebResponse myHttpWebResponse; //=无法连接到远程服务器 try { myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); /* HttpStatusCode.OK 等效于 HTTP 状态 200。OK 指示请求成功,且请求的信息包含在响应中。这是最常接收的状态代码。 HttpStatusCode.BadGateway 等效于 HTTP 状态 502。BadGateway 指示中间代理服务器从另一代理或原始服务器接收到错误响应。 HttpStatusCode.NotFound 等效于 HTTP 状态 404。NotFound 指示请求的资源不在服务器上。 */ if (myHttpWebResponse.StatusCode == HttpStatusCode.OK) { istrue = true; } else { istrue = false; } myHttpWebResponse.Close(); } catch (WebException ex) { istrue = false; Console.WriteLine(ex.Status); //ConnectFailure } //判断网站是否正常 Console.WriteLine(istrue.ToString()); //不正常重启APACHE服务 if (istrue == false) { ServiceController sc = new ServiceController(); sc.ServiceName = "apache2.2"; if (sc.CanStop) { if (sc.Status == ServiceControllerStatus.Running) { sc.Stop(); Console.WriteLine("停止apahe服务器"); } } Console.WriteLine(sc.Status); if (sc.Status == ServiceControllerStatus.Stopped) { try { sc.Start(); Console.WriteLine("时间:{0}", DateTime.Now.ToString());//时间 Console.WriteLine("启动apahe服务器"); } catch { } } sc.Close(); } Thread.Sleep(6000); Console.WriteLine("6秒"); //每6秒检测一次,实际环境可设为60秒或更多 } } } }
希望大家给出意见。
自己感觉不足的地方,一是超时没有判断。二是对apache的假死还没想到合适的办法。
相关文章推荐
- C# 通过程序监控网站是否正常
- C# 通过程序监控网站是否正常
- c#实现网站监控查看是否正常示例
- 如何用.net程序来判断一个网站是否正常打开小解
- C#利用HttpWebRequest 测试网站是否可以正常访问
- 如何监控网站URL是否正常(curl & wget)
- 监控网站是否可以正常打开的Shell脚本分享
- 一起动手实现Windows服务程序(监控网站是否能访问)
- 一起动手实现Windows服务程序(监控网站是否能访问)
- Zabbix自定义监控网站服务是否能够正常响应
- 监控网站是否可以正常打开的Shell脚本分享
- 简单监控网站访问是否正常的shell脚本,邮件报警。网站恢复后继续运行。
- Zabbix自定义监控网站服务是否能够正常响应
- .NET C# 【小技巧】控制台程序,运行是否弹出窗口选择!
- 简单的hbase监控程序,用于监测hbase集群是否正常运行--66
- 一起动手实现Windows服务程序(监控网站是否能访问) .
- .net中判断该应用程序是否已经启动,防止重复启动,监控程序启动是否正常
- C#实现对站点、程序池状态的监控,以及URL能正常返回的监控,状态异常,邮件预警
- Apache、nginx配置的网站127.0.0.1可以正常访问,内外网的ip地址无法访问,谁的锅?
- mysql和nginx服务是否正常监控脚本