为PHP应用环境接入听云应用性能监控实践

软件环境

apache 2.4
php 7.2 NTS
Ubuntu 18.04 x64

安装探针

下载探针

登陆听云官方网站:https://www.tingyun.com
1.选择听云server服务
图片1
2.点击右上角下载图标按钮
图片2
3.根据你的环境选择对应文件版本下载
这里,我的软件环境是Ubuntu 18.04 x64、php 7.2的,所以我选择了图示的版本
图片3
命令方式下载

1
wget http://download.tingyun.com/agent/php/2.10.0/tingyun-agent-php-2.10.0.x86_64.deb

安装探针

1
2
3
4
##安装
dpkg -i tingyun-agent-php-2.10.0.x86_64.deb
##清理
rm -rf tingyun-agent-php-2.10.0.x86_64.deb

配置探针

查看php扩展目录

1
php -r "echo ini_get('extension_dir');"

查看PHP API版本

1
php -i|grep "PHP API"|awk '{print $4}'                      

查看Thread Safety

1
php -i|grep "Thread Safety"|awk '{print $4}'                      

安装扩展

上面安装探针路径为/usr/lib/networkbench,所以根据上面的信息做软链

1
2
ls -al /usr/lib/networkbench/agent/x64/
ln -s /usr/lib/networkbench/agent/x64/networkbench-20170718.so /usr/local/lib/php/extensions/no-debug-non-zts-20170718/networkbench.so

设置配置

在此之前我们需要取得授权码,通过新建应用方式获得
图片4
如图所示位置复制字串备用
图片5
在过程中可以下载需要的探针,因为上面我们安装好了所以这里不需要下载了,然后点击下一步
图片6
这一步我们不需要做什么操作,因为我们使用了上面的安装方式
图片7
到这一步,我们去设置配置文件并启动服务,等待这里自动跳转即可
图片8
以下是配置文件内容:
nbs.license_key项值就是上面复制备用的那一长串的字串。其它根据你的情况自定义,如nbs.app_name可自定义为应用名称等

; start networkbench ini

extension=networkbench.so

[networkbench]

;
; Setting: nbs.agent_enabled
; Type   : boolean
; Scope  : per-directory
; Default: true
; Info   : Enable or disable the agent. Please note that you cannot globally
;          disable the agent and then selectively enable it on a per-directory
;          basis. If you disable the agent in the global INI file then the
;          agent will not initialize at all. However, you can selectively
;          disable the agent on a per-directory basis.
; 通过该选项启用或禁用探针
; 请注意您不能在当前配置文件内禁用探针,否则探针会无法启动
; 当您在同一台服务器上部署多个应用,并且想将禁用采集某一应用的数据时,您需要修改APACHE或php-fpm或NGINX的配置文件。
; Apache 方式:修改httpd.conf 或 .htaccess, 通过 php_flag nbs.agent_enabled off 来设置
; php-fpm方式:修改php-fpm.conf, 通过 php_flag[nbs.agent_enabled] = off 来设置
; nginx方式:修改nginx.conf, 通过 fastcgi_param PHP_VALUE "nbs.agent_enabled=false" 来设置
;
nbs.agent_enabled = true

;
; Setting: nbs.license_key
; Type   : string
; Scope  : per-directory
; Default: none
; Info   : Sets the Networkbench license key to use. This can vary from directory
;          to directory if you are running a multi-tenant system.
; 与您的听云账号关联。
; 探针采集到得数据,会上传到该 license key绑定的账号下。
;
nbs.license_key = "dbb0000000000000000000000000059ec"


;
; Setting: nbs.auto_app_naming
; Type   : integer
; Scope  : system
; Default: 0
; 0 - Disable automatic naming
; 1 - Enable automatic naming, rule: Use virtual server name and port of the host as application name
; 2 - Enable automatic naming, rule: Use nbs.app_name and port of the host as application name
;
; 应用自动命名
; 0 - 禁用自动命名,默认禁用
; 1 - 启动自动命名,命名规则:使用虚拟主机的域名+端口作为应用名称
; 2 - 启动自动命名,命名规则:使用nbs.app_name+端口作为应用名称
; 当您在同一台服务器上部署多个应用,并且使用了虚拟主机 <VirtualHost> 时,建议打开此选项
; 当<VirtualHost> 中配置了ServerName,建议使用 nbs.auto_app_naming = 1
; 如果<VirtualHost> 中没有配置ServerName,建议使用 nbs.auto_app_naming = 2,否则由于未校验域名,可能会产生一些非预期之内的应用
;
nbs.auto_app_naming = 0

;
; Setting: nbs.app_name
; Type   : string
; Scope  : per-directory
; Default: "PHP Application"
; Info   : Sets the name of the application that metrics will be reported into.
;          This can in fact be a list , each of which must be separated by a semi-colon.
;          The first name in any such list is considered the 'primary' application name
;          and must be unique for each account / license key.
; 应用名称
; 当您在同一台服务器上部署多个应用,并且想将不同的应用区分开时,您需要修改APACHE或php-fpm或NGINX的配置文件来区分不同的应用
; Apache 方式:修改httpd.conf 或 .htaccess, 通过 php_value nbs.app_name "my app name" 来设置
; php-fpm方式:修改php-fpm.conf, 通过 php_value[nbs.app_name] = "my app name" 来设置
; nginx方式:修改nginx.conf, 通过 fastcgi_param PHP_VALUE "nbs.app_name=my app name"; 来设置
;
nbs.app_name="app"

;
; Setting: nbs.auto_action_naming
; Type   : boolean
; Scope  : system
; Default: true
; true - Enable action automatic naming, rule: use router name of framework or php script file name as action name
; false - Disable action automatic naming, rule: use URI as action name
;
; 事务自动命名
; true - 启用事务自动命名,默认启用,命名规则:优先使用框架的路由名称,否则使用php脚本名称
; false - 禁用事务自动命名,命名规则: 使用请求的URI作为事务名称
;
nbs.auto_action_naming = true

;
; Setting: nbs.prefix
; Type   : string
; Scope  : system
; Default: "/usr"
; Info   : Sets the path of the agent libs install.
; 探针安装路径, 例如 /home/usr
; 汇总进程位置为 %nbs.prefix%/bin/networkbench
; php扩展so、安装脚本、SSL证书路径等路径为 %nbs.prefix%/lib/agent/
;
nbs.prefix="/usr"

;
; Setting: nbs.runtime_root
; Type   : string
; Scope  : system
; Default: "/var"
; Info   : Sets the path of the agent runtime path.
; 探针运行路径, 例如 /home/usr/var
; 日志路径为 %nbs.runtime_root%/log/networkbench/
; pid文件路径为 %nbs.runtime_root%/run/networkbench/
;
nbs.runtime_root="/var"

;
; Setting: nbs.agent_log_level
; Type   : string
; Scope  : system
; Level  : off,critical,error,warning,info,verbose,debug
; Default: "info"
; Info   : Sets the level of detail to include in the log file. You should
;          rarely need to change this from the default, and usually only under
;          the guidance of technical support.
; PHP扩展日志记录级别
;
nbs.agent_log_level = "info"

;
; The daemon automatically started by the agent,
; there is no need to start the daemon before starting Apache or PHP-FPM.
; The follow settings are options that control the behavior of the daemon.
; These settings are copyed into the \tmp\.networkbench.cfg when the agent
; starts the daemon.
;
; The agent will only launch the daemon if one isn't already running. Also
; note that the agent will NOT stop the daemon once it has started.

; networkbench进程是介于听云服务器和PHP之间的一个汇总进程
; PHP扩展本身不与听云服务器进行数据交换
; PHP扩展通过将数据发送到networkbench进程进行处理来减少对系统资源的消耗
; 汇总进程会被PHP自动启动,不需要用户手动启动

;
; Setting: nbs.daemon_log_level
; Type   : string
; Scope  : system
; Level  : off,critical,error,warning,info,verbose,debug
; Default: "info"
; Info   : Sets the level of detail to include in the daemon log. You should
;          rarely need to change this from the default, and usually only under
;          the guidance of technical support.
; 汇总进程日志级别
;
nbs.daemon_log_level = "info"

;
; Setting: nbs.audit_mode
; Type   : boolean
; Scope  : system
; Default: false
; info   : Sets whether or not to record all uncompressed, un-encoded
;          content that is sent from your machine to the networkbench servers
;          into the daemon log file. This allows you to satisfy yourself that
;          the agent is not sending any sensitive data to our servers.
; 是否启用数据审计模式
; 启用数据审计模式后,所有从听云服务器接收到的和提交到听云服务器的原始数据都会被记录到汇总进程日志中
;
nbs.audit_mode = false

;
; Setting: nbs.action_tracer.log_sql
; Type   : boolean
; Scope  : system
; Default: false
; info   : If set to true, only record the sql statement to log file rather than
;          report data to networkbench data collectors
; 汇总进程日志中打印SQL
; 设置为true时,过程跟踪时SQL语句的记录只写到汇总进程日志中,不提交到听云服务器上
;
nbs.action_tracer.log_sql = false

;
; Setting: nbs.ssl
; Type   : boolean
; Scope  : system
; Default: true
; Info   : Sets whether or not communication with networkbench data collectors
;          should use a secure HTTP connection or not.
; 使用HTTPS协议传输数据
;
nbs.ssl = true

;
; Setting: nbs.proxy_host
; Type   : string
; Scope  : system
; Default: none
; Info   : Sets the host use as an egress proxy.
;          This is only used if your site requires a proxy in order to access
;          external servers on the internet, in this case the networkbench data
;          collection servers.
; 代理服务器主机
; 如果需要通过代理服务器连接,在此设置代理服务器主机
;
nbs.proxy_host =

;
; Setting: nbs.proxy_port
; Type   : string
; Scope  : system
; Default: none
; Info   : Sets the port to use as an egress proxy.
;          This is only used if your site requires a proxy in order to access
;          external servers on the internet, in this case the networkbench data
;          collection servers.
; 代理服务器端口
; 如果需要通过代理服务器连接,在此设置代理服务器端口
;
nbs.proxy_port =

;
; Setting: nbs.proxy_user
; Type   : string
; Scope  : system
; Default: none
; Info   : Sets the user credentials to use as an egress proxy.
;          This is only used if your site requires a proxy in order to access
;          external servers on the internet, in this case the networkbench data
;          collection servers.
; 代理服务器用户名
; 如果需要通过代理服务器连接,在此设置代理服务器用户名
;
nbs.proxy_user =

;
; Setting: nbs.proxy_host
; Type   : string
; Scope  : system
; Default: none
; Info   : Sets the credentials to use as an egress proxy.
;          This is only used if your site requires a proxy in order to access
;          external servers on the internet, in this case the networkbench data
;          collection servers.
; 代理服务器密码
; 如果需要通过代理服务器连接,在此设置代理服务器密码
;
nbs.proxy_password =

; Setting: nbs.cli
; Type   : boolean
; Scope  : system
; Default: false
; Info   : enable or disable profiler in the CLI mode
;          false - disable
;          true  - enable
; cli模式下是否启用监控
; 启用后,在不使用听云api的情况下,探针将每个cli进程处理为一个事务数据
; 如果进程长时间不退出,记录的性能数据比较多,可能导致cli内存持续增长
;
nbs.cli = false

; networkbench ini end

我们将配置文件放置在php配置中去

1
2
3
4
5
6
##新建或者编辑
vi networkbench.ini
##复制到php配置,这里根据你的php配置进行更改,也可直接放在php.ini中
cp networkbench.ini /usr/local/etc/php/conf.d/networkbench.ini
##创建日志文件
mkdir -p /var/log/networkbench && touch /var/log/networkbench/daemon.log

服务启用

重启PHP宿主服务器(比如apache, php-fpm等)

结束

此后,当您的php应用有http请求进入时,在几分钟后性能数据将发送到听云Server中。
接下来你就可以在听云控制面板查看应用的性能数据了。
图片9