Added api for metaconsole. Ticket: #1474

This commit is contained in:
mdtrooper 2014-12-15 19:38:05 +01:00
parent 15d5607787
commit ccb1668391
5 changed files with 834 additions and 185 deletions

815
pandora_console/include/functions_api.php Normal file → Executable file

File diff suppressed because it is too large Load Diff

View File

@ -581,7 +581,7 @@ function events_comment ($id_event, $comment = '', $action = 'Added comment', $m
$comments_format = 'new';
}
}
switch($comments_format) {
case 'new':
$comment_for_json['comment'] = $comment;
@ -677,36 +677,125 @@ function events_get_description ($id_event) {
*
* @return int event id
*/
function events_create_event ($event, $id_group, $id_agent, $status = 0, $id_user = "", $event_type = "unknown", $priority = 0, $id_agent_module = 0, $id_aam = 0, $critical_instructions = '', $warning_instructions = '', $unknown_instructions = '', $source="Pandora", $tags="", $custom_data="") {
function events_create_event ($event, $id_group, $id_agent, $status = 0,
$id_user = "", $event_type = "unknown", $priority = 0,
$id_agent_module = 0, $id_aam = 0, $critical_instructions = '',
$warning_instructions = '', $unknown_instructions = '',
$source="Pandora", $tags="", $custom_data="", $server_id = 0) {
global $config;
switch ($config["dbtype"]) {
case "mysql":
$sql = sprintf ('INSERT INTO tevento (id_agente, id_grupo, evento, timestamp,
estado, utimestamp, id_usuario, event_type, criticity,
id_agentmodule, id_alert_am, critical_instructions, warning_instructions, unknown_instructions, source, tags, custom_data)
VALUES (%d, %d, "%s", NOW(), %d, UNIX_TIMESTAMP(NOW()), "%s", "%s", %d, %d, %d, "%s", "%s", "%s", "%s", "%s", "%s")',
$id_agent, $id_group, $event, $status, $id_user, $event_type,
$priority, $id_agent_module, $id_aam, $critical_instructions, $warning_instructions, $unknown_instructions, $source, $tags, $custom_data);
break;
case "postgresql":
$sql = sprintf ('INSERT INTO tevento (id_agente, id_grupo, evento, timestamp,
estado, utimestamp, id_usuario, event_type, criticity,
id_agentmodule, id_alert_am, critical_instructions, warning_instructions, unknown_instructions, source, tags, custom_data)
VALUES (%d, %d, "%s", NOW(), %d, ceil(date_part(\'epoch\', CURRENT_TIMESTAMP)), "%s", "%s", %d, %d, %d, "%s", "%s", "%s", "%s", "%s", "%s")',
$id_agent, $id_group, $event, $status, $id_user, $event_type,
$priority, $id_agent_module, $id_aam, $critical_instructions, $warning_instructions, $unknown_instructions, $source, $tags, $custom_data);
break;
case "oracle":
$sql = sprintf ('INSERT INTO tevento (id_agente, id_grupo, evento, timestamp,
estado, utimestamp, id_usuario, event_type, criticity,
id_agentmodule, id_alert_am, critical_instructions, warning_instructions, unknown_instructions, source, tags, custom_data)
VALUES (%d, %d, "%s", CURRENT_TIMESTAMP, %d, ceil((sysdate - to_date(\'19700101000000\',\'YYYYMMDDHH24MISS\')) * (86400)), "%s", "%s", %d, %d, %d, "%s", "%s", "%s", "%s", "%s", "%s")',
$id_agent, $id_group, $event, $status, $id_user, $event_type,
$priority, $id_agent_module, $id_aam, $critical_instructions, $warning_instructions, $unknown_instructions, $source, $tags, $custom_data);
break;
$table_events = 'tevento';
if (defined ('METACONSOLE')) {
$table_events = 'tmetaconsole_event';
switch ($config["dbtype"]) {
case "mysql":
$sql = sprintf ('
INSERT INTO ' . $table_events . ' (id_agente, id_grupo, evento,
timestamp, estado, utimestamp, id_usuario,
event_type, criticity, id_agentmodule, id_alert_am,
critical_instructions, warning_instructions,
unknown_instructions, source, tags, custom_data,
server_id)
VALUES (%d, %d, "%s", NOW(), %d, UNIX_TIMESTAMP(NOW()),
"%s", "%s", %d, %d, %d, "%s", "%s", "%s", "%s",
"%s", "%s", %d)',
$id_agent, $id_group, $event, $status, $id_user,
$event_type, $priority, $id_agent_module, $id_aam,
$critical_instructions, $warning_instructions,
$unknown_instructions, $source, $tags, $custom_data,
$server_id);
break;
case "postgresql":
$sql = sprintf ('
INSERT INTO ' . $table_events . ' (id_agente, id_grupo, evento,
timestamp, estado, utimestamp, id_usuario,
event_type, criticity, id_agentmodule, id_alert_am,
critical_instructions, warning_instructions,
unknown_instructions, source, tags, custom_data,
server_id)
VALUES (%d, %d, "%s", NOW(), %d,
ceil(date_part(\'epoch\', CURRENT_TIMESTAMP)), "%s",
"%s", %d, %d, %d, "%s", "%s", "%s", "%s", "%s",
"%s", %d)',
$id_agent, $id_group, $event, $status, $id_user,
$event_type, $priority, $id_agent_module, $id_aam,
$critical_instructions, $warning_instructions,
$unknown_instructions, $source, $tags, $custom_data,
$server_id);
break;
case "oracle":
$sql = sprintf ('
INSERT INTO ' . $table_events . ' (id_agente, id_grupo, evento,
timestamp, estado, utimestamp, id_usuario,
event_type, criticity, id_agentmodule, id_alert_am,
critical_instructions, warning_instructions,
unknown_instructions, source, tags, custom_data,
server_id)
VALUES (%d, %d, "%s", CURRENT_TIMESTAMP, %d,
ceil((sysdate - to_date(\'19700101000000\',\'YYYYMMDDHH24MISS\')) * (86400)),
"%s", "%s", %d, %d, %d, "%s", "%s", "%s", "%s",
"%s", "%s", %d)',
$id_agent, $id_group, $event, $status, $id_user,
$event_type, $priority, $id_agent_module, $id_aam,
$critical_instructions, $warning_instructions,
$unknown_instructions, $source, $tags, $custom_data,
$server_id);
break;
}
}
else {
switch ($config["dbtype"]) {
case "mysql":
$sql = sprintf ('
INSERT INTO ' . $table_events . ' (id_agente, id_grupo, evento,
timestamp, estado, utimestamp, id_usuario,
event_type, criticity, id_agentmodule, id_alert_am,
critical_instructions, warning_instructions,
unknown_instructions, source, tags, custom_data)
VALUES (%d, %d, "%s", NOW(), %d, UNIX_TIMESTAMP(NOW()),
"%s", "%s", %d, %d, %d, "%s", "%s", "%s", "%s",
"%s", "%s")',
$id_agent, $id_group, $event, $status, $id_user,
$event_type, $priority, $id_agent_module, $id_aam,
$critical_instructions, $warning_instructions,
$unknown_instructions, $source, $tags, $custom_data);
break;
case "postgresql":
$sql = sprintf ('
INSERT INTO ' . $table_events . ' (id_agente, id_grupo, evento,
timestamp, estado, utimestamp, id_usuario,
event_type, criticity, id_agentmodule, id_alert_am,
critical_instructions, warning_instructions,
unknown_instructions, source, tags, custom_data)
VALUES (%d, %d, "%s", NOW(), %d,
ceil(date_part(\'epoch\', CURRENT_TIMESTAMP)), "%s",
"%s", %d, %d, %d, "%s", "%s", "%s", "%s", "%s",
"%s")',
$id_agent, $id_group, $event, $status, $id_user,
$event_type, $priority, $id_agent_module, $id_aam,
$critical_instructions, $warning_instructions,
$unknown_instructions, $source, $tags, $custom_data);
break;
case "oracle":
$sql = sprintf ('
INSERT INTO ' . $table_events . ' (id_agente, id_grupo, evento,
timestamp, estado, utimestamp, id_usuario,
event_type, criticity, id_agentmodule, id_alert_am,
critical_instructions, warning_instructions,
unknown_instructions, source, tags, custom_data)
VALUES (%d, %d, "%s", CURRENT_TIMESTAMP, %d,
ceil((sysdate - to_date(\'19700101000000\',\'YYYYMMDDHH24MISS\')) * (86400)),
"%s", "%s", %d, %d, %d, "%s", "%s", "%s", "%s",
"%s", "%s")',
$id_agent, $id_group, $event, $status, $id_user,
$event_type, $priority, $id_agent_module, $id_aam,
$critical_instructions, $warning_instructions,
$unknown_instructions, $source, $tags, $custom_data);
break;
}
}
return (int) db_process_sql ($sql, "insert_id");
}

View File

@ -465,10 +465,10 @@ function event_comment() {
var params = [];
params.push("page=include/ajax/events");
params.push("add_comment=1");
params.push("event_id="+event_id);
params.push("comment="+comment);
params.push("meta="+meta);
params.push("history="+history);
params.push("event_id=" + event_id);
params.push("comment=" + comment);
params.push("meta=" + meta);
params.push("history=" + history);
$('#button-comment_button').attr('disabled','disabled');
$('#response_loading').show();

View File

@ -163,10 +163,10 @@ if ($update_pressed || $open_filter) {
$table = html_get_predefined_table('transparent', 2);
$table->styleTable = 'width: 23px; float: right; background: #ECECEC;';
$table->width = '98%';
$table->style[0] = 'text-align:left;';
$table->style[1] = 'text-align:right;';
$table->style[0] = 'text-align: left;';
$table->style[1] = 'text-align: right;';
$table->data[0][1] = '<a id="events_graph_link" href="javascript:show_events_graph_dialog()">' . html_print_image('images/chart_curve.png', true, array('title' => __('Show events graph'))) . '</a>';
$table->data[0][1] = '<a id="events_graph_link" href="javascript: show_events_graph_dialog()">' . html_print_image('images/chart_curve.png', true, array('title' => __('Show events graph'))) . '</a>';
$table->cellstyle[0][1] = 'background: #ECECEC;';
html_print_table($table);
@ -610,7 +610,8 @@ enterprise_hook('close_meta_frame');
unset($table);
// Values to be used from javascript library
html_print_input_hidden('ajax_file', ui_get_full_url("ajax.php", false, false, false));
html_print_input_hidden('ajax_file',
ui_get_full_url("ajax.php", false, false, false));
html_print_input_hidden('meta', (int)$meta);
html_print_input_hidden('history', (int)$history);

View File

@ -33,18 +33,23 @@ Where options:\n
-create_event
-name <event_name> : Free text
-group <id_group> : Group ID (use 0 for 'all')
-agent :Agent ID
-agent : Agent ID
Optional parameters:
[-status <status>] : 0 New, 1 Validated, 2 In process
[-user <id_user>] : User comment (use in combination with -comment option)
[type <event_type>] : unknown, alert_fired, alert_recovered, alert_ceased
alert_manual_validation, system, error, new_agent
configuration_change, going_unknown, going_down_critical,
going_down_warning, going_up_normal
[-criticity <criticity>] : 0 Maintance, 1 Informative, 2 Normal,
3 Warning, 4 Crit, 5 Minor, 6 Major
[-type <event_type>] : unknown, alert_fired, alert_recovered, alert_ceased
alert_manual_validation, system, error, new_agent
configuration_change, going_unknown, going_down_critical,
going_down_warning, going_up_normal
[-criticity <criticity>] : 0 Maintance,
1 Informative,
2 Normal,
3 Warning,
4 Crit,
5 Minor,
6 Major
[-am <id_agent_module>] : ID Agent Module linked to event
[-alert <id_alert_am>] : ID Alert Module linked to event
[-c_instructions <critical_instructions>]
@ -53,19 +58,15 @@ Optional parameters:
[-user_comment <comment>]
[-owner_user <user for the event>]
[-source <source>] : (By default 'Pandora')
[-tag <tags>] : Tag (must exist in the system to be imported)";
[-tag <tags>] : Tag (must exist in the system to be imported)
[-custom_data <custom_data>]: Custom data should be a base 64 encoded JSON document
[-server_id <server_id>] : The pandora node server_id";
print "Credential/API syntax: \n\n\t";
print "<credentials>: API credentials separated by comma: <api_pass>,<user>,<pass>\n\n";
print "Example of event generation:\n\n";
#~ print "\t./pandora_revent.pl -p http://192.168.70.160/pandora_console/include/api.php -u pot12,admin,pandora \
#~ \t-create_event -name \"Sample event executed from commandline\" -group 2 -type \"system\" -agent 2 \
#~ \t-user \"admin\" -status 0 -am 0 -alert 9 -criticity 3 -comment \"User comments\" -tag \"tags\" \
#~ \t-source \"Commandline\" -extra 3 -c_instructions \"Critical instructions\" \
#~ \t-w_instructions \"Warning instructions\" -u_instructions \"Unknown instructions\" -owner \"other\" ";
print "\t./pandora_revent.pl -p http://localhost/pandora_console/include/api.php -u 1234,admin,pandora \
\t-create_event -name \"SampleEvent\" -group 2 -agent 189 -status 0 -user \"admin\" -type \"system\" \
\t-criticity 3 -am 0 -alert 9 -c_instructions \"Critical instructions\" -w_instructions \"Warning instructions\" \
@ -136,6 +137,8 @@ sub tool_api_main () {
my $id_event;
my $option = $ARGV[4];
my $call_api;
my $custom_data = "";
my $server_id = 0;
#~ help or api path (required)
if ($ARGV[0] eq '-h') {
@ -232,6 +235,12 @@ sub tool_api_main () {
if ($line eq '-owner_user') {
$owner_user = $ARGV[$i + 1];
}
if ($line eq '-custom_data') {
$custom_data = $ARGV[$i + 1];
}
if ($line eq '-server_id') {
$server_id = $ARGV[$i + 1];
}
$i++;
}
@ -264,7 +273,10 @@ sub tool_api_main () {
"|" . $user_comment .
"|" . $owner_user .
"|" . $source .
"|" . $tags;
"|" . $tags .
"|" . $custom_data .
"|" . $server_id;
$call_api = $api_path . '?' .
'op=set&' .
'op2=create_event&' .