หน้าเว็บ

วันจันทร์ที่ 28 ตุลาคม พ.ศ. 2562

Directadmin API Command

Directadmin API Command Directadmin สามารถใช้ Command ที่เป็น API เข้าไปเรียกข้อมูลมาใช้งานได้
https://www.directadmin.com/search_versions.php?help=no&versions=yes&query=CMD_API_

เช่น อ่านค่าเมล์ ของ User ว่าเต็มหรือไม่แล้วแจ้งเตือนในเมล์
ใช้ httpsocket.php ร่วมกับ CMD_API_POP

ไฟล์อยู่ที่
http://www.scivalve.com/checkuse/checkmail.php

Code:
  1.  
  2.  
  3. include("httpsocket.php");
  4. //nou de socket is geinclude kunnen we hem starten
  5. $sock = new HTTPSocket;
  6.  
  7. //nu gaan we inloggen in Direct Admin (verander wel de gegevens)
  8. $sock->connect('127.0.0.1',2222);
  9. $sock->set_login('XXXXX','XXXXXXXX');
  10.  
  11. $domain='scivalve.com' ;
  12.  
  13. function formatSizeUnits($bytes)
  14. {
  15.    if($bytes >= 1099511627776){
  16.     $bytes = number_format($bytes / 1099511627776, 2) . ' TB';
  17.    } elseif($bytes >= 1073741824){
  18.     $bytes = number_format($bytes / 1073741824, 2) . ' GB';
  19.    } elseif($bytes >= 1048576){
  20.       $bytes = number_format($bytes / 1048576, 2) . ' MB';
  21.    } elseif ($bytes >= 1024){
  22.       $bytes = number_format($bytes / 1024, 2) . ' KB';
  23.    } elseif ($bytes > 1){
  24.       $bytes = $bytes . ' bytes';
  25.    } elseif ($bytes == 1){
  26.       $bytes = $bytes . ' byte';
  27.    } else {
  28.       $bytes = '-';
  29.    }
  30. return $bytes;
  31. }//Close Functions.
  32.  
  33. //User Susspende เช็คไม่ได้ต้องเขียนไว้เทียบเอา
  34. $UserSuspended="chanakan
  35. jirasak
  36. julalak
  37. kongsit
  38. mathuros
  39. monthon
  40. nattapol
  41. nontiwa
  42. pattanan
  43. porramate
  44. sarawut
  45. sarawut_e
  46. sudjai
  47. tanupat
  48. thanyalak";
  49.  
  50. $sock->query('/CMD_API_POP', array(
  51.    'action'     => 'full_list',
  52.    'type'       => 'quota',
  53.    //'suspended'    => 'yes',
  54.    'domain'     => $domain
  55. ));
  56. $user_quota = $sock->fetch_parsed_body();
  57. //print_r($user_quota);
  58. $i = 0;
  59. foreach ($user_quota as $key => $value) {
  60.     //echo $key.":".$value."<br>";
  61.     //ใช้ List Run บน Crontab แล้ว Error
  62.     //list($quota, $sent, $usage, $Use, $usage_bytes) = explode('&', $value);  
  63.     $pieces = explode("&", $value);
  64.     $quota = $pieces[0];
  65.     //$sent = $pieces[1];
  66.     $usage = $pieces[2];
  67.     //$Use = $pieces[3];
  68.     //$usage_bytes = $pieces[4];
  69.     //echo $key.":".$quota.":".$usage;
  70.     $quotaValue = explode('=', $quota);
  71.     $usageValue = explode('=', $usage);
  72.  
  73.     if (strpos($UserSuspended, "'".$key."'") !== false) {
  74.         if((($quotaValue[1] - $usageValue[1]) <= 0) && ($quotaValue[1] != 0)){
  75.             //echo $key." : ส่งเมล์ <br>";
  76.             $i = $i+1;
  77.             if($i == 1){
  78.                 $EmailUser = $i." : ".$key."@scivalve.com  ".formatSizeUnits($quotaValue[1])."/".formatSizeUnits($usageValue[1])."<br>";   
  79.             } else {   
  80.                 $EmailUser = $EmailUser.$i." : ".$key."@scivalve.com  ".formatSizeUnits($quotaValue[1])."/".formatSizeUnits($usageValue[1])."<br>"
  81.             }     
  82.         }//Close if.
  83.     }//Close if.
  84. }//Close for.
  85. //echo $EmailUser;
  86.  
  87. if($i > 0){
  88.     //ส่งเมล์ 
  89.     //อ้าง Path Run บนเว็บผ่าน แต่ Run ผ่าน Crontab Error 
  90.     require("/home/scivalve/public_html/mailer/class.phpmailer.php");
  91.     $mail = new PHPMailer();
  92.     $body = " พบเมล์ scivalve ของ User ใกล้เต็ม กรุณาตรวจสอบ ดังนี้ Quota/Used<br> $EmailUser "
  93.    
  94.     $mail->CharSet = "utf-8";
  95.     $mail->IsSMTP();
  96.     $mail->SMTPDebug = 0;
  97.     $mail->SMTPAuth = true;
  98.     $mail->Host = "ssl://smtp.gmail.com"; // SMTP server
  99.     $mail->Port = 465; // พอร์ท
  100.     $mail->Username = "XXXXXX@gmail.com"; // account SMTP
  101.     $mail->Password = "XXXXXX"; // รหัสผ่าน SMTP   
  102.    
  103.     $mail->SetFrom("scivalvethailand@gmail.com", "scivalve.com");
  104.     $mail->AddReplyTo("scivalvethailand@gmail.com", "scivalve.com");
  105.     $mail->Subject = "Check Mail Full @scivalve.com";
  106.    
  107.     $mail->MsgHTML($body);
  108.    
  109.     //if($fileRename0 != ""){   
  110.     //  $file = "message/".$fileRename0;   
  111.     //  $mail->addAttachment($file, $fileRename0);
  112.     //}
  113.     $mail->AddAddress("suwit@scivalve.com", "SCI"); // ผู้รับคนที่ 1
  114.     $mail->AddAddress("nikom@scivalve.com", "SCI"); // ผู้รับคนที่ 2
  115.     $mail->AddAddress("seksan@scivalve.com", "SCI"); // ผู้รับคนที่ 3
  116.     $mail->Send();
  117.  
  118.     //if(!$mail->Send()) {
  119.     //  $report= "Mailer Error: " . $mail->ErrorInfo;
  120.     //} else {
  121.     //  $report=    " <center><br><font =class='txt1'><b>Send message complete<br>Please wait for back to home page<b></font>";
  122.     //} 
  123. }


Test Run command
  1. /usr/local/bin/php /home/scivalve/public_html/checkuse/checkmail.php


ssh ไปที่ Server VPS สร้าง Crontab ใช้ Vi ยาก ใช้ nana ไฟล์อยู่ที่
  1. nano /var/spool/cron/root

ตั้งไว้ เช็คทุกวัน หกโมงเช้า จันทร์ - เสาร์
0 6 * * 1-6 /usr/local/bin/php /home/scivalve/public_html/checkuse/checkmail.php

เมล์ที่แจ้งเตือน ตัวอย่าง

ไม่มีความคิดเห็น:

แสดงความคิดเห็น