zabbix_管理_队列数据读取过程

时间 : 19-01-03 栏目 : linux运维 作者 : 老薛 评论 : 0 点击 : 248 次


系统环境

系统版本:CentOS release 6.7 (Final)

内核版本:2.6.32-573.el6.x86_64

软件版本:zabbix-3.4.1

一、           Zabbix web端构造请求

/*query.php文件*/

$queueData =
$zabbixServer->getQueue($queueRequests[$config],
get_cookie('zbx_sessionid'), QUEUE_DETAIL_ITEM_COUNT);

/*getQueue函数*/

include/classes/server/CZabbixServer.php

二、           Zabbix_web发送请求

/*request函数所在文件*/

frontends/php/include/classes/server/CZabbixServer.php

return $this->request($request);

    $request
= [

                        'request' =>
'queue.get',

                        'sid' => $sid,

                        'type' => $type

                ];

    /*queue.get 宏定义 所在文件*/

include/zbxjson.h:#define ZBX_PROTO_VALUE_GET_QUEUE             "queue.get"

三、           Zabbix_server接收请求

/*src/zabbix_server/trapper/trapper.c*/

else if (0 == strcmp(value, ZBX_PROTO_VALUE_GET_QUEUE))

                        {

                                if (0 !=
(program_type & ZBX_PROGRAM_TYPE_SERVER))

                                        ret =
recv_getqueue(sock, &jp);

                        }

/******************************************************************************

 *                                                                           
*

 *
Function: recv_getqueue                                                   
*

 *                                                                           
*

 *
Purpose: process queue request                                             *

 *                                                                           
*

 *
Parameters:  sock  - [IN] the request socket                               *

 *             
jp    - [IN] the request data                                 *

 *                                                                           
*

 *
Return value:  SUCCEED - processed
successfully                            *

 *                FAIL - an error occurred                                    *

 *                                                                           
*

 ******************************************************************************/

static int      recv_getqueue(zbx_socket_t *sock, struct
zbx_json_parse *jp)

{

       
const char             
*__function_name = "recv_getqueue";

       
int                     ret =
FAIL, request_type = ZBX_GET_QUEUE_UNKNOWN, now, i, limit;

       
char                   
type[MAX_STRING_LEN], sessionid[MAX_STRING_LEN],
limit_str[MAX_STRING_LEN];

       
zbx_user_t              user;

       
zbx_vector_ptr_t        queue;

       
struct zbx_json         json;

       
zbx_hashset_t           queue_stats;

       
zbx_queue_stats_t       *stats;

       
zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);

       
if (FAIL == zbx_json_value_by_name(jp, ZBX_PROTO_TAG_SID, sessionid,
sizeof(sessionid)) ||

                        SUCCEED != DBget_user_by_active_session(sessionid,
&user) || USER_TYPE_SUPER_ADMIN > user.type)

       
{

                zbx_send_response_raw(sock,
ret, "Permission denied.", CONFIG_TIMEOUT);

                goto out;

       
}

       
if (FAIL != zbx_json_value_by_name(jp, ZBX_PROTO_TAG_TYPE, type,
sizeof(type)))

       
{

                if (0 == strcmp(type,
ZBX_PROTO_VALUE_GET_QUEUE_OVERVIEW))

                {

                        request_type =
ZBX_GET_QUEUE_OVERVIEW;

                }

                else if (0 == strcmp(type,
ZBX_PROTO_VALUE_GET_QUEUE_PROXY))

                {

                        request_type =
ZBX_GET_QUEUE_PROXY;

                }

                else if (0 == strcmp(type,
ZBX_PROTO_VALUE_GET_QUEUE_DETAILS))

                {

                        request_type =
ZBX_GET_QUEUE_DETAILS;

if (FAIL == zbx_json_value_by_name(jp,
ZBX_PROTO_TAG_LIMIT, limit_str, sizeof(limit_str)) ||

                                        FAIL ==
is_uint31(limit_str, &limit))

                        {

                               
zbx_send_response_raw(sock, ret, "Unsupported limit value.",
CONFIG_TIMEOUT);

                                goto out;

                        }

                }

       
}

       
if (ZBX_GET_QUEUE_UNKNOWN == request_type)

       
{

                zbx_send_response_raw(sock,
ret, "Unsupported request type.", CONFIG_TIMEOUT);

                goto out;

       
}

       
now = time(NULL);

       
zbx_vector_ptr_create(&queue);

       
/*Item
结果延期队列查询*/

       
DCget_item_queue(&queue,
ZBX_QUEUE_FROM_DEFAULT, ZBX_QUEUE_TO_INFINITY);

       
zbx_json_init(&json, ZBX_JSON_STAT_BUF_LEN);

       
switch (request_type)

       
{

                case ZBX_GET_QUEUE_OVERVIEW:

                       
zbx_hashset_create(&queue_stats, 32, ZBX_DEFAULT_UINT64_HASH_FUNC,

                                       
ZBX_DEFAULT_UINT64_COMPARE_FUNC);

                        /* gather queue stats
by item type */

                        for (i = 0; i <
queue.values_num; i++)

        
               {

                               
zbx_queue_item_t        *item =
queue.values[i];

                               
zbx_uint64_t            id =
item->type;

                                if (NULL ==
(stats = zbx_hashset_search(&queue_stats, &id)))

                                {

                                       
zbx_queue_stats_t       data =
{.id = id};

                                        stats =
zbx_hashset_insert(&queue_stats, &data, sizeof(data));

                                }

                               
queue_stats_update(stats, now - item->nextcheck);

                        }

                       
zbx_json_addstring(&json, ZBX_PROTO_TAG_RESPONSE,
ZBX_PROTO_VALUE_SUCCESS,

                                        ZBX_JSON_TYPE_STRING);

                       
queue_stats_export(&queue_stats, "itemtype", &json);

                       
zbx_hashset_destroy(&queue_stats);

                        break;

                case ZBX_GET_QUEUE_PROXY:

                        zbx_hashset_create(&queue_stats, 32,
ZBX_DEFAULT_UINT64_HASH_FUNC,

                                       
ZBX_DEFAULT_UINT64_COMPARE_FUNC);

                        /* gather queue stats
by proxy hostid */

                        for (i = 0; i <
queue.values_num; i++)

                        {

                               
zbx_queue_item_t        *item =
queue.values[i];

                               
zbx_uint64_t            id =
item->proxy_hostid;

                                if (NULL ==
(stats = zbx_hashset_search(&queue_stats, &id)))

                                {

                                       
zbx_queue_stats_t       data =
{.id = id};

                                        stats =
zbx_hashset_insert(&queue_stats, &data, sizeof(data));

                                }

                               
queue_stats_update(stats, now - item->nextcheck);

}

                       
zbx_json_addstring(&json, ZBX_PROTO_TAG_RESPONSE,
ZBX_PROTO_VALUE_SUCCESS,

                                        ZBX_JSON_TYPE_STRING);

                       
queue_stats_export(&queue_stats, "proxyid", &json);

                       
zbx_hashset_destroy(&queue_stats);

                        break;

                case ZBX_GET_QUEUE_DETAILS:

                        zbx_vector_ptr_sort(&queue,
(zbx_compare_func_t)queue_compare_by_nextcheck_asc);

                       
zbx_json_addstring(&json, ZBX_PROTO_TAG_RESPONSE,
ZBX_PROTO_VALUE_SUCCESS,

                                       
ZBX_JSON_TYPE_STRING);

                       
zbx_json_addarray(&json, ZBX_PROTO_TAG_DATA);

                        for (i = 0; i <
queue.values_num && i < limit; i++)

                        {

                               
zbx_queue_item_t        *item =
queue.values[i];

                               
zbx_json_addobject(&json, NULL);

                               
zbx_json_adduint64(&json, "itemid", item->itemid);

                                zbx_json_adduint64(&json,
"nextcheck", item->nextcheck);

                               
zbx_json_close(&json);

                        }

                       
zbx_json_close(&json);

                       
zbx_json_adduint64(&json, "total", queue.values_num);

                        break;

       
}

       
zabbix_log(LOG_LEVEL_DEBUG, "%s() json.buffer:'%s'",
__function_name, json.buffer);

       
(void)zbx_tcp_send_raw(sock, json.buffer);

DCfree_item_queue(&queue);

       
zbx_vector_ptr_destroy(&queue);

       
zbx_json_free(&json);

       
ret = SUCCEED;

out:

       
zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name,
zbx_result_string(ret));

       
return ret;

}

本文标签

除非注明,文章均为( 老薛 )原创,转载请保留链接: http://www.bdkyr.com/xtyw002/2730.html

zabbix_管理_队列数据读取过程:等您坐沙发呢!

发表评论

7 + 7 = ?


博主微信号,很高兴为您提供帮助

随便看看

为您推荐

0