summaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorhugetoad <hugetoad@97f52cf1-0a1b-0410-bd0e-c28be96e8082>2005-06-09 07:47:36 +0000
committerhugetoad <hugetoad@97f52cf1-0a1b-0410-bd0e-c28be96e8082>2005-06-09 07:47:36 +0000
commit3bfe748df43bfd96313b73e60c3b84bd4cae0104 (patch)
treef0cfc91da5f92a1d7c31f8444ccf19369f6dcec7 /include
parent3071201c00953d21bd6b01d61761a9b3ca0b4eba (diff)
downloadzabbix-3bfe748df43bfd96313b73e60c3b84bd4cae0104.tar.gz
zabbix-3bfe748df43bfd96313b73e60c3b84bd4cae0104.tar.xz
zabbix-3bfe748df43bfd96313b73e60c3b84bd4cae0104.zip
- better modulatisation of code (Alexei)
git-svn-id: svn://svn.zabbix.com/trunk@1834 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'include')
-rw-r--r--include/expression.c1012
-rw-r--r--include/expression.h34
-rw-r--r--include/functions.c310
3 files changed, 0 insertions, 1356 deletions
diff --git a/include/expression.c b/include/expression.c
deleted file mode 100644
index 1e6f4c70..00000000
--- a/include/expression.c
+++ /dev/null
@@ -1,1012 +0,0 @@
-/*
-** ZABBIX
-** Copyright (C) 2000-2005 SIA Zabbix
-**
-** This program is free software; you can redistribute it and/or modify
-** it under the terms of the GNU General Public License as published by
-** the Free Software Foundation; either version 2 of the License, or
-** (at your option) any later version.
-**
-** This program is distributed in the hope that it will be useful,
-** but WITHOUT ANY WARRANTY; without even the implied warranty of
-** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-** GNU General Public License for more details.
-**
-** You should have received a copy of the GNU General Public License
-** along with this program; if not, write to the Free Software
-** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-**/
-
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <math.h>
-
-#include "functions.h"
-#include "common.h"
-#include "db.h"
-#include "log.h"
-#include "zlog.h"
-
-/*
- * Delete all right spaces from given string
- */
-void rtrim_spaces(char *c)
-{
- int i,len;
-
- len=strlen(c);
- for(i=len-1;i>=0;i--)
- {
- if( c[i] == ' ')
- {
- c[i]=0;
- }
- else break;
- }
-}
-
-/*
- * Delete all left spaces from given string
- */
-void ltrim_spaces(char *c)
-{
- int i,spaces;
-
-/* Number of left spaces */
- spaces=0;
- for(i=0;c[i]!=0;i++)
- {
- if( c[i] == ' ')
- {
- spaces++;
- }
- else break;
- }
- for(i=0;c[i+spaces]!=0;i++)
- {
- c[i]=c[i+spaces];
- }
-
- c[strlen(c)-spaces]=0;
-}
-
-/*
- * Delete all left and right spaces from given string
- */
-void lrtrim_spaces(char *c)
-{
- ltrim_spaces(c);
- rtrim_spaces(c);
-}
-
-/* Convert string to double. This function supports prefixes 'K', 'M', 'G' */
-double str2double(char *str)
-{
- if(str[strlen(str)-1] == 'K')
- {
- str[strlen(str)-1] = 0;
- return (double)1024*atof(str);
- }
- else if(str[strlen(str)-1] == 'M')
- {
- str[strlen(str)-1] = 0;
- return (double)1024*1024*atof(str);
- }
- else if(str[strlen(str)-1] == 'G')
- {
- str[strlen(str)-1] = 0;
- return (double)1024*1024*1024*atof(str);
- }
- return atof(str);
-}
-
-/*
- * Return 0 if arguments are equal (differs less than 0.000001), 1 - otherwise
- */
-int cmp_double(double a,double b)
-{
- if(fabs(a-b)<0.000001)
- {
- return 0;
- }
- return 1;
-}
-
-/*
- * Return SUCCEED if parameter has format X.X<prefix> or X, where X is [0..9]{1,n}
- * In other words, parameter is float number :)
- * Prefix: K,M,G (kilo, mega, giga)
- */
-int is_double(char *c)
-{
- int i;
- int dot=-1;
-
- zabbix_log(LOG_LEVEL_DEBUG, "Starting is_double:[%s]", c );
- for(i=0;c[i]!=0;i++)
- {
- if((c[i]>='0')&&(c[i]<='9'))
- {
- continue;
- }
-
- if((c[i]=='.')&&(dot==-1))
- {
- dot=i;
-
- if((dot!=0)&&(dot!=(int)strlen(c)-1))
- {
- continue;
- }
- }
- /* Last digit is prefix 'K', 'M', 'G' */
- if( ((c[i]=='K')||(c[i]=='M')||(c[i]=='G')) && (i == (int)strlen(c)-1))
- {
- continue;
- }
-
- zabbix_log(LOG_LEVEL_DEBUG, "It is NOT double [%s]",c );
- return FAIL;
- }
- zabbix_log(LOG_LEVEL_DEBUG, "It is double" );
- return SUCCEED;
-}
-
-/*
- * Delete all right EOL characters from
- */
-void delete_reol(char *c)
-{
- int i,j;
-
- zabbix_log( LOG_LEVEL_DEBUG, "Before deleting EOL:%s", c );
-
- j=0;
- for(i=(int)strlen(c)-1;i>=0;i--)
- {
- if( c[i] != '\n') break;
- c[i]=0;
- }
-
- zabbix_log(LOG_LEVEL_DEBUG, "After deleting EOL:%s", c );
-}
-
-/*
- * Delete all spaces from given string
- */
-void delete_spaces(char *c)
-{
- int i,j;
-
- zabbix_log( LOG_LEVEL_DEBUG, "Before deleting spaces:%s", c );
-
- j=0;
-/* for(i=0;i<(int)strlen(c);i++)*/
- for(i=0;c[i]!=0;i++)
- {
- if( c[i] != ' ')
- {
- c[j]=c[i];
- j++;
- }
- }
- c[j]=0;
-
- zabbix_log(LOG_LEVEL_DEBUG, "After deleting spaces:%s", c );
-}
-
-/*
- * Locate character in given string. FAIL - not found, otherwise character position is returned
- */
-int find_char(char *str,char c)
-{
- int i;
-
- zabbix_log( LOG_LEVEL_DEBUG, "Before find_char:%s[%c]", str, c );
-
-/* for(i=0;i<(int)strlen(str);i++)*/
- for(i=0;str[i]!=0;i++)
- {
- if(str[i]==c) return i;
- }
- return FAIL;
-}
-
-/*
- * Evaluate simple expression
- * Simple expression is either <float> or <float> <operator> <float>
- */
-int evaluate_simple (double *result,char *exp)
-{
- double value1,value2;
- char first[MAX_STRING_LEN],second[MAX_STRING_LEN];
- int i,j,l;
-
- zabbix_log( LOG_LEVEL_DEBUG, "Evaluating simple expression [%s]", exp );
-
-/* Remove left and right spaces */
- lrtrim_spaces(exp);
-
- if( is_double(exp) == SUCCEED )
- {
-/* *result=atof(exp);*/
-/* str2double support prefixes */
- *result=str2double(exp);
- return SUCCEED;
- }
-
- if( find_char(exp,'|') != FAIL )
- {
- zabbix_log( LOG_LEVEL_DEBUG, "| is found" );
- l=find_char(exp,'|');
- strscpy( first, exp );
- first[l]=0;
- j=0;
-/* for(i=l+1;i<(int)strlen(exp);i++)*/
- for(i=l+1;exp[i]!=0;i++)
- {
- second[j]=exp[i];
- j++;
- }
- second[j]=0;
- if( evaluate_simple(&value1,first) == FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "Cannot evaluate expression [%s]", first );
- zabbix_syslog("Cannot evaluate expression [%s]", first );
- return FAIL;
- }
- if( value1 == 1)
- {
- *result=value1;
- return SUCCEED;
- }
- if( evaluate_simple(&value2,second) == FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "Cannot evaluate expression [%s]", second );
- zabbix_syslog("Cannot evaluate expression [%s]", second );
- return FAIL;
- }
- if( value2 == 1)
- {
- *result=value2;
- return SUCCEED;
- }
- *result=0;
- return SUCCEED;
- }
- else if( find_char(exp,'&') != FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "& is found" );
- l=find_char(exp,'&');
- strscpy( first, exp );
- first[l]=0;
- j=0;
-/* for(i=l+1;i<(int)strlen(exp);i++)*/
- for(i=l+1;exp[i]!=0;i++)
- {
- second[j]=exp[i];
- j++;
- }
- second[j]=0;
- zabbix_log(LOG_LEVEL_DEBUG, "[%s] [%s]",first,second );
- if( evaluate_simple(&value1,first) == FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "Cannot evaluate expression [%s]", first );
- zabbix_syslog("Cannot evaluate expression [%s]", first );
- return FAIL;
- }
- if( evaluate_simple(&value2,second) == FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "Cannot evaluate expression [%s]", second );
- zabbix_syslog("Cannot evaluate expression [%s]", second );
- return FAIL;
- }
- if( (value1 == 1) && (value2 == 1) )
- {
- *result=1;
- }
- else
- {
- *result=0;
- }
- return SUCCEED;
- }
- else if( find_char(exp,'>') != FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "> is found" );
- l=find_char(exp,'>');
- strscpy(first, exp);
- first[l]=0;
- j=0;
-/* for(i=l+1;i<(int)strlen(exp);i++)*/
- for(i=l+1;exp[i]!=0;i++)
- {
- second[j]=exp[i];
- j++;
- }
- second[j]=0;
- if( evaluate_simple(&value1,first) == FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "Cannot evaluate expression [%s]", first );
- zabbix_syslog("Cannot evaluate expression [%s]", first );
- return FAIL;
- }
- if( evaluate_simple(&value2,second) == FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "Cannot evaluate expression [%s]", second );
- zabbix_syslog("Cannot evaluate expression [%s]", second );
- return FAIL;
- }
- if( value1 > value2 )
- {
- *result=1;
- }
- else
- {
- *result=0;
- }
- return SUCCEED;
- }
- else if( find_char(exp,'<') != FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "< is found" );
- l=find_char(exp,'<');
- strscpy(first, exp);
- first[l]=0;
- j=0;
-/* for(i=l+1;i<(int)strlen(exp);i++)*/
- for(i=l+1;exp[i]!=0;i++)
- {
- second[j]=exp[i];
- j++;
- }
- second[j]=0;
- zabbix_log(LOG_LEVEL_DEBUG, "[%s] [%s]",first,second );
- if( evaluate_simple(&value1,first) == FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "Cannot evaluate expression [%s]", first );
- zabbix_syslog("Cannot evaluate expression [%s]", first );
- return FAIL;
- }
- if( evaluate_simple(&value2,second) == FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "Cannot evaluate expression [%s]", second );
- zabbix_syslog("Cannot evaluate expression [%s]", second );
- return FAIL;
- }
- if( value1 < value2 )
- {
- *result=1;
- }
- else
- {
- *result=0;
- }
- zabbix_log(LOG_LEVEL_DEBUG, "Result [%f]",*result );
- return SUCCEED;
- }
- else if( find_char(exp,'*') != FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "* is found" );
- l=find_char(exp,'*');
- strscpy(first, exp);
- first[l]=0;
- j=0;
-/* for(i=l+1;i<(int)strlen(exp);i++)*/
- for(i=l+1;exp[i]!=0;i++)
- {
- second[j]=exp[i];
- j++;
- }
- second[j]=0;
- if( evaluate_simple(&value1,first) == FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "Cannot evaluate expression [%s]", first );
- zabbix_syslog("Cannot evaluate expression [%s]", first );
- return FAIL;
- }
- if( evaluate_simple(&value2,second) == FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "Cannot evaluate expression [%s]", second );
- zabbix_syslog("Cannot evaluate expression [%s]", second );
- return FAIL;
- }
- *result=value1*value2;
- return SUCCEED;
- }
- else if( find_char(exp,'/') != FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "/ is found" );
- l=find_char(exp,'/');
- strscpy(first, exp);
- first[l]=0;
- j=0;
-/* for(i=l+1;i<(int)strlen(exp);i++)*/
- for(i=l+1;exp[i]!=0;i++)
- {
- second[j]=exp[i];
- j++;
- }
- second[j]=0;
- if( evaluate_simple(&value1,first) == FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "Cannot evaluate expression [%s]", first );
- zabbix_syslog("Cannot evaluate expression [%s]", first );
- return FAIL;
- }
- if( evaluate_simple(&value2,second) == FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "Cannot evaluate expression [%s]", second );
- zabbix_syslog("Cannot evaluate expression [%s]", second );
- return FAIL;
- }
- if(cmp_double(value2,0) == 0)
- {
- zabbix_log(LOG_LEVEL_WARNING, "Division by zero. Cannot evaluate expression [%s/%s]", first,second );
- zabbix_syslog("Division by zero. Cannot evaluate expression [%s/%s]", first,second );
- return FAIL;
- }
- else
- {
- *result=value1/value2;
- }
- return SUCCEED;
- }
- else if( find_char(exp,'+') != FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "+ is found" );
- l=find_char(exp,'+');
- strscpy(first, exp);
- first[l]=0;
- j=0;
-/* for(i=l+1;i<(int)strlen(exp);i++)*/
- for(i=l+1;exp[i]!=0;i++)
- {
- second[j]=exp[i];
- j++;
- }
- second[j]=0;
- if( evaluate_simple(&value1,first) == FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "Cannot evaluate expression [%s]", first );
- zabbix_syslog("Cannot evaluate expression [%s]", first );
- return FAIL;
- }
- if( evaluate_simple(&value2,second) == FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "Cannot evaluate expression [%s]", second );
- zabbix_syslog("Cannot evaluate expression [%s]", second );
- return FAIL;
- }
- *result=value1+value2;
- return SUCCEED;
- }
- else if( find_char(exp,'-') != FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "- is found" );
- l=find_char(exp,'-');
- strscpy(first, exp);
- first[l]=0;
- j=0;
-/* for(i=l+1;i<(int)strlen(exp);i++)*/
- for(i=l+1;exp[i]!=0;i++)
- {
- second[j]=exp[i];
- j++;
- }
- second[j]=0;
- if( evaluate_simple(&value1,first) == FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "Cannot evaluate expression [%s]", first );
- zabbix_syslog("Cannot evaluate expression [%s]", first );
- return FAIL;
- }
- if( evaluate_simple(&value2,second) == FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "Cannot evaluate expression [%s]", second );
- zabbix_syslog("Cannot evaluate expression [%s]", second );
- return FAIL;
- }
- *result=value1-value2;
- return SUCCEED;
- }
- else if( find_char(exp,'=') != FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "= is found" );
- l=find_char(exp,'=');
- strscpy(first, exp);
- first[l]=0;
- j=0;
-/* for(i=l+1;i<(int)strlen(exp);i++)*/
- for(i=l+1;exp[i]!=0;i++)
- {
- second[j]=exp[i];
- j++;
- }
- second[j]=0;
- if( evaluate_simple(&value1,first) == FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "Cannot evaluate expression [%s]", first );
- zabbix_syslog("Cannot evaluate expression [%s]", first );
- return FAIL;
- }
- if( evaluate_simple(&value2,second) == FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "Cannot evaluate expression [%s]", second );
- zabbix_syslog("Cannot evaluate expression [%s]", second );
- return FAIL;
- }
- if( cmp_double(value1,value2) ==0 )
- {
- *result=1;
- }
- else
- {
- *result=0;
- }
- return SUCCEED;
- }
- else if( find_char(exp,'#') != FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "# is found" );
- l=find_char(exp,'#');
- strscpy(first, exp);
- first[l]=0;
- j=0;
-/* for(i=l+1;i<(int)strlen(exp);i++)*/
- for(i=l+1;exp[i]!=0;i++)
- {
- second[j]=exp[i];
- j++;
- }
- second[j]=0;
- if( evaluate_simple(&value1,first) == FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "Cannot evaluate expression [%s]", first );
- zabbix_syslog("Cannot evaluate expression [%s]", first );
- return FAIL;
- }
- if( evaluate_simple(&value2,second) == FAIL )
- {
- zabbix_log(LOG_LEVEL_DEBUG, "Cannot evaluate expression [%s]", second );
- zabbix_syslog("Cannot evaluate expression [%s]", second );
- return FAIL;
- }
- if( cmp_double(value1,value2) != 0 )
- {
- *result=1;
- }
- else
- {
- *result=0;
- }
- return SUCCEED;
- }
- else
- {
- zabbix_log( LOG_LEVEL_WARNING, "Format error or unsupported operator. Exp: [%s]", exp );
- return FAIL;
- }
- return SUCCEED;
-}
-
-/*
- * Evaluate expression. Example of input expression: ({15}>10)|({123}=1)
- */
-int evaluate(int *result,char *exp)
-{
- double value;
- char res[MAX_STRING_LEN];
- char simple[MAX_STRING_LEN];
- int i,l,r;
-
- zabbix_log(LOG_LEVEL_DEBUG, "In evaluate([%s])",exp);
-
- strscpy( res,exp );
-
- while( find_char( exp, ')' ) != FAIL )
- {
- l=-1;
- r=find_char(exp,')');
- for(i=r;i>=0;i--)
- {
- if( exp[i] == '(' )
- {
- l=i;
- break;
- }
- }
- if( r == -1 )
- {
- zabbix_log(LOG_LEVEL_WARNING, "Cannot find left bracket [(]. Expression:[%s]", exp );
- zabbix_syslog("Cannot find left bracket [(]. Expression:[%s]", exp );
- return FAIL;
- }
- for(i=l+1;i<r;i++)
- {
- simple[i-l-1]=exp[i];
- }
- simple[r-l-1]=0;
-
- if( evaluate_simple( &value, simple ) != SUCCEED )
- {
- /* Changed to LOG_LEVEL_DEBUG */
- zabbix_log( LOG_LEVEL_DEBUG, "Unable to evaluate simple expression1 [%s]", simple );
- zabbix_syslog("Unable to evaluate simple expression1 [%s]", simple );
- return FAIL;
- }
-
- zabbix_log(LOG_LEVEL_DEBUG, "Expression1:[%s]", exp );
-
- exp[l]='%';
- exp[l+1]='l';
- exp[l+2]='f';
-/* exp[l]='%';
- exp[l+1]='f';
- exp[l+2]=' ';*/
-
- for(i=l+3;i<=r;i++) exp[i]=' ';
-
- snprintf(res,sizeof(res)-1,exp,value);
- strcpy(exp,res);
- delete_spaces(res);
- zabbix_log(LOG_LEVEL_DEBUG, "Expression4:[%s]", res );
- }
- if( evaluate_simple( &value, res ) != SUCCEED )
- {
- zabbix_log(LOG_LEVEL_WARNING, "Unable to evaluate simple expression2 [%s]", simple );
- zabbix_syslog("Unable to evaluate simple expression2 [%s]", simple );
- return FAIL;
- }
- zabbix_log( LOG_LEVEL_DEBUG, "Evaluate end:[%lf]", value );
- *result=value;
- return SUCCEED;
-}
-
-/* Translate {DATE}, {TIME} */
-/* Doesn't work yet */
-void substitute_simple_macros(DB_TRIGGER *trigger, DB_ACTION *action, char *exp)
-{
- int found = SUCCEED;
- char *s;
- char sql[MAX_STRING_LEN];
- char str[MAX_STRING_LEN];
- char tmp[MAX_STRING_LEN];
-
- time_t now;
- struct tm *tm;
-
- DB_RESULT *result;
-
- zabbix_log(LOG_LEVEL_DEBUG, "In substitute_simple_macros [%s]",exp);
-
- while (found == SUCCEED)
- {
- strscpy(str, exp);
-
-
- if( (s = strstr(str,"{HOSTNAME}")) != NULL )
- {
-/* snprintf(sql,sizeof(sql)-1,"select distinct t.description,h.host from triggers t, functions f,items i, hosts h where t.triggerid=%d and f.triggerid=t.triggerid and f.itemid=i.itemid and h.hostid=i.hostid", trigger->triggerid);*/
- snprintf(sql,sizeof(sql)-1,"select distinct h.host from triggers t, functions f,items i, hosts h where t.triggerid=%d and f.triggerid=t.triggerid and f.itemid=i.itemid and h.hostid=i.hostid", trigger->triggerid);
- result = DBselect(sql);
-
- if(DBnum_rows(result) == 0)
- {
- zabbix_log( LOG_LEVEL_ERR, "No hostname in substitute_simple_macros. Triggerid [%d]", trigger->triggerid);
- zabbix_syslog("No hostname in substitute_simple_macros. Triggerid [%d]", trigger->triggerid);
- strscpy(tmp, "*UNKNOWN*");
- DBfree_result(result);
- }
- else
- {
- strscpy(tmp,DBget_field(result,0,0));
-
- DBfree_result(result);
- }
-
- s[0]=0;
- strcpy(exp, str);
- strncat(exp, tmp, MAX_STRING_LEN);
- strncat(exp, s+strlen("{HOSTNAME}"), MAX_STRING_LEN);
-
- found = SUCCEED;
- }
- else if( (s = strstr(str,"{IPADDRESS}")) != NULL )
- {
- snprintf(sql,sizeof(sql)-1,"select distinct h.ip from triggers t, functions f,items i, hosts h where t.triggerid=%d and f.triggerid=t.triggerid and f.itemid=i.itemid and h.hostid=i.hostid and i.useip=1", trigger->triggerid);
- result = DBselect(sql);
-
- if(DBnum_rows(result) == 0)
- {
- zabbix_log( LOG_LEVEL_ERR, "No IP address in substitute_simple_macros. Triggerid [%d]", trigger->triggerid);
- zabbix_syslog("No IP address in substitute_simple_macros. Triggerid [%d]", trigger->triggerid);
- strscpy(tmp, "*UNKNOWN IP*");
- DBfree_result(result);
- }
- else
- {
- strscpy(tmp,DBget_field(result,0,0));
-
- DBfree_result(result);
- }
-
- s[0]=0;
- strcpy(exp, str);
- strncat(exp, tmp, MAX_STRING_LEN);
- strncat(exp, s+strlen("{IPADDRESS}"), MAX_STRING_LEN);
-
- found = SUCCEED;
- }
- else if( (s = strstr(str,"{DATE}")) != NULL )
- {
- now=time(NULL);
- tm=localtime(&now);
- snprintf(tmp,sizeof(tmp)-1,"%.4d.%.2d.%.2d",tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday);
-
- s[0]=0;
- strcpy(exp, str);
- strncat(exp, tmp, MAX_STRING_LEN);
- strncat(exp, s+strlen("{DATE}"), MAX_STRING_LEN);
-
- found = SUCCEED;
- }
- else if( (s = strstr(str,"{TIME}")) != NULL )
- {
- now=time(NULL);
- tm=localtime(&now);
- snprintf(tmp,sizeof(tmp)-1,"%.2d:%.2d:%.2d",tm->tm_hour,tm->tm_min,tm->tm_sec);
-
- s[0]=0;
- strcpy(exp, str);
- strncat(exp, tmp, MAX_STRING_LEN);
- strncat(exp, s+strlen("{TIME}"), MAX_STRING_LEN);
-
- found = SUCCEED;
- }
- else if( (s = strstr(str,"{STATUS}")) != NULL )
- {
- /* This is old value */
- if(trigger->value == TRIGGER_VALUE_TRUE)
- {
- snprintf(tmp,sizeof(tmp)-1,"OFF");
- }
- else
- {
- snprintf(tmp,sizeof(tmp)-1,"ON");
- }
-
- s[0]=0;
- strcpy(exp, str);
- strncat(exp, tmp, MAX_STRING_LEN);
- strncat(exp, s+strlen("{STATUS}"), MAX_STRING_LEN);
-
- found = SUCCEED;
- }
- else
- {
- found = FAIL;
- }
- }
-
- zabbix_log( LOG_LEVEL_DEBUG, "Result expression [%s]", exp );
-}
-
-/*
- * Translate "{127.0.0.1:system[procload].last(0)}" to "1.34"
- */
-/*
- * Make this function more secure. Get rid of snprintf. Utilise substr()
-*/
-int substitute_macros(DB_TRIGGER *trigger, DB_ACTION *action, char *exp)
-{
- char res[MAX_STRING_LEN];
- char macro[MAX_STRING_LEN];
- char host[MAX_STRING_LEN];
- char key[MAX_STRING_LEN];
- char function[MAX_STRING_LEN];
- char parameter[MAX_STRING_LEN];
- static char value[MAX_STRING_LEN];
- int i;
- int r,l;
- int r1,l1;
-
- zabbix_log(LOG_LEVEL_DEBUG, "In substitute_macros([%s])",exp);
-
- substitute_simple_macros(trigger, action, exp);
-
- while( find_char(exp,'{') != FAIL )
- {
- l=find_char(exp,'{');
- r=find_char(exp,'}');
-
- if( r == FAIL )
- {
- zabbix_log( LOG_LEVEL_WARNING, "Cannot find right bracket. Expression:[%s]", exp );
- zabbix_syslog("Cannot find right bracket. Expression:[%s]", exp );
- return FAIL;
- }
-
- if( r < l )
- {
- zabbix_log( LOG_LEVEL_WARNING, "Right bracket is before left one. Expression:[%s]", exp );
- zabbix_syslog("Right bracket is before left one. Expression:[%s]", exp );
- return FAIL;
- }
-
- for(i=l+1;i<r;i++)
- {
- macro[i-l-1]=exp[i];
- }
- macro[r-l-1]=0;
-
- zabbix_log( LOG_LEVEL_DEBUG, "Macro:%s", macro );
-
- /* macro=="host:key.function(parameter)" */
-
- r1=find_char(macro,':');
-
- for(i=0;i<r1;i++)
- {
- host[i]=macro[i];
- }
- host[r1]=0;
-
- zabbix_log( LOG_LEVEL_DEBUG, "Host:%s", host );
-
- r1=r1+1;
-/* Doesn't work if the key contains '.' */
-/* l1=find_char(macro+r1,'.');*/
-
- l1=FAIL;
- for(i=0;(macro+r1)[i]!=0;i++)
- {
- if((macro+r1)[i]=='.') l1=i;
- }
-
- for(i=r1;i<l1+r1;i++)
- {
- key[i-r1]=macro[i];
- }
- key[l1]=0;
-
- zabbix_log( LOG_LEVEL_DEBUG, "Key:%s", key );
-
- l1=l1+r1+1;
- r1=find_char(macro+l1,'(');
-
- for(i=l1;i<l1+r1;i++)
- {
- function[i-l1]=macro[i];
- }
- function[r1]=0;
-
- zabbix_log( LOG_LEVEL_DEBUG, "Function:%s", function );
-
- l1=l1+r1+1;
- r1=find_char(macro+l1,')');
-
- for(i=l1;i<l1+r1;i++)
- {
- parameter[i-l1]=macro[i];
- }
- parameter[r1]=0;
-
- zabbix_log( LOG_LEVEL_DEBUG, "Parameter:%s", parameter );
-
- i=get_lastvalue(value,host,key,function,parameter);
- zabbix_log( LOG_LEVEL_DEBUG, "Value3 [%s]", value );
-
-
- zabbix_log( LOG_LEVEL_DEBUG, "Value4 [%s]", exp );
- exp[l]='%';
- exp[l+1]='s';
-
- zabbix_log( LOG_LEVEL_DEBUG, "Value41 [%s]", exp+l+2 );
- zabbix_log( LOG_LEVEL_DEBUG, "Value42 [%s]", exp+r+1 );
- strcpy(exp+l+2,exp+r+1);
-
- zabbix_log( LOG_LEVEL_DEBUG, "Value5 [%s]", exp );
-
- snprintf(res,sizeof(res)-1,exp,value);
- strcpy(exp,res);
-/* delete_spaces(exp); */
- zabbix_log( LOG_LEVEL_DEBUG, "Expression4:[%s]", exp );
- }
-
- zabbix_log( LOG_LEVEL_DEBUG, "Result expression:%s", exp );
-
- return SUCCEED;
-}
-
-/*
- * Translate "({15}>10)|({123}=0)" to "(6.456>10)|(0=0)"
- */
-int substitute_functions(char *exp)
-{
- double value;
- char functionid[MAX_STRING_LEN];
- char res[MAX_STRING_LEN];
- int i,l,r;
-
- zabbix_log(LOG_LEVEL_DEBUG, "BEGIN substitute_functions (%s)", exp);
-
- while( find_char(exp,'{') != FAIL )
- {
- l=find_char(exp,'{');
- r=find_char(exp,'}');
- if( r == FAIL )
- {
- zabbix_log( LOG_LEVEL_WARNING, "Cannot find right bracket. Expression:[%s]", exp );
- zabbix_syslog("Cannot find right bracket. Expression:[%s]", exp );
- return FAIL;
- }
- if( r < l )
- {
- zabbix_log( LOG_LEVEL_WARNING, "Right bracket is before left one. Expression:[%s]", exp );
- zabbix_syslog("Right bracket is before left one. Expression:[%s]", exp );
- return FAIL;
- }
-
- for(i=l+1;i<r;i++)
- {
- functionid[i-l-1]=exp[i];
- }
- functionid[r-l-1]=0;
-
- if( DBget_function_result( &value, functionid ) != SUCCEED )
- {
-/* It may happen because of functions.lastvalue is NULL, so this is not warning */
- zabbix_log( LOG_LEVEL_DEBUG, "Unable to get value for functionid [%s]", functionid );
- zabbix_syslog("Unable to get value for functionid [%s]", functionid );
- return FAIL;
- }
-
-
- zabbix_log( LOG_LEVEL_DEBUG, "Expression1:[%s]", exp );
-
- exp[l]='%';
- exp[l+1]='l';
- exp[l+2]='f';
-/* exp[l]='%';
- exp[l+1]='f';
- exp[l+2]=' ';*/
-
- zabbix_log( LOG_LEVEL_DEBUG, "Expression2:[%s]", exp );
-
- for(i=l+3;i<=r;i++) exp[i]=' ';
-
- zabbix_log( LOG_LEVEL_DEBUG, "Expression3:[%s]", exp );
-
- snprintf(res,sizeof(res)-1,exp,value);
- strcpy(exp,res);
- delete_spaces(exp);
- zabbix_log( LOG_LEVEL_DEBUG, "Expression4:[%s]", exp );
- }
- zabbix_log( LOG_LEVEL_DEBUG, "Expression:[%s]", exp );
- zabbix_log( LOG_LEVEL_DEBUG, "END substitute_functions" );
- return SUCCEED;
-}
-
-/*
- * Evaluate complex expression. Example: ({127.0.0.1:system[procload].last(0)}>1)|({127.0.0.1:system[procload].max(300)}>3)
- */
-int evaluate_expression(int *result,char *expression)
-{
- zabbix_log(LOG_LEVEL_DEBUG, "In evaluate_expression(%s)", expression );
-
- delete_spaces(expression);
- if( substitute_functions(expression) == SUCCEED)
- {
- if( evaluate(result, expression) == SUCCEED)
- {
- return SUCCEED;
- }
- }
- zabbix_log(LOG_LEVEL_WARNING, "Evaluation of expression [%s] failed", expression );
- zabbix_syslog("Evaluation of expression [%s] failed", expression );
- return FAIL;
-}
-
diff --git a/include/expression.h b/include/expression.h
deleted file mode 100644
index 027e6f21..00000000
--- a/include/expression.h
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
-** ZABBIX
-** Copyright (C) 2000-2005 SIA Zabbix
-**
-** This program is free software; you can redistribute it and/or modify
-** it under the terms of the GNU General Public License as published by
-** the Free Software Foundation; either version 2 of the License, or
-** (at your option) any later version.
-**
-** This program is distributed in the hope that it will be useful,
-** but WITHOUT ANY WARRANTY; without even the implied warranty of
-** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-** GNU General Public License for more details.
-**
-** You should have received a copy of the GNU General Public License
-** along with this program; if not, write to the Free Software
-** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-**/
-
-
-#ifndef ZABBIX_EXPRESSION_H
-#define ZABBIX_EXPRESSION_H
-
-#include "common.h"
-#include "db.h"
-
-int cmp_double(double a,double b);
-int find_char(char *str,char c);
-int substitute_functions(char *exp);
-int substitute_macros(DB_TRIGGER *trigger, DB_ACTION *action, char *exp);
-int evaluate_expression (int *result,char *expression);
-void delete_reol(char *c);
-
-#endif
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;i<DBnum_rows(result);i++)
- {
- media.mediatypeid=atoi(DBget_field(result,i,0));
- media.sendto=DBget_field(result,i,1);
- media.active=atoi(DBget_field(result,i,2));
- media.severity=atoi(DBget_field(result,i,3));
- media.period=DBget_field(result,i,4);
-
- zabbix_log( LOG_LEVEL_DEBUG, "Trigger severity [%d] Media severity [%d] Period [%s]",trigger->priority, media.severity, media.period);
- if(((1<<trigger->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;i<DBnum_rows(result);i++)
- {
- send_to_user_medias(trigger, action, atoi(DBget_field(result,i,0)));
- }
- DBfree_result(result);
- }
- else
- {
- zabbix_log( LOG_LEVEL_WARNING, "Unknown recipient type [%d] for actionid [%d]",action->recipient,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;i<DBnum_rows(result);i++)
- {
-
- zabbix_log( LOG_LEVEL_DEBUG, "i=[%d]",i);
-/* zabbix_log( LOG_LEVEL_ERR, "Fetched: ID [%s] %s %s %s %s\n",DBget_field(result,i,0),DBget_field(result,i,1),DBget_field(result,i,2),DBget_field(result,i,3),DBget_field(result,i,4));*/
-
- action.actionid=atoi(DBget_field(result,i,0));
- action.userid=atoi(DBget_field(result,i,1));
- action.delay=atoi(DBget_field(result,i,2));
- strscpy(action.subject,DBget_field(result,i,3));
- strscpy(action.message,DBget_field(result,i,4));
- action.scope=atoi(DBget_field(result,i,5));
- action.severity=atoi(DBget_field(result,i,6));
- action.recipient=atoi(DBget_field(result,i,7));
- action.good=atoi(DBget_field(result,i,8));
-
- if(ACTION_SCOPE_TRIGGER==action.scope)
- {
-/* substitute_hostname(trigger->triggerid,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->priority<action.severity)
- {
- continue;
- }
-
- snprintf(sql,sizeof(sql)-1,"select distinct h.hostid from hosts h,items i,triggers t,functions f where h.hostid=i.hostid and i.itemid=f.itemid and f.triggerid=t.triggerid and t.triggerid=%d", trigger->triggerid);
- result2 = DBselect(sql);
-
- for(j=0;j<DBnum_rows(result2);j++)
- {
- snprintf(sql,sizeof(sql)-1,"select distinct a.actionid from actions a,hosts h,items i,triggers t,functions f where h.hostid=i.hostid and i.itemid=f.itemid and f.triggerid=t.triggerid and a.triggerid=%d and a.scope=1 and a.actionid=%d and a.triggerid=h.hostid",atoi(DBget_field(result2,j,0)),action.actionid);
- result3 = DBselect(sql);
- if(DBnum_rows(result3)==0)
- {
- DBfree_result(result3);
- continue;
- }
- DBfree_result(result3);
-
- 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);
-
- 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->priority<action.severity)
- {
- continue;
- }
-/* -- */
- 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);
-
-/* 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!
*/