示例#1
0
文件: reports.php 项目: MrWnn/cacti
/** generate_report		create the complete mail for a single report and send it
 * @param array $report	- complete row of reports table for the report to work upon
 * @param bool $force	- when forced, lastsent time will not be entered (e.g. Send Now)
 */
function generate_report($report, $force = false)
{
    global $config, $alignment;
    include_once $config["base_path"] . "/lib/time.php";
    include_once $config["base_path"] . "/lib/rrd.php";
    reports_log(__FUNCTION__ . ", report_id: " . $report["id"], false, "REPORTS TRACE", POLLER_VERBOSITY_MEDIUM);
    $body = reports_generate_html($report["id"], REPORTS_OUTPUT_EMAIL);
    $time = time();
    # get config option for first-day-of-the-week
    $first_weekdayid = read_graph_config_option("first_weekdayid");
    $offset = 0;
    $graphids = array();
    $attachments = array();
    while (true) {
        $pos = strpos($body, "<GRAPH:", $offset);
        if ($pos) {
            $offset = $pos + 7;
            $graph = substr($body, $pos + 7, 10);
            $arr = explode(":", $graph);
            $arr1 = explode(">", $arr[1]);
            $graphid = $arr[0];
            $timespan = $arr1[0];
            $graphids[$graphid . ":" . $timespan] = $graphid;
        } else {
            break;
        }
    }
    if (sizeof($graphids)) {
        foreach ($graphids as $key => $graphid) {
            $arr = explode(":", $key);
            $timesp = $arr[1];
            $timespan = array();
            # get start/end time-since-epoch for actual time (now()) and given current-session-timespan
            get_timespan($timespan, $time, $timesp, $first_weekdayid);
            # provide parameters for rrdtool graph
            $graph_data_array = array('graph_start' => $timespan["begin_now"], 'graph_end' => $timespan["end_now"], 'graph_width' => $report["graph_width"], 'graph_height' => $report["graph_height"], 'output_flag' => RRDTOOL_OUTPUT_STDOUT);
            if ($report["thumbnails"] == "on") {
                $graph_data_array['graph_nolegend'] = true;
            }
            switch ($report["attachment_type"]) {
                case REPORTS_TYPE_INLINE_PNG:
                    $attachments[] = array('attachment' => @rrdtool_function_graph($graphid, "", $graph_data_array), 'filename' => 'graph_' . $graphid . ".png", 'mime_type' => 'image/png', 'graphid' => $graphid, 'timespan' => $timesp, 'inline' => 'inline');
                    break;
                case REPORTS_TYPE_INLINE_JPG:
                    $attachments[] = array('attachment' => png2jpeg(@rrdtool_function_graph($graphid, "", $graph_data_array)), 'filename' => 'graph_' . $graphid . ".jpg", 'mime_type' => 'image/jpg', 'graphid' => $graphid, 'timespan' => $timesp, 'inline' => 'inline');
                    break;
                case REPORTS_TYPE_INLINE_GIF:
                    $attachments[] = array('attachment' => png2gif(@rrdtool_function_graph($graphid, "", $graph_data_array)), 'filename' => 'graph_' . $graphid . ".gif", 'mime_type' => 'image/gif', 'graphid' => $graphid, 'timespan' => $timesp, 'inline' => 'inline');
                    break;
                case REPORTS_TYPE_ATTACH_PNG:
                    $attachments[] = array('attachment' => @rrdtool_function_graph($graphid, "", $graph_data_array), 'filename' => 'graph_' . $graphid . ".png", 'mime_type' => 'image/png', 'graphid' => $graphid, 'timespan' => $timesp, 'inline' => 'attachment');
                    break;
                case REPORTS_TYPE_ATTACH_JPG:
                    $attachments[] = array('attachment' => png2jpeg(@rrdtool_function_graph($graphid, "", $graph_data_array)), 'filename' => 'graph_' . $graphid . ".jpg", 'mime_type' => 'image/jpg', 'graphid' => $graphid, 'timespan' => $timesp, 'inline' => 'attachment');
                    break;
                case REPORTS_TYPE_ATTACH_GIF:
                    $attachments[] = array('attachment' => png2gif(@rrdtool_function_graph($graphid, "", $graph_data_array)), 'filename' => 'graph_' . $graphid . ".gif", 'mime_type' => 'image/gif', 'graphid' => $graphid, 'timespan' => $timesp, 'inline' => 'attachment');
                    break;
                case REPORTS_TYPE_INLINE_PNG_LN:
                    $attachments[] = array('attachment' => @rrdtool_function_graph($graphid, "", $graph_data_array), 'filename' => '', 'mime_type' => 'image/png', 'graphid' => $graphid, 'timespan' => $timesp, 'inline' => 'inline');
                    break;
                case REPORTS_TYPE_INLINE_JPG_LN:
                    $attachments[] = array('attachment' => png2jpeg(@rrdtool_function_graph($graphid, "", $graph_data_array)), 'filename' => '', 'mime_type' => 'image/jpg', 'graphid' => $graphid, 'timespan' => $timesp, 'inline' => 'inline');
                    break;
                case REPORTS_TYPE_INLINE_GIF_LN:
                    $attachments[] = array('attachment' => png2gif(@rrdtool_function_graph($graphid, "", $graph_data_array)), 'filename' => '', 'mime_type' => 'image/gif', 'graphid' => $graphid, 'timespan' => $timesp, 'inline' => 'inline');
                    break;
                case REPORTS_TYPE_ATTACH_PDF:
                    #				$attachments[] = array(
                    #					'attachment' => png2gif(@rrdtool_function_graph($graphid, "", $graph_data_array)),
                    #					'filename'   => 'graph_' . $graphid . ".gif",
                    #					'mime_type'  => 'image/gif',
                    #					'graphid'    => $graphid,
                    #					'timespan'   => $timesp,
                    #					'inline'     => 'attachment'
                    #				);
                    break;
            }
        }
    }
    if ($report["subject"] != '') {
        $subject = $report["subject"];
    } else {
        $subject = $report["name"];
    }
    if (!isset($report['bcc'])) {
        $report['bcc'] = '';
    }
    $v = db_fetch_cell("SELECT cacti FROM version");
    $headers['User-Agent'] = 'Cacti-Reports-v' . $v;
    $error = mailer(array($report['from_email'], $report['from_name']), $report['email'], '', $report['bcc'], '', $subject, $body, 'Cacti Reporting Requires and HTML Email Client', $attachments, $headers);
    session_start();
    if (strlen($error)) {
        if (isset($_REQUEST["id"])) {
            $_SESSION['reports_error'] = "Problems sending Report '" . $report['name'] . "'.  Problem with e-mail Subsystem Error is '{$error}'";
            if (!isset($_POST["selected_items"])) {
                raise_message("reports_error");
            }
        } else {
            reports_log(__FUNCTION__ . ", Problems sending Report '" . $report['name'] . "'.  Problem with e-mail Subsystem Error is '{$error}'", false, "REPORTS", POLLER_VERBOSITY_LOW);
        }
    } elseif (isset($_REQUEST)) {
        $_SESSION['reports_message'] = "Report '" . $report['name'] . "' Sent Successfully";
        if (!isset($_POST["selected_items"])) {
            raise_message("reports_message");
        }
    }
    if (!isset($_REQUEST["id"]) && !$force) {
        $int = read_config_option("poller_interval");
        if ($int == '') {
            $int = 300;
        }
        $next = reports_interval_start($report["intrvl"], $report["count"], $report["offset"], $report['mailtime']);
        $next = floor($next / $int) * $int;
        $sql = "UPDATE reports SET mailtime={$next}, lastsent=" . time() . " WHERE id = " . $report['id'];
        reports_log(__FUNCTION__ . ", update sql: " . $sql, false, "REPORTS TRACE", POLLER_VERBOSITY_MEDIUM);
        db_execute($sql);
    }
}
示例#2
0
function reports_edit()
{
    global $config;
    global $fields_reports_edit;
    include_once $config['base_path'] . '/lib/reports.php';
    /* ================= input validation ================= */
    input_validate_input_number(get_request_var_request('id'));
    input_validate_input_number(get_request_var_request('page'));
    /* ==================================================== */
    /* clean up rule name */
    if (isset($_REQUEST['name'])) {
        $_REQUEST['name'] = sanitize_search_string(get_request_var_request('name'));
    }
    /* clean up tab name */
    if (isset($_REQUEST['tab'])) {
        $_REQUEST['tab'] = sanitize_search_string(get_request_var_request('tab'));
    }
    /* remember these search fields in session vars so we don't have to keep passing them around */
    load_current_session_value('page', 'sess_reports_edit_current_page', '1');
    load_current_session_value('tab', 'sess_reports_edit_tab', 'detail');
    load_current_session_value('rows', 'sess_reports_edit_rows', read_config_option('num_rows_table'));
    /* display the report */
    $report = array();
    if (!empty($_REQUEST['id'])) {
        $report = db_fetch_row('SELECT * FROM reports WHERE id=' . $_REQUEST['id']);
        # reformat mailtime to human readable format
        $report['mailtime'] = date(reports_date_time_format(), $report['mailtime']);
        # setup header
        $header_label = '[edit: ' . $report['name'] . ']';
    } else {
        $header_label = '[new]';
        # initialize mailtime with current timestamp
        $report['mailtime'] = date(reports_date_time_format(), floor(time() / read_config_option('poller_interval')) * read_config_option('poller_interval'));
    }
    /* if there was an error on the form, display the date in the correct format */
    if (isset($_SESSION['sess_field_values']['mailtime'])) {
        $_SESSION['sess_field_values']['mailtime'] = date(reports_date_time_format(), $_SESSION['sess_field_values']['mailtime']);
    }
    $tabs = array('details' => 'Details', 'items' => 'Items', 'preview' => 'Preview', 'events' => 'Events');
    /* set the default settings category */
    if (!isset($_REQUEST['tab'])) {
        $_REQUEST['tab'] = 'details';
    }
    $current_tab = $_REQUEST['tab'];
    print "<table width='100%' cellpadding='0' cellspacing='0' border='0'><tr><td><div class='tabs'><nav><ul>\n";
    if (sizeof($tabs)) {
        foreach (array_keys($tabs) as $tab_short_name) {
            if ($tab_short_name != 'details' && !empty($_REQUEST['id']) || $tab_short_name == 'details') {
                print "<li class='subTab'><a " . ($tab_short_name == $current_tab ? "class='selected'" : "") . " href='" . $config['url_path'] . get_reports_page() . "?" . "action=edit&tab=" . $tab_short_name . (isset($report["id"]) ? "&id=" . $_REQUEST["id"] : "") . "'>{$tabs[$tab_short_name]}</a></li>\n";
            }
        }
    }
    print "</ul></nav></div></td>\n";
    if (!empty($_REQUEST['id'])) {
        print "<td align='right'><a class='textHeader' href='" . htmlspecialchars(get_reports_page() . '?action=send&id=' . $_REQUEST['id'] . '&tab=' . $_REQUEST['tab']) . "'>Send Report</a></td>\n";
    }
    print "</tr></table>\n";
    switch ($_REQUEST['tab']) {
        case 'details':
            print '<form name="report" action="' . get_reports_page() . '" method="post">';
            html_start_box("<strong>Report Details</strong> {$header_label}", '100%', '', '3', 'center', '');
            draw_edit_form(array('config' => array('no_form_tag' => true), 'fields' => inject_form_variables($fields_reports_edit, $report)));
            html_end_box();
            form_hidden_box('id', isset($report['id']) ? $report['id'] : '0', '');
            form_hidden_box('save_component_report', '1', '');
            ?>
		<script type='text/javascript'>
		var cformat = document.getElementById('cformat');
		cformat.setAttribute('onclick', 'changeFormat()');

		function addLoadEvent(func) {
			if(typeof window.onload != 'function')
				window.onload = func;
			else {
				var oldLoad = window.onload;

				window.onload = function() {
					if(oldLoad) oldLoad();
					func();
				}
			}
		}
					
		function changeFormat() {
			if (cformat && cformat.checked) {
				document.getElementById('row_font_size').style.display='none';
				document.getElementById('row_format_file').style.display='';
			}else{
				document.getElementById('row_font_size').style.display='';
				document.getElementById('row_format_file').style.display='none';
			}
		}

		addLoadEvent(changeFormat);
		</script>
		<?php 
            form_save_button(get_reports_page(), 'return');
            break;
        case 'items':
            html_start_box("<strong>Report Items</strong> {$header_label}", '100%', '', '3', 'center', get_reports_page() . '?action=item_edit&id=' . $_REQUEST['id']);
            /* display the items */
            if (!empty($report['id'])) {
                display_reports_items($report['id']);
            }
            html_end_box();
            break;
        case 'events':
            if (($timestamp = strtotime($report['mailtime'])) === false) {
                $timestamp = time();
            }
            $poller_interval = read_config_option('poller_interval');
            if ($poller_interval == '') {
                $poller_interval = 300;
            }
            $timestamp = floor($timestamp / $poller_interval) * $poller_interval;
            $next = reports_interval_start($report['intrvl'], $report['count'], $report['offset'], $timestamp);
            $date_format = reports_date_time_format() . ' - l';
            html_start_box("<strong>Scheduled Events</strong> {$header_label}", '100%', '', '3', 'center', '');
            for ($i = 0; $i < 14; $i++) {
                form_alternate_row('line' . $i, true);
                form_selectable_cell(date($date_format, $next), $i);
                form_end_row();
                $next = reports_interval_start($report['intrvl'], $report['count'], $report['offset'], $next);
            }
            html_end_box(false);
            break;
        case 'preview':
            html_start_box("<strong>Report Preview</strong> {$header_label}", '100%', '', '3', 'center', '');
            print "\t\t\t\t\t<tr><td>\n";
            print reports_generate_html($report['id'], REPORTS_OUTPUT_STDOUT);
            print "\t\t\t\t\t</td></tr>\n";
            html_end_box(false);
            break;
    }
}