2009-12-07 Miguel de Dios <miguel.dedios@artica.es>

* include/functions_modules.php: in the function "create_agent_module" add
	new parameter $disableACL (by default false) for use in API.
	
	* include/functions_api.php: add new functions for add new features to
	api, functions: "set_new_agent" create agent, "set_delete_agent"
	delete agent, "set_create_network_module" create network module,
	"get_module_data" get module data as CSV format in a period time,
	"get_graph_module_data" get a image file of module data in a period time.
	
	* include/functions_db.php: in function "delete_agent" add new parameter
	$disableACL (by default false) for use in API for example.
	
	* operation/agentes/datos_agente.php: clean source code.



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@2170 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
mdtrooper 2009-12-07 18:00:00 +00:00
parent a9c938ea40
commit dd26a2649f
5 changed files with 234 additions and 9 deletions

View File

@ -1,3 +1,19 @@
2009-12-07 Miguel de Dios <miguel.dedios@artica.es>
* include/functions_modules.php: in the function "create_agent_module" add
new parameter $disableACL (by default false) for use in API.
* include/functions_api.php: add new functions for add new features to
api, functions: "set_new_agent" create agent, "set_delete_agent"
delete agent, "set_create_network_module" create network module,
"get_module_data" get module data as CSV format in a period time,
"get_graph_module_data" get a image file of module data in a period time.
* include/functions_db.php: in function "delete_agent" add new parameter
$disableACL (by default false) for use in API for example.
* operation/agentes/datos_agente.php: clean source code.
2009-12-07 Miguel de Dios <miguel.dedios@artica.es> 2009-12-07 Miguel de Dios <miguel.dedios@artica.es>
* operation/agentes/datos_agente.php: add a select box for select chunck to * operation/agentes/datos_agente.php: add a select box for select chunck to

View File

@ -17,6 +17,7 @@
//Set character encoding to UTF-8 - fixes a lot of multibyte character headaches //Set character encoding to UTF-8 - fixes a lot of multibyte character headaches
require_once('functions_agents.php'); require_once('functions_agents.php');
require_once('functions_modules.php');
/** /**
* Parse the "other" parameter. * Parse the "other" parameter.
@ -594,4 +595,206 @@ function get_tree_agents($trash1, $trahs2, $other, $returnType)
returnData($returnType, $data, $separator); returnData($returnType, $data, $separator);
} }
/**
* Create a new agent, and print the id for new agent.
*
* @param $thrash1 Don't use.
* @param $thrash2 Don't use.
* @param array $other it's array, $other as param is <agent_name>;<ip>;<id_parent>;<id_group>;
* <cascade_protection>;<interval_sec>;<id_os>;<name_server>;<custom_id>;<learning_mode>;<disabled>;<description> in this order
* and separator char (after text ; ) and separator (pass in param othermode as othermode=url_encode_separator_<separator>)
* example:
*
* api.php?op=set&op2=new_agent&other=pepito|1.1.1.1|0|4|0|30|8|miguel-portatil||0|0|nose%20nose&other_mode=url_encode_separator_|
*
* @param $thrash3 Don't use.
*/
function set_new_agent($thrash1, $thrash2, $other, $thrash3) {
$name = $other['data'][0];
$ip = $other['data'][1];
$idParent = $other['data'][2];
$idGroup = $other['data'][3];
$cascadeProtection = $other['data'][4];
$intervalSeconds = $other['data'][5];
$idOS = $other['data'][6];
$nameServer = $other['data'][7];
$customId = $other['data'][8];
$learningMode = $other['data'][9];
$disabled = $other['data'][10];
$description = $other['data'][11];
if (get_agent_id ($name)) {
returnError('agent_name_exist', 'The name of agent yet exist in DB.');
}
else if (($idParent != 0) &&
(get_db_value_sql('SELECT id_agente FROM tagente WHERE id_agente = ' . $idParent) === false)) {
returnError('parent_agent_not_exist', 'The agent parent don`t exist.');
}
else if (get_db_value_sql('SELECT id_grupo FROM tgrupo WHERE id_grupo = ' . $idGroup) === false) {
returnError('id_grupo_not_exist', 'The group don`t exist.');
}
else if (get_db_value_sql('SELECT id_os FROM tconfig_os WHERE id_os = ' . $idOS) === false) {
returnError('id_os_not_exist', 'The OS don`t exist.');
}
else if (get_db_value_sql('SELECT name FROM tserver WHERE name LIKE "' . $nameServer . '"') === false) {
returnError('server_not_exist', 'The Pandora Server don`t exist.');
}
else {
$idAgente = process_sql_insert ('tagente',
array ('nombre' => $name,
'direccion' => $ip,
'id_grupo' => $idGroup,
'intervalo' => $intervalSeconds,
'comentarios' => $description,
'modo' => $learningMode,
'id_os' => $idOS,
'disabled' => $disabled,
'cascade_protection' => $cascadeProtection,
'server_name' => $nameServer,
'id_parent' => $idParent,
'custom_id' => $customId));
returnData('string',
array('type' => 'string', 'data' => $idAgente));
}
}
/**
* Delete a agent with the name pass as parameter.
*
* @param string $id Name of agent to delete.
* @param $thrash1 Don't use.
* @param $thrast2 Don't use.
* @param $thrash3 Don't use.
*/
function set_delete_agent($id, $thrash1, $thrast2, $thrash3) {
$agentName = $id;
$idAgent[0] = get_agent_id($agentName);
if (!delete_agent ($idAgent, true))
returnError('error_delete', 'Error in delete operation.');
else
returnData('string', array('type' => 'string', 'data' => __('Correct Delete')));
}
/**
* Create a module in agent. And return the id_agent_module of new module.
*
* @param string $id Name of agent to add the module.
* @param $thrash1 Don't use.
* @param array $other it's array, $other as param is <name_module>;<disabled>;<id_module_type>;
* <id_module_group>;<min_warning>;<max_warning>;<min_critical>;<max_critical>;<ff_threshold>;
* <history_data>;<ip_target>;<tcp_port>;<snmp_community>;<snmp_oid>;<module_interval>;<post_process>;
* <min>;<max>;<custom_id>;<description> in this order
* and separator char (after text ; ) and separator (pass in param othermode as othermode=url_encode_separator_<separator>)
* example:
*
* api.php?op=set&op2=create_network_module&id=pepito&other=prueba|0|7|1|0|0|0|0|0|1|127.0.0.1|0||0|180|0|0|0||latency%20ping&other_mode=url_encode_separator_|
*
* @param $thrash3 Don't use
*/
function set_create_network_module($id, $thrash1, $other, $thrash3) {
$agentName = $id;
$idAgent = get_agent_id($agentName);
$name = $other['data'][0];
$values = array(
'id_agente' => $idAgent,
'disabled' => $other['data'][1],
'id_tipo_modulo' => $other['data'][2],
'id_module_group' => $other['data'][3],
'min_warning' => $other['data'][4],
'max_warning' => $other['data'][5],
'min_critical' => $other['data'][6],
'max_critical' => $other['data'][7],
'min_ff_event' => $other['data'][8],
'history_data' => $other['data'][9],
'ip_target' => $other['data'][10],
'tcp_port' => $other['data'][11],
'snmp_community' => $other['data'][12],
'snmp_oid' => $other['data'][13],
'module_interval' => $other['data'][14],
'post_process' => $other['data'][15],
'min' => $other['data'][16],
'max' => $other['data'][17],
'custom_id' => $other['data'][18],
'descripcion' => $other['data'][19],
);
$idModule = create_agent_module($idAgent, $name, $values, true);
if ($idModule === false)
returnError('error_create_network_module', 'Error in creation network module.');
else
returnData('string', array('type' => 'string', 'data' => $idModule));
}
/**
* Get module data in CSV format.
*
* @param integer $id The ID of module in DB.
* @param $thrash1 Don't use.
* @param array $other it's array, $other as param is <separator>;<period> in this order
* and separator char (after text ; ) and separator (pass in param othermode as othermode=url_encode_separator_<separator>)
* example:
*
* api.php?op=get&op2=module_data&id=17&other=;|604800&other_mode=url_encode_separator_|
*
* @param $returnType Don't use.
*/
function get_module_data($id, $thrash1, $other, $returnType) {
$separator = $other['data'][0];
$periodSeconds = $other['data'][1];
$sql = sprintf ("SELECT utimestamp, datos
FROM tagente_datos
WHERE id_agente_modulo = %d AND utimestamp > %d
ORDER BY utimestamp DESC", $id, get_system_time () - $periodSeconds);
$data['type'] = 'array';
$data['list_index'] = array('utimestamp', 'datos');
$data['data'] = get_db_all_rows_sql($sql);
if ($data === false)
returnError('error_query_module_data', 'Error in the query of module data.');
else
returnData('csv', $data, $separator);
}
/**
* Return a image file of sparse graph of module data in a period time.
*
* @param integer $id id of a module data.
* @param $thrash1 Don't use.
* @param array $other it's array, $other as param is <period>;<width>;<height>;<label>;<start_date>; in this order
* and separator char (after text ; ) and separator (pass in param othermode as othermode=url_encode_separator_<separator>)
* example:
*
* api.php?op=get&op2=graph_module_data&id=17&other=604800|555|245|pepito|2009-12-07&other_mode=url_encode_separator_|
*
* @param $thrash2 Don't use.
*/
function get_graph_module_data($id, $thrash1, $other, $thrash2) {
$period = $other['data'][0];
$width = $other['data'][1];
$height = $other['data'][2];
$graph_type = 'sparse';
$draw_alerts = 0;
$draw_events = 0;
$zoom = 1;
$label = $other['data'][3];
$avg_only = 0;
$start_date = $other['data'][4];
$date = strtotime($start_date);
$image = "fgraph.php?tipo=" . $graph_type .
"&draw_alerts=" . $draw_alerts . "&draw_events=" . $draw_events .
"&id=" . $id . "&zoom=" . $zoom . "&label=" . $label .
"&height=" . $height . "&width=" . $width . "&period=" . $period .
"&avg_only=" . $avg_only . "&date=" . $date;
header('Location: ' . $image);
}
?> ?>

View File

@ -2765,10 +2765,11 @@ function temp_sql_delete ($table, $row, $value) {
* Delete an agent from the database. * Delete an agent from the database.
* *
* @param mixed An array of agents ids or a single integer id to be erased * @param mixed An array of agents ids or a single integer id to be erased
* @param bool Disable the ACL checking, for default false.
* *
* @return bool False if error, true if success. * @return bool False if error, true if success.
*/ */
function delete_agent ($id_agents) { function delete_agent ($id_agents, $disableACL = false) {
global $config; global $config;
$error = false; $error = false;
@ -2789,7 +2790,7 @@ function delete_agent ($id_agents) {
/* Check for deletion permissions */ /* Check for deletion permissions */
$id_group = get_agent_group ($id_agent); $id_group = get_agent_group ($id_agent);
if (! give_acl ($config['id_user'], $id_group, "AW")) { if ((! give_acl ($config['id_user'], $id_group, "AW")) && !$disableACL) {
process_sql_rollback (); process_sql_rollback ();
return false; return false;
} }

View File

@ -162,12 +162,17 @@ function update_agent_module ($id, $values, $onlyNoDeletePending = false) {
* @param int Agent id. * @param int Agent id.
* @param int Module name id. * @param int Module name id.
* @param array Extra values for the module. * @param array Extra values for the module.
* @param bool Disable the ACL checking, for default false.
* *
* @return New module id if the module was created. False if not. * @return New module id if the module was created. False if not.
*/ */
function create_agent_module ($id_agent, $name, $values = false) { function create_agent_module ($id_agent, $name, $values = false, $disableACL = false) {
if (empty ($id_agent) || ! user_access_to_agent ($id_agent, 'AW'))
return false; if (!$disableACL) {
if (empty ($id_agent) || ! user_access_to_agent ($id_agent, 'AW'))
return false;
}
if (empty ($name)) if (empty ($name))
return false; return false;
if (! is_array ($values)) if (! is_array ($values))

View File

@ -52,10 +52,10 @@ if (preg_match ("/string/", get_moduletype_name (get_agentmodule_type ($module_i
$string_type = 1; $string_type = 1;
} else { } else {
$sql = sprintf ("SELECT * $sql = sprintf ("SELECT *
FROM tagente_datos FROM tagente_datos
WHERE id_agente_modulo = %d AND utimestamp > %d WHERE id_agente_modulo = %d AND utimestamp > %d
ORDER BY utimestamp DESC ORDER BY utimestamp DESC
LIMIT %d OFFSET %d", $module_id, get_system_time () - $period, $config['block_size'], get_parameter ('offset')); LIMIT %d OFFSET %d", $module_id, get_system_time () - $period, $config['block_size'], get_parameter ('offset'));
$sqlCount = sprintf ("SELECT COUNT(*) FROM tagente_datos WHERE id_agente_modulo = %d AND utimestamp > %d ORDER BY utimestamp DESC", $module_id, get_system_time () - $period); $sqlCount = sprintf ("SELECT COUNT(*) FROM tagente_datos WHERE id_agente_modulo = %d AND utimestamp > %d ORDER BY utimestamp DESC", $module_id, get_system_time () - $period);
$string_type = 0; $string_type = 0;
} }