Cacti中为每主机定义不同的Down机报警邮件接收地址

cacti属于轻量级的监控系统,默认只能配置一个报警信箱,为了协同作业,需要增加报警信箱,实现每台主机配置一个报警信箱,详情如下:

环境:Cacti 0.8.7g Monitor1.2.1 Thold 0.4.1

1、cacti.host表增加一个字段  alertMail

ALTER TABLE host ADD alertMail varchar(200) default '' not null AFTER monitor_text;

2、修改脚本/plugins/monitor/setup.php, 设置Host管理表单项,找到monitor_config_form()方法, 在$fields_host_edit3['monitor_text']数组变量下加 入$fields_host_edit3['alertMail']数组,如下:

    $fields_host_edit3['alertMail'] = array(

    "method" => "textbox",

    "friendly_name" => "Alert Email",

    "description" => "This is the email address that will be sent when this host is reported as down.",

    "value" => "|arg1:alertMail|",

    "max_length" => "250",

    );

找到monitor_api_device_save()方法,在return $save;一行的上面,增加如下代码

if (isset($_POST['alertMail']))

    $save['alertMail'] = form_input_validate($_POST['alertMail'], 'alertMail', '', true, 3);

else

    $save['alertMail'] = form_input_validate('', 'alertMail', '', true, 3);

3、修改脚本/plugins/thold/includes/polling.php, 将thold_update_host_status()方法替换为如下:

    function thold_update_host_status () {

        global $config;

        // Return if we aren't set to notify

        $deadnotify = (read_config_option('alert_deadnotify') == 'on');

        if (!$deadnotify) return 0;

        include_once($config['base_path'] . '/plugins/thold/thold_functions.php');


    $alert_email = read_config_option('alert_email');
    $ping_failure_count = read_config_option('ping_failure_count');
    // Lets find hosts that were down, but are now back up
    $failed = read_config_option('thold_failed_hosts', true);
    $failed = explode(',', $failed);
    if (!empty($failed)) {
            foreach($failed as $id) {
                    if ($id != '') {
                            $host = db_fetch_row('SELECT id, status, description, hostname, alertMail FROM host WHERE id = ' . $id);
                            if ($host['status'] == HOST_UP) {
                                    $subject = 'Host Notice : ' . $host['description'] . ' (' . $host['hostname'] . ') returned from DOWN state';
                                    $msg = $subject;
                                    if($host['alertMail']){
                                            $alert_email .= ','.$host['alertMail'];
                                    }
                                    if ($alert_email == '') {
                                            cacti_log('THOLD: Can not send Host Recovering email since the \'Alert e-mail\' setting is not set!', true, 'POLLER');
                                    } else {
                                            thold_mail($alert_email, '', $subject, $msg, '');
                                    }
                            }
                    }
            }
    }

    // Lets find hosts that are down
    $hosts = db_fetch_assoc('SELECT id, description, hostname, status_last_error, alertMail FROM host WHERE disabled="" AND status=' . HOST_DOWN . ' AND status_event_count=' . $ping_failure_count);
    $total_hosts = sizeof($hosts);
    if (count($hosts)) {
            foreach($hosts as $host) {
                    $subject = 'Host Error : ' . $host['description'] . ' (' . $host['hostname'] . ') is DOWN';
                    $msg = 'Host Error : ' . $host['description'] . ' (' . $host['hostname'] . ') is DOWN<br>Message : ' . $host['status_last_error'];
                    if($host['alertMail']){
                            $alert_email .= ','.$host['alertMail'];
                    }
                    if ($alert_email == '') {
                            cacti_log('THOLD: Can not send Host Down email since the \'Alert e-mail\' setting is not set!', true, 'POLLER');
                    } else {
                            thold_mail($alert_email, '', $subject, $msg, '');
                    }
            }
    }

    // Now lets record all failed hosts
    $hosts = db_fetch_assoc('SELECT id FROM host WHERE status != ' . HOST_UP);
    $failed = array();
    if (!empty($hosts)) {
            foreach ($hosts as $host) {
                    $failed[] = $host['id'];
            }
    }
    $failed = implode(',', $failed);
    db_execute("REPLACE INTO settings (name, value) VALUES ('thold_failed_hosts', '$failed')");

    return $total_hosts;
}

完成。

你可能感兴趣的:(Cacti中为每主机定义不同的Down机报警邮件接收地址)