function update_device_alert_table($device)
{
    $dbc = array();
    $alert_table = array();
    $msg = "<h4>Building alerts for device " . $device['hostname'] . '</h4>';
    $msg_class = '';
    $conditions = cache_device_conditions($device);
    //foreach ($conditions['cond'] as $test_id => $test)
    //{
    //  #print_vars($test);
    //  #echo('<span class="label label-info">Matched '.$test['alert_name'].'</span> ');
    //}
    $db_cs = dbFetchRows("SELECT * FROM `alert_table` WHERE `device_id` = ?", array($device['device_id']));
    foreach ($db_cs as $db_c) {
        $dbc[$db_c['entity_type']][$db_c['entity_id']][$db_c['alert_test_id']] = $db_c;
    }
    $msg .= '<br /><h5>Checkers matching this device:</h5> ';
    foreach ($conditions['cond'] as $alert_test_id => $alert_test) {
        $msg .= '<span class="label label-info">' . $alert_test['alert_name'] . '</span> ';
        foreach ($alert_test['assoc'] as $assoc_id => $assoc) {
            // Check that the entity_type matches the one we're interested in.
            // echo("Matching $assoc_id (".$assoc['entity_type'].")");
            list($entity_table, $entity_id_field, $entity_name_field) = entity_type_translate($assoc['entity_type']);
            $alert = $conditions['cond'][$assoc['alert_test_id']];
            $entities = match_device_entities($device['device_id'], $assoc['attributes'], $assoc['entity_type']);
            foreach ($entities as $id => $entity) {
                $alert_table[$assoc['entity_type']][$entity[$entity_id_field]][$assoc['alert_test_id']][] = $assoc_id;
            }
            // echo(count($entities)." matched".PHP_EOL);
            $msg .= '<br />';
        }
    }
    $msg .= '<h5>Matching entities:</h5>';
    foreach ($alert_table as $entity_type => $entities) {
        foreach ($entities as $entity_id => $entity) {
            $entity_name = entity_name($entity_type, $entity_id);
            $msg .= '<span class="label label-ok">' . $entity_name . '</span> ';
            foreach ($entity as $alert_test_id => $b) {
                #        echo(str_pad($entity_type, "20").str_pad($entity_id, "20").str_pad($alert_test_id, "20"));
                #        echo(str_pad(implode($b,","), "20"));
                $msg .= '<span class="label label-info">' . $conditions['cond'][$alert_test_id]['alert_name'] . '</span><br >';
                $msg_class = 'success';
                if (isset($dbc[$entity_type][$entity_id][$alert_test_id])) {
                    if ($dbc[$entity_type][$entity_id][$alert_test_id]['alert_assocs'] != implode($b, ",")) {
                        $update_array = array('alert_assocs' => implode($b, ","));
                    }
                    #echo("[".$dbc[$entity_type][$entity_id][$alert_test_id]['alert_assocs']."][".implode($b,",")."]");
                    if (is_array($update_array)) {
                        dbUpdate($update_array, 'alert_table', '`alert_table_id` = ?', array($dbc[$entity_type][$entity_id][$alert_test_id]['alert_table_id']));
                        unset($update_array);
                    }
                    unset($dbc[$entity_type][$entity_id][$alert_test_id]);
                } else {
                    $alert_table_id = dbInsert(array('device_id' => $device['device_id'], 'entity_type' => $entity_type, 'entity_id' => $entity_id, 'alert_test_id' => $alert_test_id, 'alert_assocs' => implode($b, ",")), 'alert_table');
                    dbInsert(array('alert_table_id' => $alert_table_id), 'alert_table-state');
                }
            }
        }
    }
    $msg .= "Checking for stale entries: ";
    foreach ($dbc as $type => $entity) {
        foreach ($entity as $entity_id => $alert) {
            foreach ($alert as $alert_test_id => $data) {
                dbDelete('alert_table', "`alert_table_id` =  ?", array($data['alert_table_id']));
                dbDelete('alert_table-state', "`alert_table_id` =  ?", array($data['alert_table_id']));
                $msg .= "-";
            }
        }
    }
    print_message($msg, $msg_class);
}
/**
 * Display alert_table entries.
 *
 * @param array $vars
 * @return none
 *
 */
function print_alert_table($vars)
{
    global $alert_rules;
    global $config;
    // This should be set outside, but do it here if it isn't
    if (!is_array($alert_rules)) {
        $alert_rules = cache_alert_rules();
    }
    /// WARN HERE
    if (isset($vars['device']) && !isset($vars['device_id'])) {
        $vars['device_id'] = $vars['device'];
    }
    if (isset($vars['entity']) && !isset($vars['entity_id'])) {
        $vars['entity_id'] = $vars['entity'];
    }
    // Short? (no pagination, small out)
    $short = isset($vars['short']) && $vars['short'];
    list($query, $param, $query_count) = build_alert_table_query($vars);
    // Fetch alerts
    $count = dbFetchCell($query_count, $param);
    $alerts = dbFetchRows($query, $param);
    // Set which columns we're going to show.
    // We hide the columns that have been given as search options via $vars
    $list = array('device_id' => FALSE, 'entity_id' => FALSE, 'entity_type' => FALSE, 'alert_test_id' => FALSE);
    foreach ($list as $argument => $nope) {
        if (!isset($vars[$argument]) || empty($vars[$argument]) || $vars[$argument] == "all") {
            $list[$argument] = TRUE;
        }
    }
    // Hide device if we know entity_id
    if (isset($vars['entity_id'])) {
        $list['device_id'] = FALSE;
    }
    // Hide entity_type if we know the alert_test_id
    if (isset($vars['alert_test_id']) || TRUE) {
        $list['entity_type'] = FALSE;
    }
    // Hide entity types in favour of icons to save space
    if ($vars['pagination'] && !$short) {
        $pagination_html = pagination($vars, $count);
        echo $pagination_html;
    }
    echo '<table class="table table-condensed table-bordered table-striped table-rounded table-hover">
  <thead>
    <tr>
      <th class="state-marker"></th>
      <th style="width: 1px;"></th>';
    // No table id
    //<th style="width: 5%;">Id</th>');
    if ($list['device_id']) {
        echo '      <th style="width: 15%">设备</th>';
    }
    if ($list['alert_test_id']) {
        echo '      <th style="min-width: 15%;">警报</th>';
    }
    if ($list['entity_type']) {
        echo '      <th style="width: 10%">类型</th>';
    }
    if ($list['entity_id']) {
        echo '      <th style="">实体</th>';
    }
    echo '
      <th style="width: 20px">状态</th>
      <th style="width: 100px;">信息</th>
      <th style="width: 90px;">已检测</th>
      <th style="width: 90px;">已更改</th>
      <th style="width: 90px;">警告</th>
      <th style="width: 20px;"></th>
    </tr>
  </thead>
  <tbody>' . PHP_EOL;
    foreach ($alerts as $alert) {
        // Process the alert entry, generating colours and classes from the data
        humanize_alert_entry($alert);
        // Get the entity array using the cache
        $entity = get_entity_by_id_cache($alert['entity_type'], $alert['entity_id']);
        // Get the device array using the cache
        $device = device_by_id_cache($alert['device_id']);
        // Get the entity_name.
        ### FIXME - This is probably duplicated effort from above. We should pass it $entity
        $entity_name = entity_name($alert['entity_type'], $entity);
        // Set the alert_rule from the prebuilt cache array
        $alert_rule = $alert_rules[$alert['alert_test_id']];
        echo '<tr class="' . $alert['html_row_class'] . '" style="cursor: pointer;" onclick="location.href=\'' . generate_url(array('page' => 'device', 'device' => $device['device_id'], 'tab' => 'alert', 'alert_entry' => $alert['alert_table_id'])) . '\'">';
        echo '<td class="state-marker"></td>';
        echo '<td style="width: 1px;"></td>';
        // If we know the device, don't show the device
        if ($list['device_id']) {
            echo '<td><span class="entity-title">' . generate_device_link($device) . '</span></td>';
        }
        // Print link to the alert rule page
        if ($list['alert_test_id']) {
            echo '<td><a href="', generate_url(array('page' => 'alert_check', 'alert_test_id' => $alert_rule['alert_test_id'])), '">', $alert_rule['alert_name'], '</a></td>';
        }
        // If we're showing all entity types, print the entity type here
        if ($list['entity_type']) {
            echo '<td>' . nicecase($alert['entity_type']) . '</td>';
        }
        // Print a link to the entity
        if ($list['entity_id']) {
            echo '<td><span class="entity-title"><i class="' . $config['entities'][$alert['entity_type']]['icon'] . '"></i> ' . generate_entity_link($alert['entity_type'], $alert['entity_id']) . '</span></td>';
        }
        echo '<td>';
        ## FIXME -- generate a nice popup with parsed information from the state array
        echo overlib_link("", '<i class="icon-info-sign"></i>', "<pre>" . print_r(json_decode($alert['state'], TRUE), TRUE) . "</pre>", NULL);
        echo '</td>';
        echo '<td class="' . $alert['class'] . '">' . $alert['last_message'] . '</td>';
        echo '<td>' . overlib_link('', $alert['checked'], format_unixtime($alert['last_checked'], 'r'), NULL) . '</td>';
        echo '<td>' . overlib_link('', $alert['changed'], format_unixtime($alert['last_changed'], 'r'), NULL) . '</td>';
        echo '<td>' . overlib_link('', $alert['alerted'], format_unixtime($alert['last_alerted'], 'r'), NULL) . '</td>';
        echo '<td><a href="' . generate_url(array('page' => 'device', 'device' => $device['device_id'], 'tab' => 'alert', 'alert_entry' => $alert['alert_table_id'])) . '"><i class="oicon-gear" /></a></td>';
        echo '</tr>';
    }
    echo '  </tbody>' . PHP_EOL;
    echo '</table>' . PHP_EOL;
    if ($vars['pagination'] && !$short) {
        echo $pagination_html;
    }
}
示例#3
0
/**
 * Display alert_table entries.
 *
 * @param array $vars
 * @return none
 *
 */
function print_alert_table($vars)
{
    global $alert_rules;
    global $config;
    // This should be set outside, but do it here if it isn't
    if (!is_array($alert_rules)) {
        $alert_rules = cache_alert_rules();
    }
    /// WARN HERE
    if (isset($vars['device']) && !isset($vars['device_id'])) {
        $vars['device_id'] = $vars['device'];
    }
    if (isset($vars['entity']) && !isset($vars['entity_id'])) {
        $vars['entity_id'] = $vars['entity'];
    }
    // Short? (no pagination, small out)
    $short = isset($vars['short']) && $vars['short'];
    list($query, $param, $query_count) = build_alert_table_query($vars);
    // Fetch alerts
    $count = dbFetchCell($query_count, $param);
    $alerts = dbFetchRows($query, $param);
    // Set which columns we're going to show.
    // We hide the columns that have been given as search options via $vars
    $list = array('device_id' => FALSE, 'entity_id' => FALSE, 'entity_type' => FALSE, 'alert_test_id' => FALSE);
    foreach ($list as $argument => $nope) {
        if (!isset($vars[$argument]) || empty($vars[$argument]) || $vars[$argument] == "all") {
            $list[$argument] = TRUE;
        }
    }
    if ($vars['format'] != "condensed") {
        $list['checked'] = TRUE;
        $list['changed'] = TRUE;
        $list['alerted'] = TRUE;
    }
    if ($vars['short'] == TRUE) {
        $list['checked'] = FALSE;
        $list['alerted'] = FALSE;
    }
    // Hide device if we know entity_id
    if (isset($vars['entity_id'])) {
        $list['device_id'] = FALSE;
    }
    // Hide entity_type if we know the alert_test_id
    if (isset($vars['alert_test_id']) || TRUE) {
        $list['entity_type'] = FALSE;
    }
    // Hide entity types in favour of icons to save space
    if ($vars['pagination'] && !$short) {
        $pagination_html = pagination($vars, $count);
        echo $pagination_html;
    }
    echo generate_box_open($vars['header']);
    echo '<table class="table table-condensed  table-striped  table-hover">';
    if ($vars['no_header'] == FALSE) {
        echo '
  <thead>
    <tr>
      <th class="state-marker"></th>
      <th style="width: 1px;"></th>';
        if ($list['device_id']) {
            echo '      <th style="width: 15%">Device</th>';
        }
        if ($list['entity_type']) {
            echo '      <th style="width: 10%">Type</th>';
        }
        if ($list['entity_id']) {
            echo '      <th style="">Entity</th>';
        }
        if ($list['alert_test_id']) {
            echo '      <th style="min-width: 15%;">Alert</th>';
        }
        echo '
      <th style="width: 100px;">Status</th>';
        if ($list['checked']) {
            echo '      <th style="width: 95px;">Checked</th>';
        }
        if ($list['changed']) {
            echo '      <th style="width: 95px;">Changed</th>';
        }
        if ($list['alerted']) {
            echo '      <th style="width: 95px;">Alerted</th>';
        }
        echo '    <th style="width: 45px;"></th>
    </tr>
  </thead>';
    }
    echo '<tbody>' . PHP_EOL;
    foreach ($alerts as $alert) {
        // Process the alert entry, generating colours and classes from the data
        humanize_alert_entry($alert);
        // Get the entity array using the cache
        $entity = get_entity_by_id_cache($alert['entity_type'], $alert['entity_id']);
        // Get the device array using the cache
        $device = device_by_id_cache($alert['device_id']);
        // Get the entity_name.
        ### FIXME - This is probably duplicated effort from above. We should pass it $entity
        $entity_name = entity_name($alert['entity_type'], $entity);
        // Set the alert_rule from the prebuilt cache array
        $alert_rule = $alert_rules[$alert['alert_test_id']];
        echo '<tr class="' . $alert['html_row_class'] . '" style="cursor: pointer;" onclick="openLink(\'' . generate_url(array('page' => 'device', 'device' => $device['device_id'], 'tab' => 'alert', 'alert_entry' => $alert['alert_table_id'])) . '\')">';
        echo '<td class="state-marker"></td>';
        echo '<td style="width: 1px;"></td>';
        // If we know the device, don't show the device
        if ($list['device_id']) {
            echo '<td><span class="entity-title">' . generate_device_link($device) . '</span></td>';
        }
        // If we're showing all entity types, print the entity type here
        if ($list['entity_type']) {
            echo '<td>' . nicecase($alert['entity_type']) . '</td>';
        }
        // Print a link to the entity
        if ($list['entity_id']) {
            echo '<td><span class="entity-title"><i class="' . $config['entities'][$alert['entity_type']]['icon'] . '"></i> ' . generate_entity_link($alert['entity_type'], $alert['entity_id']) . '</span></td>';
        }
        // Print link to the alert rule page
        if ($list['alert_test_id']) {
            echo '<td class="entity"><a href="', generate_url(array('page' => 'alert_check', 'alert_test_id' => $alert_rule['alert_test_id'])), '">', escape_html($alert_rule['alert_name']), '</a></td>';
        }
        echo '<td>';
        echo '<span class="label label-' . ($alert['html_row_class'] != 'up' ? $alert['html_row_class'] : 'success') . '">' . generate_tooltip_link('', $alert['status'], '<div class="small" style="max-width: 500px;"><strong>' . $alert['last_message'] . '</strong></div>', $alert['alert_class']) . '</span>';
        echo '</td>';
        // echo('<td class="'.$alert['class'].'">'.$alert['last_message'].'</td>');
        if ($list['checked']) {
            echo '<td>' . generate_tooltip_link('', $alert['checked'], format_unixtime($alert['last_checked'], 'r')) . '</td>';
        }
        if ($list['changed']) {
            echo '<td>' . generate_tooltip_link('', $alert['changed'], format_unixtime($alert['last_changed'], 'r')) . '</td>';
        }
        if ($list['alerted']) {
            echo '<td>' . generate_tooltip_link('', $alert['alerted'], format_unixtime($alert['last_alerted'], 'r')) . '</td>';
        }
        echo '<td>';
        // This stuff should go in an external entity popup in the future.
        $state = json_decode($alert['state'], true);
        $alert['state_popup'] = '';
        if (count($state['failed'])) {
            $alert['state_popup'] .= generate_box_open(array('title' => 'Failed Tests'));
            //'<h4>Failed Tests</h4>';
            $alert['state_popup'] .= '<table style="min-width: 400px;" class="table   table-striped table-condensed">';
            $alert['state_popup'] .= '<thead><tr><th>Metric</th><th>Cond</th><th>Value</th><th>Measured</th></tr></thead>';
            foreach ($state['failed'] as $test) {
                $alert['state_popup'] .= '<tr><td><strong>' . $test['metric'] . '</strong></td><td>' . $test['condition'] . '</td><td>' . $test['value'] . '</td><td><i class="red">' . $state['metrics'][$test['metric']] . '</i></td></tr>';
            }
            $alert['state_popup'] .= '</table>';
            $alert['state_popup'] .= generate_box_close();
        }
        $alert['state_popup'] .= generate_entity_popup_graphs($alert, array('entity_type' => 'alert_entry'));
        // Print (i) icon with popup of state.
        echo overlib_link("", '<i class="icon-info-sign text-primary"></i>', $alert['state_popup'], NULL);
        echo '&nbsp;&nbsp;<a href="' . generate_url(array('page' => 'device', 'device' => $device['device_id'], 'tab' => 'alert', 'alert_entry' => $alert['alert_table_id'])) . '"><i class="icon-cog text-muted"></i></a>';
        echo '</td>';
        echo '</tr>';
    }
    echo '  </tbody>' . PHP_EOL;
    echo '</table>' . PHP_EOL;
    echo generate_box_close();
    if ($vars['pagination'] && !$short) {
        echo $pagination_html;
    }
}
示例#4
0
            $sensors_db['measured'][$entry['measured_class']][$entry['measured_entity']][] = $entry;
        } else {
            $sensors_db[$sensor_type][$entry['sensor_id']] = $entry;
        }
    }
}
//r($sensors_db['measured']);
// Now print founded bundle (measured_class+sensor)
if (isset($sensors_db['measured'])) {
    foreach ($sensors_db['measured'] as $measured_class => $measured_entity) {
        $box_args = array('title' => nicecase($measured_class) . ' sensors', 'icon' => 'oicon-node');
        echo generate_box_open($box_args);
        echo ' <table class="table table-condensed table-striped">';
        foreach ($measured_entity as $entity_id => $entry) {
            $entity = get_entity_by_id_cache($measured_class, $entity_id);
            $entity_name = entity_name($measured_class, $entity);
            $entity_link = generate_entity_link($measured_class, $entity);
            $entity_type = entity_type_translate_array($measured_class);
            //echo('      <tr class="'.$port['row_class'].'">
            //  <td class="state-marker"></td>
            echo '      <tr>
        <td colspan="6" class="entity"><i class="' . $entity_type['icon'] . '"></i> ' . $entity_link . '</td></tr>';
            foreach ($entry as $sensor) {
                // Remove port name from sensor description
                $sensor['sensor_descr'] = trim(str_ireplace($entity_name, '', $sensor['sensor_descr']));
                if (empty($sensor['sensor_descr'])) {
                    // Some time sensor descriptions equals to entity name
                    $sensor['sensor_descr'] = nicecase($sensor['sensor_class']);
                }
                print_sensor_row($sensor, $vars);
            }
function update_device_group_table($device)
{
    $groups_table_db = array();
    $group_table = array();
    $msg = '<h4 style="">构建组成员资格 ' . generate_device_link($device) . ':</h4>';
    $msg_class = '';
    $msg_enable = FALSE;
    $groups = cache_device_groups($device);
    $g_t_dbs = dbFetchRows("SELECT * FROM `group_table` WHERE `device_id` = ?", array($device['device_id']));
    foreach ($g_t_dbs as $g_t_db) {
        $groups_table_db[$g_t_db['entity_type']][$g_t_db['entity_id']][$g_t_db['group_id']] = $g_t_db;
    }
    $msg .= PHP_EOL;
    $msg .= '  <h4 style="">匹配这个设备组:</h4> ';
    foreach ($groups['group'] as $group_id => $group_test) {
        $msg .= '<span class="label label-info">' . $group_test['group_name'] . '</span> ';
        $msg_enable = TRUE;
        foreach ($group_test['assoc'] as $assoc_id => $assoc) {
            // Check that the entity_type matches the one we're interested in.
            // echo("匹配 $assoc_id (".$assoc['entity_type'].")");
            list($entity_table, $entity_id_field, $entity_name_field) = entity_type_translate($assoc['entity_type']);
            $group = $groups['group'][$assoc['group_id']];
            $entities = match_device_entities($device['device_id'], $assoc['entity_attribs'], $assoc['entity_type']);
            foreach ($entities as $id => $entity) {
                $group_table[$assoc['entity_type']][$entity[$entity_id_field]][$assoc['group_id']][] = $assoc_id;
            }
            // echo(count($entities)." matched".PHP_EOL);
        }
    }
    $msg .= PHP_EOL;
    $msg .= '  <h4>匹配实体:</h4> ';
    foreach ($group_table as $entity_type => $entities) {
        foreach ($entities as $entity_id => $entity) {
            $entity_name = entity_name($entity_type, $entity_id);
            $msg .= '<span class="label label-ok">' . $entity_name . '</span> ';
            $msg_enable = TRUE;
            foreach ($entity as $group_id => $b) {
                #        echo(str_pad($entity_type, "20").str_pad($entity_id, "20").str_pad($group_id, "20"));
                #        echo(str_pad(implode($b,","), "20"));
                $msg .= '<span class="label label-info">' . $groups['group'][$group_id]['group_name'] . '</span><br >';
                $msg_class = 'success';
                if (isset($groups_table_db[$entity_type][$entity_id][$group_id])) {
                    if ($groups_table_db[$entity_type][$entity_id][$group_id]['group_assocs'] != implode($b, ",")) {
                        $update_array = array('group_assocs' => implode($b, ","));
                    }
                    #echo("[".$groups_table_db[$entity_type][$entity_id][$group_id]['group_assocs']."][".implode($b,",")."]");
                    if (is_array($update_array)) {
                        dbUpdate($update_array, 'group_table', '`group_table_id` = ?', array($groups_table_db[$entity_type][$entity_id][$group_id]['group_table_id']));
                        unset($update_array);
                    }
                    unset($groups_table_db[$entity_type][$entity_id][$group_id]);
                } else {
                    $group_table_id = dbInsert(array('device_id' => $device['device_id'], 'entity_type' => $entity_type, 'entity_id' => $entity_id, 'group_id' => $group_id, 'group_assocs' => implode($b, ",")), 'group_table');
                }
            }
        }
    }
    $msg .= PHP_EOL;
    $msg .= "  <h4>检测过时的内容:</h4> ";
    foreach ($groups_table_db as $type => $entity) {
        foreach ($entity as $entity_id => $alert) {
            foreach ($alert as $group_id => $data) {
                dbDelete('group_table', "`group_table_id` =  ?", array($data['group_table_id']));
                //dbDelete('group_table-state', "`group_table_id` =  ?", array($data['group_table_id']));
                $msg .= "-";
                $msg_enable = TRUE;
            }
        }
    }
    if ($msg_enable) {
        print_message($msg, $msg_class);
    }
}
   }
   if ($list['entity_id']) {
       echo '      <th style="">实体</th>';
   }
   echo '
   </tr>
 </thead>
 <tbody>' . PHP_EOL;
   foreach ($members as $member) {
       // Get the entity array using the cache
       $entity = get_entity_by_id_cache($member['entity_type'], $member['entity_id']);
       // Get the device array using the cache
       $device = device_by_id_cache($member['device_id']);
       // Get the entity_name.
       ### FIXME - This is probably duplicated effort from above. We should pass it $entity
       $entity_name = entity_name($member['entity_type'], $entity);
       echo '<tr class="' . $entity['html_row_class'] . '" style="cursor: pointer;">';
       echo '<td style="width: 1px; background-color: ' . $entity['table_tab_colour'] . '; margin: 0px; padding: 0px"></td>';
       echo '<td style="width: 1px;"></td>';
       // If we know the device, don't show the device
       if ($list['device_id']) {
           echo '<td><span class="entity-title">' . generate_device_link($device) . '</span></td>';
       }
       // If we're showing all entity types, print the entity type here
       if ($list['entity_type']) {
           echo '<td>' . nicecase($member['entity_type']) . '</td>';
       }
       // PRint a link to the entity
       if ($list['entity_id']) {
           echo '<td><span class="entity-title">' . generate_entity_link($member['entity_type'], $member['entity_id']) . '</span></td>';
       }