示例#1
0
        private TargetLogRecord PingTarget(Target target)  // ICMP echo request and response.  This method is called if the target's monitor type is "Ping".
        {
            TargetLogRecord tlr = null;

            try
            {
                string strResultMessageForLog;
                long   lResponseTime;
                bool   bSuccess = NetworkPing.Send(target.URL, out strResultMessageForLog, out lResponseTime);

                string strResult = string.Format(@"Result of ping: '{0}'", strResultMessageForLog);

                LogToConsole(strResult);
                // TO_DO : Also log to database via LogToDatabase()
                tlr = new TargetLogRecord(target.TargetID, DateTime.UtcNow, TargetLogRecordStatus.Pass,
                                          strResult, 0 /* HTTP error code */, m_unLocationID, Convert.ToUInt32(lResponseTime) /* Response time in milliseconds */);
            }
            catch (Exception ex)
            {
                string strMessage = string.Format(@"Ping target: Caught {0} : {1}", ex.GetType().FullName, ex.Message);

                LogToConsole(strMessage);
                // TO_DO : Also log to database via LogToDatabase()
                tlr = new TargetLogRecord(target.TargetID, DateTime.UtcNow, TargetLogRecordStatus.Fail,
                                          strMessage, 0 /* HTTP error code */, m_unLocationID, 0 /* Response time in milliseconds */);
            }

            return(tlr);
        }
示例#2
0
        private TargetLogRecord PostToTarget(Target target)    // This method is called if the target's monitor type is "HTTP Post"
        {
            TargetLogRecord tlr = null;

            try
            {
                PostSubmitter post = new PostSubmitter();

                post.Url = target.URL;

                foreach (TargetFormField tff in target.FormFields)
                {
                    post.PostItems.Add(tff.FieldName, tff.FieldValue);
                }

                post.Type = PostSubmitter.PostTypeEnum.Post;

                string strResult = string.Format(@"Result of post to target: '{0}'", post.Post());

                // At this point, we know that we got a response, but is it the right response?
                // Or is it something like a custom 404 HTTP error page?  Or a "Login failed" page?

                //LogToConsole(strResult);  // This result is a page of HTML; it can be rather long.

                // TO_DO : Also log to database via LogToDatabase()
                tlr = new TargetLogRecord(target.TargetID, DateTime.UtcNow, TargetLogRecordStatus.Pass,
                                          strResult, 0 /* HTTP error code */, m_unLocationID, 0 /* Response time in milliseconds */);
            }
            catch (Exception ex)
            {
                uint unHTTPStatusCode = 0;

                if (ex is WebException)
                {
                    WebException ex2 = ex as WebException;

                    if (ex2.Response is HttpWebResponse)
                    {
                        HttpWebResponse httpwr = ex2.Response as HttpWebResponse;
                        HttpStatusCode  httpsc = httpwr.StatusCode;

                        unHTTPStatusCode = HTTPStatusCodeToUInt(httpsc);
                    }
                }

                string strMessage = string.Format(@"Post to target: Caught {0} : {1}", ex.GetType().FullName, ex.Message);

                LogToConsole(strMessage);
                // TO_DO : Also log to database via LogToDatabase()
                tlr = new TargetLogRecord(target.TargetID, DateTime.UtcNow, TargetLogRecordStatus.Fail,
                                          strMessage, unHTTPStatusCode /* HTTP error code */, m_unLocationID, 0 /* Response time in milliseconds */);
            }

            return(tlr);
        }
示例#3
0
        // Send an e-mail to each of the target's account's contacts.

        private void SendFailureNotificationEmails(Target target, TargetLogRecord tlr)
        {
            if (!m_dictSystemConfigurationEntries.ContainsKey(SystemConfigurationEntryKeys.SMTPServerAddress))
            {
                throw new Exception(@"Flare system configuration error: No SMTP server address is specified");
            }

            if (!m_dictSystemConfigurationEntries.ContainsKey(SystemConfigurationEntryKeys.SMTPServerUserName))
            {
                throw new Exception(@"Flare system configuration error: No SMTP server user name is specified");
            }

            if (!m_dictSystemConfigurationEntries.ContainsKey(SystemConfigurationEntryKeys.SMTPServerPassword))
            {
                throw new Exception(@"Flare system configuration error: No SMTP server password is specified");
            }

            string       strSMTPServerAddress  = m_dictSystemConfigurationEntries[SystemConfigurationEntryKeys.SMTPServerAddress];
            string       strSMTPServerUserName = m_dictSystemConfigurationEntries[SystemConfigurationEntryKeys.SMTPServerUserName];
            string       strSMTPServerPassword = m_dictSystemConfigurationEntries[SystemConfigurationEntryKeys.SMTPServerPassword];
            const string strFrom    = @"Flare Server Monitoring";
            const string strSubject = @"Server monitoring failure notification";

            foreach (Contact contact in m_lstAllContacts)
            {
                if (contact.AccountID != target.AccountID || !contact.Enabled)  // We could filter out disabled contacts at the SQL level
                {
                    continue;
                }

                string strBody = string.Format(@"Hello {0} {1}; Your server '{2}' was unreachable at {3} Universal Time.",
                                               contact.FirstName, contact.LastName, target.Name, MySqlUtils.DateTimeToString(tlr.TimeStamp));

#if DO_NOT_SEND_EMAILS
                LogToConsole(@"**** Simulating the sending of e-mail ****");
                LogToConsole(string.Format(@"SMTP Server Address : {0}", strSMTPServerAddress));
                LogToConsole(string.Format(@"SMTP Server User Name : {0}", strSMTPServerUserName));
                LogToConsole(string.Format(@"SMTP Server Password : {0}", strSMTPServerPassword));
                LogToConsole(string.Format(@"From : {0}", strFrom));
                LogToConsole(string.Format(@"To : {0}", contact.EmailAddress));
                LogToConsole(string.Format(@"Subject : {0}", strSubject));
                LogToConsole(string.Format(@"Body : {0}", strBody));
                LogToConsole(@"**** End of e-mail ****");
#else
                MailUtils.SendMail(strSMTPServerAddress, strSMTPServerUserName, strSMTPServerPassword,
                                   strFrom, contact.EmailAddress, null, null, strSubject, strBody);
#endif
            }
        }
示例#4
0
        private void ThreadMain_TestTarget()
        {
            LogToConsole(@"**** Thread starting ****");

            Target target = null;

            lock (m_qTargetsReadyForTesting)
            {
                if (m_qTargetsReadyForTesting.Count <= 0)
                {
                    // Throw an exception when we attempt to Dequeue() ?
                    return;
                }

                target = m_qTargetsReadyForTesting.Dequeue();
            }

            LogToConsole(string.Format(@"Testing the target {0} (at {1}).", target.Name, target.URL));

            // In a separate thread:
            // - HTTP Post to the target
            // - Handle any errors (by logging to a database and sending e-mail(s))
            // - Re-enqueue the TargetInfo object while the priority queue is locked.
            TargetLogRecord tlr = null;

            switch (target.MonitorType)
            {
            case MonitorType.eHTTPGet:
                tlr = GetFromTarget(target);
                break;

            case MonitorType.eHTTPPost:
                tlr = PostToTarget(target);
                break;

            case MonitorType.ePing:
                tlr = PingTarget(target);
                break;

                // Default case: Throw an exception?
            }

            if (tlr != null)
            {
                MySqlConnection con = DatabaseConnectionProvider.GetMySqlConnection();

                tlr.Insert(con);
                target.UpdateLastTargetLogID(tlr.LogID, con);

                if (tlr.Status == TargetLogRecordStatus.Fail)
                {
                    // Send an e-mail to each of the target's account's contacts.
                    SendFailureNotificationEmails(target, tlr);
                }
            }

            //target.LastMonitoredAt = DateTime.UtcNow;
            target.UpdateLastMonitoredAt(DateTime.UtcNow, DatabaseConnectionProvider.GetMySqlConnection());
            target.DateTimeOfNextMonitor = target.LastMonitoredAt.Value + target.MonitorIntervalAsTimeSpan;   // or += target.MonitorIntervalAsTimeSpan;
            // Update the target's LastMonitoredAt in the database
            m_pqWaitingTargets.Enqueue(target);

            LogToConsole(string.Format(@"The target {0} has been dequeued, bumped, and re-enqueued.", target.Name));

            LogToConsole(@"**** Thread ending ****");
        }