Beispiel #1
0
 function create($filename, $schema_id)
 {
     $cmd = array();
     if (pathinfo($filename, PATHINFO_DIRNAME) === '.') {
         $filename = $this->cfg->paths->db . DIRECTORY_SEPARATOR . $filename;
     }
     $schema = array_pop($this->cfg->xpath("schemas/schema[@id='{$schema_id}']"));
     $archive = array_pop($this->cfg->xpath("archives/archive[@id='{$schema->attributes()->archive}']"));
     $cmd[] = "create {$filename}";
     if (!empty($schema->attributes()->step)) {
         $cmd[] = '--step ' . $schema->attributes()->step;
     }
     // set DS
     static $ds_defaults = array('type' => 'COUNTER', 'heartbeat' => '300', 'min' => 'U', 'max' => 'U');
     foreach ($schema as $data_sources) {
         foreach ($data_sources as $ds) {
             $rec = array();
             foreach ($ds_defaults as $k => $v) {
                 $rec[$k] = !empty($ds->attributes()->{$k}) ? (string) $ds->attributes()->{$k} : $v;
             }
             $cmd[] = 'DS:' . ds_encode($ds->attributes()->id) . ':' . join(':', $rec);
         }
     }
     // set RRA
     foreach ($archive as $rra) {
         $cmd[] = 'RRA:' . $rra->attributes()->cf . ':' . $rra->attributes()->xff . ':' . $rra->attributes()->steps . ':' . $rra->attributes()->rows;
     }
     // execute the command
     return $this->exec(join(' ', $cmd));
 }
Beispiel #2
0
 function build_defs($xml)
 {
     static $var_prefix = 'var';
     $var_index = 0;
     foreach ($xml->var as $rec) {
         $row = array();
         $var = (string) $rec->attributes()->id;
         $sy = new ShuntingYard((string) $rec);
         $token = $sy->first();
         while ($token !== FALSE) {
             if ($token->type === T_DEF) {
                 $var_key = $token->value;
                 if (isset($this->vars[$var_key])) {
                     $row[] = $this->vars[$var_key];
                 } elseif ($tmp = preg_split('/:/', $var_key) and array_shift($tmp) === 'rrd') {
                     if (!in_array(strtoupper(end($tmp)), array('AVERAGE', 'MIN', 'MAX', 'LAST'))) {
                         $cf = 'AVERAGE';
                     } else {
                         $cf = strtoupper(array_pop($tmp));
                     }
                     $ds = ds_encode(array_pop($tmp));
                     $filename = absolute_path($this->cfg->rrd->paths->db . DIRECTORY_SEPARATOR . join('.', $tmp) . '.rrd', @$this->opts['base_path']['db']);
                     $var_value = "{$filename}:{$ds}:{$cf}";
                     $var_name = $var_prefix . $var_index;
                     $var_index++;
                     $this->vars[$var_key] = $var_name;
                     $this->cmd[] = "DEF:{$var_name}={$var_value}";
                     $row[] = $var_name;
                 } else {
                     die("Undefined variable `{$var_key}`\n");
                 }
             } elseif ($token->type === T_UNARY_MINUS) {
                 // multiply by minus one in order to reverse the sign
                 $row[] = -1;
                 $row[] = '*';
             } elseif ($token->type === T_UNARY_PLUS) {
                 // simple skip it
             } else {
                 $row[] = $token->value;
             }
             $token = $sy->next();
         }
         $var_key = $var;
         if (isset($this->vars[$var_key])) {
             die("Duplicate variable definition for`{$var}`");
         } elseif (count($row) === 1 and strpos($row[0], $var_prefix) === 0) {
             // simple variable, duplicate key
             $this->vars[$var_key] = $row[0];
         } else {
             $var_name = $var_prefix . $var_index;
             $var_index++;
             $this->vars[$var_key] = $var_name;
             $cdef = 'CDEF:' . $var_name . '=' . join(',', $row);
             $this->cmd[] = $cdef;
         }
     }
 }