function thold_poller_output($rrd_update_array) { global $config, $debug; include_once $config['base_path'] . '/plugins/thold/thold_functions.php'; include_once $config['library_path'] . '/snmp.php'; $rrd_reindexed = array(); $rrd_time_reindexed = array(); $rra_ids = ''; $x = 0; foreach ($rrd_update_array as $item) { if (isset($item['times'][key($item['times'])])) { if ($x) { $rra_ids .= ','; } $rra_ids .= $item['local_data_id']; $rrd_reindexed[$item['local_data_id']] = $item['times'][key($item['times'])]; $rrd_time_reindexed[$item['local_data_id']] = key($item['times']); $x++; } } if ($rra_ids != '') { $thold_items = db_fetch_assoc("SELECT thold_data.id, thold_data.name AS thold_name, thold_data.graph_id,\r\n\t\t\tthold_data.percent_ds, thold_data.expression,\r\n\t\t\tthold_data.data_type, thold_data.cdef, thold_data.rra_id,\r\n\t\t\tthold_data.data_id, thold_data.lastread,\r\n\t\t\tUNIX_TIMESTAMP(thold_data.lasttime) AS lasttime, thold_data.oldvalue,\r\n\t\t\tdata_template_rrd.data_source_name as name,\r\n\t\t\tdata_template_rrd.data_source_type_id, data_template_data.rrd_step,\r\n\t\t\tdata_template_rrd.rrd_maximum\r\n\t\t\tFROM thold_data\r\n\t\t\tLEFT JOIN data_template_rrd\r\n\t\t\tON (data_template_rrd.id = thold_data.data_id)\r\n\t\t\tLEFT JOIN data_template_data\r\n\t\t\tON ( data_template_data.local_data_id = thold_data.rra_id )\r\n\t\t\tWHERE data_template_rrd.data_source_name!=''\r\n\t\t\tAND thold_data.rra_id IN({$rra_ids})", false); } else { return $rrd_update_array; } if (sizeof($thold_items)) { foreach ($thold_items as $t_item) { thold_debug("Checking Threshold:'" . $t_item["thold_name"] . "', Graph:'" . $t_item["graph_id"] . "'"); $item = array(); $currenttime = 0; $currentval = thold_get_currentval($t_item, $rrd_reindexed, $rrd_time_reindexed, $item, $currenttime); switch ($t_item['data_type']) { case 0: break; case 1: if ($t_item['cdef'] != 0) { $currentval = thold_build_cdef($t_item['cdef'], $currentval, $t_item['rra_id'], $t_item['data_id']); } break; case 2: if ($t_item['percent_ds'] != '') { $currentval = thold_calculate_percent($t_item, $currentval, $rrd_reindexed); } break; case 3: if ($t_item['expression'] != '') { $currentval = thold_calculate_expression($t_item, $currentval, $rrd_reindexed, $rrd_time_reindexed); } break; } if (is_numeric($currentval)) { $currentval = round($currentval, 4); } else { $currentval = ''; } db_execute("UPDATE thold_data SET tcheck=1, lastread='{$currentval}',\r\n\t\t\tlasttime='" . date("Y-m-d H:i:s", $currenttime) . "',\r\n\t\t\toldvalue='" . $item[$t_item['name']] . "'\r\n\t\t\tWHERE rra_id = " . $t_item['rra_id'] . "\r\n\t\t\tAND data_id = " . $t_item['data_id']); } } return $rrd_update_array; }
$sql_query = "SELECT tdd.id, tdd.rrd_reindexed, tdd.rrd_time_reindexed, \n\ttd.id AS thold_id td.name AS thold_name, td.local_graph_id,\n\ttd.percent_ds, td.expression, td.data_type, td.cdef, td.local_data_id,\n\ttd.data_template_rrd_id, td.lastread,\n\tUNIX_TIMESTAMP(td.lasttime) AS lasttime, td.oldvalue,\n\tdtr.data_source_name as name, dtr.data_source_type_id, \n\tdtd.rrd_step, dtr.rrd_maximum\n\tFROM plugin_thold_daemon_data AS tdd\n\tINNER JOIN thold_data AS td\n\tON td.id = tdd.id\n\tLEFT JOIN data_template_rrd AS dtr\n\tON dtr.id = td.data_template_rrd_id\n\tLEFT JOIN data_template_data AS dtd\n\tON dtd.local_data_id = td.local_data_id\n\tWHERE tdd.pid = '{$pid}'\n\tAND dtr.data_source_name!=''"; $tholds = db_fetch_assoc($sql_query, false); if (sizeof($tholds)) { /* hold data of all CDEFs in memory to reduce the number of SQL queries to minimum */ $cdefs = array(); $cdefs_tmp = db_fetch_assoc('SELECT cdef_id, sequence, type, value FROM cdef_items ORDER BY cdef_id, sequence'); if ($cdefs_tmp & sizeof($cdefs_tmp) > 0) { foreach ($cdefs_tmp as $cdef_tmp) { $cdefs[$cdef_tmp['cdef_id']][] = $cdef_tmp; } } unset($cdefs_tmp); $rrd_reindexed = array(); $rrd_time_reindexed = array(); foreach ($tholds as $thold_data) { thold_debug("Checking Threshold Name: '" . $thold_data['thold_name'] . "', Graph: '" . $thold_data['local_graph_id'] . "'"); $item = array(); $rrd_reindexed[$thold_data['local_data_id']] = unserialize($thold_data['thold_server_rrd_reindexed']); $rrd_time_reindexed[$thold_data['local_data_id']] = $thold_data['thold_server_rrd_time_reindexed']; $currenttime = 0; $currentval = thold_get_currentval($thold_data, $rrd_reindexed, $rrd_time_reindexed, $item, $currenttime); switch ($thold_data['data_type']) { case 0: break; case 1: if ($thold_data['cdef'] != 0) { $currentval = thold_build_cdef($cdefs[$thold_data['cdef']], $currentval, $thold_data['local_data_id'], $thold_data['data_template_rrd_id']); } break; case 2: if ($thold_data['percent_ds'] != '') {
function exec_script($command) { global $debug; cacti_log('THOLD ALERT COMMAND: ' . $command, false, 'SYSTEM'); $command_output = array(); $command_return = 999; if ($command == '') { thold_debug('DEBUG: thold exec_script: no command defined, do nothing'); return; } exec($command, $command_output, $command_return); thold_debug('DEBUG: thold exec_script: command ==>' . $command, true, 'POLLER'); foreach ($command_output as $key => $value) { thold_debug('DEBUG: thold exec_script: command output ==> ' . $key . ' = ' . $value, true, 'POLLER'); } thold_debug('DEBUG: thold exec_script: command return value ==> ' . $command_return, true, 'POLLER'); }
function thold_mail($to, $from, $subject, $message, $filename, $headers = '') { global $config; thold_debug('Preparing to send email'); include_once $config['base_path'] . '/plugins/settings/include/mailer.php'; include_once $config['base_path'] . '/plugins/thold/setup.php'; $subject = trim($subject); $message = str_replace('<SUBJECT>', $subject, $message); $how = read_config_option('settings_how'); if ($how < 0 && $how > 2) { $how = 0; } if ($how == 0) { $Mailer = new Mailer(array('Type' => 'PHP')); } else { if ($how == 1) { $sendmail = read_config_option('settings_sendmail_path'); $Mailer = new Mailer(array('Type' => 'DirectInject', 'DirectInject_Path' => $sendmail)); } else { if ($how == 2) { $smtp_host = read_config_option('settings_smtp_host'); $smtp_port = read_config_option('settings_smtp_port'); $smtp_username = read_config_option('settings_smtp_username'); $smtp_password = read_config_option('settings_smtp_password'); $Mailer = new Mailer(array('Type' => 'SMTP', 'SMTP_Host' => $smtp_host, 'SMTP_Port' => $smtp_port, 'SMTP_Username' => $smtp_username, 'SMTP_Password' => $smtp_password)); } } } if ($from == '') { $from = read_config_option('thold_from_email'); $fromname = read_config_option('thold_from_name'); if ($from == '') { if (isset($_SERVER['HOSTNAME'])) { $from = 'Cacti@' . $_SERVER['HOSTNAME']; } else { $from = 'Cacti@localhost'; } } if ($fromname == '') { $fromname = 'Cacti'; } $from = $Mailer->email_format($fromname, $from); if ($Mailer->header_set('From', $from) === false) { print 'ERROR: ' . $Mailer->error() . "\n"; return $Mailer->error(); } } else { $from = $Mailer->email_format('Cacti', $from); if ($Mailer->header_set('From', $from) === false) { print 'ERROR: ' . $Mailer->error() . "\n"; return $Mailer->error(); } } if ($to == '') { return 'Mailer Error: No <b>TO</b> address set!!<br>If using the <i>Test Mail</i> link, please set the <b>Alert e-mail</b> setting.'; } $to = explode(',', $to); foreach ($to as $t) { if (trim($t) != '' && !$Mailer->header_set('To', $t)) { print 'ERROR: ' . $Mailer->error() . "\n"; return $Mailer->error(); } } $wordwrap = read_config_option('settings_wordwrap'); if ($wordwrap == '') { $wordwrap = 76; } if ($wordwrap > 9999) { $wordwrap = 9999; } if ($wordwrap < 0) { $wordwrap = 76; } $Mailer->Config['Mail']['WordWrap'] = $wordwrap; if (!$Mailer->header_set('Subject', $subject)) { print 'ERROR: ' . $Mailer->error() . "\n"; return $Mailer->error(); } if (is_array($filename) && !empty($filename) && strstr($message, '<GRAPH>') !== 0) { foreach ($filename as $val) { $graph_data_array = array('output_flag' => RRDTOOL_OUTPUT_STDOUT); if (function_exists('imagecreatefrompng') && function_exists('imagejpeg')) { $data = @png2jpeg(rrdtool_function_graph($val['local_graph_id'], $val['rra_id'], $graph_data_array)); $ext = 'jpg'; } else { $data = @rrdtool_function_graph($val['local_graph_id'], $val['rra_id'], $graph_data_array); $ext = 'png'; } if ($data != '') { $cid = $Mailer->content_id(); if ($Mailer->attach($data, $val['filename'] . ".{$ext}", "image/{$ext}", 'inline', $cid) == false) { print 'ERROR: ' . $Mailer->error() . "\n"; return $Mailer->error(); } $message = str_replace('<GRAPH>', "<br><br><img src='cid:{$cid}'>", $message); } else { $message = str_replace('<GRAPH>', "<br><img src='" . $val['file'] . "'><br>Could not open!<br>" . $val['file'], $message); } } } $text = array('text' => '', 'html' => ''); if ($filename == '') { $message = str_replace('<br>', "\n", $message); $message = str_replace('<BR>', "\n", $message); $message = str_replace('</BR>', "\n", $message); $text['text'] = strip_tags($message); } else { $text['html'] = $message . '<br>'; $text['text'] = strip_tags(str_replace('<br>', "\n", $message)); } $v = thold_version(); $Mailer->header_set('X-Mailer', 'Cacti-Thold-v' . $v['version']); $Mailer->header_set('User-Agent', 'Cacti-Thold-v' . $v['version']); if (read_config_option('thold_email_prio') == 'on') { $Mailer->header_set('X-Priority', '1'); } thold_debug("Sending email to '" . trim(implode(',', $to), ',') . "'"); if ($Mailer->send($text) == false) { print 'ERROR: ' . $Mailer->error() . "\n"; return $Mailer->error(); } return ''; }
function thold_poller_output(&$rrd_update_array) { global $config, $debug; include_once $config['base_path'] . '/plugins/thold/thold_functions.php'; include_once $config['library_path'] . '/snmp.php'; $rrd_reindexed = array(); $rrd_time_reindexed = array(); $local_data_ids = ''; $x = 0; foreach ($rrd_update_array as $item) { if (isset($item['times'][key($item['times'])])) { if ($x) { $local_data_ids .= ','; } $local_data_ids .= $item['local_data_id']; $rrd_reindexed[$item['local_data_id']] = $item['times'][key($item['times'])]; $rrd_time_reindexed[$item['local_data_id']] = key($item['times']); $x++; } } if ($local_data_ids != '') { if (read_config_option('thold_daemon_enable')) { /* assign a new process id */ $thold_pid = time() . '_' . rand(); $thold_items = db_fetch_assoc("SELECT id, local_data_id FROM thold_data WHERE thold_daemon_pid = '' AND thold_data.local_data_id IN ({$local_data_ids})"); if ($thold_items) { /* avoid that concurrent processes will work on the same thold items */ db_execute("UPDATE thold_data SET thold_data.thold_daemon_pid = '{$thold_pid}' WHERE thold_daemon_pid = '' AND thold_data.local_data_id IN ({$local_data_ids});"); /* cache required polling data. prefer bulk inserts for performance reasons - start with chunks of 1000 items*/ $sql_max_inserts = 1000; $thold_items = array_chunk($thold_items, $sql_max_inserts); $sql_insert = 'INSERT INTO `plugin_thold_daemon_data` ( id, pid, rrd_reindexed, rrd_time_reindexed ) VALUES '; $sql_values = ''; foreach ($thold_items as $packet) { foreach ($packet as $thold_item) { $sql_values .= "('" . $thold_item['id'] . "','" . $thold_pid . "','" . serialize($rrd_reindexed[$thold_item['local_data_id']]) . "','" . $rrd_time_reindexed[$thold_item['local_data_id']] . "'),"; } db_execute($sql_insert . substr($sql_values, 0, -1)); $sql_values = ''; } /* queue a new thold process */ db_execute("INSERT INTO `plugin_thold_daemon_processes` ( pid ) VALUES('{$thold_pid}')"); } return $rrd_update_array; } /* hold data of all CDEFs in memory to reduce the number of SQL queries to minimum */ $cdefs = array(); $cdefs_tmp = db_fetch_assoc('SELECT cdef_id, sequence, type, value FROM cdef_items ORDER BY cdef_id, sequence'); if ($cdefs_tmp & sizeof($cdefs_tmp) > 0) { foreach ($cdefs_tmp as $cdef_tmp) { $cdefs[$cdef_tmp['cdef_id']][] = $cdef_tmp; } } unset($cdefs_tmp); $tholds = db_fetch_assoc("SELECT td.id, \n\t\t\ttd.name AS thold_name, td.local_graph_id,\n\t\t\ttd.percent_ds, td.expression,\n\t\t\ttd.data_type, td.cdef, td.local_data_id,\n\t\t\ttd.data_template_rrd_id, td.lastread,\n\t\t\tUNIX_TIMESTAMP(td.lasttime) AS lasttime, td.oldvalue,\n\t\t\tdtr.data_source_name as name,\n\t\t\tdtr.data_source_type_id, dtd.rrd_step,\n\t\t\tdtr.rrd_maximum\n\t\t\tFROM thold_data AS td\n\t\t\tLEFT JOIN data_template_rrd AS dtr\n\t\t\tON (dtr.id = td.data_template_rrd_id)\n\t\t\tLEFT JOIN data_template_data AS dtd\n\t\t\tON dtd.local_data_id = td.local_data_id\n\t\t\tWHERE dtr.data_source_name!=''\n\t\t\tAND td.local_data_id IN({$local_data_ids})", false); } else { return $rrd_update_array; } if (sizeof($tholds)) { foreach ($tholds as $thold_data) { thold_debug("Checking Threshold: Name: '" . $thold_data['thold_name'] . "', Graph: '" . $thold_data['local_graph_id'] . "'"); $item = array(); $currenttime = 0; $currentval = thold_get_currentval($thold_data, $rrd_reindexed, $rrd_time_reindexed, $item, $currenttime); switch ($thold_data['data_type']) { case 0: break; case 1: if ($thold_data['cdef'] != 0) { $currentval = thold_build_cdef($cdefs[$thold_data['cdef']], $currentval, $thold_data['local_data_id'], $thold_data['data_template_rrd_id']); } break; case 2: if ($thold_data['percent_ds'] != '') { $currentval = thold_calculate_percent($thold_data, $currentval, $rrd_reindexed); } break; case 3: if ($thold_data['expression'] != '') { $currentval = thold_calculate_expression($thold_data, $currentval, $rrd_reindexed, $rrd_time_reindexed); } break; } if (is_numeric($currentval)) { $currentval = round($currentval, 4); } else { $currentval = ''; } db_execute("UPDATE thold_data SET tcheck=1, lastread='{$currentval}',\n\t\t\t\tlasttime='" . date('Y-m-d H:i:s', $currenttime) . "',\n\t\t\t\toldvalue='" . (isset($item[$thold_data['name']]) ? $item[$thold_data['name']] : '') . "'\n\t\t\t\tWHERE local_data_id = " . $thold_data['local_data_id'] . "\n\t\t\t\tAND data_template_rrd_id = " . $thold_data['data_template_rrd_id']); } } return $rrd_update_array; }
function thold_mail($to_email, $from_email, $subject, $message, $filename, $headers = '') { thold_debug('Preparing to send email'); $subject = trim($subject); $message = str_replace('<SUBJECT>', $subject, $message); if ($from_email == '') { $from_email = read_config_option('thold_from_email'); $from_name = read_config_option('thold_from_name'); if ($from_email == '') { if (isset($_SERVER['HOSTNAME'])) { $from_email = 'Cacti@' . $_SERVER['HOSTNAME']; } else { $from_email = 'Cacti@localhost'; } } if ($from_name == '') { $from_name = 'Cacti'; } } if ($to_email == '') { return __('Mailer Error: No <b>TO</b> address set!!<br>If using the <i>Test Mail</i> link, please set the <b>Alert e-mail</b> setting.'); } $attachments = array(); if (is_array($filename) && !empty($filename) && strstr($message, '<GRAPH>') !== 0) { foreach ($filename as $val) { $graph_data_array = array('graph_start' => time() - 86400, 'graph_end' => time(), 'image_format' => 'png', 'graph_theme' => 'modern', 'output_flag' => RRDTOOL_OUTPUT_STDOUT, 'disable_cache' => true); $attachments[] = array('attachment' => @rrdtool_function_graph($val['local_graph_id'], '', $graph_data_array, ''), 'filename' => 'graph_' . $val['local_graph_id'] . '.png', 'mime_type' => 'image/png', 'local_graph_id' => $val['local_graph_id'], 'local_data_id' => $val['local_data_id'], 'inline' => 'inline'); } } $text = array('text' => '', 'html' => ''); if ($filename == '') { $message = str_replace('<br>', "\n", $message); $message = str_replace('<BR>', "\n", $message); $message = str_replace('</BR>', "\n", $message); $text['text'] = strip_tags($message); } else { $text['html'] = $message . '<br>'; $text['text'] = strip_tags(str_replace('<br>', "\n", $message)); } $version = db_fetch_cell("SELECT version \n\t\tFROM plugin_config \n\t\tWHERE name='thold'"); $headers['X-Mailer'] = 'Cacti-Thold-v' . $version; $headers['User-Agent'] = 'Cacti-Thold-v' . $version; if (read_config_option('thold_email_prio') == 'on') { $headers['X-Priority'] = '1'; } thold_debug("Sending email to '" . trim($to_email, ', ') . "'"); $error = mailer(array($from_email, $from_name), $to_email, '', '', '', $subject, $text['html'], $text['text'], $attachments, $headers); if (strlen($error)) { cacti_log('ERROR: Sending Email Failed. Error was ' . $error, true, 'THOLD'); return $error; } return ''; }