Skip to content

Instantly share code, notes, and snippets.

@mikeda
Created July 8, 2013 08:47
Show Gist options
  • Select an option

  • Save mikeda/5947245 to your computer and use it in GitHub Desktop.

Select an option

Save mikeda/5947245 to your computer and use it in GitHub Desktop.
ZabbixでRDSを監視するexternalscriptsのサンプル。いろいろ古い
#!/usr/bin/php
<?php
//最新版だとPHP5.2~らしいのでSDKは1.3.0を使う
require_once(dirname(__FILE__) .'/../helper/aws/sdk-1.3.0/sdk.class.php');
//error_reporting(E_ALL);
$debug_log = "/var/log/zabbix/rds.log";
//$args = print_r($argv, true);
//debug_log($args);
$units = array(
'CPUUtilization' => 'Percent',
'DatabaseConnections' => 'Count',
'FreeableMemory' => 'Bytes',
'FreeStorageSpace' => 'Bytes',
'ReadIOPS' => 'Count/Second',
'ReadLatency' => 'Seconds',
'ReadThroughput' => 'Bytes/Second',
'SwapUsage' => 'Bytes',
'WriteIOPS' => 'Count/Second',
'WriteLatency' => 'Seconds',
'WriteThroughput' => 'Bytes/Second',
);
$db = $argv[2];
$region = $argv[3];
$metric = $argv[4];
$namespace = 'AWS/RDS';
//$statistics = array('Average', 'Maximum', 'SampleCount');
$statistics = array('Average', 'SampleCount');
/*
* 値の格納にタイムラグがあるので1分毎、5分ぶん取得して最新値を利用
* -> 5分以上のラグが発生するとSampleCount==0が起こりうる
* -> タイムスタンプ指定でzabbix_senderを使うように修正したほうがいいかな
*/
$start = date('c', time() - 300);
$end = date('c');
$cw = new AmazonCloudWatch();
$cw->set_region($region);
$opt = array(
'Dimensions' => array(
array('Name' => 'DBInstanceIdentifier', 'Value' => $db)
),
);
$res = $cw->get_metric_statistics($namespace, $metric,
$start, $end, 60,
$statistics, $units[$metric], $opt);
if(! $res->isOK()){
echo 1;
exit(1);
}
$datapoints = $res->body->GetMetricStatisticsResult->Datapoints->member;
// Populate an array with the metrics for sorting
$dataRows = array();
foreach ($datapoints as $datapoint)
{
$timestamp = (string) $datapoint->Timestamp;
$dataRows[$timestamp] =
array('Timestamp' => (string) $datapoint->Timestamp,
'Units' => (string) $datapoint->Unit,
'Samples' => (string) $datapoint->Samples,
'Average' => (float) $datapoint->Average,
);
}
ksort($dataRows);
$lastRow = array_pop($dataRows);
echo $lastRow["Average"];
exit(0);
### end of main
function debug_log($msg) {
global $debug_log;
$msg = date("Y/m/d H:i:s") ." ". $msg ."\n";
file_put_contents($debug_log, $msg, FILE_APPEND);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment