Merge pull request #1460 from spinza/bulkinsert

Create bulk insert db function and implement in unix-agent process polling.
This commit is contained in:
Daniel Preussker
2015-07-17 11:25:30 +02:00
2 changed files with 59 additions and 8 deletions
+55
View File
@@ -108,6 +108,61 @@ function dbInsert($data, $table) {
}//end dbInsert()
/*
* Passed an array and a table name, it attempts to insert the data into the table.
* $data is an array (rows) of key value pairs. keys are fields. Rows need to have same fields.
* Check for boolean false to determine whether insert failed
* */
function dbBulkInsert($data, $table) {
global $db_stats;
// the following block swaps the parameters if they were given in the wrong order.
// it allows the method to work for those that would rather it (or expect it to)
// follow closer with SQL convention:
// insert into the TABLE this DATA
if (is_string($data) && is_array($table)) {
$tmp = $data;
$data = $table;
$table = $tmp;
}
if (count($data) === 0) {
return false;
}
if (count($data[0]) === 0) {
return false;
}
$sql = 'INSERT INTO `'.$table.'` (`'.implode('`,`', array_keys($data[0])).'`) VALUES ';
$values ='';
foreach ($data as $row) {
if ($values != '') {
$values .= ',';
}
$rowvalues='';
foreach ($row as $key => $value) {
if ($rowvalues != '') {
$rowvalues .= ',';
}
$rowvalues .= "'".mres($value)."'";
}
$values .= "(".$rowvalues.")";
}
$time_start = microtime(true);
$result = dbQuery($sql.$values);
// logfile($fullSql);
$time_end = microtime(true);
$db_stats['insert_sec'] += number_format(($time_end - $time_start), 8);
$db_stats['insert']++;
return $result;
}//end dbBulkInsert()
/*
* Passed an array, table name, WHERE clause, and placeholder parameters, it attempts to update a record.
* Returns the number of affected rows
+4 -8
View File
@@ -112,20 +112,16 @@ if ($device['os_group'] == 'unix') {
if (!empty($agent_data['ps'])) {
echo 'Processes: ';
dbDelete('processes', 'device_id = ?', array($device['device_id']));
$query = "INSERT INTO `processes` (`device_id`,`pid`,`user`,`vsz`,`rss`,`cputime`,`command`) VALUES ";
$values = "";
$data=array();
foreach (explode("\n", $agent_data['ps']) as $process) {
$process = preg_replace('/\((.*),([0-9]*),([0-9]*),([0-9\:]*),([0-9]*)\)\ (.*)/', '\\1|\\2|\\3|\\4|\\5|\\6', $process);
list($user, $vsz, $rss, $cputime, $pid, $command) = explode('|', $process, 6);
if (!empty($command)) {
if ($values != "") {
$values .= ",";
}
$values .= "('".mres($device['device_id'])."','".mres($pid)."','".mres($user)."','".mres($vsz)."','".mres($rss)."','".mres($cputime)."','".mres($command)."')";
$data[]=array('device_id' => $device['device_id'], 'pid' => $pid, 'user' => $user, 'vsz' => $vsz, 'rss' => $rss, 'cputime' => $cputime, 'command' => $command);
}
}
if ($values != "") {
dbQuery($query.$values);
if (count($data) > 0) {
dbBulkInsert('processes',$data);
}
echo "\n";
}