/**
 * Check all alerts for a device to see if they should be notified or not
 *
 * @param array device
 * @return NULL
 */
function process_alerts($device)
{
    global $config, $alert_rules, $alert_assoc;
    echo "Processing alerts for " . $device['hostname'] . PHP_EOL;
    $alert_table = cache_device_alert_table($device['device_id']);
    $sql = "SELECT * FROM  `alert_table`";
    $sql .= " LEFT JOIN  `alert_table-state` ON  `alert_table`.`alert_table_id` =  `alert_table-state`.`alert_table_id`";
    $sql .= " WHERE  `device_id` =  ?";
    foreach (dbFetchRows($sql, array($device['device_id'])) as $entry) {
        echo 'Alert: ' . $entry['alert_table_id'] . ' Status: ' . $entry['alert_status'] . ' ';
        // If the alerter is now OK and has previously alerted, send an recovery notice.
        if ($entry['alert_status'] == '1' && $entry['has_alerted'] == '1') {
            $alert = $alert_rules[$entry['alert_test_id']];
            $state = json_decode($entry['state'], TRUE);
            $conditions = json_decode($alert['conditions'], TRUE);
            $entity = get_entity_by_id_cache($entry['entity_type'], $entry['entity_id']);
            $graphs = "";
            $metric_text = "";
            foreach ($state['metrics'] as $metric => $value) {
                $metric_text .= $metric . " = " . $value . PHP_EOL . "<br />";
            }
            // FIXME De-dup this shit soon.
            // - adama
            $message = '
<head>
    <title>Observium Alert</title>
<style>
.observium{ width:100%; max-width: 500px; -webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px; border:1px solid #DDDDDD; background-color:#FAFAFA;
 font-size: 13px; color: #777777; }
.header{ font-weight: bold; font-size: 16px; padding: 5px; color: #555555; }
.red { color: #cc0000; }
#deviceinfo tr:nth-child(odd) { background: #ffffff; }
</style>
<style type="text/css"></style></head>
<body>
<table class="observium">
  <tbody>
    <tr>
      <td>
        <table class="observium" id="deviceinfo">
  <tbody>
    <tr><td class="header">RECOVERY</td><td><a style="float: right;" href="' . generate_url(array('page' => 'device', 'device' => $device['device_id'], 'tab' => 'alert', 'alert_entry' => $entry['alert_table_id'])) . '">Modify</a></td></tr>
    <tr><td><b>Alert</b></font></td><td class="red">' . $alert['alert_message'] . '</font></td></tr>
    <tr><td><b>Entity</b></font></td><td>' . generate_entity_link($entry['entity_type'], $entry['entity_id'], $entity['entity_name']) . '</font></td></tr>';
            if (strlen($entity['entity_descr']) > 0) {
                $message .= '<tr><td><b>Descr</b></font></td><td>' . $entity['entity_descr'] . '</font>';
            }
            $message .= '
    <tr><td><b>Metrics</b></font></td><td>' . $metric_text . '</font></td></tr>
    <tr><td><b>Duration</b></font></td><td>' . formatUptime(time() - $entry['last_failed']) . '</font></td></tr>
    <tr><td colspan="2" class="header">Device</td></tr>
    <tr><td><b>Device</b></font></td><td>' . generate_device_link($device) . '</font></td></tr>
    <tr><td><b>Hardware</b></font></td><td>' . $device['hardware'] . '</font></td></tr>
    <tr><td><b>Operating System</b></font></td><td>' . $device['os_text'] . ' ' . $device['version'] . ' ' . $device['features'] . '</font></td></tr>
    <tr><td><b>Location</b></font></td><td>' . htmlspecialchars($device['location']) . '</font></td></tr>
    <tr><td><b>Uptime</b></font></td><td>' . deviceUptime($device) . '</font></td></tr>
  </tbody></table>
</td></tr>
<tr><td>
<center>' . $graphs . '</center></td></tr>
</tbody></table>
</body>
</html>';
            alert_notify($device, "RECOVER: [" . $device['hostname'] . "] [" . $alert['entity_type'] . "] [" . $entity['entity_name'] . "] " . $alert['alert_message'], $message);
            $update_array['last_recovered'] = time();
            $update_array['has_alerted'] = 0;
            dbUpdate($update_array, 'alert_table-state', '`alert_table_id` = ?', array($entry['alert_table_id']));
        }
        if ($entry['alert_status'] == '0') {
            echo 'Alert tripped. ';
            // Has this been alerted more frequently than the alert interval in the config?
            /// FIXME -- this should be configurable per-entity or per-checker
            if (time() - $entry['last_alerted'] < $config['alerts']['interval'] && !isset($GLOBALS['spam'])) {
                $entry['suppress_alert'] = TRUE;
            }
            // Check if alert has ignore_until set.
            if (is_numeric($entry['ignore_until']) && $entry['ignore_until'] > time()) {
                $entry['suppress_alert'] = TRUE;
            }
            if ($entry['suppress_alert'] != TRUE) {
                echo 'Requires notification. ';
                $alert = $alert_rules[$entry['alert_test_id']];
                $state = json_decode($entry['state'], TRUE);
                $conditions = json_decode($alert['conditions'], TRUE);
                $entity = get_entity_by_id_cache($entry['entity_type'], $entry['entity_id']);
                $condition_text = "";
                foreach ($state['failed'] as $failed) {
                    $condition_text .= $failed['metric'] . " " . $failed['condition'] . " " . $failed['value'] . " (" . $state['metrics'][$failed['metric']] . ")<br />";
                }
                $graphs = "";
                $metric_text = "";
                foreach ($state['metrics'] as $metric => $value) {
                    $metric_text .= $metric . " = " . $value . PHP_EOL . "<br />";
                }
                if (is_array($config['entities'][$entry['entity_type']]['graph'])) {
                    // We can draw a graph for this type/metric pair!
                    $graph_array = $config['entities'][$entry['entity_type']]['graph'];
                    foreach ($graph_array as $key => $val) {
                        // Check to see if we need to do any substitution
                        if (substr($val, 0, 1) == "@") {
                            $nval = substr($val, 1);
                            echo " replaced " . $val . " with " . $entity[$nval] . " from entity. " . PHP_EOL . "<br />";
                            $graph_array[$key] = $entity[$nval];
                        }
                    }
                    print_r($graph_array);
                    $image_data_uri = generate_alert_graph($graph_array);
                    print_r(strlen($image_data_uri));
                    $graphs .= '<img src="' . $image_data_uri . '">' . "<br />";
                    unset($graph_array);
                }
                #$css = data_uri($config['html_dir'].'/css/bootstrap-mini.css' ,'text/css');
                $message = '
<head>
    <title>Observium Alert</title>
<style>
.observium{ width:100%; max-width: 500px; -webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px; border:1px solid #DDDDDD; background-color:#FAFAFA;
 font-size: 13px; color: #777777; }
.header{ font-weight: bold; font-size: 16px; padding: 5px; color: #555555; }
.red { color: #cc0000; }
#deviceinfo tr:nth-child(odd) { background: #ffffff; }
</style>
<style type="text/css"></style></head>
<body>
<table class="observium">
  <tbody>
    <tr>
      <td>
        <table class="observium" id="deviceinfo">
  <tbody>
    <tr><td class="header">ALERT</td><td><a style="float: right;" href="' . generate_url(array('page' => 'device', 'device' => $device['device_id'], 'tab' => 'alert', 'alert_entry' => $entry['alert_table_id'])) . '">Modify</a></td></tr>
    <tr><td><b>Alert</b></font></td><td class="red">' . $alert['alert_message'] . '</font></td></tr>
    <tr><td><b>Entity</b></font></td><td>' . generate_entity_link($entry['entity_type'], $entry['entity_id'], $entity['entity_name']) . '</font></td></tr>';
                if (strlen($entity['entity_descr']) > 0) {
                    $message .= '<tr><td><b>Descr</b></font></td><td>' . $entity['entity_descr'] . '</font>';
                }
                $message .= '
    <tr><td><b>Conditions</b></font></td><td>' . $condition_text . '</font></td></tr>
    <tr><td><b>Metrics</b></font></td><td>' . $metric_text . '</font></td></tr>
    <tr><td><b>Duration</b></font></td><td>' . formatUptime(time() - $entry['last_failed']) . '</font></td></tr>
    <tr><td colspan="2" class="header">Device</td></tr>
    <tr><td><b>Device</b></font></td><td>' . generate_device_link($device) . '</font></td></tr>
    <tr><td><b>Hardware</b></font></td><td>' . $device['hardware'] . '</font></td></tr>
    <tr><td><b>Operating System</b></font></td><td>' . $device['os_text'] . ' ' . $device['version'] . ' ' . $device['features'] . '</font></td></tr>
    <tr><td><b>Location</b></font></td><td>' . htmlspecialchars($device['location']) . '</font></td></tr>
    <tr><td><b>Uptime</b></font></td><td>' . deviceUptime($device) . '</font></td></tr>
  </tbody></table>
</td></tr>
<tr><td>
<center>' . $graphs . '</center></td></tr>
</tbody></table>
</body>
</html>';
                alert_notify($device, "ALERT: [" . $device['hostname'] . "] [" . $alert['entity_type'] . "] [" . $entity['entity_name'] . "] " . $alert['alert_message'], $message);
                $update_array['last_alerted'] = time();
                $update_array['has_alerted'] = 1;
                dbUpdate($update_array, 'alert_table-state', '`alert_table_id` = ?', array($entry['alert_table_id']));
            } else {
                echo "No notification required. " . (time() - $entry['last_alerted']);
            }
        } elseif ($entry['alert_status'] == '1') {
            echo "Status: OK. ";
        } else {
            echo "Unknown status.";
        }
        echo PHP_EOL;
    }
}
function process_alerts($device)
{
    global $config, $alert_rules, $alert_assoc;
    echo "处理警报 " . $device['hostname'] . PHP_EOL;
    $alert_table = cache_device_alert_table($device['device_id']);
    $sql = "SELECT * FROM `alert_table`";
    $sql .= " LEFT JOIN `alert_table-state` ON `alert_table`.`alert_table_id` = `alert_table-state`.`alert_table_id`";
    $sql .= " WHERE `device_id` = ? AND `alert_status` IS NOT NULL;";
    foreach (dbFetchRows($sql, array($device['device_id'])) as $entry) {
        echo 'Alert: ' . $entry['alert_table_id'] . ' Status: ' . $entry['alert_status'] . ' ';
        // If the alerter is now OK and has previously alerted, send an recovery notice.
        if ($entry['alert_status'] == '1' && $entry['has_alerted'] == '1') {
            $alert = $alert_rules[$entry['alert_test_id']];
            if (!$alert['suppress_recovery']) {
                $state = json_decode($entry['state'], TRUE);
                $conditions = json_decode($alert['conditions'], TRUE);
                $entity = get_entity_by_id_cache($entry['entity_type'], $entry['entity_id']);
                $metric_array = array();
                foreach ($state['metrics'] as $metric => $value) {
                    $metric_array[] = $metric . ' = ' . $value;
                }
                $message_tags = array('ALERT_STATE' => 'RECOVERY', 'ALERT_URL' => generate_url(array('page' => 'device', 'device' => $device['device_id'], 'tab' => 'alert', 'alert_entry' => $entry['alert_table_id'])), 'ALERT_MESSAGE' => $alert['alert_message'], 'METRICS' => implode(PHP_EOL . '             ', $metric_array), 'DURATION' => formatUptime(time() - $entry['last_failed']), 'ENTITY_LINK' => generate_entity_link($entry['entity_type'], $entry['entity_id'], $entity['entity_name']), 'ENTITY_NAME' => $entity['entity_name'], 'ENTITY_DESCRIPTION' => $entity['entity_descr'], 'DEVICE_HOSTNAME' => $device['hostname'], 'DEVICE_LINK' => generate_device_link($device), 'DEVICE_HARDWARE' => $device['hardware'], 'DEVICE_OS' => $device['os_text'] . ' ' . $device['version'] . ' ' . $device['features'], 'DEVICE_LOCATION' => $device['location'], 'DEVICE_UPTIME' => deviceUptime($device));
                $message['text'] = simple_template('alert/email_text.tpl', $message_tags, array('is_file' => TRUE));
                //$message_tags['CONDITIONS'] = nl2br($message_tags['CONDITIONS']);
                $message_tags['METRICS'] = nl2br($message_tags['METRICS']);
                $message['html'] = simple_template('alert/email_html.tpl', $message_tags, array('is_file' => TRUE));
                //logfile('debug.log', var_export($message, TRUE));
                alert_notify($device, alert_generate_subject('RECOVER', $device, $alert, $entity), $message, $entry['alert_test_id']);
                log_alert('恢复发送的通知', $device, $entry, 'RECOVER_NOTIFY');
            } else {
                echo '已限制恢复.';
                log_alert('限制恢复通知', $device, $entry, 'RECOVER_SUPPRESSED');
            }
            $update_array['last_recovered'] = time();
            $update_array['has_alerted'] = 0;
            dbUpdate($update_array, 'alert_table-state', '`alert_table_id` = ?', array($entry['alert_table_id']));
        }
        if ($entry['alert_status'] == '0') {
            echo '警报已停止. ';
            // Has this been alerted more frequently than the alert interval in the config?
            /// FIXME -- this should be configurable per-entity or per-checker
            if (time() - $entry['last_alerted'] < $config['alerts']['interval'] && !isset($GLOBALS['spam'])) {
                $entry['suppress_alert'] = TRUE;
            }
            // Check if alert has ignore_until set.
            if (is_numeric($entry['ignore_until']) && $entry['ignore_until'] > time()) {
                $entry['suppress_alert'] = TRUE;
            }
            // Check if alert has ignore_until_ok set.
            if (is_numeric($entry['ignore_until_ok']) && $entry['ignore_until_ok'] == '1') {
                $entry['suppress_alert'] = TRUE;
            }
            if ($entry['suppress_alert'] != TRUE) {
                echo 'Requires notification. ';
                $alert = $alert_rules[$entry['alert_test_id']];
                $state = json_decode($entry['state'], TRUE);
                $conditions = json_decode($alert['conditions'], TRUE);
                $entity = get_entity_by_id_cache($entry['entity_type'], $entry['entity_id']);
                $condition_array = array();
                foreach ($state['failed'] as $failed) {
                    $condition_array[] = $failed['metric'] . " " . $failed['condition'] . " " . $failed['value'] . " (" . $state['metrics'][$failed['metric']] . ")";
                }
                $graphs = "";
                $metric_array = array();
                foreach ($state['metrics'] as $metric => $value) {
                    $metric_array[] = $metric . ' = ' . $value;
                }
                if (is_array($config['entities'][$entry['entity_type']]['graph'])) {
                    // We can draw a graph for this type/metric pair!
                    $graph_array = $config['entities'][$entry['entity_type']]['graph'];
                    foreach ($graph_array as $key => $val) {
                        // Check to see if we need to do any substitution
                        if (substr($val, 0, 1) == "@") {
                            $nval = substr($val, 1);
                            echo " replaced " . $val . " with " . $entity[$nval] . " from entity. " . PHP_EOL . "<br />";
                            $graph_array[$key] = $entity[$nval];
                        }
                    }
                    //print_r($graph_array);
                    //logfile('debug.log', var_export($graph_array, TRUE));
                    $image_data_uri = generate_alert_graph($graph_array);
                    //print_r($image_data_uri);
                    //logfile('debug.log', var_export($image_data_uri, TRUE));
                    $graphs .= '<img src="' . $image_data_uri . '"><br />';
                    unset($graph_array);
                }
                $message_tags = array('ALERT_STATE' => 'ALERT', 'ALERT_URL' => generate_url(array('page' => 'device', 'device' => $device['device_id'], 'tab' => 'alert', 'alert_entry' => $entry['alert_table_id'])), 'ALERT_MESSAGE' => $alert['alert_message'], 'CONDITIONS' => implode(PHP_EOL . '             ', $condition_array), 'METRICS' => implode(PHP_EOL . '             ', $metric_array), 'DURATION' => formatUptime(time() - $entry['last_failed']), 'ENTITY_LINK' => generate_entity_link($entry['entity_type'], $entry['entity_id'], $entity['entity_name']), 'ENTITY_NAME' => $entity['entity_name'], 'ENTITY_DESCRIPTION' => $entity['entity_descr'], 'ENTITY_GRAPHS' => $graphs, 'DEVICE_HOSTNAME' => $device['hostname'], 'DEVICE_LINK' => generate_device_link($device), 'DEVICE_HARDWARE' => $device['hardware'], 'DEVICE_OS' => $device['os_text'] . ' ' . $device['version'] . ' ' . $device['features'], 'DEVICE_LOCATION' => $device['location'], 'DEVICE_UPTIME' => deviceUptime($device));
                $message['text'] = simple_template('alert/email_text.tpl', $message_tags, array('is_file' => TRUE));
                $message_tags['CONDITIONS'] = nl2br($message_tags['CONDITIONS']);
                $message_tags['METRICS'] = nl2br($message_tags['METRICS']);
                $message['html'] = simple_template('alert/email_html.tpl', $message_tags, array('is_file' => TRUE));
                //logfile('debug.log', var_export($message, TRUE));
                alert_notify($device, alert_generate_subject('ALERT', $device, $alert, $entity), $message, $entry['alert_test_id']);
                log_alert('Alert notification sent', $device, $entry, 'ALERT_NOTIFY');
                $update_array['last_alerted'] = time();
                $update_array['has_alerted'] = 1;
                dbUpdate($update_array, 'alert_table-state', '`alert_table_id` = ?', array($entry['alert_table_id']));
            } else {
                echo "没有通知要求. " . (time() - $entry['last_alerted']);
            }
        } else {
            if ($entry['alert_status'] == '1') {
                echo "状态: OK. ";
            } else {
                if ($entry['alert_status'] == '2') {
                    echo "Status: Notification Delayed. ";
                } else {
                    if ($entry['alert_status'] == '3') {
                        echo "Status: Notification Suppressed. ";
                    } else {
                        echo "未知的状态.";
                    }
                }
            }
        }
        echo PHP_EOL;
    }
}
        }
        $emails = parse_email($email);
        if ($emails) {
            $rcpts_full = array();
            foreach ($emails as $to => $to_name) {
                $rcpts_full[] = empty($to_name) ? $to : '"' . trim($to_name) . '" <' . $to . '>';
            }
            $rcpts_full = implode(', ', $rcpts_full);
            print_message("尝试发送测试通知 %W" . $device['hostname'] . "%n 到这些邮箱:\n  %g{$rcpts_full}%n", 'color', FALSE);
            // Create multipart (plain+html) message
            $template_html = '<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
  </head>
  <body>
    <tt>{{MESSAGE}}</tt><br />
  </body>
</html>';
            // Use template here just for testing
            $message_multipart = array('text' => simple_template('{{MESSAGE}}', array('MESSAGE' => $message)), 'html' => simple_template($template_html, array('MESSAGE' => $message)));
            $status = alert_notify($device, "TEST: [" . $device['hostname'] . "]", $message_multipart, $alert_id);
        } else {
            // no one recipient
            print_message("%r有一些收件人%n 为找到 %W" . $device['hostname'] . "%n.\n  所有设备上设置SNMP设备或默认收件人: \$config['email']['default']", 'color');
        }
        print_message("通知 " . $device['hostname'] . ($status ? ' %gSENT' : ' %rNOT sent') . "%n.", 'color');
    }
} else {
    print_message("使用方法:\n{$scriptname} -h device [-d debug]\n\nSYNTAX:\n-h <设备id> | <设备名称的通配符>  发送测试电子邮件管理员所述设备.\n-d                                           Enable debug mode.\n -dd                                         More verbose debugging output.\n\n%r无效的参数!%n", 'color');
}
// EOF
Exemple #4
0
<div class='homepage-content-header'>Welcome to the Theamus Installer</div>
<div class='homepage-content-tag'>
    <?php 
$mod_rewrite_enabled = false;
if (function_exists('apache_get_modules')) {
    if (in_array('mod_rewrite', apache_get_modules())) {
        $mod_rewrite_enabled = true;
    }
}
if ($mod_rewrite_enabled == false) {
    alert_notify('info', 'The Apache module <strong>mod_rewrite</strong> must be enabled for Theamus to work.');
} else {
    echo '<button type="button" id="next-step" class="btn btn-primary">Get Started <span class="glyphicon ion-arrow-right-c"></span></button>';
}
?>
</div>

<script type='text/javascript'>
    $(function() {
        $('#next-step').click(function() {
            window.location = Theamus.base_url+'/install/dependencies/';
        });
    });
</script>