setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT); // MySQL 5.7 forces NO_ZERO_DATE on $cnn_id->query("SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'STRICT_TRANS_TABLES', ''))"); $cnn_id->query("SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'TRADITIONAL', ''))"); $cnn_id->query("SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'NO_ZERO_DATE', ''))"); $cnn_id->query("SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'NO_ZERO_IN_DATE', ''))"); $cnn_id->query("SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY', ''))"); $database_sessions["$odevice:$port:$db_name"] = $cnn_id; return $cnn_id; } catch (PDOException $e) { // Must catch this exception or else PDO will display an error with our username/password //print $e->getMessage(); //exit; } $i++; usleep(40000); } return FALSE; } /* db_close - closes the open connection @returns - the result of the close command */ function db_close($db_conn = false) { global $database_sessions, $database_default, $database_hostname, $database_port; /* check for a connection being passed, if not use legacy behavior */ if (!is_object($db_conn)) { $db_conn = $database_sessions["$database_hostname:$database_port:$database_default"]; if (!is_object($db_conn)) { return false; } } $db_conn = null; $database_sessions["$database_hostname:$database_port:$database_default"] = null; return true; } /* db_execute - run an sql query and do not return any output @param $sql - the sql query to execute @param $log - whether to log error messages, defaults to true @returns - '1' for success, '0' for error */ function db_execute($sql, $log = true, $db_conn = false) { return db_execute_prepared($sql, array(), $log, $db_conn); } /* db_execute_prepared - run an sql query and do not return any output @param $sql - the sql query to execute @param $log - whether to log error messages, defaults to true @returns - '1' for success, '0' for error */ function db_execute_prepared($sql, $parms = array(), $log = true, $db_conn = false) { global $database_sessions, $database_default, $config, $database_hostname, $database_port, $database_total_queries; $database_total_queries++; /* check for a connection being passed, if not use legacy behavior */ if (!is_object($db_conn)) { $db_conn = $database_sessions["$database_hostname:$database_port:$database_default"]; if (!is_object($db_conn)) { return false; } } $sql = db_strip_control_chars($sql); cacti_log('DEVEL: SQL Exec: "' . $sql . '"', false, 'DBCALL', POLLER_VERBOSITY_DEVDBG); $errors = 0; $db_conn->affected_rows = 0; while (true) { $query = $db_conn->prepare($sql); $query->execute($parms); if ($query->errorCode()) { $errorinfo = $query->errorInfo(); $en = $errorinfo[1]; } elseif ($db_conn->errorCode()) { $errorinfo = $db_conn->errorInfo(); $en = $errorinfo[1]; } else { $en = ''; } if ($en == '') { // With PDO, we have to free this up $db_conn->affected_rows = $query->rowCount(); $query->closeCursor(); unset($query); return true; } elseif ($log) { if ($en == 1213 || $en == 1205) { $errors++; if ($errors > 30) { cacti_log("ERROR: Too many Lock/Deadlock errors occurred! SQL:'" . $sql . "'", true, 'DBCALL', POLLER_VERBOSITY_DEBUG); return false; } else { usleep(500000); continue; } } else if ($en == 1153) { if (strlen($sql) > 1024) { $sql = substr($sql, 0, 1024) . '...'; } cacti_log("ERROR: A DB Exec Failed!, Error:$en, SQL:'" . $sql . "'", false, 'DBCALL', POLLER_VERBOSITY_DEBUG); cacti_log('ERROR: A DB Exec Failed!, Error: ' . $errorinfo[2], false, 'DBCALL', POLLER_VERBOSITY_DEBUG); cacti_debug_backtrace('SQL'); return false; } else { cacti_log("ERROR: A DB Exec Failed!, Error:$en, SQL:'" . $sql . "'", FALSE, 'DBCALL'); cacti_log('ERROR: A DB Exec Failed!, Error: ' . $errorinfo[2], false); cacti_debug_backtrace('SQL'); return false; } } else { $query->closeCursor(); unset($query); return false; } } unset($query); return false; } /* db_fetch_cell - run a 'select' sql query and return the first column of the first row found @param $sql - the sql query to execute @param $col_name - use this column name instead of the first one @param $log - whether to log error messages, defaults to true @returns - (bool) the output of the sql query as a single variable */ function db_fetch_cell($sql, $col_name = '', $log = TRUE, $db_conn = FALSE) { return db_fetch_cell_prepared($sql, array(), $col_name, $log, $db_conn); } /* db_fetch_cell_prepared - run a 'select' sql query and return the first column of the first row found @param $sql - the sql query to execute @param $col_name - use this column name instead of the first one @param $log - whether to log error messages, defaults to true @returns - (bool) the output of the sql query as a single variable */ function db_fetch_cell_prepared($sql, $parms = array(), $col_name = '', $log = true, $db_conn = false) { global $database_sessions, $database_default, $config, $database_hostname, $database_port, $database_total_queries; $database_total_queries++; /* check for a connection being passed, if not use legacy behavior */ if (!is_object($db_conn)) { $db_conn = $database_sessions["$database_hostname:$database_port:$database_default"]; if (!is_object($db_conn)) { return false; } } $sql = db_strip_control_chars($sql); cacti_log('DEVEL: SQL Cell: "' . $sql . '"', false, 'DBCALL', POLLER_VERBOSITY_DEVDBG); $db_conn->affected_rows = 0; $query = $db_conn->prepare($sql); $query->execute($parms); $errorinfo = $query->errorInfo(); $en = $errorinfo[1]; if ($en == '') { $db_conn->affected_rows = $query->rowCount(); $q = $query->fetchAll(PDO::FETCH_BOTH); $query->closeCursor(); unset($query); if (isset($q[0]) && is_array($q[0])) { if ($col_name != '') { return $q[0][$col_name]; } else { return reset($q[0]); } } return false; }else if ($log) { cacti_log("ERROR: SQL Cell Failed!, Error:$en, SQL:'" . $sql . "'", false, 'DBCALL', POLLER_VERBOSITY_DEVDBG); cacti_log('ERROR: SQL Cell Failed!, Error: ' . $errorinfo[2], false, 'DBCALL', POLLER_VERBOSITY_DEVDBG); cacti_debug_backtrace('SQL'); } if (isset($query)) { unset($query); } return false; } /* db_fetch_row - run a 'select' sql query and return the first row found @param $sql - the sql query to execute @param $log - whether to log error messages, defaults to true @returns - the first row of the result as a hash */ function db_fetch_row($sql, $log = true, $db_conn = false) { return db_fetch_row_prepared($sql, array(), $log, $db_conn); } /* db_fetch_row_prepared - run a 'select' sql query and return the first row found @param $sql - the sql query to execute @param $log - whether to log error messages, defaults to true @returns - the first row of the result as a hash */ function db_fetch_row_prepared($sql, $parms = array(), $log = true, $db_conn = false) { global $database_sessions, $database_default, $config, $database_hostname, $database_port, $database_total_queries; $database_total_queries++; /* check for a connection being passed, if not use legacy behavior */ if (!is_object($db_conn)) { $db_conn = $database_sessions["$database_hostname:$database_port:$database_default"]; if (!is_object($db_conn)) { return false; } } $sql = db_strip_control_chars($sql); if ($log) { cacti_log('DEVEL: SQL Row: "' . $sql . '"', false, 'DBCALL', POLLER_VERBOSITY_DEVDBG); } $db_conn->affected_rows = 0; $query = $db_conn->prepare($sql); $query->execute($parms); $errorinfo = $query->errorInfo(); $en = $errorinfo[1]; if ($en == '') { $db_conn->affected_rows = $query->rowCount(); if ($query->rowCount()) { $q = $query->fetchAll(PDO::FETCH_ASSOC); $query->closeCursor(); unset($query); if (isset($q[0])) { return $q[0]; } else { return array(); } } else { $query->closeCursor(); return array(); } } elseif ($log) { cacti_log("ERROR: SQL Row Failed!, Error:$en, SQL:'" . $sql . "'", false, 'DBCALL', POLLER_VERBOSITY_DEVDBG); cacti_log('ERROR: SQL Row Failed!, Error: ' . $errorinfo[2], false, 'DBCALL', POLLER_VERBOSITY_DEVDBG); cacti_debug_backtrace('SQL'); } if (isset($query)) { unset($query); } return array(); } /* db_fetch_assoc - run a 'select' sql query and return all rows found @param $sql - the sql query to execute @param $log - whether to log error messages, defaults to true @returns - the entire result set as a multi-dimensional hash */ function db_fetch_assoc($sql, $log = true, $db_conn = false) { return db_fetch_assoc_prepared($sql, array(), $log, $db_conn); } /* db_fetch_assoc_prepared - run a 'select' sql query and return all rows found @param $sql - the sql query to execute @param $log - whether to log error messages, defaults to true @returns - the entire result set as a multi-dimensional hash */ function db_fetch_assoc_prepared($sql, $parms = array(), $log = true, $db_conn = false) { global $database_sessions, $database_default, $config, $database_hostname, $database_port, $database_total_queries; $database_total_queries++; /* check for a connection being passed, if not use legacy behavior */ if (!is_object($db_conn)) { $db_conn = $database_sessions["$database_hostname:$database_port:$database_default"]; if (!is_object($db_conn)) { return false; } } $sql = db_strip_control_chars($sql); cacti_log('DEVEL: SQL Assoc: "' . $sql . '"', false, 'DBCALL', POLLER_VERBOSITY_DEVDBG); $db_conn->affected_rows = 0; $query = $db_conn->prepare($sql); $query->execute($parms); $errorinfo = $query->errorInfo(); $en = $errorinfo[1]; if ($en == '') { $db_conn->affected_rows = $query->rowCount(); $a = $query->fetchAll(PDO::FETCH_ASSOC); $query->closeCursor(); unset($query); if (!is_array($a)) { $a = array(); } return $a; } elseif ($log) { cacti_log("ERROR: SQL Assoc Failed!, Error:$en, SQL:'" . $sql . "'", false, 'DBCALL'); cacti_log('ERROR: SQL Assoc Failed!, Error: ' . $errorinfo[2], false, 'DBCALL'); cacti_debug_backtrace('SQL'); } if (isset($query)) unset($query); return array(); } /* db_fetch_insert_id - get the last insert_id or auto incriment @returns - the id of the last auto increment row that was created */ function db_fetch_insert_id($db_conn = false) { global $database_sessions, $database_default, $database_hostname, $database_port; /* check for a connection being passed, if not use legacy behavior */ if (!is_object($db_conn)) { $db_conn = $database_sessions["$database_hostname:$database_port:$database_default"]; } if (is_object($db_conn)) { return $db_conn->lastInsertId(); } return false; } /* db_affected_rows - return the number of rows affected by the last transaction * @returns - the number of rows affected by the last transaction */ function db_affected_rows($db_conn = false) { global $database_sessions, $database_default, $database_hostname, $database_port; /* check for a connection being passed, if not use legacy behavior */ if (!is_object($db_conn)) { $db_conn = $database_sessions["$database_hostname:$database_port:$database_default"]; if (!is_object($db_conn)) { return false; } } return $db_conn->affected_rows; } /* db_add_column - add a column to table @param $table - the name of the table @param $column - array of column data ex: array('name' => 'test' . rand(1, 200), 'type' => 'varchar (255)', 'NULL' => false) @param $log - whether to log error messages, defaults to true @returns - '1' for success, '0' for error */ function db_add_column($table, $column, $log = true, $db_conn = false) { global $database_sessions, $database_default, $database_hostname, $database_port; /* check for a connection being passed, if not use legacy behavior */ if (!is_object($db_conn)) { $db_conn = $database_sessions["$database_hostname:$database_port:$database_default"]; if (!is_object($db_conn)) { return false; } } $result = db_fetch_assoc('SHOW columns FROM `' . $table . '`', $log, $db_conn); $columns = array(); foreach($result as $arr) { $columns[] = $arr['Field']; } if (isset($column['name']) && !in_array($column['name'], $columns)) { $sql = 'ALTER TABLE `' . $table . '` ADD `' . $column['name'] . '`'; if (isset($column['type'])) $sql .= ' ' . $column['type']; if (isset($column['unsigned'])) $sql .= ' unsigned'; if (isset($column['NULL']) && $column['NULL'] === false) $sql .= ' NOT NULL'; if (isset($column['NULL']) && $column['NULL'] === true && !isset($column['default'])) $sql .= ' default NULL'; if (isset($column['default'])) { if (strtolower($column['type']) == 'timestamp' && $column['default'] === 'CURRENT_TIMESTAMP') { $sql .= ' default CURRENT_TIMESTAMP'; } else { $sql .= ' default ' . (is_numeric($column['default']) ? $column['default'] : "'" . $column['default'] . "'"); } } if (isset($column['on_update'])) $sql .= ' ON UPDATE ' . $column['on_update']; if (isset($column['auto_increment'])) $sql .= ' auto_increment'; if (isset($column['comment'])) $sql .= " COMMENT '" . $column['comment'] . "'"; if (isset($column['after'])) $sql .= ' AFTER ' . $column['after']; return db_execute($sql, $log, $db_conn); } return true; } /* db_remove_column - remove a column to table @param $table - the name of the table @param $column - column name @param $log - whether to log error messages, defaults to true @returns - '1' for success, '0' for error */ function db_remove_column($table, $column, $log = true, $db_conn = false) { global $database_sessions, $database_default, $database_hostname, $database_port; /* check for a connection being passed, if not use legacy behavior */ if (!is_object($db_conn)) { $db_conn = $database_sessions["$database_hostname:$database_port:$database_default"]; if (!is_object($db_conn)) { return false; } } $result = db_fetch_assoc('SHOW columns FROM `' . $table . '`', $log, $db_conn); $columns = array(); foreach($result as $arr) { $columns[] = $arr['Field']; } if (isset($column) && in_array($column, $columns)) { $sql = 'ALTER TABLE `' . $table . '` DROP `' . $column . '`'; return db_execute($sql, $log, $db_conn); } return true; } /* db_add_index - adds a new index to a table @param $table - the name of the table @param $type - the type of the index @param $key - the name of the index @param $columns - an array that defines the columns to include in the index @returns - (bool) the result of the operation true or false */ function db_add_index($table, $type, $key, $columns) { if (!is_array($columns)) { $columns = array($columns); } $sql = 'ALTER TABLE `' . $table . '` ADD ' . $type . ' `' . $key . '`(`' . implode('`,`', $columns) . '`)'; if (db_index_exists($table, $key, false)) { $type = str_ireplace('UNIQUE ', '', $type); db_execute("ALTER TABLE $table DROP $type $key"); } return db_execute($sql); } /* db_index_exists - checks whether an index exists @param $table - the name of the table @param $index - the name of the index @param $log - whether to log error messages, defaults to true @returns - (bool) the output of the sql query as a single variable */ function db_index_exists($table, $index, $log = true, $db_conn = false) { $_keys = array_rekey(db_fetch_assoc("SHOW KEYS FROM `$table`", $log, $db_conn), "Key_name", "Key_name"); return in_array($index, $_keys); } /* db_table_exists - checks whether a table exists @param $table - the name of the table @param $log - whether to log error messages, defaults to true @returns - (bool) the output of the sql query as a single variable */ function db_table_exists($table, $log = true, $db_conn = false) { return (db_fetch_cell("SHOW TABLES LIKE '$table'", '', $log, $db_conn) ? true : false); } /* db_cacti_initialized - checks whether cacti has been initialized properly and if not exits with a message @param $is_web - is the session a web session. @returns - (null) */ function db_cacti_initialized($is_web = true) { global $database_sessions, $database_default, $config, $database_hostname, $database_port, $config; $db_conn = $database_sessions["$database_hostname:$database_port:$database_default"]; if (!is_object($db_conn)) { return FALSE; } $query = $db_conn->prepare('SELECT cacti FROM version'); $query->execute(); $errorinfo = $query->errorInfo(); $query->closeCursor(); if ($errorinfo[1] != 0) { print ($is_web ? '
':''); print ($is_web ? '| Fatal Error - Cacti Database Not Initialized |
| ':'');
print ($is_web ? ' ':'') . 'The Cacti Database has not been initialized. Please initilize it before continuing.' . ($is_web ? ' ':"\n"); print ($is_web ? '':'') . 'To initilize the Cacti database, issue the following commands either as root or using a valid account.' . ($is_web ? ' ':"\n"); print ($is_web ? '':'') . ' mysqladmin -uroot -p create cacti' . ($is_web ? ' ':"\n"); print ($is_web ? '':'') . ' mysql -uroot -p -e "grant all on cacti.* to \'someuser\'@\'localhost\' identified by \'somepassword\'"' . ($is_web ? ' ':"\n"); print ($is_web ? '':'') . ' mysql -uroot -p -e "grant select on mysql.time_zone_name to \'someuser\'@\'localhost\' identified by \'somepassword\'"' . ($is_web ? ' ':"\n"); print ($is_web ? '':'') . ' mysql -uroot -p cacti < /pathcacti/cacti.sql' . ($is_web ? ' ':"\n"); print ($is_web ? '':'') . 'Where /pathcacti/ is the path to your Cacti install location.' . ($is_web ? ' ':"\n"); print ($is_web ? '':'') . 'Change someuser and somepassword to match your site preferences. The defaults are cactiuser for both user and password.' . ($is_web ? ' ':"\n"); print ($is_web ? '':'') . 'NOTE: When installing a remote poller, the config.php file must be writable by the Web Server account, and must include valid connection information to the main Cacti server. The file should be changed to read only after the install is completed.' . ($is_web ? ' ':"\n"); print ($is_web ? ' |