Esempio n. 1
0
<?php

require_once 'AWSSDKforPHP/sdk.class.php';
define('AWS_KEY', getenv('EC2_KEY_ID'));
define('AWS_SECRET_KEY', getenv('EC2_SECRET_KEY'));
define('AWS_ACCOUNT_ID', getenv('AWS_ACCOUNT_ID'));
$m = new Mongo();
$cw = new AmazonCloudWatch();
$cw->set_region('monitoring.' . getenv('EC2_REGION') . '.amazonaws.com');
$db = $m->admin;
$local = $m->selectDB("local");
$replset = $local->selectCollection("system.replset");
$replica_set_conf = $replset->findOne();
$ismaster = $db->command(array('ismaster' => true));
$server_status = $db->command(array('serverStatus' => true));
$replica_set_status = $db->command(array('replSetGetStatus' => true));
if (isset($server_status['repl']['arbiterOnly']) && $server_status['repl']['arbiterOnly']) {
    $state = 'arbiter';
} else {
    if ($server_status['repl']['ismaster']) {
        $state = 'primary';
    } else {
        $state = 'secondary';
    }
}
switch ($state) {
    case 'primary':
        # here we do the replica set metrics
        add_replica_set_metrics($cw, $ismaster, $server_status, $replica_set_status, $replica_set_conf);
    case 'secondary':
        # and the metrics for primary & secondary
Esempio n. 2
0
<?php

require_once '/usr/share/php/AWSSDKforPHP/sdk.class.php';
define('AWS_KEY', 'AKIAJKI7FFIP2BHMXF7A');
define('AWS_SECRET_KEY', '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');
define('AWS_ACCOUNT_ID', '382146031153');
$cw = new AmazonCloudWatch();
$cw->set_region($cw::REGION_EU_W1);
$measure = $_GET['measure'];
$statistics = $_GET['statistics'];
$unit = $_GET['unit'];
$dimensions = explode('=', urldecode($_GET['dimensions']));
$namespace = $_GET['namespace'];
$period = $_GET['period'];
$threshold = (int) $_GET['threshold'];
if (!isset($measure) || !isset($statistics) || !isset($unit) || !isset($dimensions) || !isset($namespace) || !isset($period) || !isset($threshold)) {
    exit("Usage: http://localhost/cloudwatch/breach.php?measure=CPUUtilization&statistics=Average&unit=Percent&dimensions=ImageId%3dami-30360344&namespace=EC2&period=900&threshold=20");
}
$now = time();
$before = $now - (int) $period;
$opt = array('Namespace' => 'AWS/' . $namespace, 'Period' => (int) $period);
$opt = array_merge($opt, CFComplexType::map(array('Dimensions' => array('member' => array('Name' => $dimensions[0], 'Value' => $dimensions[1])))));
$response = $cw->get_metric_statistics($measure, $statistics, $unit, date("c", $before), date("c", $now), $opt);
$measurement = $response->body->member(0)->{$statistics}(0);
$breach = $measurement >= $threshold;
echo $breach ? 1 : 0;