
「さくらインターネットVPSリソース情報自動報告プログラム(Ver.0.0.1)」のアップデートをリリースします。
スポンサードリンク
アップデート内容
Ver.0.02

このバージョンでは、ホスト名を取得するようにしました。
VPS のIP アドレスは、Conf ファイルでログイン情報として記述しますので、VPS コントロールパネルに表示される情報を解析する方法ではなく、Conf ファイルに記載されたIP アドレスからホスト名を取得するようになっています。
取得した情報は、メール本文にIP アドレスとともに記載され、複数のVPS を管理する際に間違いが起きにくいように役立てることができます。
Ver.0.03

このバージョンでは、ファイルを保存しようとするディレクトリの存在チェックを行い、不存在の場合は作成するようになりました。
これにより、予めディレクトリを作成しておかなくても動的にチェックして作成することができるようになりました。現在のところ、作成されるディレクトリは「0755」に固定されています。
ちなみに、設定ファイルの終わりに「$save_path = “../data/” . $ipaddress . “/”;」などとすると、複数のVPS のグラフデータをIP アドレス毎に分けて保存することができます(が、ホスト名は、設定ファイルが読み込まれた後に取得されるので、現在のところ、ディレクトリ名に使用できません)。
ソースコード
sakuravps.php
<?php
/* ==============================================
*
* SAKURA Internet Resources Reporter
*
* for SAKURA VPS CONTROL PANEL
*
* ==============================================
* License: GNU GPL
* Code : PHP, UTF-8
* Auther : pc.casey.jp, 2013
* Version:
* Ver.0.0.1 2013.03.17 1st.
* Ver.0.0.2 2013.03.19 add hostname by ip address.
* Ver.0.0.3 2013.03.19 add check and make dir.
* Usage :
* - shell
* php /virtual/path/to/sakuravps.php
* - cron
* see sakuravps.sh file
* - http (no recomend)
* http://example.com/sakuravps.php
*/
/* ------------------------------------
* settings
* ------------------------------------*/
// conf file
$conf = dirname(__FILE__) . '/sakuravps.conf';
/* ----------------------------------
* define [do not change!]
* ----------------------------------*/
// program version
$version = "0.0.3";
// get start time
$sTime = time();
// loggin page url
$url = 'https://secure.sakura.ad.jp/vpscontrol/';
// dev
$debug = false;
/* ------------------------------------
* MAIN section
* ------------------------------------*/
// start output
header("HTTP/1.0 200 OK");
header("Content-type:text/html;charset=utf-8");
// read config file
if(file_exists($conf)){
require_once $conf;
}else{
die('Can not load a configuration file');
}
// program name
$host_name = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : php_uname("n");
$program_name = "SAKURA Internet Resources Reporter ";
$program_name .= "for SAKURA VPS CONTROL PANEL Ver.";
$program_name .= $version . " on " . $host_name;
$program_name .= " (C) https://pc.casey.jp , 2013";
// get date time
//$date = date('Y/m/d H:i:s');
$file_date = date('Y-m-d_H-i-s');
// get data from sakura internet
$saved_files = getData();
// saved files
$mail_body .= "\n\nSaved Files:\n";
if($saved_files){
$mail_body .= implode("\n", $saved_files);
if($double_save){
$mail_body .= "\n *Double Save Mode ENABLED!\n";
}
}else{
$mail_body .= "NONE!(Check your configuration file)\n";
}
// hostname ( ip address )
$mail_body .= "\nHost: " . gethostbyaddr($ipaddress);
$mail_body .= " ( " . $ipaddress . " )" . "\n";
// add mail footer
$mail_body .= "\n\n" . $program_name;
$eTime = time() - $sTime;
$mail_body .= " (" . $eTime . "sec+)";
// debug mode
if($debug){
print $mail_body;
$mail=false;
}
// mail send
if($mail){
mailSend();
}
// end of script
print "OK!\n";
exit;
/* ------------------------------------
* mail send with PHPMailer
* ------------------------------------*/
function mailSend(){
// init
global $mail_addr_to,
$mail_subject,
$mail_body,
$mail_addr_from;
global $save_path, $saved_files;
if(!$saved_files){
return;
}
require("PHPMailer/class.phpmailer.php");
mb_language("japanese");
mb_internal_encoding("UTF-8");
$mail = new PHPMailer();
$mail->CharSet = "iso-2022-jp";
$mail->Encoding = "7bit";
$mail->AddAddress($mail_addr_to);
$mail->From = $mail_addr_from;
$mail->FromName = $mail_addr_from;
$mail->Subject = $mail_subject;
$mail->Body = $mail_body;
foreach($saved_files as $file){
$mail->AddAttachment($save_path . $file);
}
//$mail->FromName = mb_encode_mimeheader(mb_convert_encoding($fromname,"JIS","UTF-8"));
//$mail->Subject = mb_encode_mimeheader(mb_convert_encoding($subject,"JIS","UTF-8"));
//$mail->Body = mb_convert_encoding($body,"JIS","UTF-8");
$mail->Send();
}
/* ------------------------------------
* secure connection
* ------------------------------------*/
function getData(){
// init
global $url;
global $ipaddress, $password;
require_once "HTTP/Client.php";
$client =& new HTTP_Client();
$classCookieManager = $client->getCookieManager();
// 1st access, get token
$client->get($url);
$response = $client->currentResponse();
// get token
preg_match(
"/name\=\"Token\" value\=\"(.*)\"/",
$response['body'],
$tokens
);
$token = $tokens[1];
// 2nd access, send id, password, token, submit
$login_params = array(
"ipaddress" => $ipaddress,
"password" => $password,
"Token" => $token,
"Submit" => "index"
);
$client->post($url, $login_params);
// 3rd access, save resources page data
global $get_disk, $get_traffic, $get_cpu;
$res = "";
// CPU
if($get_cpu){
$res[] = _getData($responce, $client, 'cpu');
}
// Traffic
if($get_traffic){
$res[] = _getData($responce, $client, 'traffic');
}
// DISK
if($get_disk){
$res[] = _getData($responce, $client, 'disk');
}
// return, saved file name array
return $res;
}
/* ------------------------------------
* Download and save the data
* ------------------------------------*/
function _getData($responce, $client, $filename){
// init
global $url, $file_date;
global $save_path, $get_type, $save_file_date;
global $double_save;
// download
$client->get($url . '/main/graph?type=' . $filename . '&period=' . $get_type);
$response = $client->currentResponse();
// save file name
if($save_file_date){
if($double_save){
checkAndMkdir($save_path);
file_put_contents($save_path . $filename . '.png', $response['body']);
}
$filename .= '_' . $file_date;
}
// save
checkAndMkdir($save_path);
file_put_contents($save_path . $filename . '.png', $response['body']);
// return, saved file name
return $filename . '.png';
}
/* ------------------------------------
* Check and Make DIR
* ------------------------------------*/
// add 2013.03.19 Ver.0.0.2
function checkAndMkdir($path){
// check dir
if(!is_dir($path)){
// mkdir
mkdir($path, 0755, true);
}
return;
}
?>
–


コメント