public void Save(string dir)
        {
            // create directory if required
            //string dir = Path.GetDirectoryName(statsFile);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            StreamWriter writer = null;

            try
            {
                writer = new StreamWriter(Path.Combine(dir, statsFile));

                foreach (int day in days.Keys)
                {
                    StatsLine statsLine = (StatsLine)days[day];

                    // write line
                    writer.WriteLine("{0} {1} {2}", day, statsLine.BytesSent, statsLine.BytesReceived);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(String.Format("Can't open '{0}' log file", statsFile), ex);
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Perform default statistics line calculation.
        ///  Suits for the following providers: IIS 6.0;
        /// </summary>
        /// <param name="line">Statistic line is being calculated</param>
        /// <param name="fields">Log file available fields</param>
        /// <param name="values">Log file available values for the line is being read</param>
        private void Default_CalculateStatisticLine(StatsLine line, string[] fields, string[] values)
        {
            int cs_bytes = Array.IndexOf(fields, "cs-bytes");
            int sc_bytes = Array.IndexOf(fields, "sc-bytes");

            // run default calculation logic
            if (cs_bytes > -1)
            {
                line.BytesReceived += Int64.Parse(values[cs_bytes]);
            }
            //
            if (sc_bytes > -1)
            {
                line.BytesSent += Int64.Parse(values[sc_bytes]);
            }
        }
        private void Load()
        {
            //
            StreamReader reader = new StreamReader(statsFile);
            string       line   = null;

            while ((line = reader.ReadLine()) != null)
            {
                // parse line
                string[] columns = line.Split(new char[] { ' ' });
                int      day     = Int32.Parse(columns[0]);

                // add new stats line to the hash
                StatsLine statsLine = new StatsLine();
                statsLine.BytesSent     = Int64.Parse(columns[1]);
                statsLine.BytesReceived = Int64.Parse(columns[2]);

                days.Add(day, statsLine);
            }
            reader.Close();
        }
Beispiel #4
0
        public DailyStatistics[] GetDailyStatistics(DateTime since, params string[] keyValues)
        {
            ArrayList days = new ArrayList();

            // read statistics
            DateTime now  = DateTime.Now;
            DateTime date = since;

            if (date == DateTime.MinValue)
            {
                date = GetLogsBeginDate();
            }

            // iterate from since to now
            while (date < now)
            {
                // get monthly statistics
                MonthlyStatistics stats = GetMonthlyStatistics(date.Year, date.Month, keyValues);
                foreach (int day in stats.Days.Keys)
                {
                    StatsLine       line       = stats[day];
                    DailyStatistics dailyStats = new DailyStatistics();
                    dailyStats.Year          = date.Year;
                    dailyStats.Month         = date.Month;
                    dailyStats.Day           = day;
                    dailyStats.BytesSent     = line.BytesSent;
                    dailyStats.BytesReceived = line.BytesReceived;

                    days.Add(dailyStats);
                }

                // advance month
                date = date.AddMonths(1);
            }

            return((DailyStatistics[])days.ToArray(typeof(DailyStatistics)));
        }
Beispiel #5
0
        public void ParseLogs <T>() where T : LogReader
        {
            // ensure calculation logic has been initialized
            // with the default calculating routine
            if (CalculateStatisticsLine == null)
            {
                CalculateStatisticsLine += new CalculateStatsLineEventHandler(Default_CalculateStatisticLine);
            }
            //
            string statsDir  = GetStatsFilePath();
            string statsName = null;
            // get site state
            LogState logState = new LogState(statsDir + logName + ".state");

            LogReader reader = (LogReader)Activator.CreateInstance(typeof(T));

            reader.Open(logsPath, logState.LastAccessed, logState.Line);

            Hashtable monthlyLogs = new Hashtable();

            while (reader.Read())
            {
                try
                {
                    // skip error and system lines
                    if (reader.ErrorLine || reader.SystemLine)
                    {
                        continue;
                    }
                    // skip block with log data if fields aren't available
                    if (!reader.CheckFieldsAvailable(_fields))
                    {
                        continue;
                    }
                    //
                    string[] dateParts = reader["date"].Split('-');                     // yyyy-mm-dd
                    int      day       = Int32.Parse(dateParts[2]);

                    string[] keyValues = new string[keyFieldsLength];
                    //
                    for (int i = 0; i < keyFieldsLength; i++)
                    {
                        keyValues[i] = reader[keyFields[i]];
                    }
                    //
                    if (ProcessKeyFields != null)
                    {
                        ProcessKeyFields(keyFields, keyValues, reader.LineFields, reader.LineValues);
                    }
                    // build stats file name
                    statsName = GetMothlyStatsFileName(dateParts[0], dateParts[1], keyValues);
                    //
                    MonthlyStatistics monthlyStats = (MonthlyStatistics)monthlyLogs[statsName];
                    if (monthlyStats == null)
                    {
                        // add new statistics
                        try
                        {
                            monthlyStats           = new MonthlyStatistics(Path.Combine(statsDir, statsName), true);
                            monthlyLogs[statsName] = monthlyStats;
                        }
                        catch (Exception ex)
                        {
                            // Handle an exception
                            Log.WriteError(String.Format("LogParser: Failed to instantiate monthly stats file '{0}' at '{0}' path", statsName, statsDir), ex);
                            // SKIP OVER THE NEXT ITERATION
                            continue;
                        }
                    }

                    // get current day from statistic
                    StatsLine dayStats = monthlyStats[day];
                    if (dayStats == null)
                    {
                        dayStats          = new StatsLine();
                        monthlyStats[day] = dayStats;
                    }
                    // perform statistics line calculation
                    // this workaround has been added due to avoid
                    // IIS 6 vs. IIS 7 log files calculation logic discrepancies
                    CalculateStatisticsLine(dayStats, reader.LineFields, reader.LineValues);
                }
                catch (Exception ex)
                {
                    Log.WriteError(String.Format("Failed to process line {0}, statistics directory path {1}, statistics file name {2}", reader.LogLine, statsDir, reader.LogName), ex);
                }
            }

            // save all accumulated statistics
            foreach (MonthlyStatistics monthlyStats in monthlyLogs.Values)
            {
                monthlyStats.Save(statsDir);
            }

            // save site state
            logState.LastAccessed = reader.LogDate;
            logState.Line         = reader.LogLine;
            logState.Save();
        }
Beispiel #6
0
		private void LogParser_CalculateStatisticsLine(StatsLine line, string[] fields, string[] values)
		{
			int cs_method = Array.IndexOf(fields, "cs-method");
			// bandwidth calculation ignores FTP 7.0 serviced commands
			if (cs_method > -1 && !IsFtpServiceCommand(values[cs_method]))
			{
				int cs_bytes = Array.IndexOf(fields, "cs-bytes");
				int sc_bytes = Array.IndexOf(fields, "sc-bytes");
				// skip empty cs-bytes value processing
				if (cs_bytes > -1 && values[cs_bytes] != "0")
					line.BytesReceived += Int64.Parse(values[cs_bytes]);
				// skip empty sc-bytes value processing
				if (sc_bytes > -1 && values[sc_bytes] != "0")
					line.BytesSent += Int64.Parse(values[sc_bytes]);
			}
		}
		private void Load()
		{
			//
			StreamReader reader = new StreamReader(statsFile);
			string line = null;
			while ((line = reader.ReadLine()) != null)
			{
				// parse line
				string[] columns = line.Split(new char[] { ' ' });
				int day = Int32.Parse(columns[0]);

				// add new stats line to the hash
				StatsLine statsLine = new StatsLine();
				statsLine.BytesSent = Int64.Parse(columns[1]);
				statsLine.BytesReceived = Int64.Parse(columns[2]);

				days.Add(day, statsLine);
			}
			reader.Close();
		}