#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "common.h" #include "db.h" #include "functions.h" #include "expression.h" /* * Evaluate function MIN */ int evaluate_MIN(float *min,int itemid,int parameter) { DB_RESULT *result; char c[256]; char *field; int now; now=time(NULL); sprintf(c,"select min(value) from history where clock>%d and itemid=%d",now-parameter,itemid); result = DBselect(c); if((result==NULL)||(DBnum_rows(result)==0)) { syslog(LOG_NOTICE, "Result for MIN is empty" ); DBfree_result(result); return FAIL; } field = DBget_field(result,0,0); if( field == NULL ) { syslog( LOG_NOTICE, "Result for MIN is empty" ); DBfree_result(result); return FAIL; } *min=atof(field); DBfree_result(result); return SUCCEED; } /* * Evaluate function MAX */ int evaluate_MAX(float *max,int itemid,int parameter) { DB_RESULT *result; char c[256]; char *field; int now; now=time(NULL); sprintf(c,"select max(value) from history where clock>%d and itemid=%d",now-parameter,itemid); result = DBselect(c); if((result==NULL)||(DBnum_rows(result)==0)) { syslog(LOG_NOTICE, "Result for MAX is empty" ); DBfree_result(result); return FAIL; } field = DBget_field(result,0,0); if( field == NULL ) { syslog( LOG_NOTICE, "Result for MAX is empty" ); DBfree_result(result); return FAIL; } *max=atof(field); DBfree_result(result); return SUCCEED; } /* * Evaluate function (min,max,prev,last,diff) */ int evaluate_FUNCTION(float *value,DB_ITEM *item,char *function,int parameter) { int ret = SUCCEED; syslog( LOG_DEBUG, "Function [%s]\n",function); if(strcmp(function,"last")==0) { if(item->lastvalue_null==1) { ret = FAIL; } else { *value=item->lastvalue; } } else if(strcmp(function,"prev")==0) { if(item->prevvalue_null==1) { ret = FAIL; } else { *value=item->prevvalue; } } else if(strcmp(function,"min")==0) { ret = evaluate_MIN(value,item->itemid,parameter); } else if(strcmp(function,"max")==0) { ret = evaluate_MAX(value,item->itemid,parameter); } else if(strcmp(function,"diff")==0) { if((item->lastvalue_null==1)||(item->prevvalue_null==1)) { ret = FAIL; } else { if(cmp_double(item->lastvalue, item->prevvalue) == 0) { *value=0; } else { *value=1; } } } else { syslog( LOG_WARNING, "Unsupported function:%s",function); ret = FAIL; } return ret; } /* * Re-calculate values of functions related to given ITEM */ void update_functions(DB_ITEM *item) { DB_FUNCTION function; DB_RESULT *result; char c[1024]; float value; int ret=SUCCEED; int i,rows; sprintf(c,"select function,parameter,itemid from functions where itemid=%d group by 1,2,3 order by 1,2,3",item->itemid); result = DBselect(c); rows=DBnum_rows(result); if((result==NULL)||(rows==0)) { syslog( LOG_NOTICE, "No functions to update."); DBfree_result(result); return; } for(i=0;ih_addr))->s_addr; servaddr_in.sin_port=htons(25); s=socket(AF_INET,SOCK_STREAM,0); if(s==0) { syslog(LOG_ERR, "Cannot create socket"); return FAIL; } myaddr_in.sin_family = AF_INET; myaddr_in.sin_port=0; myaddr_in.sin_addr.s_addr=INADDR_ANY; if( connect(s,(struct sockaddr *)&servaddr_in,sizeof(struct sockaddr_in)) == -1 ) { syslog(LOG_ERR, "Cannot connect to SMTP server [%s]",smtp_server); return FAIL; } c=(char *)malloc(1024); if(c == NULL) { syslog(LOG_ERR, "Malloc error."); close(s); return FAIL; } sprintf(c,"HELO %s\n",smtp_helo); e=sendto(s,c,strlen(c),0,(struct sockaddr *)&servaddr_in,sizeof(struct sockaddr_in)); if(e == -1) { syslog(LOG_ERR, "Error sending HELO to mailserver."); close(s); free(c); return FAIL; } i=sizeof(struct sockaddr_in); i=recvfrom(s,c,1023,0,(struct sockaddr *)&servaddr_in,&i); if(i == -1) { syslog(LOG_ERR, "Error receiving answer on HELO request."); close(s); free(c); return FAIL; } sprintf(c,"MAIL FROM: %s\n",smtp_email); e=sendto(s,c,strlen(c),0,(struct sockaddr *)&servaddr_in,sizeof(struct sockaddr_in)); if(e == -1) { syslog(LOG_ERR, "Error sending MAIL FROM to mailserver."); close(s); free(c); return FAIL; } i=sizeof(struct sockaddr_in); i=recvfrom(s,c,1023,0,(struct sockaddr *)&servaddr_in,&i); if(i == -1) { syslog(LOG_ERR, "Error receiving answer on MAIL FROM request."); close(s); free(c); return FAIL; } sprintf(c,"RCPT TO: <%s>\n",mailto); e=sendto(s,c,strlen(c),0,(struct sockaddr *)&servaddr_in,sizeof(struct sockaddr_in)); if(e == -1) { syslog(LOG_ERR, "Error sending RCPT TO to mailserver."); close(s); free(c); return FAIL; } i=sizeof(struct sockaddr_in); i=recvfrom(s,c,1023,0,(struct sockaddr *)&servaddr_in,&i); if(i == -1) { syslog(LOG_ERR, "Error receiving answer on RCPT TO request."); close(s); free(c); return FAIL; } sprintf(c,"DATA\nSubject: %s\n",mailsubject); e=sendto(s,c,strlen(c),0,(struct sockaddr *)&servaddr_in,sizeof(struct sockaddr_in)); if(e == -1) { syslog(LOG_ERR, "Error sending DATA to mailserver."); close(s); free(c); return FAIL; } i=sizeof(struct sockaddr_in); i=recvfrom(s,c,1023,0,(struct sockaddr *)&servaddr_in,&i); if(i == -1) { syslog(LOG_ERR, "Error receivng answer on DATA request."); close(s); free(c); return FAIL; } sprintf(c,"%s\n",mailbody); e=sendto(s,c,strlen(c),0,(struct sockaddr *)&servaddr_in,sizeof(struct sockaddr_in)); if(e == -1) { syslog(LOG_ERR, "Error sending mail body to mailserver."); close(s); free(c); return FAIL; } sprintf(c,".\n"); e=sendto(s,c,strlen(c),0,(struct sockaddr *)&servaddr_in,sizeof(struct sockaddr_in)); if(e == -1) { syslog(LOG_ERR, "Error sending . to mailserver."); close(s); free(c); return FAIL; } i=sizeof(struct sockaddr_in); i=recvfrom(s,c,1023,0,(struct sockaddr *)&servaddr_in,&i); if(i == -1) { syslog(LOG_ERR, "Error receivng answer on . request."); close(s); free(c); return FAIL; } sprintf(c,"\n"); e=sendto(s,c,strlen(c),0,(struct sockaddr *)&servaddr_in,sizeof(struct sockaddr_in)); if(e ==- 1) { syslog(LOG_ERR, "Error sending \\n to mailserver."); close(s); free(c); return FAIL; } i=sizeof(struct sockaddr_in); i=recvfrom(s,c,1023,0,(struct sockaddr *)&servaddr_in,&i); if(i == -1) { syslog(LOG_ERR, "Error receivng answer on \\n request."); close(s); free(c); return FAIL; } sprintf(c,"QUIT\n"); e=sendto(s,c,strlen(c),0,(struct sockaddr *)&servaddr_in,sizeof(struct sockaddr_in)); if(e == -1) { syslog(LOG_ERR, "Error sending QUIT to mailserver."); close(s); free(c); return FAIL; } close(s); free(c); return SUCCEED; } /* * Send message to user. Message will be sent to all medias registered to given user. */ void send_to_user(int actionid,int userid,char *smtp_server,char *smtp_helo,char *smtp_email,char *subject,char *message) { DB_MEDIA media; char c[1024]; DB_RESULT *result; int i,rows; int now; sprintf(c,"select type,sendto,active from media where active=0 and userid=%d",userid); result = DBselect(c); rows=DBnum_rows(result); for(i=0;i0) { syslog( LOG_DEBUG, "Will not apply actions"); return; } } syslog( LOG_DEBUG, "Applying actions"); /* Get smtp_server and smtp_helo from config */ sprintf(c,"select smtp_server,smtp_helo,smtp_email from config"); result = DBselect(c); strcpy(smtp_server,DBget_field(result,0,0)); strcpy(smtp_helo,DBget_field(result,0,1)); strcpy(smtp_email,DBget_field(result,0,2)); DBfree_result(result); now = time(NULL); sprintf(c,"select actionid,userid,delay,subject,message from actions where triggerid=%d and good=%d and nextcheck<=%d",triggerid,good,now); result = DBselect(c); rows = DBnum_rows(result); for(i=0;i3 and i.itemid=f.itemid and i.lastclock<=%d and t.istrue!=2 and f.triggerid=t.triggerid and f.itemid%%%d=%d group by t.triggerid,t.expression,t.istrue,t.dep_level",lastclock,suckers-1,sucker_num-1); } else { sprintf(c,"select t.triggerid,t.expression,t.istrue,t.dep_level from triggers t,functions f,items i where i.status<>3 and i.itemid=f.itemid and t.istrue!=2 and f.triggerid=t.triggerid and f.itemid=%d group by t.triggerid,t.expression,t.istrue,t.dep_level",sucker_num); } result = DBselect(c); rows = DBnum_rows(result); if(rows == 0) { syslog( LOG_NOTICE, "No triggers to update" ); DBfree_result(result); return; } for(i=0;ihistory>0) { sprintf(c,"insert into history (itemid,clock,value) values (%d,%d,%g)",item->itemid,now,value); DBexecute(c); } if((item->prevvalue_null == 1) || (cmp_double(value,item->lastvalue) != 0) || (cmp_double(item->prevvalue,item->lastvalue) != 0) ) { sprintf(c,"update items set nextcheck=%d,prevvalue=lastvalue,lastvalue=%f,lastclock=%d where itemid=%d",now+item->delay,value,now,item->itemid); item->prevvalue=item->lastvalue; item->lastvalue=value; item->prevvalue_null=item->lastvalue_null; item->lastvalue_null=0; } else { sprintf(c,"update items set NextCheck=%d,LastClock=%d where ItemId=%d",now+item->delay,now,item->itemid); } DBexecute(c); update_functions( item ); }