From 3bfe748df43bfd96313b73e60c3b84bd4cae0104 Mon Sep 17 00:00:00 2001 From: hugetoad Date: Thu, 9 Jun 2005 07:47:36 +0000 Subject: - better modulatisation of code (Alexei) git-svn-id: svn://svn.zabbix.com/trunk@1834 97f52cf1-0a1b-0410-bd0e-c28be96e8082 --- include/functions.c | 310 ---------------------------------------------------- 1 file changed, 310 deletions(-) (limited to 'include/functions.c') diff --git a/include/functions.c b/include/functions.c index 19822b86..fa6862a9 100644 --- a/include/functions.c +++ b/include/functions.c @@ -45,7 +45,6 @@ #include "security.h" #include "functions.h" -#include "expression.h" /* Delete trailing zeroes */ /* 10.0100 -> 10.01, 10. -> 10 */ @@ -627,315 +626,6 @@ void update_functions(DB_ITEM *item) DBfree_result(result); } -/* 1 - within period, 0 - out of period */ -int check_time_period(char *period) -{ - time_t now; - char tmp[MAX_STRING_LEN]; - char *s; - int d1,d2,h1,h2,m1,m2; - int day, hour, min; - struct tm *tm; - int ret = 0; - - - zabbix_log( LOG_LEVEL_DEBUG, "In check_time_period(%s)",period); - - now = time(NULL); - tm = localtime(&now); - - day=tm->tm_wday; - if(0 == day) day=7; - hour = tm->tm_hour; - min = tm->tm_min; - - strscpy(tmp,period); - s=(char *)strtok(tmp,";"); - while(s!=NULL) - { - zabbix_log( LOG_LEVEL_DEBUG, "Period [%s]",s); - - if(sscanf(s,"%d-%d,%d:%d-%d:%d",&d1,&d2,&h1,&m1,&h2,&m2) == 6) - { - zabbix_log( LOG_LEVEL_DEBUG, "%d-%d,%d:%d-%d:%d",d1,d2,h1,m1,h2,m2); - if( (day>=d1) && (day<=d2) && (60*hour+min>=60*h1+m1) && (60*hour+min<=60*h2+m2)) - { - ret = 1; - break; - } - } - else - { - zabbix_log( LOG_LEVEL_ERR, "Time period format is wrong [%s]",period); - } - - s=(char *)strtok(NULL,";"); - } - return ret; -} - -/* Cannot use action->userid as it may also represent groupd id*/ -void send_to_user_medias(DB_TRIGGER *trigger,DB_ACTION *action, int userid) -{ - DB_MEDIA media; - char sql[MAX_STRING_LEN]; - DB_RESULT *result; - - int i; - - snprintf(sql,sizeof(sql)-1,"select mediatypeid,sendto,active,severity,period from media where active=%d and userid=%d",MEDIA_STATUS_ACTIVE,userid); - result = DBselect(sql); - - for(i=0;ipriority, media.severity, media.period); - if(((1<priority)&media.severity)==0) - { - zabbix_log( LOG_LEVEL_DEBUG, "Won't send message"); - continue; - } - if(check_time_period(media.period) == 0) - { - zabbix_log( LOG_LEVEL_DEBUG, "Won't send message"); - continue; - } - - DBadd_alert(action->actionid,media.mediatypeid,media.sendto,action->subject,action->message); - } - DBfree_result(result); -} - -/* - * Send message to user. Message will be sent to all medias registered to given user. - */ -void send_to_user(DB_TRIGGER *trigger,DB_ACTION *action) -{ - char sql[MAX_STRING_LEN]; - DB_RESULT *result; - - int i; - - if(action->recipient == RECIPIENT_TYPE_USER) - { - send_to_user_medias(trigger, action, action->userid); - } - else if(action->recipient == RECIPIENT_TYPE_GROUP) - { - snprintf(sql,sizeof(sql)-1,"select u.userid from users u, users_groups ug where ug.usrgrpid=%d and ug.userid=u.userid", action->userid); - result = DBselect(sql); - for(i=0;irecipient,action->actionid); - zabbix_syslog("Unknown recipient type [%d] for actionid [%d]",action->recipient,action->actionid); - } -} - -void apply_actions(DB_TRIGGER *trigger,int alarmid,int trigger_value) -{ - int escalationid; - char sql[MAX_STRING_LEN]; - - zabbix_log( LOG_LEVEL_WARNING, "In apply_actions(triggerid:%d,alarmid:%d,trigger_value:%d)",trigger->triggerid, alarmid, trigger_value); - - if((escalationid=DBget_default_escalation_id())>0) - { - snprintf(sql,sizeof(sql)-1,"insert into escalation_log (triggerid,alarmid,escalationid,level,adminlevel,nextcheck,status) values (%d,%d,%d,%d,%d,%d,%d)", trigger->triggerid, alarmid, escalationid, 0, 0, 0, 0); - DBexecute(sql); - } - else - { - zabbix_log( LOG_LEVEL_WARNING, "No default escalation defined"); - } -} - -/* - * Apply actions if any. - */ -/*void apply_actions(int triggerid,int good)*/ -void apply_actions_old(DB_TRIGGER *trigger,int alarmid,int trigger_value) -{ - DB_RESULT *result,*result2,*result3; - - DB_ACTION action; - - char sql[MAX_STRING_LEN]; - - int i,j; - int now; - - zabbix_log( LOG_LEVEL_WARNING, "In apply_actions(triggerid:%d,alarmid:%d,trigger_value:%d)",trigger->triggerid, alarmid, trigger_value); - - if(TRIGGER_VALUE_TRUE == trigger_value) - { - zabbix_log( LOG_LEVEL_DEBUG, "Check dependencies"); - - snprintf(sql,sizeof(sql)-1,"select count(*) from trigger_depends d,triggers t where d.triggerid_down=%d and d.triggerid_up=t.triggerid and t.value=%d",trigger->triggerid, TRIGGER_VALUE_TRUE); - result = DBselect(sql); - if(DBnum_rows(result) == 1) - { - if(atoi(DBget_field(result,0,0))>0) - { - zabbix_log( LOG_LEVEL_DEBUG, "Will not apply actions"); - DBfree_result(result); - return; - } - } - DBfree_result(result); - } - - zabbix_log( LOG_LEVEL_DEBUG, "Applying actions"); - - now = time(NULL); - -/* snprintf(sql,sizeof(sql)-1,"select actionid,userid,delay,subject,message,scope,severity,recipient,good from actions where (scope=%d and triggerid=%d and good=%d and nextcheck<=%d) or (scope=%d and good=%d) or (scope=%d and good=%d)",ACTION_SCOPE_TRIGGER,trigger->triggerid,trigger_value,now,ACTION_SCOPE_HOST,trigger_value,ACTION_SCOPE_HOSTS,trigger_value);*/ - snprintf(sql,sizeof(sql)-1,"select actionid,userid,delay,subject,message,scope,severity,recipient,good from actions where (scope=%d and triggerid=%d and (good=%d or good=2) and nextcheck<=%d) or (scope=%d and (good=%d or good=2)) or (scope=%d and (good=%d or good=2))",ACTION_SCOPE_TRIGGER,trigger->triggerid,trigger_value,now,ACTION_SCOPE_HOST,trigger_value,ACTION_SCOPE_HOSTS,trigger_value); - result = DBselect(sql); - zabbix_log( LOG_LEVEL_DEBUG, "SQL [%s]", sql); - - for(i=0;itriggerid,action.message); - substitute_hostname(trigger->triggerid,action.subject);*/ - - substitute_macros(trigger, &action, action.message); - substitute_macros(trigger, &action, action.subject); - - send_to_user(trigger,&action); - snprintf(sql,sizeof(sql)-1,"update actions set nextcheck=%d where actionid=%d",now+action.delay,action.actionid); - DBexecute(sql); - } - else if(ACTION_SCOPE_HOST==action.scope) - { - if(trigger->prioritytriggerid); - result2 = DBselect(sql); - - for(j=0;jdescription); - if(TRIGGER_VALUE_TRUE == trigger_value) - { - strncat(action.subject," (ON)", MAX_STRING_LEN); - } - else - { - strncat(action.subject," (OFF)", MAX_STRING_LEN); - } - strscpy(action.message,action.subject); - - substitute_macros(trigger, &action, action.message); - substitute_macros(trigger, &action, action.subject); - - send_to_user(trigger,&action); - snprintf(sql,sizeof(sql)-1,"update actions set nextcheck=%d where actionid=%d",now+action.delay,action.actionid); - DBexecute(sql); - } - DBfree_result(result2); - -/* snprintf(sql,sizeof(sql)-1,"select * from actions a,triggers t,hosts h,functions f where a.triggerid=t.triggerid and f.triggerid=t.triggerid and h.hostid=a.triggerid and t.triggerid=%d and a.scope=%d",trigger->triggerid,ACTION_SCOPE_HOST); - result2 = DBselect(sql); - if(DBnum_rows(result2)==0) - { - DBfree_result(result2); - continue; - } - DBfree_result(result2); - - strscpy(action.subject,trigger->description); - if(TRIGGER_VALUE_TRUE == trigger_value) - { - strncat(action.subject," (ON)", MAX_STRING_LEN); - } - else - { - strncat(action.subject," (OFF)", MAX_STRING_LEN); - } - strscpy(action.message,action.subject); - - substitute_macros(trigger, &action, action.message); - substitute_macros(trigger, &action, action.subject);*/ - } - else if(ACTION_SCOPE_HOSTS==action.scope) - { -/* Added in Zabbix 1.0beta10 */ - if(trigger->prioritydescription); - if(TRIGGER_VALUE_TRUE == trigger_value) - { - strncat(action.subject," (ON)", MAX_STRING_LEN); - } - else - { - strncat(action.subject," (OFF)", MAX_STRING_LEN); - } - strscpy(action.message,action.subject); - - substitute_macros(trigger, &action, action.message); - substitute_macros(trigger, &action, action.subject); - -/* substitute_hostname(trigger->triggerid,action.message); - substitute_hostname(trigger->triggerid,action.subject);*/ - - send_to_user(trigger,&action); - snprintf(sql,sizeof(sql)-1,"update actions set nextcheck=%d where actionid=%d",now+action.delay,action.actionid); - DBexecute(sql); - } - else - { - zabbix_log( LOG_LEVEL_WARNING, "Unsupported scope [%d] for actionid [%d]", action.scope, action.actionid); - zabbix_syslog("Unsupported scope [%d] for actionid [%d]", action.scope, action.actionid); - } - - } - zabbix_log( LOG_LEVEL_DEBUG, "Actions applied for trigger %d %d", trigger->triggerid, trigger_value ); - DBfree_result(result); -} - /* * Recursive function! */ -- cgit