Hướng dẫn dùng disk size trong PHP

Home » Categories » Server Monitoring

Article Number: 180 | Last Updated: Wed, Sep 7, 2016 at 12:42 PM

Nội dung chính

  • Not the answer you're looking for? Browse other questions tagged php or ask your own question.
  • Description
  • Return Values

Trong bài viết dưới dây vHost sẽ hướng dẫn quý khách sử dụng PHP Script để montior dung lượng disk sử dụng phân vùng root trên VPS hoặc Server, đây là chỉ một ví dụ quý khách có thể monitor bất kì vấn đề gì trên Server của mình với PHP Script

Để có thể sử dụng PHP Script monitor dung lượng disk hoặc các vấn đề khác cần phải chuẩn bị những thứ cơ bản sau:

  • Tạo contact, thông tin chi tiết quý khách có thể tham khảo tại link sau //docs.vhost.vn/article.php?id=177
  • Tạo Notification List, thông tin chi tiết quý khách có thể tham khảo tại link sau //docs.vhost.vn/article.php?id=178
  • Server hoặc VPS đã được cài đặt các dịch vụ cơ bản để chạy Web như Apache, PHP để có thể sử dụng URL truy cập thông qua trình duyệt
  • Tạo file PHP tại đường dẫn có thể truy cập thông qua domain hoặc IP, ở đây vHost tạo file tới tên serverstatus.php

Mọi PHP Script tạo ra để monitor phải trả về 3 biến chuỗi json sau:

       + result: true hoặc false, tương ứng với kết quả test
       + error_message: thông báo lỗi được gửi đến email trong một cảnh báo
       response_time: thời gian để thực thi xong PHP Script

Đoạn Script trên sẽ trả về result=false tương ứng với kết quả trả về trên Server Monitor là fail nếu dung lượng disk sử dụng trên phân vùng root lớn hơn 2%, và ngược lại với result=true kết quả trên Server Monitor sẽ là Pass

Sau khi đã tạo PHP Script và có thể truy cập URL thông qua trình duyệt ở đây vHost tạo với URL sau //103.232.120.135/serverstatus.php?key=rootpath, cần phải tạo một check như bên dưới để monitor, thông tin chi tiết về cách tạo Check quý khách có thể tham khảo tại link //docs.vhost.vn/article.php?id=179

Mọi yêu cầu cần được hỗ trợ và giải đáp xin vui lòng liên hệ lại vHost

Email: 

Hotline: 19006806

Website: //vhost.vn/

Posted by: Trinh Trong Son - Fri, Dec 18, 2015 at 11:16 AM This article has been viewed 4084 times.
Filed Under: Server Monitoring

Attachments

There are no attachments for this article.

Comments

There are no comments for this article. Be the first to post a comment.

Add Comment

Related Articles

Hướng dẫn sử dụng Monitor View Check

Viewed 3077 times since Tue, Jun 20, 2017

Hướng dẫn sử dụng Monitor Add Check

Viewed 1354 times since Tue, Jun 20, 2017

Hướng dẫn sử dụng Notification Lists

Viewed 1083 times since Tue, Jun 20, 2017

Hướng dẫn tạo một Notification List

Viewed 2874 times since Thu, Dec 17, 2015

Hướng dẫn tạo một Check để monitor

Viewed 6034 times since Thu, Dec 17, 2015

Hướng dẫn sử dụng Add Alert Contact

Viewed 1277 times since Tue, Jun 20, 2017

Hướng dẫn sử dụng Add Notification List

Viewed 1258 times since Tue, Jun 20, 2017

Hướng dẫn sử dụng API Alerts

Viewed 1492 times since Tue, Jun 20, 2017

I have a domain name like 'mydomain.com'. I want to check how much space available on server with this domain name with the help of php. Like for our hard drive we use disk_total_space["C:"];. Can anyone help me to get the solution.

asked Dec 6, 2012 at 11:44

Depending on the structure of the file system you can use something like the following for Linux hosts:

$df = round[disk_free_space["/var/www"] / 1024 / 1024 / 1024];
print["Free space: $df GB"];

Or in your case it sounds like you're running on Windows so:

$df = round[disk_free_space["C:"] / 1024 / 1024 / 1024];
print["Free space: $df GB"];

answered Dec 6, 2012 at 11:49

PeterJPeterJ

3,62423 gold badges51 silver badges67 bronze badges

0

The main function is used to get space available is same as above said. you have to use disk_free_space[]. but the different is to know the server path. And put it in braces.

answered Dec 7, 2012 at 9:10

PremPrem

6973 silver badges10 bronze badges

1

use php disk_free_space["/"];

it Returns available space on filesystem or disk partition

answered Dec 6, 2012 at 11:48

Not the answer you're looking for? Browse other questions tagged php or ask your own question.

[PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8]

disk_free_spaceReturns available space on filesystem or disk partition

Description

disk_free_space[string $directory]: float|false

Parameters

directory

A directory of the filesystem or disk partition.

Note:

Given a file name instead of a directory, the behaviour of the function is unspecified and may differ between operating systems and PHP versions.

Return Values

Returns the number of available bytes as a float or false on failure.

Examples

Example #1 disk_free_space[] example

Notes

Note: This function will not work on remote files as the file to be examined must be accessible via the server's filesystem.

wiede at gmx dot net

11 years ago

Transformation is possible WITHOUT using loops:

Anonymous

8 years ago

$si_prefix = array[ 'B', 'KB', 'MB', 'GB', 'TB', 'EB', 'ZB', 'YB' ];

you are missing the petabyte after terabyte

'B', 'KB', 'MB', 'GB', 'TB', 'EB', 'ZB', 'YB'

should look like

'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'

sam

13 years ago

Nice, but please be aware of the prefixes.

SI specifies a lower case 'k' as 1'000 prefix.
It doesn't make sense to use an upper case 'K' as binary prefix,
while the decimal Mega [M and following] prefixes in SI are uppercase.
Furthermore, there are REAL binary prefixes since a few years.

Do it the [newest and recommended] "IEC" way:

KB's are calculated decimal; power of 10 [1000 bytes each]
KiB's are calculated binary; power of 2 [1024 bytes each].
The same goes for MB, MiB and so on...

Feel free to read:
//en.wikipedia.org/wiki/Binary_prefix

Nitrogen

15 years ago

Another easy way to convert bytes to human readable sizes would be this:



It simply takes the $Bytes and divides it by 1024 bytes untill it's no longer over or equal to 1024, meanwhile it increases the $Index to allocate which suffix belongs to the return [adding 'bytes' to the end to save some space].
You can easily modify it so it's shorter, but I made it so it's more clearer.

Nitrogen.

Jawira Portugal

8 months ago

This is not documented yet.
If $directory is invalid, then disk_free_space[] will return false and ALSO throw a Warning: "disk_free_space[]: No such file or directory"

somedude

5 years ago

With respect to Linux filesystems, I'll point out that this function returns the space available in the current volume or mountpoint, not the total physical disk space.  That is, this function used on the '/root' volume shows the free space in /root, which is different from '/home', and so on.

Matthieu S

5 years ago

On Windows, this also works with distant files, by using their full network path.

For instance, this will give the % of free disk space on the share "dir" from remote host "server" :


It can also work with drive letters mapped to a network path in certain cases.

pavel at sunhater dot com

3 years ago

Chủ Đề