当前位置: 首页 > 工具软件 > WeatherMap > 使用案例 >

自定义weatherMap数据源之Mysql

高英彦
2023-12-01
WeatherMap  使用mysql当数据源
把WeatherMapDataSource_mysql.php文件放入lib/datasources文件夹下

打开WeatherMapDataSource_mysql.php文修改mysql相关配置

        $map->add_hint("mysql_host", "127.0.0.1");
        $map->add_hint("mysql_user","user");
        $map->add_hint("mysql_password","pwd");
        $map->add_hint("mysql_port","3306");

查询语句使用的是下面的SQL

select $field_in,$field_out from $table_name where host='$filter' order by id desc LIMIT 1"

所以,要求数据表包含下面两个字段(自己可以修改源码,改为自己的关键字)
`id` int(11) NOT NULL AUTO_INCREMENT,
`host` varchar(255) NOT NULL,



数据格式:mysql:databasename:table:in:out:filter
in 入带宽字段
out 出带宽字段
filter 主机名称(精确匹配)

例如mysql:test:bw:bw_in:bw_out:172.16.1.1

数据库名 test

数据表名 bw

入带宽字段 bw_in

出带宽字段 bw_out

查询的主机名 172.16.1.1


我的配置文件如下

configs/mysql.conf
FONTDEFINE 100 docs/example/Vera.ttf 10

WIDTH 1400
HEIGHT 1000
HTMLSTYLE overlib
KEYFONT 100
TITLE beijing
TIMEPOS 676 17 Created: %b %d %Y %H:%M:%S

KEYPOS DEFAULT 73 100 Traffic Load
KEYTEXTCOLOR 0 0 0
KEYOUTLINECOLOR 0 0 0
KEYBGCOLOR 255 255 255
BGCOLOR 255 255 255
TITLECOLOR 0 0 0
TIMECOLOR 0 0 0
SCALE DEFAULT 0    0    192 192 192
SCALE DEFAULT 0    1    255 255 255
SCALE DEFAULT 1    10   140   0 255
SCALE DEFAULT 10   25    32  32 255
SCALE DEFAULT 25   40     0 192 255
SCALE DEFAULT 40   55     0 240   0
SCALE DEFAULT 55   70   240 240   0
SCALE DEFAULT 70   85   255 192   0
SCALE DEFAULT 85   100  255   0   0

SET nowarn_clipping 1

# End of global section


# TEMPLATE-only NODEs:
NODE DEFAULT
        MAXVALUE 100


# TEMPLATE-only LINKs:
LINK DEFAULT
        WIDTH 4
        BWLABEL bits
        BANDWIDTH 200M


# regular NODEs:
NODE node00352fish
        LABEL Node
        ICON images/router_up.gif
        POSITION 427 118

NODE node08813
        LABEL Node
        ICON images/router_up.gif
        POSITION 392 265


# regular LINKs:
LINK node00352fish-node08813
        INFOURL /images/router_up.gif
        OVERLIBGRAPH /images/router_up.gif
        TARGET mysql:test:traffic:bw_in:bw_out:172.16.1.97
        NODES node00352fish node08813



# That's All Folks!
执行下面命令生成html或png
./weathermap  --config configs/mysql.conf  --htmloutput mysql.html 
./weathermap  --config configs/mysql.conf  --output mysql.png

插件WeatherMapDataSource_mysql.php代码如下

php版本要求5.4

<?php
// Sample Pluggable datasource for PHP Weathermap 0.9
// - read a pair of values from a database, and return it

// TARGET mysql:databasename:table:in:out:filter

class WeatherMapDataSource_mysql extends WeatherMapDataSource {

    function Init(&$map)
    {
        if(! function_exists("mysql_real_escape_string") ) return FALSE;
        if(! function_exists("mysql_connect") ) return FALSE;

        $map->add_hint("mysql_host", "127.0.0.1");
        $map->add_hint("mysql_user","user");
        $map->add_hint("mysql_password","pwd");
        $map->add_hint("mysql_port","3306");

        return(TRUE);
    }

    function Recognise($targetstring)
    {
        if(preg_match("/^mysql:([^:]+):([^:]+):([^:]+):([^:]+):([^:]+)$/",$targetstring,$matches))
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }

    function ReadData($targetstring, &$map, &$item)
    {
        $data[IN] = NULL;
        $data[OUT] = NULL;
        $data_time = 0;

        if(preg_match("/^mysql:([^:]+):([^:]+):([^:]+):([^:]+):([^:]+)$/",$targetstring,$matches))
        {
            $database_user = $map->get_hint('mysql_user');
            $database_pass = $map->get_hint('mysql_password');
            $database_host = $map->get_hint('mysql_host');
            $database_port = $map->get_hint('mysql_port');

            $database_name = $matches[1];
            $table_name = $matches[2];
            $field_in = $matches[3];
            $field_out = $matches[4];
            $filter = $matches[5];

            wm_warn($database_name . " " . $table_name . " " . $field_in . " " . $field_out . " " . $filter);

            $SQL = "select $field_in,$field_out from $table_name where host='$filter' order by id desc LIMIT 1";
            if(mysql_connect($database_host. ":". $database_port,$database_user,$database_pass))
            {
                if(mysql_select_db($database_name))
                {
                    $result = mysql_query($SQL);
                    if (!$result)
                    {
                        wm_warn("mysql ReadData: Invalid query: " . mysql_error()."\n");
                    }
                    else
                    {
                        $row = mysql_fetch_assoc($result);
                        $data[IN] = $row[$field_in];
                        $data[OUT] = $row[$field_out];
                    }
                }
                else
                {
                    wm_warn("mysql ReadData: failed to select database: ".mysql_error()."\n");
                }
            }
            else
            {
                wm_warn("mysql ReadData: failed to connect to database server: ".mysql_error()."\n");
            }

            $data_time = time();
        }


        wm_debug ("mysql ReadData: Returning (".($data[IN]===NULL?'NULL':$data[IN]).",".($data[OUT]===NULL?'NULL':$data[IN]).",$data_time)\n");

        return( array($data[IN], $data[OUT], $data_time) );
    }
}

// vim:ts=4:sw=4:

 类似资料: