Skip to content

Commit

Permalink
QA: #453 - Adjustment of calling parameters and bug fixing
Browse files Browse the repository at this point in the history
- Revert thold_mail() calling parameters to more sane defaults
- Continue conversion of 'type' to 'topic'
- Add hostname and notification list to Queue GUI
  • Loading branch information
TheWitness committed Jul 24, 2023
1 parent ae6639c commit b5984d4
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 39 deletions.
12 changes: 6 additions & 6 deletions includes/polling.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ function thold_update_host_status() {
'device' => $host,
'subject' => $subject,
'message' => $msg,
'email' => $alert_email
'email' => $alert_email
)
);

Expand All @@ -533,7 +533,7 @@ function thold_update_host_status() {
} elseif ($host['thold_send_email'] == '0') {
cacti_log('NOTE: Device[' . $host['id'] . '] Hostname[' . $host['hostname'] . '] did not send a Device recovering email for \'' . $host['description'] . '\', disabled per Device setting!', true, 'THOLD');
} elseif ($alert_email != '') {
thold_mail($notify_list_id, $alert_email, '', '', $subject, $msg, '', '', 'thold_uhost_mail');
thold_mail($alert_email, '', '', $subject, $msg, '', '', '', $notify_list_id, $host, 'thold_uhost_mail');
}

$command = read_config_option('thold_device_command');
Expand Down Expand Up @@ -582,7 +582,7 @@ function thold_update_host_status() {
'envrionment' => $environment
);

thold_notification_add('thold_uhost_cmd', $data);
thold_notification_add('thold_uhost_cmd', $data, 'id', 0, $host);
}
} else {
cacti_log('WARNING: Device Up Command for Device[' . $host['id'] . '] Command[' . $command . '] Is either Not found or Not executable!', false, 'THOLD');
Expand Down Expand Up @@ -697,7 +697,7 @@ function thold_update_host_status() {
'device' => $host,
'subject' => $subject,
'message' => $msg,
'email' => $alert_email
'email' => $alert_email
)
);

Expand All @@ -708,7 +708,7 @@ function thold_update_host_status() {
} elseif ($host['thold_send_email'] == '0') {
cacti_log('NOTE: Device[' . $host['id'] . '] Hostname[' . $host['hostname'] . '] did not send a Device down email for \'' . $host['description'] . '\', disabled per Device setting!', true, 'THOLD');
} elseif ($alert_email != '') {
thold_mail($notify_list_id, $alert_email, '', '', $subject, $msg, '', '', 'thold_dhost_mail');
thold_mail($alert_email, '', '', $subject, $msg, '', '', '', $notify_list_id, $host, 'thold_dhost_mail');
}

$command = read_config_option('thold_device_command');
Expand Down Expand Up @@ -756,7 +756,7 @@ function thold_update_host_status() {
'envrionment' => $environment
);

thold_notification_add('thold_dhost_cmd', $data);
thold_notification_add('thold_dhost_cmd', $data, 'id', 0, $host);
}
} else {
cacti_log('WARNING: Device Down Command for Device[' . $host['id'] . '] Command[' . $command . '] Is either Not found or Not executable!', false, 'THOLD');
Expand Down
51 changes: 41 additions & 10 deletions notify_queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ function notify_queue() {
<input type='text' class='ui-state-default ui-corner-all' id='filter' size='25' value='<?php print html_escape_request_var('filter');?>'>
</td>
<td>
<?php print __('Type', 'thold');?>
<?php print __('Topic', 'thold');?>
</td>
<td>
<select id='topic' onChange='applyFilter()'>
Expand Down Expand Up @@ -357,7 +357,9 @@ function clearFilter() {
$sql_where = '';

if (get_request_var('filter') != '') {
$sql_where = 'WHERE (object_name LIKE ' . db_qstr('%' . get_request_var('filter') . '%') . ')';
$sql_where = 'WHERE (nq.object_name LIKE ' . db_qstr('%' . get_request_var('filter') . '%') . ' OR ' .
'h.hostname LIKE ' . db_qstr('%' . get_request_var('filter') . '%') . ' OR ' .
'nq.hostname LIKE ' . db_qstr('%' . get_request_var('filter') . '%') . ')';
}

if (get_request_var('topic') != '' && get_request_var('topic') != '-1') {
Expand All @@ -371,18 +373,29 @@ function clearFilter() {
}

$total_rows = db_fetch_cell("SELECT COUNT(*)
FROM notification_queue
FROM notification_queue AS nq
LEFT JOIN host AS h
ON h.id = nq.host_id
$sql_where");

$sql_order = get_order_string();
$sql_limit = ' LIMIT ' . ($rows*(get_request_var('page')-1)) . ',' . $rows;

$notify_list = db_fetch_assoc("SELECT *
FROM notification_queue
$notifications = db_fetch_assoc("SELECT *
FROM notification_queue AS nq
LEFT JOIN host AS h
ON h.id = nq.host_id
$sql_where
$sql_order
$sql_limit");

$lists = array_rekey(
db_fetch_assoc('SELECT id, name
FROM plugin_notification_lists
ORDER BY id'),
'id', 'name'
);

$nav = html_nav_bar('notify_queue.php?filter=' . get_request_var('filter'), MAX_DISPLAY_PAGES, get_request_var('page'), $rows, $total_rows, 5, __('Notifications', 'thold'), 'page', 'main');

form_start('notify_queue.php', 'chk');
Expand All @@ -393,7 +406,7 @@ function clearFilter() {

$display_text = array(
'topic' => array(
'display' => __('Type', 'thold'),
'display' => __('Topic', 'thold'),
'align' => 'left',
'sort' => 'ASC',
'tip' => __('The supported notification topic.', 'thold')
Expand All @@ -403,8 +416,18 @@ function clearFilter() {
'align' => 'left',
'tip' => __('The name of the object as defined by the caller.', 'thold')
),
'hostname' => array(
'display' => __('Hostname', 'thold'),
'align' => 'left',
'tip' => __('The hostname that was the source of this event.', 'thold')
),
'nosort' => array(
'display' => __('Notifaction List', 'thold'),
'align' => 'left',
'tip' => __('The Notification List used if any.', 'thold')
),
'object_id' => array(
'display' => __('Event Object ID', 'thold'),
'display' => __('Object ID', 'thold'),
'align' => 'right',
'sort' => 'DESC',
'tip' => __('The Object ID defined by the caller. Generally its unique \'id\'.', 'thold')
Expand Down Expand Up @@ -437,14 +460,22 @@ function clearFilter() {

html_header_sort_checkbox($display_text, get_request_var('sort_column'), get_request_var('sort_direction'), false);

if (cacti_sizeof($notify_list)) {
foreach ($notify_list as $n) {
if (cacti_sizeof($notifications)) {
foreach ($notifications as $n) {
$data = json_decode($n['event_data'], true);

form_alternate_row('line' . $n['id'], false);

form_selectable_cell($thold_notification_topics[$n['topic']], $n['id']);
form_selectable_cell($n['object_name'], $n['id']);
form_selectable_cell($n['hostname'], $n['id']);

if (isset($lists[$n['notification_list_id']])) {
form_selectable_cell($lists[$n['notification_list_id']], $n['id']);
} else {
form_selectable_cell(__('Not Specified', 'thold'), $n['id']);
}

form_selectable_cell($n['id'], $n['id'], '', 'right');
form_selectable_cell($n['event_time'], $n['id'], '', 'right');
form_selectable_cell($n['event_processed'] == 0 ? __('Pending', 'thold'):__('Done', 'thold'), $n['id'], '', 'right');
Expand All @@ -467,7 +498,7 @@ function clearFilter() {

html_end_box(false);

if (cacti_sizeof($notify_list)) {
if (cacti_sizeof($notifications)) {
print $nav;
}

Expand Down
63 changes: 40 additions & 23 deletions thold_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2043,6 +2043,7 @@ function thold_check_threshold(&$thold_data) {
}
} else {
/* function called during polling */
$h['id'] = $thold_data['host_id'];
$h['snmp_engine_id'] = $thold_data['snmp_engine_id'];
$h['description'] = $thold_data['description'];
$h['hostname'] = $thold_data['hostname'];
Expand Down Expand Up @@ -2172,7 +2173,8 @@ function thold_check_threshold(&$thold_data) {

if (trim($alert_emails) != '' && $thold_data['acknowledgment'] == '') {
$alert_msg = get_thold_alert_text($thold_data['data_source_name'], $thold_data, $h, $thold_data['lastread'], $thold_data['local_graph_id']);
thold_mail($notify_list_id, $alert_emails, $alert_bcc_emails, '', $subject, $alert_msg, $file_array);

thold_mail($alert_emails, $alert_bcc_emails, '', $subject, $alert_msg, $file_array, '', $notify_list_id, $h);
}

thold_command_execution($thold_data, $h, $breach_up, $breach_down);
Expand Down Expand Up @@ -2290,9 +2292,7 @@ function thold_check_threshold(&$thold_data) {
if (trim($warning_emails) != '' && $thold_data['acknowledgment'] == '') {
$warn_msg = get_thold_warning_text($thold_data['data_source_name'], $thold_data, $h, $thold_data['lastread'], $thold_data['local_graph_id']);



thold_mail($notify_list_id, $warning_emails, $warning_bcc_emails, '', $subject, $warn_msg, $file_array);
thold_mail($warning_emails, $warning_bcc_emails, '', $subject, $warn_msg, $file_array, '', $notify_list_id, $h);
}

$save = array(
Expand Down Expand Up @@ -2357,7 +2357,7 @@ function thold_check_threshold(&$thold_data) {
if (trim($alert_emails) != '' && $thold_data['acknowledgment'] == '') {
$warn_msg = get_thold_warning_text($thold_data['data_source_name'], $thold_data, $h, $thold_data['lastread'], $thold_data['local_graph_id']);

thold_mail($notify_list_id, $alert_emails, $alert_bcc_emails, '', $subject, $warn_msg, $file_array);
thold_mail($alert_emails, $alert_bcc_emails, '', $subject, $warn_msg, $file_array, '', $notify_list_id, $h);
}

$save = array(
Expand Down Expand Up @@ -2452,7 +2452,7 @@ function thold_check_threshold(&$thold_data) {
if (trim($warning_emails) != '' && $thold_data['restored_alert'] != 'on' && $thold_data['acknowledgment'] == '') {
$restoral_msg = get_thold_restoral_text($thold_data['data_source_name'], $thold_data, $h, $thold_data['lastread'], $thold_data['local_graph_id']);

thold_mail($notify_list_id, $warning_emails, $warning_bcc_emails, '', $subject, $restoral_msg, $file_array);
thold_mail($warning_emails, $warning_bcc_emails, '', $subject, $restoral_msg, $file_array, '', $notify_list_id, $h);
}

$save = array(
Expand Down Expand Up @@ -2506,7 +2506,7 @@ function thold_check_threshold(&$thold_data) {
if (trim($alert_emails) != '' && $thold_data['restored_alert'] != 'on' && $thold_data['acknowledgment'] == '') {
$restoral_msg = get_thold_restoral_text($thold_data['data_source_name'], $thold_data, $h, $thold_data['lastread'], $thold_data['local_graph_id']);

thold_mail($notify_list_id, $alert_emails, $alert_bcc_emails, '', $subject, $restoral_msg, $file_array);
thold_mail($alert_emails, $alert_bcc_emails, '', $subject, $restoral_msg, $file_array, '', $notify_list_id, $h);
}

thold_command_execution($thold_data, $h, false, false, true);
Expand Down Expand Up @@ -2602,7 +2602,7 @@ function thold_check_threshold(&$thold_data) {
if (trim($alert_emails) != '' && $thold_data['acknowledgment'] == '') {
$restoral_msg = get_thold_restoral_text($thold_data['data_source_name'], $thold_data, $h, $thold_data['lastread'], $thold_data['local_graph_id']);

thold_mail($notify_list_id, $alert_emails, $alert_bcc_emails, '', $subject, $restoral_msg, $file_array);
thold_mail($alert_emails, $alert_bcc_emails, '', $subject, $restoral_msg, $file_array, '', $notify_list_id, $h);
}

thold_command_execution($thold_data, $h, false, false, true);
Expand Down Expand Up @@ -2709,7 +2709,8 @@ function thold_check_threshold(&$thold_data) {

if (trim($alert_emails) != '' && $thold_data['acknowledgment'] == '') {
$alert_msg = get_thold_alert_text($thold_data['data_source_name'], $thold_data, $h, $thold_data['lastread'], $thold_data['local_graph_id']);
thold_mail($notify_list_id, $alert_emails, $alert_bcc_emails, '', $subject, $alert_msg, $file_array);

thold_mail($alert_emails, $alert_bcc_emails, '', $subject, $alert_msg, $file_array, '', $notify_list_id, $h);
}

thold_command_execution($thold_data, $h, $breach_up, $breach_down, false);
Expand Down Expand Up @@ -2922,7 +2923,7 @@ function thold_check_threshold(&$thold_data) {
if (trim($alert_emails) != '' && $thold_data['acknowledgment'] == '') {
$alert_msg = get_thold_alert_text($thold_data['data_source_name'], $thold_data, $h, $thold_data['lastread'], $thold_data['local_graph_id']);

thold_mail($notify_list_id, $alert_emails, $alert_bcc_emails, '', $subject, $alert_msg, $file_array);
thold_mail($alert_emails, $alert_bcc_emails, '', $subject, $alert_msg, $file_array, '', $notify_list_id, $h);
}

thold_command_execution($thold_data, $h, $breach_up, $breach_down, false);
Expand Down Expand Up @@ -3063,7 +3064,7 @@ function thold_check_threshold(&$thold_data) {

$notify_list_id = $thold_data['notify_warning'];

thold_mail($notify_list_id, $warning_emails, $warning_bcc_emails, '', $subject, $warn_msg, $file_array);
thold_mail($warning_emails, $warning_bcc_emails, '', $subject, $warn_msg, $file_array, '', $notify_list_id, $h);
}

$save = array(
Expand Down Expand Up @@ -3175,7 +3176,7 @@ function thold_check_threshold(&$thold_data) {
if (trim($warning_emails) != '' && $thold_data['restored_alert'] != 'on') {
$restoral_msg = get_thold_restoral_text($thold_data['data_source_name'], $thold_data, $h, $thold_data['lastread'], $thold_data['local_graph_id']);

thold_mail($notify_list_id, $warning_emails, $warning_bcc_emails, '', $subject, $restoral_msg, $file_array);
thold_mail($warning_emails, $warning_bcc_emails, '', $subject, $restoral_msg, $file_array, '', $notify_list_id, $h);
}

$save = array(
Expand Down Expand Up @@ -3247,7 +3248,7 @@ function thold_check_threshold(&$thold_data) {
if (trim($alert_emails) != '' && $thold_data['restored_alert'] != 'on') {
$restoral_msg = get_thold_restoral_text($thold_data['data_source_name'], $thold_data, $h, $thold_data['lastread'], $thold_data['local_graph_id']);

thold_mail($notify_list_id, $alert_emails, $alert_bcc_emails, '', $subject, $restoral_msg, $file_array);
thold_mail($alert_emails, $alert_bcc_emails, '', $subject, $restoral_msg, $file_array, '', $notify_list_id, $h);
}

thold_command_execution($thold_data, $h, false, false, true);
Expand Down Expand Up @@ -3489,7 +3490,7 @@ function thold_command_execution(&$thold_data, &$h, $breach_up, $breach_down, $b
'environment' => $environment
);

thold_notification_add('thold_cmd', $data);
thold_notification_add('thold_cmd', $data, 'id', 0, $h);
} else {
exec($cmd, $output, $return);
}
Expand All @@ -3508,7 +3509,7 @@ function thold_command_execution(&$thold_data, &$h, $breach_up, $breach_down, $b
'environment' => $environment
);

thold_notification_add('thold_cmd', $data);
thold_notification_add('thold_cmd', $data, 'id', 0, $h);
} else {
exec($cmd, $output, $return);
}
Expand All @@ -3527,7 +3528,7 @@ function thold_command_execution(&$thold_data, &$h, $breach_up, $breach_down, $b
'environment' => $environment
);

thold_notification_add('thold_cmd', $data);
thold_notification_add('thold_cmd', $data, 'id', 0, $h);
} else {
exec($cmd, $output, $return);
}
Expand Down Expand Up @@ -5531,7 +5532,7 @@ function thold_create_from_template($local_data_id, $local_graph_id, $data_templ
}

/* Sends a group of graphs to a user */
function thold_mail($notify_list_id, $to_email, $bcc_email, $from_email, $subject, $message, $filename, $headers = array(), $topic = 'thold_mail') {
function thold_mail($to_email, $bcc_email, $from_email, $subject, $message, $filename, $headers = array(), $notify_list_id = 0, &$host = array(), $topic = 'thold_mail') {
thold_debug('Preparing to send email');

$subject = trim($subject);
Expand Down Expand Up @@ -5645,7 +5646,7 @@ function thold_mail($notify_list_id, $to_email, $bcc_email, $from_email, $subjec
'html' => $thold_send_text_only != 'on'
);

thold_notification_add($topic, $data, $notify_list_id);
thold_notification_add($topic, $data, 'id', $notify_list_id, $host);
} else {
$any_error = mailer(
array($from_email, $from_name),
Expand Down Expand Up @@ -5679,7 +5680,7 @@ function thold_mail($notify_list_id, $to_email, $bcc_email, $from_email, $subjec
'html' => $thold_send_text_only != 'on'
);

thold_notification_add($topic, $data);
thold_notification_add($topic, $data, 'id', $notify_list_id, $host);
} else {
$error = mailer(
array($from_email, $from_name),
Expand Down Expand Up @@ -5711,7 +5712,7 @@ function thold_mail($notify_list_id, $to_email, $bcc_email, $from_email, $subjec
return '';
}

function thold_notification_add($topic, &$data, $list_id = 0, $id = 'id') {
function thold_notification_add($topic, &$data, $id = 'id', $list_id = 0, &$host = array()) {
$now = date('Y-m-d H:i:s');

if (isset($data[$id])) {
Expand All @@ -5730,10 +5731,26 @@ function thold_notification_add($topic, &$data, $list_id = 0, $id = 'id') {
$name = 'Not Specfified';
}

if (isset($host['id'])) {
$host_id = $host['id'];
} else {
$host_id = 0;
}

if (isset($host['hostname'])) {
if ($host['hostname'] == 'localhost') {
$hostname = $host['description'];
} else {
$hostname = $host['hostname'];
}
} else {
$hostname = 'unknown';
}

db_execute_prepared('INSERT INTO notification_queue
(topic, notification_list_id, object_id, object_name, event_time, event_data) VALUES
(?, ?, ?, ?, ?, ?)',
array($topic, $list_id, $id, $name, $now, json_encode($data, JSON_THROW_ON_ERROR)));
(topic, notification_list_id, object_id, object_name, host_id, hostname, event_time, event_data) VALUES
(?, ?, ?, ?, ?, ?, ?, ?)',
array($topic, $list_id, $id, $name, $host_id, $hostname, $now, json_encode($data, JSON_THROW_ON_ERROR)));
}

function thold_notification_execute($pid = 0, $max_records = 'all') {
Expand Down

0 comments on commit b5984d4

Please sign in to comment.