Changeset 852


Ignore:
Timestamp:
Oct 25, 2017, 10:55:05 PM (7 years ago)
Author:
chronos
Message:
  • Modified: Use better transaction mechanism for netwatch import and latency measurement.
  • Fixed: Query duration mesurement using outdated microtime result.
Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Install/deb/debian/control

    r850 r852  
    88Package: centrala
    99Architecture: all
    10 Depends: ${shlibs:Depends}, ${misc:Depends}, apache2, php, dbconfig-mysql, fping, htmldoc
     10Depends: ${shlibs:Depends}, ${misc:Depends}, apache2, php, dbconfig-mysql, fping, htmldoc, php-mbstring
    1111Description: User portal and network administration web system
    1212HomePage: http://svn.zdechov.net/trac/centrala
  • trunk/Modules/NetworkConfigLinux/Generators/Latency.php

    r842 r852  
    2222    exec('fping -e -r '.$RetryLimit.' -t '.$Timeout.' -a '.implode(' ', $Hosts).' 2>/dev/null', $Output);
    2323
    24     $this->Database->query('BEGIN');
    25     foreach($Output as $Index => $Line) {
     24    $Queries = array();
     25    foreach($Output as $Index => $Line)
     26    {
    2627      $IP = substr($Line, 0, strPos($Line, ' '));
    2728      $Line = substr($Line, strpos($Line, '(') + 1);
    2829      $Value = substr($Line, 0, strpos($Line, ' '));
    2930      //echo($IP.' '.$Value."\n");
    30       $this->Database->insert('NetworkInterfaceLatency', array('Interface' => $HostInterface[$IP],
     31      $Queries[] = $this->Database->GetInsert('NetworkInterfaceLatency', array('Interface' => $HostInterface[$IP],
    3132        'Value' => $Value, 'Time' => TimeToMysqlDateTime($Time)));
    3233    }
    33     $this->Database->query('COMMIT');
     34    $this->Database->Transaction($Queries);
    3435  }
    3536
  • trunk/Modules/NetworkConfigRouterOS/Generators/NetwatchImport.php

    r841 r852  
    4242    }
    4343
    44     $this->Database->query('BEGIN');
    45 
     44    $Queries = array();
    4645    foreach($Interfaces as $Index => $Interface)
    4746    {
    4847      // Update last online time if still online
    4948      if($Interface['NewOnline'])
    50         $DbResult = $this->Database->update('NetworkInterface', '`Id` = "'.$Interface['Id'].'"',
     49        $Queries[] = $this->Database->GetUpdate('NetworkInterface', '`Id` = "'.$Interface['Id'].'"',
    5150          array('LastOnline' => TimeToMysqlDateTime($StartTime)));
    5251
     
    5453      {
    5554        // Online state changed
    56         $DbResult = $this->Database->query('INSERT INTO `NetworkInterfaceUpDown` (`Interface`,
     55        $Queries[] = 'INSERT INTO `NetworkInterfaceUpDown` (`Interface`,
    5756         `State`, `Time`, `Duration`) VALUES ('.$Interface['Id'].', '.$Interface['NewOnline'].', "'.
    58           TimeToMysqlDateTime($StartTime).'", NULL)');
     57          TimeToMysqlDateTime($StartTime).'", NULL)';
    5958        // Update previous record duration in UpDown table
    60         $this->Database->query('UPDATE `NetworkInterfaceUpDown` AS `TM` SET `Duration` = TIMESTAMPDIFF(SECOND, '.
     59        $Queries[] = 'UPDATE `NetworkInterfaceUpDown` AS `TM` SET `Duration` = TIMESTAMPDIFF(SECOND, '.
    6160          '`TM`.`Time`, (SELECT `Time` FROM (SELECT * FROM `NetworkInterfaceUpDown`) AS `TA` WHERE (`TA`.`Time` > `TM`.`Time`) '.
    6261          'AND (`TA`.`Interface`=`TM`.`Interface`) ORDER BY `TA`.`Time` ASC LIMIT 1)) '.
    63           'WHERE (`TM`.`Duration` IS NULL) AND (`TM`.`Interface` ='.$Interface['Id'].')');
    64         $this->Database->update('NetworkInterface', '`Id` = "'.$Interface['Id'].'"',
     62          'WHERE (`TM`.`Duration` IS NULL) AND (`TM`.`Interface` ='.$Interface['Id'].')';
     63        $Queries[] = $this->Database->GetUpdate('NetworkInterface', '`Id` = "'.$Interface['Id'].'"',
    6564          array('Online' => $Interface['NewOnline']));
    6665      }
    6766    }
     67    $this->Database->Transaction($Queries);
    6868
    6969    // Set offline all interfaces which were not updated as online
     
    8181    }
    8282
     83    $Queries = array();
    8384    // Update device online state
    8485    $DbResult = $this->Database->select('NetworkInterface', '`Device`, SUM(`Online`) AS `SumOnline`', '`Online` = 1 GROUP BY `Device`');
     
    8687    {
    8788      if($Device['SumOnline'] > 0)
    88         $this->Database->update('NetworkDevice', 'Id='.$Device['Device'], array('LastOnline' => TimeToMysqlDateTime($StartTime), 'Online' => 1));
     89        $Queries[] = $this->Database->GetUpdate('NetworkDevice', 'Id='.$Device['Device'], array('LastOnline' => TimeToMysqlDateTime($StartTime), 'Online' => 1));
    8990    }
    90     $this->Database->update('NetworkDevice', '`LastOnline` < "'.TimeToMysqlDateTime($StartTime).'"', array('Online' => 0));
    91 
    92     $this->Database->query('COMMIT');
     91    $Queries[] = $this->Database->GetUpdate('NetworkDevice', '`LastOnline` < "'.TimeToMysqlDateTime($StartTime).'"', array('Online' => 0));
     92    $this->Database->Transaction($Queries);
    9393  }
    9494
  • trunk/Packages/Common/Database.php

    r851 r852  
    33// Extended database class
    44// Date: 2016-01-11
     5
     6function microtime_float()
     7{
     8  list($usec, $sec) = explode(" ", microtime());
     9  return ((float)$usec + (float)$sec);
     10}
    511
    612class DatabaseResult
     
    5157    $this->LogFile = dirname(__FILE__).'/../../Query.log';
    5258  }
     59 
    5360
    5461  function Connect($Host, $User, $Password, $Database)
     
    8592  {
    8693    if(!$this->Connected()) throw new Exception(T('Not connected to database'));
    87     if(($this->ShowSQLQuery == true) or ($this->LogSQLQuery == true)) $QueryStartTime = microtime();
     94    if(($this->ShowSQLQuery == true) or ($this->LogSQLQuery == true)) $QueryStartTime = microtime_float();
    8895    $this->LastQuery = $Query;
     96    //echo('a'.$this->ShowSQLQuery.'<'.$QueryStartTime.', '.microtime_float());
    8997    if(($this->ShowSQLQuery == true) or ($this->LogSQLQuery == true))
    90       $Duration = ' ; '.round(microtime() - $QueryStartTime, 4). ' s';
     98      $Duration = ' ; '.round(microtime_float() - $QueryStartTime, 4). ' s';
    9199    if($this->LogSQLQuery == true)
    92100      file_put_contents($this->LogFile, $Query.$Duration."\n", FILE_APPEND);
     
    123131  function insert($Table, $Data)
    124132  {
     133    $this->query($this->GetInsert($Table, $Data));
     134    $this->insert_id = $this->PDO->lastInsertId();
     135  }
     136 
     137  function GetInsert($Table, $Data)
     138  {
    125139    $Name = '';
    126140    $Values = '';
     
    137151    $Name = substr($Name, 1);
    138152    $Values = substr($Values, 1);
    139     $this->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
    140     $this->insert_id = $this->PDO->lastInsertId();
     153    return('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
    141154  }
    142155
    143156  function update($Table, $Condition, $Data)
     157  {
     158    $this->query($this->GetUpdate($Table, $Condition, $Data));
     159  }
     160 
     161  function GetUpdate($Table, $Condition, $Data)
    144162  {
    145163    $Values = '';
     
    154172    }
    155173    $Values = substr($Values, 2);
    156     //echo('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')');
    157     $this->query('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')');
     174    return('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')');
    158175  }
    159176
Note: See TracChangeset for help on using the changeset viewer.