Esempio n. 1
0
        protected override void OnStart(string[] args)
        {
            strWorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
            System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);

            NativeFunc.Init();

            frequncy          = new Timer();
            frequncy.Interval = NativeFunc.period * 1000;   //every 30 secs
            frequncy.Elapsed += new System.Timers.ElapsedEventHandler(this.frequncy_Tick);
            frequncy.Enabled  = true;

            NativeFunc.Bob();
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         //                            .WriteTo.LiterateConsole()
                         .WriteTo.RollingFile(System.AppDomain.CurrentDomain.BaseDirectory + "logs\\tcpmon-{Date}.txt")
                         .CreateLogger();

            if (args.Length == 0)
            {
//                File.AppendAllText("c:\\temp\\mylog.txt", "Run it as a service");
                // Run your service normally.
                Log.Information("Run it as a service");
                ServiceBase[] ServicesToRun = new ServiceBase[] { new SmartFixService() };
                ServiceBase.Run(ServicesToRun);
            }
            else if (args.Length == 1)
            {
                switch (args[0])
                {
                case "-install":
                    InstallService();
                    StartService();
                    break;

                case "-uninstall":
                    StopService();
                    UninstallService();
                    break;

                case "-normal":
                    NativeFunc.Init();
                    NativeFunc.Bob();
                    break;

                case "-testmail":
                    System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);
                    NativeFunc.loadSettings("settings.json");
                    NativeFunc.notifyAdmin();
                    Console.WriteLine("Done! check your email inbox or the log file");
                    break;

                default:
                    throw new NotImplementedException();
                }
            }
        }
Esempio n. 3
0
        public static void Bob()
        {
            Log.Information("get total sockets");
            int total = 0;

            NativeFunc.MIB_TCPTABLE tcpTableData = new NativeFunc.MIB_TCPTABLE();
            tcpTableData = GetTcpTableInfo();
            for (int i = 0; i < tcpTableData.dwNumEntries; i++)
            {
                ushort localPort = GetTcpPort(tcpTableData.table[i].dwLocalPort);
//                ushort localPort = GetTcpPort(tcpTableData.table[i].dwRemotePort);

                if (localPort == port)
                {
                    //https://msdn.microsoft.com/en-us/library/windows/desktop/aa366909(v=vs.85).aspx
                    if (tcpTableData.table[i].dwState == state)
                    {
                        total++;
                    }
                }
            }

            Log.Information("{0} connections which have state = {1}", total, state);

            if (total >= threshold)
            {
                NativeFunc.notifyAdmin();
            }

            if (serviceMonitored.restart.Value)
            {
                Log.Information("Service {0} is configured as restart-able", serviceMonitored.name.Value);
                if (total >= serviceMonitored.condition.Value)
                {
                    Log.Information("Service {0} is going to restart", serviceMonitored.name.Value);
                    int timeoutMilliseconds = (int)(1000 * serviceMonitored.timeout.Value);
                    RestartService(serviceMonitored.name.Value, timeoutMilliseconds);
                }
            }
        }