public static void addLog(string[] logs) { // Create the object here to save processing time! MysqlFactory dbCon = new MysqlFactory(); int line = 1; foreach (String str in logs) { // Sanitize the data firstly - squid inserts multiple spaces in the first column *ack* var temp = Regex.Replace(str, @"\s+", " ").Split(' '); if (temp.Count() != 10) { Console.WriteLine("ERROR: Malformed line in the proxy log - please see http://wiki.squid-cache.org/Features/LogFormat for proper format on line " + line); break; } else { LogRecord record = new LogRecord(); // convert unix timestamp to native DateTime type and then convert to string in format appropriate for mysql server DateTime data type. record.dateStamp = HelperMethods.UnixTimeStampToDateTime(Convert.ToDouble(temp[0])).ToString("yyyy-MM-dd hh:mm:ss"); record.duration = Int32.Parse(temp[1]); record.clientAddress = temp[2]; record.squidReturnCode = temp[3].Split('/')[0]; record.httpReturnCode = temp[3].Split('/')[1]; record.bytes = Int32.Parse(temp[4]); record.requestMethod = temp[5]; record.url = temp[6]; record.rfc931 = temp[7]; record.hierarchyCode = temp[8]; record.type = temp[9]; // http://www.bytechaser.com/en/articles/ckcwh8nsyt/display-progress-bar-in-console-application-in-c.aspx dbCon.Insert(record); } line++; } }
static void Main(string[] args) { // Check configuration HelperMethods.checkConfiguration(args); Console.WriteLine("Squid Log File Analyzer v1.0"); Console.WriteLine(); if (Options.daemonMode == true) { Console.WriteLine("Daemon mode not implemented yet."); Console.WriteLine("Finished - press any key to exit..."); Console.ReadKey(); Environment.Exit(0); } if (String.IsNullOrEmpty(Options.generate) == false) { // Create the object here to save processing time! MysqlFactory dbCon = new MysqlFactory(); Console.WriteLine("Writing data..."); // Generate top url list LogWriter lwUrls = new LogWriter(@"reports\squid.html", "", dbCon.SelectTopUrls(DateTime.Parse(Options.generate))); lwUrls.WriteTopUrls(); // Generate highest traffic volume from users LogWriter lwBytes = new LogWriter(@"reports\squid.html", "", dbCon.SelectTopBytes(DateTime.Parse(Options.generate))); lwBytes.WriteTopBytes(); Console.WriteLine("Finished - press any key to exit..."); Console.ReadKey(); Environment.Exit(0); } if (String.IsNullOrEmpty(Options.slurp) == false) { LogReader lr = new LogReader(Options.slurp); lr.Read(); var results = lr.Output; int line = 1; Console.WriteLine("Total number of records to read: " + results.Count()); // Add logs to database HelperMethods.addLog(results.ToArray()); Console.WriteLine("Finished - press any key to exit..."); Console.ReadKey(); Environment.Exit(0); } if (Options.listenMode != 0) { // Create UDP listner for squid messages over UDP LogListener loglis = new LogListener(); Thread thr = new Thread(new ThreadStart(loglis.Listen)); thr.Start(); Console.WriteLine(); Console.WriteLine("To terminate use Ctrl + C ..."); Console.ReadKey(); } }