📄 db.c
字号:
log_warn("SENDMAIL","Can not connect to the Mail Server!"); return 1; } sendmail_write(sock,"HELO %s\n",from); sendmail_write(sock,"MAIL FROM:<%s>\n",from); sendmail_write(sock,"RCPT TO:<%s>\n",to); sendmail_write(sock,"DATA\n",NULL); sendmail_write(sock,"From:%s\n",from); sendmail_write(sock,"To:%s\n",to); sendmail_write(sock,"Subject:%s\n",subject); sendmail_write(sock,"\n",NULL); sendmail_write(sock,"%s\n",body); sendmail_write(sock,".\n",NULL ); sendmail_write(sock,"QUIT\n",NULL); close(sock); return 0;} //求子串void substring(char *sub, char *s, int pos, int len){ char *count; if( (pos < 1) || (pos > strlen(s)) || (len < 0) || (len > strlen(s)-pos+1)) return ; count = s+pos-1; strncpy(sub,count,len);} //匹配字符串int myindex(char *s, char *t){ int n,m,i=0; char *sub; n=strlen(s); m=strlen(t); sub=(char *)malloc(sizeof(char)* m); while(i <= n-m+1) { memset(sub,0,sizeof(sub)); substring(sub,s,i,m); sub[m]='\0'; if(strcmp(sub,t) !=0 ) i++; else { free(sub); return i; } } free(sub); return 0;}/*********************************************************************************************************** 函数功能: 处理changed state to up事件 ***********************************************************************************************************/void process_downtoup(MYSQL *conn, struct eventinfo *log){ char *count; char updatequery[1024]; memset(updatequery,0,1024); count=log->content+strlen(log->content)-19; log_debug("UPDATEQUERY",count); if(strcmp(count,"changed state to up") == 0) //此内容中包含changed state to up { sprintf(updatequery,"UPDATE EVENT_INFO_%d SET PROCESS_FLAG=1,PROCESS_PEOPLE='Automatic' WHERE IP='%s' AND FACILITY='LINK' AND RIGHT(CONTENT,LENGTH(\"changed state to down\"))='changed state to down'",getday(),log->ip); log_debug("UPDATEQUERY",updatequery); if(mysql_real_query(conn,updatequery,strlen(updatequery))!=0) { log_warn("UPDATEQUERY",(char *)mysql_error(conn)); if(CR_SERVER_LOST == mysql_errno(conn)) //与服务器失去连接,重新发起连接 conn=doconnect(); return ; } log_debug("UPDATEQUERY","I have changed the process_flag!"); } return ; }/********************************************************************************************************************* 函数功能: 匹配紧急事件,紧急事件的特征存放与数据库的EMERGENCY_EVENT表中。 @ conn: 与数据库的连接指针 @ log: 从缓冲区中取出的日志消息 @ zcbh: 资产编号 @ admin_email: 管理员的邮箱,如果没有紧急事件匹配,则该字段为空**********************************************************************************************************************/int matchevent(MYSQL *conn,struct eventinfo * log,char *zcbh,char *admin_mail){ char buffer[200],admin_name[20]; MYSQL_RES *res; MYSQL_ROW rows; int FLAG=0; memset(buffer,0,200); memset(admin_name,0,20); sprintf(buffer,"SELECT * FROM EMERGENCY_EVENT WHERE ZCBH='%s'",zcbh); if(mysql_real_query(conn,buffer,strlen(buffer))!=0) //通过资产编号来查询所有符合的记录 { log_alert("DB","query error when match the emergency event!"); if(CR_SERVER_LOST == mysql_errno(conn)) //与服务器失去连接,重新发起连接 conn=doconnect(); return 0; } res=mysql_store_result(conn); //存储结果 while((rows=mysql_fetch_row(res))!=NULL) //对每个记录进行匹配 { if(strcmp(log->facility,rows[1])!=0) //facility 不匹配,则匹配下一条记录,否则,继续匹配下一字段 { log_debug("DB","The facility doesn't match!"); continue; } else if(strcmp(log->serverity,rows[2])!=0) // serverity不匹配,则匹配下一条记录,否则,继续匹配下一字段 { log_debug("DB","The severity doesn't match!"); continue; } else if(myindex(log->content,rows[3])==0) //content 不匹配,则匹配下一条记录,否则,继续匹配下一字段 { log_debug("DB","The content doesn't match!"); log_debug("DB",rows[3]); continue; } else //匹配成功 { strcpy(admin_name,rows[4]); //取出管理员的名字 FLAG=1; break; } } if(FLAG==0) { log_debug("DB","I can't get the admin's name!"); return 0; } if(FLAG==1) { log_debug("DB","I get the admin' name!"); memset(buffer,0,200); sprintf(buffer,"SELECT EMAIL FROM USER_INFO WHERE NAME='%s'",admin_name); if(mysql_real_query(conn,buffer,strlen(buffer))!=0) //根据管理员的名字来查询管理员的邮箱 { log_warn("DB"," Can not get the admin email !"); if(CR_SERVER_LOST == mysql_errno(conn)) //与服务器失去连接,重新发起连接 conn=doconnect(); return 0; } res=mysql_store_result(conn); rows=mysql_fetch_row(res); if(rows) strcpy(admin_mail,rows[0]); //取出管理员的邮箱 return 1; } return 0;}/********************************************************************************************************************* 函数功能:将日志消息存储于数据库中 @ Q : 队列指针**********************************************************************************************************************/void storeintodb(){ char buffer[1024],zcbh[16],zcbhquery[100],admin_mail[64]; struct eventinfo *log=NULL; initparameters(); //初始化mysql客户端所需的各种连接参数 memset(buffer,0,1024); memset(zcbh,0,16); memset(zcbhquery,0,100); memset(admin_mail,0,64); signal(SIGHUP,(void *)reconfig); // conn=doconnect(); //建立与数据库的连接 while(1) { pthread_mutex_lock(&ndone_mutex); //互斥锁,与服务器消息接收线程进行通信 while(sem==0) pthread_cond_wait(&ndone_cond,&ndone_mutex); //等待通知去队列中去消息 log=(struct eventinfo *)malloc(sizeof(struct eventinfo)); if(!log) { log_warn("DB","The log can not malloc !"); continue; } bzero(log, sizeof(struct eventinfo)); Dequeue(Q,log); //出队 if(strcmp(log->source, "2") == 0) { log_debug("DB TRAP_TIME", log->time_stamp); log_debug("DB TRAP_FACILITY", log->facility); log_debug("DB TRAP_SEVERITY", log->serverity); log_debug("DB TRAP_CONTENT", log->content); log_debug("DB AGENT_IP", log->ip); log_debug("DB SOURCE", log->source); } sem--; conn=doconnect(); //建立与数据库的连接 if(conn==NULL) continue; //连接数据库失败,重新等待数据 // if(CR_SERVER_LOST == mysql_errno(conn)) //与服务器失去连接,重新发起连接 // conn=doconnect(); sprintf(zcbhquery,"SELECT ZCBH FROM IT_PROPERTY WHERE IP='%s'",log->ip); //根据ip地址查询资产编号 // if(strcmp(log->source, "2") == 0)// log_debug("TRAP", zcbhquery); process_select_query(conn,zcbhquery,zcbh); if(strcmp(log->source,"1") == 0) process_downtoup(conn,log); sprintf(buffer,"insert into EVENT_INFO_%d values(\"%s\",'%s','%s','%s','%s','%s','%s','','','')",getday(),log->time_stamp,zcbh,log->ip,log->source,log->facility,log->serverity,log->content); //转换成sql语句 log_debug("DB",buffer); process_query(conn,buffer); //存入数据库 if(strcmp(log->serverity,severity_level) <= 0 ) //怀疑为紧急事件 { log_debug("DB","I want to match the event!"); if(matchevent(conn,log,zcbh,admin_mail)) //匹配紧急事件成功 { if(sendmail(from_addr, //通知管理员 admin_mail, "There are some emergency event to deal with!", log->content, mail_server,25)==0) log_info("DB","there is a emergency event! Has informed the admin! "); else log_warn("DB","Send mail to the admin failure!"); } } free(log); log=NULL; disconnect(conn); pthread_mutex_unlock(&ndone_mutex); //释放互斥锁 }}void start_db_service(){ pthread_t t1; int ret1=0; ret1=pthread_create(&t1,NULL,(void *)storeintodb,(void*)Q); if(ret1 != 0) { log_fatal("DBTHREAD","The thread create error for storeintodb!"); exit(1) ; } pthread_detach(t1);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -