最近一项目windows平台,需要针对 Serv-U ftp服务器进行实时监控,从而得知上传的成功后的文件信息,并且通过post提交给php进行分析处理

所以最后决定采用c#编写一个针对ftp日志文件的实时监控的windows服务,通过监控serv-u设置的日志文件大小变化进行分析,并且得到上传成功的文件路径

windows服务编写还好,就是调试有些困难,很少用c#.代码写得还是得简陋了些

主要功能:

xml格式的配置文件,在配置文件中指定ftp日志文件根目录,http请求地址及监控日志存放位置

按日期生成监控系统日志

主要代码

 

C#代码
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Diagnostics;  
  6. using System.Linq;  
  7. using System.ServiceProcess;  
  8. using System.Text;  
  9. using System.Net;  
  10. using System.IO;  
  11. using System.Configuration;  
  12. using System.Collections.Specialized;  
  13.   
  14. namespace ftpScan  
  15. {  
  16.     public partial class Service1 : ServiceBase  
  17.     {  
  18.         System.Timers.Timer timer2 = new System.Timers.Timer(4500);  
  19.   
  20.         public Service1()  
  21.         {  
  22.             InitializeComponent();  
  23.         }  
  24.   
  25.         public static string logpath = SystemConfig.GetConfigData("logpath"string.Empty);  
  26.         public static string httpurl = SystemConfig.GetConfigData("httpurl"string.Empty);  
  27.         public static string ftppath = SystemConfig.GetConfigData("ftppath"string.Empty);  
  28.         public static string ScanLogPath = SystemConfig.GetConfigData("ScanLogPath"string.Empty);  
  29.         public static string s1 = " Received file ";  
  30.         public static string s2 = " successfully ";  
  31.         public static long count = 0;  
  32.         public static long size=0;  
  33.   
  34.         public void log(string msg)  
  35.         {  
  36.             System.DateTime currentTime = new System.DateTime();  
  37.             currentTime = System.DateTime.Now;  
  38.             string strFilePath =ScanLogPath + "ftpScan_" + currentTime.ToString("yyyy_MM_dd") + ".log";  
  39.   
  40.             if (!File.Exists(strFilePath))  
  41.             {  
  42.                 FileStream fs = new FileStream(strFilePath, FileMode.Create);  //实例化一个StreamWriter-->与fs相关联    
  43.                 StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);  
  44.                 sw.WriteLine(currentTime.ToString() + " | " + msg);  
  45.                 sw.Close();  
  46.                 fs.Close();  
  47.             }  
  48.             else  
  49.             {  
  50.                 FileStream fs = new FileStream(strFilePath, FileMode.Append);  
  51.                 StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);  
  52.                 sw.WriteLine(currentTime.ToString() + " | " + msg);  
  53.                 sw.Close();  
  54.                 fs.Close();  
  55.             }  
  56.   
  57.         }  
  58.   
  59.         public void http(string dir, string filename)  
  60.         {  
  61.             string url = httpurl + "dir/" + dir + "/file/" + filename;  
  62.             log("提交http处理 目录:" + dir + "  文件名:" + filename);  
  63.             try  
  64.             {  
  65.                 System.Net.WebClient wc = new WebClient();  
  66.                 NameValueCollection na = new NameValueCollection();  
  67.                 na.Add("dir",dir);  
  68.                 na.Add("file",filename);  
  69.                 wc.UploadValues(httpurl, "POST", na);  
  70.                // log(ss.ToString());  
  71.                 //wc.DownloadData(url);  
  72.             }  
  73.             catch  
  74.             {  
  75.                 log("url:" + url + " 无效!");  
  76.             }  
  77.         }  
  78.   
  79.         public void scan()  
  80.         {  
  81.             System.DateTime currentTime = new System.DateTime();  
  82.             currentTime = System.DateTime.Now;  
  83.             char[] s3 = s1.ToCharArray();  
  84.             long count2 = count;  
  85.             string logfile = logpath + "ftp_" + currentTime.ToString("yyyy_MM_dd") + ".log";  
  86.   
  87.             //Console.WriteLine(logfile);  
  88.             if (!File.Exists(logfile))  
  89.             {  
  90.                 log("今日日志文件 " + logfile + " 不存在,已成功创建空的今日日志文件!");  
  91.                 FileStream fs = new FileStream(logfile, FileMode.Create);  //实例化一个StreamWriter-->与fs相关联    
  92.                 fs.Close();  
  93.             }  
  94.             else  
  95.             {  
  96.   
  97.                 System.IO.FileInfo f = new FileInfo(logfile);  
  98.                 //MessageBox.Show(f.Length.ToString());   
  99.                 if (f.Length != size)  
  100.                 {  
  101.                     StreamReader sr = new StreamReader(logfile);  
  102.   
  103.                     string content = sr.ReadToEnd();  
  104.                     string[] arr = content.Split("".ToCharArray());  
  105.                     sr.Close();  
  106.                     if (arr.Length > count)  
  107.                     {  
  108.                         long n = 0;  
  109.                         foreach (string re in arr)  
  110.                         {  
  111.                             string s = re;  
  112.                             if (n >= count)  
  113.                             {  
  114.                                 //Console.WriteLine(s);  
  115.                                 if (s.Contains(s1) && s.Contains(s2))  
  116.                                 {  
  117.                                     s = s.Replace(s1, "|");  
  118.                                     s = s.Replace(s2, "|");  
  119.                                     Console.WriteLine(s);  
  120.                                     string[] ary = s.Split("|".ToCharArray());  
  121.                                     s = ary[1];  
  122.                                     s = s.Replace(ftppath, "");  
  123.                                     string[] ary2 = s.Split("\\".ToCharArray()); 
  124.                                     if (ary2.Length == 2) 
  125.                                     { 
  126.                                         this.http(ary2[0], ary2[1]); 
  127.                                     } 
  128.                                 } 
  129.                             } 
  130.                             n++; 
  131.                         } 
  132.                          
  133.                     } 
  134.                     else  
  135.                     { 
  136.                         long n = 0; 
  137.                         foreach (string re in arr) 
  138.                         { 
  139.                             string s = re; 
  140.  
  141.                                 //Console.WriteLine(s); 
  142.                             if (s.Contains(s1) && s.Contains(s2)) 
  143.                             { 
  144.                                 s = s.Replace(s1, "|");  
  145.                                 s = s.Replace(s2, "|"); 
  146.                                 Console.WriteLine(s); 
  147.                                 string[] ary = s.Split("|".ToCharArray()); 
  148.                                 s = ary[1]; 
  149.                                 s = s.Replace(ftppath, "");  
  150.                                 string[] ary2 = s.Split("\\".ToCharArray()); 
  151.                                 if (ary2.Length == 2) 
  152.                                 { 
  153.                                   this.http(ary2[0], ary2[1]); 
  154.                                 } 
  155.                             } 
  156.                             n++; 
  157.                         } 
  158.                     } 
  159.                     log("监控到了更新,原行:" + count.ToString() + " 原大小:" + size.ToString() + " 【新行:" + arr.Length.ToString() + " 新大小:" + f.Length.ToString() + ""); 
  160.                     count = arr.Length; 
  161.                     size = f.Length; 
  162.                      
  163.                 } 
  164.             } 
  165.             //Contains //查找字符串 
  166.         } 
  167.  
  168.  
  169.         protected override void OnStart(string[] args) 
  170.         { 
  171.             //log("启动监控ftp日志服务!");  
  172.             //Process.Start("D://test//scoutFolder.exe"); 
  173.             log("启动监控ftp日志服务!"); 
  174.  
  175.             timer2.Elapsed += new System.Timers.ElapsedEventHandler(theout); 
  176.             timer2.Enabled = true; 
  177.         } 
  178.  
  179.         protected override void OnStop() 
  180.         { 
  181.             timer2.Enabled = false; 
  182.             log("停止监控ftp日志服务!");  
  183.         }  
  184.   
  185.         public void theout(object source, System.Timers.ElapsedEventArgs e)  
  186.         {  
  187.   
  188.             scan();  
  189.         }  
  190.     }  
  191. }