Ad Widget

Collapse

PATCH: adding trigger sql error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sarek
    Junior Member
    • Aug 2006
    • 24

    #1

    PATCH: adding trigger sql error

    The code for imploding a trigger expression wasn't parsing well with the key I was using for an external script. I removed the loop and made it into a regular expression that will return an array of arrays that contain the expression broken down.

    Code:
    --- zabbix-1.1.1-old/frontends/php/include/triggers.inc.php     2006-05-19 05:18:27.000000000 -0400
    +++ zabbix-1.1.1/frontends/php/include/triggers.inc.php 2006-08-29 15:02:09.000000000 -0400
    @@ -446,7 +446,7 @@
     
            function        explode_exp ($expression, $html)
            {
    -#              echo "EXPRESSION:",$expression,"<Br>";
    +//             echo "EXPRESSION:",$expression,"<Br>";
     
                    $functionid='';
                    $exp='';
    @@ -497,110 +497,37 @@
            function        implode_exp ($expression, $triggerid)
            # Translate localhost:procload.last(0)>10 to {12}>10
            {
    -//             echo "Expression:$expression<br>";
    -               $exp='';
    -               $state="";
    -               for($i=0,$max=strlen($expression); $i<$max; $i++)
    -               {
    -                       if($expression[$i] == '{')
    -                       {
    -                               if($state=="")
    -                               {
    -                                       $host='';
    -                                       $key='';
    -                                       $function='';
    -                                       $parameter='';
    -                                       $state='HOST';
    -                                       continue;
    -                               }
    -                       }
    -                       if( ($expression[$i] == '}')&&($state=="") )
    -                       {
     //                             echo "HOST:$host<BR>";
     //                             echo "KEY:$key<BR>";
     //                             echo "FUNCTION:$function<BR>";
     //                             echo "PARAMETER:$parameter<BR>";
    -                               $state='';
    -
    -                               $res=DBselect("select i.itemid from items i,hosts h".
    -                                       " where i.key_=".zbx_dbstr($key).
    -                                       " and h.host=".zbx_dbstr($host).
    -                                       " and h.hostid=i.hostid");
    -                               $row=DBfetch($res);
    -
    -                               $itemid=$row["itemid"];
    +               $regex = "/\{([0-9a-zA-Z\_\.-]+)\:([]\[0-9a-zA-Z\_\*\/\.\,\'\:\(\) -]+)\.([a-z]{3,11})\(([#0-9a-zA-Z\_\/\.\,[:space:]]+)\)\}/";
    +               preg_match_all($regex, $expression, $arr, PREG_SET_ORDER);
    +               $nummatch=count($arr);
    +               for ($i=0;$i<$nummatch;$i++)
    +               {
    +                       $key = $arr[$i][2];
    +                       $host = $arr[$i][1];
    +                       $function = $arr[$i][3];
    +                       $parameter = $arr[$i][4];
     
    -                               $res=DBexecute("insert into functions (itemid,triggerid,function,parameter)".
    -                                       " values ($itemid,$triggerid,".zbx_dbstr($function).",".
    -                                       zbx_dbstr($parameter).")");
    -                               if(!$res)
    -                               {
    -                                       return  $res;
    -                               }
    -                               $functionid=DBinsert_id($res,"functions","functionid");
    -
    -                               $exp=$exp.'{'.$functionid.'}';
    -
    -                               continue;
    -                       }
    -                       if($expression[$i] == '(')
    -                       {
    -                               if($state == "FUNCTION")
    -                               {
    -                                       $state='PARAMETER';
    -                                       continue;
    -                               }
    -                       }
    -                       if($expression[$i] == ')')
    -                       {
    -                               if($state == "PARAMETER")
    -                               {
    -                                       $state='';
    -                                       continue;
    -                               }
    -                       }
    -                       if(($expression[$i] == ':') && ($state == "HOST"))
    -                       {
    -                               $state="KEY";
    -                               continue;
    -                       }
    -                       if($expression[$i] == '.')
    -                       {
    -                               if($state == "KEY")
    -                               {
    -                                       $state="FUNCTION";
    -                                       continue;
    -                               }
    -                               // Support for '.' in KEY
    -                               if($state == "FUNCTION")
    -                               {
    -                                       $state="FUNCTION";
    -                                       $key=$key.".".$function;
    -                                       $function="";
    -                                       continue;
    -                               }
    -                       }
    -                       if($state == "HOST")
    +                       $res=DBselect("select i.itemid from items i,hosts h".
    +                               " where i.key_=".zbx_dbstr($key).
    +                               " and h.host=".zbx_dbstr($host).
    +                               " and h.hostid=i.hostid");
    +                       $row=DBfetch($res);
    +
    +                       $itemid=$row["itemid"];
    +
    +                       $res=DBexecute("insert into functions (itemid,triggerid,function,parameter)".
    +                               " values ($itemid,$triggerid,".zbx_dbstr($function).",".
    +                               zbx_dbstr($parameter).")");
    +                       if(!$res)
                            {
    -                               $host=$host.$expression[$i];
    -                               continue;
    -                       }
    -                       if($state == "KEY")
    -                       {
    -                               $key=$key.$expression[$i];
    -                               continue;
    -                       }
    -                       if($state == "FUNCTION")
    -                       {
    -                               $function=$function.$expression[$i];
    -                               continue;
    -                       }
    -                       if($state == "PARAMETER")
    -                       {
    -                               $parameter=$parameter.$expression[$i];
    -                               continue;
    +                               return  $res;
                            }
    -                       $exp=$exp.$expression[$i];
    +                       $functionid=DBinsert_id($res,"functions","functionid");
    +                               $exp=$exp.'{'.$functionid.'}';
                    }
                    return $exp;
            }
Working...