验证 Cloudkitty 功能

Cloudkitty 主要分为 processor 和 api 俩个服务。processor 负责对项目资源进行批价入库等操作;api 对外提供各种接口,主要是计费规则设置和价格查询。

因此,我们只需要验证这两个服务都能正常工作即可。

验证API

可以通过设置价格并查询资源价格来进行验证。

设置价格

这里我们对volume进行价格设置来验证。

  1. 创建Group
[[email protected] ~]# cloudkitty hashmap-group-create -n volumes
+----------+--------------------------------------+
| Property | Value                                |
+----------+--------------------------------------+
| group_id | 71afeb7f-4ebb-49e7-b538-9da8077c96aa |
| name     | volumes                              |
+----------+--------------------------------------+
  1. 创建Service
[[email protected] ~]# cloudkitty hashmap-service-create -n volume
+------------+--------------------------------------+
| Property   | Value                                |
+------------+--------------------------------------+
| name       | volume                               |
| service_id | e7550989-3cc6-4efb-8008-2357d0c63634 |
+------------+--------------------------------------+
  1. 创建Field
[[email protected] ~]# cloudkitty hashmap-field-create -n size -s e7550989-3cc6-4efb-8008-2357d0c63634
+------------+--------------------------------------+
| Property   | Value                                |
+------------+--------------------------------------+
| field_id   | 58673556-5f05-4e5e-9a6a-e7bd4bc50ba1 |
| name       | size                                 |
| service_id | e7550989-3cc6-4efb-8008-2357d0c63634 |
+------------+--------------------------------------+
  1. 创建Threshold
[[email protected] ~]# cloudkitty hashmap-threshold-create -l 0 -t step -f 58673556-5f05-4e5e-9a6a-e7bd4bc50ba1 -c 1.2 -g 71afeb7f-4ebb-49e7-b538-9da8077c96aa
+--------------+--------------------------------------+
| Property     | Value                                |
+--------------+--------------------------------------+
| cost         | 1.20000000                           |
| field_id     | 58673556-5f05-4e5e-9a6a-e7bd4bc50ba1 |
| group_id     | 71afeb7f-4ebb-49e7-b538-9da8077c96aa |
| level        | 0E-8                                 |
| service_id   | None                                 |
| tenant_id    | None                                 |
| threshold_id | 31d0777c-4497-47ca-9e54-bf5079dfdb98 |
| type         | step                                 |
+--------------+--------------------------------------+

如果各命令返回如上示例中的结果,则表明功能正常。

查询价格

由于cloudkitty cli并没有提供quote命令,所以这里我们可以通过cURL来进行验证。

[[email protected] ~]# curl POST -H "X-Auth-Token:$USER_TOKEN" -H "Content-type: application/json" -d '{"resources": [{"volume": 250, "service": "volume", "desc": {"size": 250}}]}' http://172.159.3.7:8889/v1/rating/quote |python -mjson.tool 
"300.0000000000000000"

如果正常返回价格(例子中是"300.0000000000000000"),则表明功能正常。

验证 Processor

经过上一步api的验证,我们已经设置了volume的价格规则。这里只需要验证服务能否正确记录项目价格数据即可。

  1. 创建项目与volume资源

计费是针对项目中的资源来说的,因此我们创建一个项目XXX,并在改项目下创建一个volume资源,这里我们可以创建一个1G大小的云硬盘。

  1. 项目配置

要想cloudkitty能够对项目计费,必须将cloudkitty用户加入到需要计费的项目中并赋予rating角色。

openstack role add --project XXX --user cloudkitty rating

cloudkitty 的初始化配置可参看官方文档 https://docs.openstack.org/cloudkitty/pike/configuration/

  1. cloudkitty.conf配置

默认的计费周期为1小时,为了能尽快验证,需要修改配置,将计费周期缩短。

[[email protected] ~]# cat /etc/cloudkitty/cloudkitty.conf 
[collect]
period = 600
wait_periods = 1
services = compute,volume,router,volume.snapshot,network.floating,network.lb.listener

例子中,我们将修改了period = 600,wait_periods = 1两个配置。将计费周期缩短到10分钟。

  1. 验证数据

等待一个周期后,即可到数据库中查看是否生成资源的价格数据,为了保证处理完成,我们可以等待20分钟(按本例子中的配置,即两个计费周期)。


MariaDB [cloudkitty]> SELECT * FROM cloudkitty.rated_data_frames where res_type!="_NO_DATA_" limit 3;
+------+---------------------+---------------------+------+---------+----------+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------+
| id   | begin               | end                 | unit | qty     | res_type | rate       | desc                                                                                                                                                                                                                                                 | tenant_id                        |
+------+---------------------+---------------------+------+---------+----------+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------+
| 3383 | 2017-10-11 08:00:00 | 2017-10-11 08:10:00 | GB   | 1.00000 | volume   | 1.20000000 | {"user_id": "8a78fef188dd406684f68804763a141a", "name": "xuanll_volume_001", "availability_zone": "nova", "volume_type": "ceph", "volume_id": "f2bf80c2-2cc5-4a7b-9b54-e75de0a647b0", "project_id": "5650d6967a08444895a0d0105c711a43", "size": "1"} | 5650d6967a08444895a0d0105c711a43 |
| 3384 | 2017-10-11 08:10:00 | 2017-10-11 08:20:00 | GB   | 1.00000 | volume   | 1.20000000 | {"user_id": "8a78fef188dd406684f68804763a141a", "name": "xuanll_volume_001", "availability_zone": "nova", "volume_type": "ceph", "volume_id": "f2bf80c2-2cc5-4a7b-9b54-e75de0a647b0", "project_id": "5650d6967a08444895a0d0105c711a43", "size": "1"} | 5650d6967a08444895a0d0105c711a43 |
| 3385 | 2017-10-11 08:20:00 | 2017-10-11 08:30:00 | GB   | 1.00000 | volume   | 1.20000000 | {"user_id": "8a78fef188dd406684f68804763a141a", "name": "xuanll_volume_001", "availability_zone": "nova", "volume_type": "ceph", "volume_id": "f2bf80c2-2cc5-4a7b-9b54-e75de0a647b0", "project_id": "5650d6967a08444895a0d0105c711a43", "size": "1"} | 5650d6967a08444895a0d0105c711a43 |
+------+---------------------+---------------------+------+---------+----------+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------+
3 rows in set (0.01 sec)

如果能够查询到当前的资源价格数据,则表明功能正常。