zabbix_web如何获取zabbix_server的运行状态(running/stop)

时间 : 19-02-25 栏目 : linux运维 作者 : 老薛 评论 : 4 点击 : 1,546 次

一、    停掉zabbix_server

/etc/init.d/zabbix_server
stop

二、    切换到zabbix_web
dashboard
页面

页面底部出现提示语如下:

Zabbix server is not running: the information displayed may
not be current

三、    ssh
zabbix_web
服务所在机器

3.1 grep 过滤上述提示所在的文件

[root@bj-server-001 www]# grep -Rn
"Zabbix server is not running: the information displayed may not be
current" * --color

locale/zh_CN/LC_MESSAGES/frontend.po:17410:msgid
"Zabbix server is not running: the information displayed may not be
current."

locale/zh_CN/LC_MESSAGES/frontend.po:17411:msgstr
"Zabbix server is not running: the information displayed may not be
current."

[root@bj-server-001 www]# vim
locale/zh_CN/LC_MESSAGES/frontend.po +17410

#: jsrpc.php:182

msgid
"Zabbix server is not running: the information displayed may not be
current."

msgstr
"Zabbix server is not running: the information displayed may not be
current."

3.2 过程分析

[root@bj-server-001 www]# vim
jsrpc.php +182

case 'zabbix.status':

                CSession::start();

                if (!CSession::keyExists('serverCheckResult')

                                ||
(CSession::getValue('serverCheckTime') + SERVER_CHECK_INTERVAL) <= time()) {

                        $zabbixServer = new CZabbixServer($ZBX_SERVER,
$ZBX_SERVER_PORT, ZBX_SOCKET_TIMEOUT, 0);

                       
CSession::setValue('serverCheckResult', $zabbixServer->isRunning(CWebUser::getSessionCookie()));

                       
CSession::setValue('serverCheckTime', time());

                }

                $result = [

                        'result' => (bool)
CSession::getValue('serverCheckResult'),

                        'message' =>
CSession::getValue('serverCheckResult')

                                ? ''

                                : _('Zabbix
server is not running: the information displayed may not be current.')

                ];

                break;

移动光标定位到CZabbixServer类,然后按下Ctrl+],跳转到CZabbixServer类所在的源文件,继续移动光标找到isRunning函数,如下所示

/**

         * Returns true if the Zabbix server is
running and false otherwise.

         *

         * @param $sid

         *

         * @return bool

         */

        public function isRunning($sid) {

                //构造response请求,

                $response = $this->request([

                        'request' => 'status.get',

                        'type' => 'ping',

                        'sid' => $sid

                ]);

               

                if ($response === false) {

                        return false;

                }

                $api_input_rules = ['type'
=> API_OBJECT, 'fields' => []];

                return
CApiInputValidator::validate($api_input_rules, $response, '/',
$this->error);

        }

//当前文件对request函数的定义在283行,直接在键盘上按下283,然后按gg即可定位到283行。

/**

         * Executes a given JSON request and
returns the result. Returns false if an error has occurred.

         *

         * @param array $params

         *

         * @return mixed    the output of the script if it has been
executed successfully or false otherwise

         */

        protected function request(array
$params) {

                // reset object state

                $this->error = null;

                $this->total = null;

                // connect to the server

                if (!$this->connect()) {

                        return false;

                }

                // set timeout

               
stream_set_timeout($this->socket, $this->timeout);

                // send the command

                if (fwrite($this->socket,
CJs::encodeJson($params)) === false) {

                        $this->error =
_s('Cannot send command, check connection with Zabbix server
"%1$s".', $this->host);

                        return false;

                }

                // read the response

                $readBytesLimit =
($this->totalBytesLimit && $this->totalBytesLimit <
$this->readBytesLimit)

                        ?
$this->totalBytesLimit

                        :
$this->readBytesLimit;

$response =
'';

                $now = time();

                $i = 0;

                while (!feof($this->socket))
{

                        $i++;

                        if ((time() - $now)
>= $this->timeout) {

                                $this->error
= _s('Connection timeout of %1$s seconds exceeded when connecting to Zabbix
server "%2$

s".',
$this->timeout, $this->host);

                                return false;

                        }

                        elseif
($this->totalBytesLimit && ($i * $readBytesLimit) >=
$this->totalBytesLimit) {

                                $this->error
= _s('Size of the response received from Zabbix server "%1$s" exceeds
the allowed size

of %2$s
bytes. This value can be increased in the ZBX_SOCKET_BYTES_LIMIT constant in
include/defines.inc.php.', $this->host, $this->

totalBytesLimit);

                                return false;

                        }

                        if (($out =
fread($this->socket, $readBytesLimit)) !== false) {

                                $response .=
$out;

                        }

                        else {

                                $this->error
= _s('Cannot read the response, check connection with the Zabbix server
"%1$s".', $this

->host);

                                return false;

                        }

                }

                fclose($this->socket);

// check if
the response is empty

                if (!strlen($response)) {

                        $this->error =
_s('Empty response received from Zabbix server "%1$s".', $this->host);

                        return false;

                }

                $response =
CJs::decodeJson($response);

                if (!$response ||
!$this->validateResponse($response)) {

                        $this->error =
_s('Incorrect response received from Zabbix server "%1$s".',
$this->host);

                        return false;

                }

                // request executed
successfully

                if ($response['response'] ==
self::RESPONSE_SUCCESS) {

                        // saves total count

                        $this->total =
array_key_exists('total', $response) ? $response['total'] : null;

                        return
$response['data'];

                }

                // an error on the server side
occurred

                else {

                        $this->error =
$response['info'];

                        return false;

                }

        }

大概过程就是通过socket,发送json请求字符串到zabbix_server

3.3 切换到zabbix_server源码所在目录

查找“status.get”关键字符串所在的文件

[root@bj-server-001 zabbix-3.4.1]# grep
-R "status.get" * --color

include/zbxjson.h:104:#define
ZBX_PROTO_VALUE_GET_STATUS               
"status.get"

说明:这个文件中的“status.get”字符串宏定义是我们需要的

[root@bj-server-001 zabbix-3.4.1]# vim
include/zbxjson.h +104

#define
ZBX_PROTO_VALUE_GET_STATUS             
"status.get"

以“ZBX_PROTO_VALUE_GET_STATUS”为过滤条件,继续查找所在文件

[root@bj-server-001 zabbix-3.4.1]# grep
-R "ZBX_PROTO_VALUE_GET_STATUS" * --color

src/zabbix_server/trapper/trapper.c:1033:                       else if (0 ==
strcmp(value, ZBX_PROTO_VALUE_GET_STATUS))

说明:关键字符的调用在src/zabbix_server/trapper/trapper.c这个文件中的1033行。

[root@bj-server-001 zabbix-3.4.1]# vim
src/zabbix_server/trapper/trapper.c +1033

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

{

   if (0 != (program_type &
ZBX_PROGRAM_TYPE_SERVER))

          ret = recv_getstatus(sock, &jp);

    }

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

{

#define
ZBX_GET_STATUS_UNKNOWN  -1

#define
ZBX_GET_STATUS_PING     0

#define ZBX_GET_STATUS_FULL     1

const
char              *__function_name =
"recv_getstatus";

zbx_user_t              user;

int   ret = FAIL, request_type =
ZBX_GET_STATUS_UNKNOWN;

char    type[MAX_STRING_LEN],
sessionid[MAX_STRING_LEN];

struct
zbx_json         json;

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

        if (SUCCEED != zbx_json_value_by_name(jp,
ZBX_PROTO_TAG_SID, sessionid, sizeof(sessionid)) ||

                        SUCCEED != DBget_user_by_active_session(sessionid,
&user))

        {

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

                goto out;

        }

if (SUCCEED
== zbx_json_value_by_name(jp,
ZBX_PROTO_TAG_TYPE, type, sizeof(type)))

{

  //zabbix web端的请求 'type' =>
'ping',

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

     {

         request_type = ZBX_GET_STATUS_PING;

  }

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

{

 request_type = ZBX_GET_STATUS_FULL;

                }

        }

        if (ZBX_GET_STATUS_UNKNOWN ==
request_type)

        {

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

                goto out;

        }

        zbx_json_init(&json,
ZBX_JSON_STAT_BUF_LEN);

switch (request_type)

{

  case ZBX_GET_STATUS_PING:

  //拼json{"response":"success","data":{}}

  zbx_json_addstring(&json, ZBX_PROTO_TAG_RESPONSE,
ZBX_PROTO_VALUE_SUCCESS, ZBX_JSON_TYPE_STRING);

  zbx_json_addobject(&json, ZBX_PROTO_TAG_DATA);

    zbx_json_close(&json);

    break;

                case ZBX_GET_STATUS_FULL:

                       
zbx_json_addstring(&json, ZBX_PROTO_TAG_RESPONSE,
ZBX_PROTO_VALUE_SUCCESS, ZBX_JSON_TYPE_STRING);

                       
zbx_json_addobject(&json, ZBX_PROTO_TAG_DATA);

                        status_stats_export(&json,
user.type);

                       
zbx_json_close(&json);

                        break;

                default:

                       
THIS_SHOULD_NEVER_HAPPEN;

        }

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

//发送拼接完毕的json字符串

//recv_getstatus()
json.buffer:'{"response":"success","data":{}}'

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

        zbx_json_free(&json);

        ret = SUCCEED;

out:

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

return ret;

#undef
ZBX_GET_STATUS_UNKNOWN

#undef
ZBX_GET_STATUS_PING

#undef
ZBX_GET_STATUS_FULL

}

整个数据流程到此结束,有不足之处,欢迎各位热心指正!

本文标签

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

zabbix_web如何获取zabbix_server的运行状态(running/stop):目前有4 条留言

  1. 4楼
    丘八
    Post: 2019-02-26 下午7:20

    文章不错,非常喜欢

  2. 地板
    阿明
    Post: 2019-04-08 上午12:50

    文章不错支持一下吧 :cool:

  3. 板凳
    156489
    Post: 2019-04-08 下午10:35

    文章不错非常喜欢,支持一下 :mrgreen:

  4. 沙发
    肖海涛
    Post: 2019-04-25 上午7:42

    看完之后,有点儿明白了。

发表评论

1 + 1 = ?


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

随便看看

为您推荐

4