Home » Categories » Server Monitoring

Hướng dẫn sử dụng PHP Script monitor disk space usage

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 http://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 http://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

<?php

$key = "rootpath";

$disk_percentage_threshold = "2"; // When disk usage reaches this perctange an error will be returned.
$disk_path = "/"; // The path to check.

if ($_GET['key'] == $key ) {
$error_message = "";
$pass = true;

// Start Response Time

$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$start_time = $mtime;

// Disk Space Test

$disk_percentage_used = ((disk_free_space($disk_path) / disk_total_space($disk_path)))*100;
$disk_percentage_used = 100 - $disk_percentage_used;
if ($disk_percentage_used >= $disk_percentage_threshold) {
if($pass) {
$error_message = "Disk usage is at " . $disk_percentage_used . "%";
}
else {
$error_message = $error_message . ", Disk usage is at " . $disk_percentage_used . "%";
}
$pass = false;
}

// End Reponse Time

$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$end_time = $mtime;
$response_time = round(($end_time - $start_time), 3);

$results = array("result" => $pass, "error_message" =>$error_message, "response_time" => $response_time);
echo json_encode($results);
}
else {
echo "Access Denied";
}

?>

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 http://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 http://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: support@vhost.vn

Hotline: 19006806

Website: http://vhost.vn/

       
      Attachments Attachments
      There are no attachments for this article.
      Comments Comments
      There are no comments for this article. Be the first to post a comment.
      Related Articles RSS Feed
      Hướng dẫn tạo một Notification List
      Viewed 3204 times since Thu, Dec 17, 2015
      Hướng dẫn sử dụng Notification Lists
      Viewed 1284 times since Tue, Jun 20, 2017
      Hướng dẫn sử dụng Add API Alert
      Viewed 1742 times since Tue, Jun 20, 2017
      Hướng dẫn sử dụng Monitor View Check
      Viewed 3536 times since Tue, Jun 20, 2017
      Hướng dẫn sử dụng API Alerts và Notification Lists
      Viewed 2003 times since Tue, Jun 20, 2017
      Hướng dẫn sử dụng Monitor Add Check
      Viewed 1559 times since Tue, Jun 20, 2017
      Hướng dẫn sử dụng API Alerts
      Viewed 1687 times since Tue, Jun 20, 2017
      Hướng dẫn sử dụng Add Alert Contact
      Viewed 1448 times since Tue, Jun 20, 2017
      Hướng dẫn sử dụng Add Notification List
      Viewed 1444 times since Tue, Jun 20, 2017
      Hướng dẫn tạo một Check để monitor
      Viewed 6372 times since Thu, Dec 17, 2015