PR

さくらインターネットVPSリソース情報自動報告プログラム(Ver.0.0.1)

XWSS016514-s

さくらインターネットのレンタルサーバーのリソース情報を自動報告する「さくらインターネット リソース情報自動報告プログラム」の姉妹版のようなもので、こちらは、さくらインターネットのVPS 向けのものです。

スポンサードリンク

XWSS016513-s

  • さくらインターネットの提供するVPS コントロールパネルに自動ログインし、リソースグラフを保存します。また、メールに添付して送信することもできます。
  • グラフは「CPU」「Traffic」「DISK」のうち好みのものを選択できます。また、「1日」もしくは「1週」を選択できます。
  • グラフは「cpu.png」または「cpu_YYYY-MM-DD_hh-mm-ss.png」で保存できます(cpu の場合)。日付保存する際に「ダブル保存モード」を有効にすると両方のファイルを保存できます。これは、外部から呼び出す際などに固有のファイル名でアクセスするような場合に便利です。
  • 「さくらインターネット リソース情報自動報告プログラム」と同階層に設置できます(「simplehtmldom」を共有できます)

ファイル構成

  • 本体
    • sakuravps.conf (設定ファイル)
    • sakuravps.php (本体)
    • sakuravps.sh (cron用など)
    • log (保存用ディレクトリ)
  • 依存関係

設置例

XWSS016515-s

レンタルサーバー用とVPS 用を同一階層に設置した例。

XWSS016516-s

ログディレクトリを見たところ(ダブル保存モード有効)。

動作例

XWSS016517-s

Gmail で受信した例。

IMG_6411-s

iPhone で受信した例。

何れも添付されたpng ファイルが表示されているため、視覚的に理解できる。

ソースコード

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.01 2013.03.17 1st.
 *  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.1";

// 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";
}
// 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;

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){
            file_put_contents($save_path . $filename . '.png', $response['body']);
        }
        $filename .= '_' . $file_date;
    }

    // save
    file_put_contents($save_path . $filename . '.png', $response['body']);

    // return, saved file name
    return $filename . '.png';
}
?>

sakuravps.conf

<?php
/* ------------------------------------
 *    Settings
 * ------------------------------------
 */

// login
$ipaddress        = "xxx.xxx.xxx";
$password        = "xxxxxxxx";

// save path
$save_path        = "./log/";

// get type
$get_cpu        = true;
$get_traffic    = true;
$get_disk        = true;

// 1d or 1w
// false: 1w
// true : 1d
$get_type        = '1d';

// save file name type
// false:
//   ex. cpu.png
// ture :
//   ex. cpu_YYYY-MM-DD_hh-mm-ss.png
$save_file_date    = false;

// double save
// if $save_file_date=true and $double_save=true then
// save 2 files (date time file and static name file)
// false: save 1 file
//   ex. (1) cpu_YYYY-MM-DD_hh-mm-ss.png OR
//       (2) cpu.png
// true : save 2 files
//   ex. (1) cpu_YYYY-MM-DD_hh-mm-ss.png AND
//       (2) cpu.png
$double_save    = true;

// mail
$mail            = ture;
$mail_subject    = "SAKURA Internet Resources Report for VPS";
$mail_body        = "GORIYOU WA KEIKAKU TEKINI :P";
$mail_addr_from    = "root@localhost";
$mail_addr_to    = "[email protected]";

// system
$path_dom        = "./simplehtmldom/";
?>

sakuravps.sh

#
# shell script for sakuravps.php on coreserver
#
# Auther : pc.casey.jp, 2013
# Code   : sh, LF
# License: GNU GPL
# Usage  : shell or cron
#  ex. every hour
#    0 * * * * sakuravps.sh
#  ex. every morning, no mail
#    0 5 * * * sakruacp.sh >/dev/null 2>&1
#
/usr/local/bin/php /virtual/path/to/sakuravps.php

ダウンロード

アップデート

さくらインターネットVPSリソース情報自動報告プログラム(Ver.0.0.3)」をリリースしました。

参考文献

  • PHPMailerを使ってかんたんにPHPで添付ファイル付きのメールを送る | PLUS

コメント