Chinaunix首页 | 论坛 | 博客
  • 博客访问: 49593
  • 博文数量: 22
  • 博客积分: 941
  • 博客等级: 准尉
  • 技术积分: 242
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-27 13:20
文章分类
文章存档

2012年(3)

2011年(7)

2010年(12)

我的朋友

分类: 系统运维

2011-02-12 17:10:37

因为NetTeam面对大量的cacti图感到头疼,提了一些需求。早先提的需求是,将每台设备所有的流量图按端口流量大小来排序。方法蛮简单的。我在lib/html_tree.php中写了两个函数,traffic_sort和get_rra_max,前一个函数用于将$graphs_array数组元素按一定的规则进行排序后返回一个新数组,get_rra_max函数以graph_id为参数从rra中获取最大值。

function traffic_sort($graphs_array) {
    $graphid_title_array = array();
    $new_graphs_array = array();

    foreach ($graphs_array as $key=>$value) {
        $rra_max = get_rra_max($value['local_graph_id']);
        $gid = $value['local_graph_id'];
        $graphid_title_array["$gid"] = $value['title_cache'];
        $graphid_maxvalue_array["$gid"] = $rra_max;
        }
    arsort($graphid_maxvalue_array,SORT_NUMERIC);

    foreach ($graphid_maxvalue_array as $m=>$n){
        $title = $graphid_title_array["$m"];
        $t = array('title_cache'=>"$title",'local_graph_id'=>"$m");
        array_push($new_graphs_array,$t);
        }
    return $new_graphs_array;
    }

function get_rra_max($local_graph_id) {
    $sql = "SELECT
    rra_max_order.rra_value
    FROM
    data_template_data,graph_templates_item,data_template_rrd,rra_max_order
    WHERE
    data_template_data.local_data_id=data_template_rrd.local_data_id
    AND graph_templates_item.task_item_id=data_template_rrd.id
    AND rra_max_order.id=data_template_data.local_data_id
    AND graph_templates_item.local_graph_id="
.$local_graph_id." limit 1";

    $result = mysql_query($sql);
    $line = mysql_fetch_array($result, MYSQL_ASSOC);
    if (!$line['rra_value']){
        $line['rra_value'] = 0;
        }

        $rets = $line['rra_value'];
        return $rets;
    }


在html_tree.php文件中生成graphs数组后,对其使用的图形模板进行了一个判断,如果是使用流量模板,就使用traffic_sort函数对graphs数组元素进行重排序,而不是采用原先的usort函数进行排序。

if ($graph_template["id"] ==2) {

    $graphs = traffic_sort($graphs);

}

//usort($graphs, 'naturally_sort_graphs');


之后,需求变了,悲催的,需求方打了个比方,说比如想挑选出某个设备下所有端口中最大流量超过其最大能承受流量的50%的端口的图形。我只好前了个前端选择项,如下图:

这时可由Traffic Search对搜索条件进行设置,这里其实传递了两个参数作为变量,judge和traffic_perc。
html_tree.php中这么修改的:

if (sizeof($graph_templates) > 0) {
            foreach ($graph_templates as $graph_template) {
                if (strlen(get_request_var_request("filter"))) {
                    $sql_where = (empty($sql_where) ? "" : "AND (title_cache LIKE '%" . get_request_var_request("filter") . "%')");
                }

                if (strlen(get_request_var_request("traffic_perc")) && strlen(get_request_var_request("judge")) ) {
                    //echo "yes";
                    $traffic_perc = get_request_var_request("traffic_perc");
                    $judge = get_request_var_request("judge");

                // insert judge here

                $graphs_sql = "SELECT
                    distinct(graph_templates_graph.title_cache),graph_templates_graph.local_graph_id
                    FROM (graph_local,graph_templates_graph,data_template_data,graph_templates_item,data_template_rrd,rra_max_order)
                    $sql_join
                    WHERE
                    graph_local.id=graph_templates_graph.local_graph_id
                    AND graph_templates_graph.local_graph_id=graph_templates_item.local_graph_id
                    AND rra_max_order.id=data_template_data.local_data_id
                    AND graph_templates_item.task_item_id=data_template_rrd.id
                    AND data_template_data.local_data_id=data_template_rrd.local_data_id
                    AND graph_local.graph_template_id="
. $graph_template["id"] . "
                    AND graph_local.host_id="
. $leaf["host_id"] . "
                    AND rra_max_order.rra_value "
.$judge.$traffic_perc."
                    $sql_where
                    ORDER BY graph_templates_graph.title_cache"
;
                    //echo $graphs_sql;
                    }
                    else
                    {
                        $graphs_sql = "SELECT
                        graph_templates_graph.title_cache,
                        graph_templates_graph.local_graph_id
                        FROM (graph_local,graph_templates_graph)
                        $sql_join
                        WHERE graph_local.id=graph_templates_graph.local_graph_id
                        AND graph_local.graph_template_id="
. $graph_template["id"] . "
                        AND graph_local.host_id="
. $leaf["host_id"] . "
                        $sql_where
                        ORDER BY graph_templates_graph.title_cache"
;
                    }

                    $graphs = db_fetch_assoc($graphs_sql);


html代码如下。

<tr width='200'>
                     <td width=200>
                        <strong>&nbsp;Traffic Search:</strong>
                            <select name='judge'>
                            <option value=">=">>=</option>
                            <option value="<="><=</option>
                            </select>
                            <select name='traffic_perc'>
                            <option value="90">90%</option>
                            <option value="80">80%</option>
                            <option value="70">70%</option>
                            <option value="60">60%</option>
                            <option value="50">50%</option>
                            <option value="40">40%</option>
                            <option value="30">30%</option>
                            <option value="20">20%</option>
                            <option value="10">10%</option>
                            </select>
                    </td>
                    </tr>


阅读(812) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-03-06 17:45:16

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com