设为首页 收藏本站
查看: 2670|回复: 0

[经验分享] 一个监测IIS,并定时重新启动的程序。

[复制链接]

尚未签到

发表于 2015-8-13 11:57:18 | 显示全部楼层 |阅读模式
  using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Net;
  namespace WuyinIISControler
{
public class MainSrv : System.ServiceProcess.ServiceBase
{
  private System.Timers.Timer timer1;
  ///
  /// 必需的设计器变量。
  ///
  private System.ComponentModel.Container components = null;
  string url =  "http://www.5inet.net/checkIIS.asp";
  int timeout = 6000;
  int repeatTime = 300000;
  EventLog log =null;
  int Times = 1;
  
  public MainSrv()
  {
   // 该调用是 Windows.Forms 组件设计器所必需的。
   InitializeComponent();
  // TODO: 在 InitComponent 调用后添加任何初始化
  }
  // 进程的主入口点
  static void Main()
  {
   System.ServiceProcess.ServiceBase[] ServicesToRun;

   // 同一进程中可以运行多个用户服务。若要将
   //另一个服务添加到此进程,请更改下行
   // 以创建另一个服务对象。例如,
   //
   //   ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
   //
   ServicesToRun = new System.ServiceProcess.ServiceBase[] { new MainSrv() };
  System.ServiceProcess.ServiceBase.Run(ServicesToRun);
  }
  ///
  /// 设计器支持所需的方法 - 不要使用代码编辑器
  /// 修改此方法的内容。
  ///
  private void InitializeComponent()
  {
   this.timer1 = new System.Timers.Timer();
   ((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
   //
   // timer1
   //
   this.timer1.Interval = 300000;
   this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
   //
   // MainSrv
   //
   this.ServiceName = "WuyinIISControler";
   ((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
  }
  ///
  /// 清理所有正在使用的资源。
  ///
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }
  ///
  /// 设置具体的操作,以便服务可以执行它的工作。
  ///
  protected override void OnStart(string[] args)
  {
   log = new EventLog();
   log.Log = "Application";
   log.Source = this.ServiceName;
   foreach(string arg in args)
   {
    if(arg.ToUpper().StartsWith("/URL:"))
     this.url = arg.Split(Convert.ToChar(":"))[1]+":"+arg.Split(Convert.ToChar(":"))[2];
    if(arg.ToUpper().StartsWith("/TIMEOUT:"))
     this.timeout = int.Parse(arg.Split(Convert.ToChar(":"))[1].ToString())*1000;
    if(arg.ToUpper().StartsWith("/REPEATTIME:"))
     this.repeatTime = int.Parse(arg.Split(Convert.ToChar(":"))[1].ToString())*1000*60;
  }
   // TODO: 在此处添加代码以启动服务。
   this.timer1.Interval = this.repeatTime;
   this.timer1.Enabled=true;
   if(!CheckIIS(this.url))
    this.RestartIIS();
  }
  private bool CheckIIS(string url)
  {
   bool b = false;
   System.Net.WebResponse wsp = null;
   string msg = "正在执行第" + this.Times.ToString() + "次检测:\n检测地址:" + url + "\n超时设置:" + (this.timeout/1000).ToString() + "秒\n检测间隔时间:" + (repeatTime/1000/60).ToString() + "分\n检测结果:";
   try
   {
    System.Net.WebRequest wq = WebRequest.Create(url);
    wq.Timeout = this.timeout;
    wsp = wq.GetResponse();
    if(wsp.Headers.Count>0)
    {
     b = true;
     msg+="正常";
    }
    wsp.Close();
   }
   catch(Exception ex)
   {
    b = false;
    msg+="错误\n详细内容:" + ex.Message;
   }
   finally
   {
     log.WriteEntry(msg);
   }
   return b;
  }
  private void RestartIIS()
  {
   try
   {
    System.Diagnostics.Process.Start("iisreset.exe","/restart");
    log.WriteEntry("正在执行重新启动命令:iisreset.exe /restart");
   }
   catch(Exception ex)
   {
    log.WriteEntry("发生错误:\n" + ex.ToString());
   }
  }

  ///
  /// 停止此服务。
  ///
  protected override void OnStop()
  {
   // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
   this.timer1.Enabled=false;
   log.Close();
   log.Dispose();
  }
  private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  {
   this.Times++;
   if(!CheckIIS(this.url))
    this.RestartIIS();
  }
}
}

===============================================================================

using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
  namespace WuyinIISControler
{
///
/// ProjectInstaller 的摘要说明。
///
[RunInstaller(true)]
public class ProjectInstaller : System.Configuration.Install.Installer
{
  private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
  private System.ServiceProcess.ServiceInstaller serviceInstaller1;
  ///
  /// 必需的设计器变量。
  ///
  private System.ComponentModel.Container components = null;
  public ProjectInstaller()
  {
   // 该调用是设计器所必需的。
   InitializeComponent();
  // TODO: 在 InitializeComponent 调用后添加任何初始化
  }
  ///
  /// 清理所有正在使用的资源。
  ///
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if(components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }
  #region 组件设计器生成的代码
  ///
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  ///
  private void InitializeComponent()
  {
   this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
   this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
   //
   // serviceProcessInstaller1
   //
   this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
   this.serviceProcessInstaller1.Password = null;
   this.serviceProcessInstaller1.Username = null;
   //
   // serviceInstaller1
   //
   this.serviceInstaller1.DisplayName = "无垠IIS控制器";
   this.serviceInstaller1.ServiceName = "WuyinIISControler";
   this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
   this.serviceInstaller1.AfterInstall+=new InstallEventHandler(serviceInstaller1_AfterInstall);
   //
   // ProjectInstaller
   //
   this.Installers.AddRange(new System.Configuration.Install.Installer[] {
                       this.serviceProcessInstaller1,
                       this.serviceInstaller1});
  }
  #endregion
  private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
  {
   System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController("WuyinIISControler");
   sc.Start(new string[]{"/url:http://www.5inet.net/checkIIS.asp","/timeout:10","/repeatTime:5"});
   sc.Dispose();
  }
}
}
DSC0000.jpg

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-98438-1-1.html 上篇帖子: 先装VS2005再装IIS,出现访问IIS元数据库失败~[解决方案] 下篇帖子: Microsoft.Office.Interop.Excel.Application IIS权限问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表