Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 154 lines (142 sloc) 5.595 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
<?php
/*
Plugin Name: Media Temple Server Status
Plugin URI: http://andrewryno.com/plugins/media-temple-server-status/
Description: A dashboard widget that displays server status information for Media Temple servers.
Version: 1.1
Author: Andrew Ryno
Author URI: http://andrewryno.com
License: GPLv2
*/

register_uninstall_hook( __FILE__, 'mtss_uninstall_hook' );

add_action( 'admin_menu', 'mtss_plugin_menu' );
add_action( 'wp_dashboard_setup', 'mtss_add_dashboard_widgets' );

// Display the Google Chart with (mt) server data on the dashboard
function mtss_dashboard_widget() {
// Get the API Key from the database and make sure they exist
$mtss_api_key = get_option( 'mtss_api_key' );
$mtss_service_id = (int) get_option( 'mtss_service_id' );
if ( empty( $mtss_api_key ) OR empty( $mtss_service_id ) ) {
echo 'Your (mt) API key and/or service ID are not set. <a href="' . get_admin_url() . 'options-general.php?page=mediatemple-server-stats">Enter them here!</a>';
return;
}

// Get the results from the API
$mt = json_decode( file_get_contents( 'https://api.mediatemple.net/api/v1/stats/' . $mtss_service_id . '/1hour.json?apikey=' . $mtss_api_key ) );
$range_stats = $mt->statsList;
?>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1.0', {packages: ['corechart']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Time');
data.addColumn('number', 'CPU');
data.addColumn('number', 'Memory');
data.addColumn('number', 'Processes');
data.addRows([
<?php foreach ($range_stats->stats as $stat): ?>
['<?php echo date('g:ia', $stat->timeStamp); ?>', <?php echo $stat->cpu / 100; ?>, <?php echo $stat->memory / 100; ?>, <?php echo $stat->processes / 100; ?>],
<?php endforeach; ?>
]);
var options = {
backgroundColor: '#f5f5f5',
chartArea: {
top: 20,
right: 0,
bottom: 0,
left: 40,
},
colors: ['#21759B', '#D54E21', '#777777'],
fontSize: '10',
title: 'Last Hour',
vAxis: {
format: '#%'
}
};
var chart = new google.visualization.LineChart(document.getElementById('mtss_chart'));
var formatter = new google.visualization.NumberFormat({
pattern: '#.##%',
fractionDigits: 2
});
formatter.format(data, 1);
formatter.format(data, 2);
chart.draw(data, options);
}
</script>
<div id="mtss_chart"></div>
<?php
}

// Add the widget to the dashboard
function mtss_add_dashboard_widgets() {
add_meta_box( 'mtss_dashboard_widget', '(mt) Server Status', 'mtss_dashboard_widget', 'dashboard', 'side', 'high' );
}

// Create a options page under the 'Settings' page
function mtss_plugin_menu() {
add_options_page( '(mt) Server Stats Options', '(mt) Server Stats', 'manage_options', 'mediatemple-server-stats', 'mtss_plugin_options' );
}

// Create the plugins page, which handles the form for adding API information
function mtss_plugin_options() {
if ( ! current_user_can( 'manage_options' )) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}

// Check to see if the form was submitted
    if ( isset( $_POST['submit'] )) {
        update_option( 'mtss_api_key', $_POST['mtss_api_key'] );
        update_option( 'mtss_service_id', $_POST['mtss_service_id'] );
?>
<div class="updated"><p><strong>Settings updated.</strong></p></div>
<?php
    }
    
    // If the form isn't submitted, get inputted values
    // otherwise get the just updated values
    $mtss_api_key = get_option( 'mtss_api_key' );
    $mtss_service_id = get_option( 'mtss_service_id' );
    
    // Allow the user to select services from a dropdown instead of inputting it themselves
if ( ! empty( $mtss_api_key ) ) {
$mt = json_decode( file_get_contents( 'https://api.mediatemple.net/api/v1/services.json?apikey=' .$mtss_api_key ) );
$services = $mt->services;
    }
?>
<div class="wrap">
<div id="icon-options-general" class="icon32"><br></div>
<h2>(mt) Server Stats Options</h2>
<form name="mtss-form" method="post" action="">
<table class="form-table">
<tbody>
<tr>
<th><label for="mtss_api_key">API Key</label></th>
<td>
<input name="mtss_api_key" id="mtss_api_key" type="text" value="<?php echo $mtss_api_key; ?>" class="regular-text code">
<span class="description">Visit <a href="https://ac.mediatemple.net/api/">your (mt) account</a> to find or create an API key</span>
</td>
</tr>
<tr>
<th><label for="mtss_service_id">Service</label></th>
<td>
<?php if ( ! empty($mtss_api_key)): ?>
<select name="mtss_service_id" id="mtss_service_id">
<option value="">Select a service</option>
<?php foreach ($services as $s): ?>
<option value="<?php echo $s->id; ?>"<?php if ($mtss_service_id == $s->id) echo ' selected="selected"'; ?>><?php echo $s->primaryDomain; ?> - <?php echo $s->serviceTypeName; ?></option>
<?php endforeach; ?>
</select>
<?php else: ?>
<select name="mtss_service_id" id="mtss_service_id" disabled="disabled">
<option>Please enter an API Key first</option>
</select>
<?php endif; ?>
</td>
</tr>
</tbody>
</table>
<p class="submit"><input type="submit" name="submit" id="submit" class="button-primary" value="Save Changes"></p>
</form>
</div>
<?php
}

// Simple uninstall hook
function mtss_uninstall_hook() {
delete_option( 'mtss_api_key' );
delete_option( 'mtss_service_id' );
}
Something went wrong with that request. Please try again.