惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - 夜渡寒潭

example windows7 64位下安装apache和php 一个js,记录方便查询。 用了锚的时候,有的层被覆盖了。 日,在ie10下面hover里不能控制table 在实现tab的时候,margin-bottom:-1px无效的问题 javascript中,在一个类里attache的event事件中,如果调用类的成员变量和方法 javascript定义类,并实例化的各类之间成员变量不干扰。 php里www用户建立的文件ftp用户无法删除的情况 再见,我的念青五笔。 如何在Linux下增加Apache的虚拟主机 如何解决在IE6下不自动换行的问题 重庆旅游网新版上线,欢迎测试! 祭5.12地震死难者文[转] __cdecl __fastcall与 __stdcall的说明(转) Windows 应用 Linux手记 - 夜渡寒潭 - 博客园 VC++中控制控件台字体颜色(转) - 夜渡寒潭 - 博客园 在Windows下编译OpenSSL
用C++实现读取windows日志并用zlib压缩后通过jwsmtp邮件发送出去.
夜渡寒潭 · 2011-08-26 · via 博客园 - 夜渡寒潭

情况是这样的, 我们的生活汇网站(http://www.shenghuo56.com)是放在一个windows服务器上的,因为该服务器上的网站比较多,应用程序池常常出错,出错后,会在windows的系统日志里有体现,所以,我打算写这样一个程序来监控windows的日志,如果发现有异常,就把日志发送到我指定的邮箱里,可以是一个139的邮箱,这样就可以做到短信提醒了。

我会在最后附上原码的,先说一下这个程序的功能。

过滤功能
1. 增量事件读取,只读取最后一次读取之后的日志
2. 可以按事件等级过滤,如error, warning, infomation等
3. 可以按来源进行过滤,如你可以指定只监控apache的日志
4. 可以指定读取[系统,应用,安全]类的日志

邮件发送功能
1. 可以配置smtp服务器
2. 只支持向一个邮箱发送邮件
3. 以附件的方式发送日志

压缩功能
1. 为了防止文件过大,把日志文件经过压缩后以邮件附件的方式发送出去。

运行方式
开发完成后,在系统中建立一个计划任务,自动运行本程序,完成采集发送功能。

主要程序片段说明,所有自己写的代码都在event_monitor.cpp中

发送邮件的功能

  1. jwsmtp::mailer mail(g_mail_to, g_mail_from, "日志报告",  
  2.     "见附件", g_smtp_server,  
  3.     jwsmtp::mailer::SMTP_PORT, false);  
  4.   
  5. string zippath = g_current_path;  
  6. zippath += "\\eventlog.zip";  
  7. bool atst = mail.attach(zippath);  
  8.   
  9.   
  10.   
  11. mail.username(g_smtp_user);  
  12. mail.password(g_smtp_password);  
  13. mail.authtype(jwsmtp::mailer::PLAIN);  
  14.   
  15.   
  16. mail.operator()();  
  17. string mret = mail.response();  
  18. if(mret.substr(0,3) != "250") {  
  19.     mret = "发送邮件失败,请检查配置:" + mret;  
  20.     write_log(mret.c_str());  
  21. else {  
  22.     char tmp[32] = {0};  
  23.     ultoa(g_max_event_time, tmp, 10);  
  24.     string ini_path = g_current_path;  
  25.     ini_path += "\\cfg.ini";  
  26.     WritePrivateProfileString("ReadStatus","last_time",tmp,ini_path.c_str());  
  27.   
  28.     write_log("发送邮件成功");  
  29. }  

使用zlib来压缩文件的功能

  1. bool zip_file() {  
  2.     int err=0;  
  3.     int opt_compress_level=Z_DEFAULT_COMPRESSION;  
  4.     void* buf=NULL;  
  5.     int size_buf=0;  
  6.   
  7.     size_buf = WRITEBUFFERSIZE;  
  8.     buf = (void*)malloc(size_buf);  
  9.   
  10.   
  11.     zipFile zf;  
  12.     int errclose;  
  13.     zlib_filefunc64_def ffunc;  
  14.     fill_win32_filefunc64A(&ffunc);  
  15.   
  16.     string zippath = g_current_path;  
  17.     zippath += "\\eventlog.zip";  
  18.     zf = zipOpen2_64(zippath.c_str(),0,NULL,&ffunc);  
  19.   
  20.     if (zf == NULL)  
  21.     {  
  22.         return false;  
  23.     }  
  24.   
  25.   
  26.     FILE * fin;  
  27.     int size_read;  
  28.     string att_path = g_current_path;  
  29.     att_path += "\\attache_lot.txt";  
  30.   
  31.     const char* filenameinzip = att_path.c_str();  
  32.     const char *savefilenameinzip;  
  33.     zip_fileinfo zi;  
  34.     unsigned long crcFile=0;  
  35.     int zip64 = 0;  
  36.   
  37.     zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =  
  38.         zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0;  
  39.     zi.dosDate = 0;  
  40.     zi.internal_fa = 0;  
  41.     zi.external_fa = 0;  
  42.     filetime((char*)filenameinzip,&zi.tmz_date,&zi.dosDate);  
  43.   
  44.     zip64 = isLargeFile(filenameinzip);  
  45.     savefilenameinzip = filenameinzip;  
  46.     while( savefilenameinzip[0] == '\\' || savefilenameinzip[0] == '/' )  
  47.     {  
  48.         savefilenameinzip++;  
  49.     }  
  50.   
  51.       
  52.     const char *tmpptr;  
  53.     const char *lastslash = 0;  
  54.     for( tmpptr = savefilenameinzip; *tmpptr; tmpptr++)  
  55.     {  
  56.         if( *tmpptr == '\\' || *tmpptr == '/')  
  57.         {  
  58.             lastslash = tmpptr;  
  59.         }  
  60.     }  
  61.     if( lastslash != NULL )  
  62.     {  
  63.         savefilenameinzip = lastslash+1;   
  64.     }  
  65.   
  66.     err = zipOpenNewFileInZip3_64(zf,savefilenameinzip,&zi,  
  67.         NULL,0,NULL,0,NULL ,  
  68.         (opt_compress_level != 0) ? Z_DEFLATED : 0,  
  69.         opt_compress_level,0,  
  70.           
  71.         -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,  
  72.         NULL,crcFile, zip64);  
  73.   
  74.     if (err != ZIP_OK) {  
  75.         printf("error in opening %s in zipfile\n",filenameinzip);  
  76.     } else {  
  77.         fin = fopen64(filenameinzip,"rb");  
  78.         if (fin==NULL)  
  79.         {  
  80.             err=ZIP_ERRNO;  
  81.             printf("error in opening %s for reading\n",filenameinzip);  
  82.         }  
  83.     }  
  84.   
  85.     if (err == ZIP_OK)  
  86.         do  
  87.         {  
  88.             err = ZIP_OK;  
  89.             size_read = (int)fread(buf,1,size_buf,fin);  
  90.             if (size_read < size_buf)  
  91.                 if (feof(fin)==0)  
  92.                 {  
  93.                     printf("error in reading %s\n",filenameinzip);  
  94.                     err = ZIP_ERRNO;  
  95.                 }  
  96.   
  97.                 if (size_read>0)  
  98.                 {  
  99.                     err = zipWriteInFileInZip (zf,buf,size_read);  
  100.                     if (err<0)  
  101.                     {  
  102.                         printf("error in writing %s in the zipfile\n",  
  103.                             filenameinzip);  
  104.                     }  
  105.   
  106.                 }  
  107.         } while ((err == ZIP_OK) && (size_read>0));  
  108.   
  109.         if (fin)  
  110.             fclose(fin);  
  111.   
  112.         if (err<0)  
  113.             err=ZIP_ERRNO;  
  114.         else  
  115.         {  
  116.             err = zipCloseFileInZip(zf);  
  117.             if (err!=ZIP_OK)  
  118.                 printf("error in closing %s in the zipfile\n",  
  119.                 filenameinzip);  
  120.         }  
  121.   
  122.         errclose = zipClose(zf,NULL);  
  123.         if (errclose != ZIP_OK)  
  124.             printf("error in closing %s\n", zippath.c_str());  
  125. }  

配置文件说明

[LogFilter]
#Security-安全日志,Application-应用日志,System-系统日志,all-所有日志
Category=all
#如果是所有来源,则写all
Source=all
#显示信息?
Infomation=0
#显示错误?
Error=1
#显示警告?
Warning=1
#显示审核成功?
Audit_Success=0
#显示审核失败
Audit_Failure=1

[SMTP]
smtp_server=smtp.qq.com
smtp_user=xxx@qq.com
smtp_password=xxxx
mail_from=xxx@qq.com
mail_to=xxx@qq.com
[ReadStatus]
last_time=0

zlib我用的是我编译过后的dll,用的是最新的,大家可以自己去下载一个自己编译了进行替换就可以了。

使用说明
注意, 这个工具不能放在有中文目录下面,目录中也不能有空格.
具体的配置文明见cfg.ini

文件说明
\ --程序根目录
说明文件.txt --本文件
event_monitor.exe --主程序
zlibwapi.dll --压缩库
cfg.ini --配置文件

在程序运行后,还会有生成一些其它文件,不用管他.

转载请注明来自生活汇http://www.shenghuo56.com

源码及可执行文件
开发环境是Visual Studio 2008
源码
可执行文件