Recently, for work reasons, we need to develop a set of server performance monitoring tools, mainly for servers with the same architecture and software configuration. Considering that php is widely used recently, php was immediately used for implementation. The main functions to be implemented are as follows: 1. system status, such as cpu memory swap and other data; 2. MySQL performance and running status; and others
Recently, for work reasons, we need to develop a set of server performance monitoring tools, mainly for servers with the same architecture and software configuration. Considering that php is widely used recently, php was immediately used for implementation. The main functions to be implemented are as follows: 1. system status, such as cpu, memory, and swap data; 2. MySQL performance and running status; and others
Recently, for work reasons, we need to develop a set of server performance monitoring tools, mainly for servers with the same architecture and software configuration. Considering that php is widely used recently, php was immediately used for implementation. The main functions to be implemented are as follows:
- 1. system status, such as cpu, memory, and swap;
- 2. MySQL performance and running status;
Others are mainly disruptive data storage and visualization.
1. Use SNMP to capture system status
SNMP is indeed a very convenient protocol. It can be used to obtain almost all the information of network devices. Both Linux and Windows can be well supported. MIB is a complex database that can obtain the information we need through filtering.
PHP5 has good support for snmp protocol v2. For convenience, you can obtain a large set of data, store it in an array, and then select the required data from the array. Snmp2_real_walk can meet this requirement. Its usage is as follows:
1
|
$status = @snmp2_real_walk($host, $community, ".1.3.6.1.4.1.2021",10,5);
|
- $ Host is the target host
- $ Community is the corresponding SNMP community code.
- "1.3.6.1.4.1.2021" is the data tree we want to obtain.
- 10 is the timeout time (seconds)
- 5 is the number of retries after failure
To prevent errors from being reported after an error occurs, use @ to forcibly disable the error.
The obtained results are stored in $ status, and then the data we need can be located from $ status. MIB may be composed of numbers or a bunch of names. You can query them here. Note that the values and results in the obtained results are mixed and must be intercepted. Below are some common values:
1 2 3 4 5 6 7 8 9 10 11
|
$host_status['uptime_1min'] = (float)@substr($status['UCD-SNMP-MIB::laLoad.1'], 9); $host_status['uptime_5min'] = (float)@substr($status['UCD-SNMP-MIB::laLoad.2'], 9); $host_status['uptime_15min'] = (float)@substr($status['UCD-SNMP-MIB::laLoad.3'], 9); $host_status['user_cpu'] = (int)@substr($status['UCD-SNMP-MIB::ssCpuUser.0'], 9); $host_status['system_cpu'] = (int)@substr($status['UCD-SNMP-MIB::ssCpuSystem.0'], 9); $host_status['idle_cpu'] = (int)@substr($status['UCD-SNMP-MIB::ssCpuIdle.0'], 9); $host_status['total_swap'] = (int)@substr($status['UCD-SNMP-MIB::memTotalSwap.0'], 9); $host_status['available_swap'] = (int)@substr($status['UCD-SNMP-MIB::memAvailSwap.0'], 9); $host_status['total_ram'] = (int)@substr($status['UCD-SNMP-MIB::memTotalReal.0'], 9); $host_status['used_ram'] = $host_status['total_ram'] - (int)@substr($status['UCD-SNMP-MIB::memAvailReal.0'], 9); $host_status['cached_memory'] = (int)@substr($status['UCD-SNMP-MIB::memCached.0'], 9);
|
What is SNMP used?UDP protocolTherefore, data may fail to be obtained. You can try again several times after the failure.
1 2 3 4 5 6
|
$i = 0; $status = array(); do { $i ++; $status = @snmp2_real_walk($host, $community, ".1.3.6.1.4.1.2021",10,5); } while(!(count($status) != 0 OR $i >= 3));
|
2. Obtain the status data of the MySQL database
It is easy to obtain the MySQL status. You only need to run the following query:
The query results are also rich. You only need to select the desired results. For example, the following example obtains the number of several queries:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
$db = @new mysqli($host, $mysql_user, $mysql_pwd); $result = @$db->query("SHOW GLOBAL STATUS"); $status = array(); if($result) { while($temp = $result->fetch_assoc()) { switch($temp['Variable_name']) { case "Com_select": case "Com_insert": case "Com_delete": case "Com_update": case "Com_change_db": $status[$temp['Variable_name']] = $temp['Value']; break; default: break; } } $db->close(); return $status; } else return false;
|
Note that the preceding value isAccumulated valueTherefore, as long as MySQL is not restarted, these values increase progressively. In actual analysis or display, the value in a certain period of time is generally required. There are two ways to deal with it:
- 1. When obtaining the latest data, the last retrieved value is queried from the database. The two values are subtracted to obtain the data values in the two queries, in this implementation, you also need to save an additional temporary value to be used as the subtraction, otherwise the "coordinates" will be lost ";
- 2. Store the latest value in the database, and perform the corresponding subtraction operation when displaying or reading it;
For personal comparison, we recommend that you use the second method, that is, to process the data during use, which at least follows the principle of "saving the original data" and the requirements may change.
3. Execute the PHP script regularly to obtain the status value.
PHP itself cannot implement the timed running function. Therefore, it only needs to use other means, such as crontab in Linux, which is executed in the background according to the set rules and time, you only need to replace the regularly executed content with the PHP script under the command line.
The following PHP can accept parameters from the command line or URL and execute related operations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
$action = ''; if(isset($_GET['action'])) $action = $_GET['action']; if(isset($argv[1])) $action = $argv[1]; switch($action) { case "snmp": refresh_host_status_by_snmp(); break; case get_option("refresh_frequency") . "min": refresh_order_count(); refresh_service_status(); break; case "1day": refresh_uptime_yesterday_in_service_list(); save_all_tasks_one_day_details(); break; default: echo "help content here.\n"; break; }
|
To save the preceding script as cli. php, you can run it in the following two ways:
- 1, http://chensd.com/cli.php? Action = 5 min
- 2./usr/bin/php cli. php 5 min
The location of the php Execution file may vary depending on the compilation or installation.
You can add the following line to the crontab to run every five minutes:
1
|
5,10,15,20,25,30,35,40,45,50,55,0 * * * * /usr/bin/php /home/xxx/cli.php 5min
|
Or
1
|
*/5 * * * * /usr/bin/php /home/xxx/cli.php 5min
|
4. Data Visualization
No matter what type of person, it is always easier to accept charts-this has little to do with IQ.
Currently, the data visualization technology is too rich. In addition to the commonly used flash and images, the current js visualization technology has entered the practical stage, in addition, the workload of generating charts is transferred to the client browser without the dependency of the plug-in. The worst thing is that iOS is okay ......
Highcharts and flot are both very good. The former is relatively more mature, while the latter is an open-source project. The parent company of Highcharts has launched Highstocks, which is also very good, but it is still in the testing stage. js has more than 300 KB.
In terms of performance, it is said that flot performance is still easy at 1000 points. According to my implementation, a diagram of highstocks shows that more than 3000 points are still smooth at the same time, and a single 5000 data records can still flexibly pull the timeline of highstocks.
Recently, for work reasons, we need to develop a set of server performance monitoring tools, mainly for servers with the same architecture and software configuration. Considering that php is widely used recently, php was immediately used for implementation. The main functions to be implemented are as follows: