Created
November 15, 2011 03:43
-
-
Save gomasaba/1366065 to your computer and use it in GitHub Desktop.
簡易なサーバーモニタリングシェルのメモ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* サーバー監視シェル | |
* Gmail経由でメール送信 | |
* | |
* @author ootatter | |
*/ | |
App::uses('CakeEmail', 'Network/Email'); | |
App::uses('ConnectionManager', 'Model'); | |
class MonitoringShell extends Shell { | |
/** | |
* Gmail access | |
* | |
*/ | |
public $gmail = array( | |
'host' => 'ssl://smtp.gmail.com', | |
'port' => 465, | |
'username' => '', | |
'password' => '' | |
); | |
/* | |
* Config | |
* | |
*/ | |
public $config = array( | |
'URL' =>'', | |
'from' => array('' => ''), | |
'mail_to'=>array( | |
''=>'', | |
) | |
); | |
/** | |
* Main method | |
* @param diskspace | |
* | |
*/ | |
public function main(){ | |
$this->check_webstatus(); | |
if(isset($this->args[0])){ | |
switch ($this->args[0]) { | |
case 'diskspace': | |
$this->check_diskspace(); | |
break; | |
default: | |
break; | |
} | |
}else{ | |
} | |
} | |
/** | |
* Web Access Check | |
* | |
*/ | |
public function check_webstatus(){ | |
$error = false; | |
$subject = '【】エラー'; | |
$message =null; | |
//apache | |
if (!file_get_contents($this->config['URL'])) { | |
$error = true; | |
$message .= 'ホームページへアクセスできませんでした。'."\n"; | |
$this->send(); | |
} | |
//mysql | |
try { | |
$db = ConnectionManager::getDataSource('default'); | |
if(!$db->isConnected()){ | |
$error = true; | |
$message .= 'データベースへアクセスできませんでした。'."\n"; | |
} | |
}catch(Exception $e) { | |
$error = true; | |
$message .= 'データベースへアクセスできませんでした。'."\n"; | |
} | |
if($error){ | |
$this->send($subject,$message); | |
} | |
} | |
/** | |
* HardDisk Space Check | |
* | |
*/ | |
public function check_diskspace(){ | |
$subject = '【】ディスク容量レポート'; | |
//ハードディスク容量チェック | |
$result = ''; | |
$current_space = intval(disk_free_space('/') / 1024 / 1024); | |
$total_space = intval(disk_total_space('/') / 1024 / 1024); | |
$result .= 'disk free: '. $current_space .'MB/'. $total_space ."MB\n"; | |
//メモリ使用量チェック | |
$memory_result = explode("\n", `free -m`); | |
$memory_pattern = "#^-/\+ buffers/cache: +([0-9]+) +([0-9]+)$#"; | |
preg_match($memory_pattern, $memory_result[2], $memory_info); | |
$result .= 'used memory: '. $memory_info[1] .'MB/'. intval($memory_info[1] + $memory_info[2]) ."MB\n"; | |
$this->send($subject,$result); | |
} | |
/** | |
* SendMail | |
* @param type $subject | |
* @param type $message | |
*/ | |
public function send($subject,$message){ | |
$email = new CakeEmail($this->gmail); | |
$res = $email->config(array('log' => 'emails')) | |
->from($this->config['from']) | |
->to($this->config['mail_to']) | |
->subject($subject) | |
->send($message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment