利用PHP实现对服务器性能与状态的监控
最近由于工作上的原因,需要开发一套服务器性能的监控工具,主要是一些同架构同软件配置的服务器。考虑到最近用php比较多,随即决定使用php来实现。主要需要实现的部分功能如下: 1、系统状态,如cpu / 内存 / swap等数据; 2、MySQL的性能与运行状态; 其它
最近由于工作上的原因,需要开发一套服务器性能的监控工具,主要是一些同架构同软件配置的服务器。考虑到最近用php比较多,随即决定使用php来实现。主要需要实现的部分功能如下:
- 1、系统状态,如cpu / 内存 / swap等数据;
- 2、MySQL的性能与运行状态;
其它主要是配合性的数据存储以及可视化。
1、使用SNMP实现对系统状态的抓取
SNMP的确是一个很方便的协议,用它可以获取网络设备几乎所有的信息,Linux和Win都可以很好的支持。MIB是一个纷杂的数据库,通过筛选可以获取我们所需要的信息。
PHP5对snmp协议v2有较好的支持。为了方便,可以获取一大组数据,将其存储到一个数组中,再从数组中挑选需要的数据。snmp2_real_walk便可以满足这个需求。它的用法如下:
<p class="line">1</p> Copy after login Copy after login Copy after login Copy after login |
<p class="line">$status = @snmp2_real_walk($host, $community, ".1.3.6.1.4.1.2021",10,5);</p> Copy after login |
- $host是目标主机
- $community是其相应的SNMP community code
- “1.3.6.1.4.1.2021”是我们要获取的数据树
- 10是超时时间(秒)
- 5是失败后的重试次数
为了防止出错后报错,使用@强行关闭报错。
获取的结果存储在$status里,之后便可以从$status中定位我们所需要的数据了。MIB可能是用数字组织的,也有可能是一堆名字,具体可以在这里进行查询。需要注意,获取的结果中,值和结果是混合在一起的,需要对字符串进行一定的截取。下面是几个比较常用的值:
<p class="line">1</p> Copy after login |
<p class="line">$host_status['uptime_1min'] = (float)@substr($status['UCD-SNMP-MIB::laLoad.1'], 9);</p><p class="line">$host_status['uptime_5min'] = (float)@substr($status['UCD-SNMP-MIB::laLoad.2'], 9);</p><p class="line">$host_status['uptime_15min'] = (float)@substr($status['UCD-SNMP-MIB::laLoad.3'], 9);</p><p class="line">$host_status['user_cpu'] = (int)@substr($status['UCD-SNMP-MIB::ssCpuUser.0'], 9);</p><p class="line">$host_status['system_cpu'] = (int)@substr($status['UCD-SNMP-MIB::ssCpuSystem.0'], 9);</p><p class="line">$host_status['idle_cpu'] = (int)@substr($status['UCD-SNMP-MIB::ssCpuIdle.0'], 9);</p><p class="line">$host_status['total_swap'] = (int)@substr($status['UCD-SNMP-MIB::memTotalSwap.0'], 9);</p><p class="line">$host_status['available_swap'] = (int)@substr($status['UCD-SNMP-MIB::memAvailSwap.0'], 9);</p><p class="line">$host_status['total_ram'] = (int)@substr($status['UCD-SNMP-MIB::memTotalReal.0'], 9);</p><p class="line">$host_status['used_ram'] = $host_status['total_ram'] - (int)@substr($status['UCD-SNMP-MIB::memAvailReal.0'], 9);</p><p class="line">$host_status['cached_memory'] = (int)@substr($status['UCD-SNMP-MIB::memCached.0'], 9);</p> Copy after login |
SNMP采用的是UDP协议,因此数据获取可能会失败,可以考虑在失败之后重试几次。
<p class="line">1</p> Copy after login |
<p class="line">$i = 0;</p><p class="line">$status = array();</p><p class="line">do {</p><p class="line"> $i ++;</p><p class="line"> $status = @snmp2_real_walk($host, $community, ".1.3.6.1.4.1.2021",10,5);</p><p class="line">} while(!(count($status) != 0 OR $i >= 3));</p> Copy after login |
2、获取MySQL数据库的状态数据
获取MySQL的状态较简单,只需要运行下面这个查询即可:
<p class="line">1</p> Copy after login Copy after login Copy after login Copy after login |
<p class="line">SHOW GLOBAL STATUS</p> Copy after login |
这个查询的结果也非常丰富,只需要从中挑选需要的即可。例如下面的例子获取了若干查询的数量:
<p class="line">1</p> Copy after login Copy after login |
<p class="line">$db = @new mysqli($host, $mysql_user, $mysql_pwd);</p><p class="line">$result = @$db->query("SHOW GLOBAL STATUS");</p><p class="line">$status = array();</p><p class="line">if($result) {</p><p class="line"> while($temp = $result->fetch_assoc()) {</p><p class="line"> switch($temp['Variable_name']) {</p><p class="line"> case "Com_select":</p><p class="line"> case "Com_insert":</p><p class="line"> case "Com_delete":</p><p class="line"> case "Com_update":</p><p class="line"> case "Com_change_db":</p><p class="line"> $status[$temp['Variable_name']] = $temp['Value'];</p><p class="line"> break;</p><p class="line"> default:</p><p class="line"> break;</p><p class="line"> }</p><p class="line"> }</p><p class="line"> $db->close();</p><p class="line"> return $status;</p><p class="line">} else</p><p class="line"> return false;</p> Copy after login |
唯一需要注意的是,上述值是MySQL服务启动之后的累加值,所以说只要MySQL不重启,这些值是递增的。而在实际分析或者显示时,一般是需要某个时间段内的值,有如下两个思路来应对:
- 1、获取最新一次的数据时,从数据库中查询到上次获取的值,二者相减,即可得到两次查询内的数据值,这个实现时还需要保存一个额外的临时值以用于作为减数,否则会失去“坐标”;
- 2、将最新获取的值存储在数据库中,在显示或者读取时,再进行相应的减法操作;
个人比较推荐采用第二种方法,即在使用时再对数据进行处理,这至少遵循了“保存原始数据”这一原则,而且需求是有可能变化的。
3、定期执行PHP脚本获取状态值
PHP本身无法实现定时运行这一功能,因此只有借助其它手段,比如,Linux中的crontab,按设定的规则和时间在后台执行,只需要将定时执行的内容换成命令行下的PHP脚本即可。
下面的这一段PHP可以实现从命令行或URL接受参数并执行相关操作的功能
<p class="line">1</p> Copy after login Copy after login |
<p class="line">$action = '';</p><p class="line">if(isset($_GET['action']))</p><p class="line"> $action = $_GET['action'];</p><p class="line">if(isset($argv[1]))</p><p class="line"> $action = $argv[1];</p><p class="line">switch($action) {</p><p class="line"> case "snmp":</p><p class="line"> refresh_host_status_by_snmp();</p><p class="line"> break;</p><p class="line"> case get_option("refresh_frequency") . "min":</p><p class="line"> refresh_order_count();</p><p class="line"> refresh_service_status();</p><p class="line"> break;</p><p class="line"> case "1day":</p><p class="line"> refresh_uptime_yesterday_in_service_list();</p><p class="line"> save_all_tasks_one_day_details();</p><p class="line"> break;</p><p class="line"> default:</p><p class="line"> echo "help content here.\n";</p><p class="line"> break;</p><p class="line">}</p> Copy after login |
将上述脚本保存为cli.php,则可以通过下面两种方法来运行:
- 1、http://chensd.com/cli.php?action=5min
- 2、/usr/bin/php cli.php 5min
php执行文件的位置可能会因编译或安装的情况而不一样。
在crontab中添加如下的一行可以每隔五分钟运行一次:
<p class="line">1</p> Copy after login Copy after login Copy after login Copy after login |
<p class="line">5,10,15,20,25,30,35,40,45,50,55,0 * * * * /usr/bin/php /home/xxx/cli.php 5min</p> Copy after login |
或
<p class="line">1</p> Copy after login Copy after login Copy after login Copy after login |
<p class="line">*/5 * * * * /usr/bin/php /home/xxx/cli.php 5min</p> Copy after login |
4、数据的可视化
无论是什么样的人,总是更容易接受图表一些——这个与智商关系不大。
现在的数据可视化技术实在是太丰富了,除了以前比较常见的flash和图片,现在的js可视化技术也进入了实用化阶段,而且将图表的生成工作量转给了客户端浏览器,也没有了插件的依赖,要命的是iOS也没问题……
Highcharts和flot都很不错,前者相对更成熟,后者是开源项目,Highcharts母公司新推出了Highstocks也很不错,但是现在还在测试阶段,净highstocks.js都有300多KB。
性能上讲,据称,flot表现1000个点时仍然轻松自如。从我实现的情况来看,highstocks一个图中同时显示3000余个点依然很流畅,单图15000个数据仍然可以灵活拉动highstocks的时间轴。
最近由于工作上的原因,需要开发一套服务器性能的监控工具,主要是一些同架构同软件配置的服务器。考虑到最近用php比较多,随即决定使用php来实现。主要需要实现的部分功能如下:
- 1、系统状态,如cpu / 内存 / swap等数据;
- 2、MySQL的性能与运行状态;
其它主要是配合性的数据存储以及可视化。原文地址:利用PHP实现对服务器性能与状态的监控, 感谢原作者分享。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

The top ten free virtual currency exchanges are ranked: 1. OKX; 2. Binance; 3. Gate.io; 4. Huobi Global; 5. Kraken; 6. Coinbase; 7. KuCoin; 8. Crypto.com; 9. MEXC Global; 10. Bitfinex. These platforms each have their own advantages.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.
