Vigiar
1
0
Derivar
Você já tinha feito uma derivação do repositório wordpress-status-plugin, anteriormente
0
wordpress-status-plugin/mozout-status/includes/class-monitor.php
2026-12-01 14:09:10 +02:00

230 linhas
10 KiB
PHP

<?php
if (!defined('ABSPATH')) {
exit;
}
/**
* Reads the state of what the site shows: the API (GET /v1/ping, no key), hosting (GET /v1/services),
* servers (GET /v1/servers) and domains (GET /v1/domains). One list call per type, kept for a short time,
* so many visitors never mean many API calls.
*/
class MOZOUT_STATUS_Monitor
{
const TYPES = array('api', 'hosting', 'vps', 'domain');
/** Everything the account has, for the Items tab. @return array type => list of array(id, name, detail) */
public static function catalog($fresh = false)
{
$hit = $fresh ? false : get_transient('mozout_status_catalog');
if (is_array($hit)) {
return $hit;
}
$out = array('hosting' => array(), 'vps' => array(), 'domain' => array(), 'errors' => array());
$res = self::all_pages('/v1/services');
if (is_wp_error($res)) {
$out['errors']['hosting'] = $res->get_error_message();
} else {
foreach ($res as $s) {
$type = isset($s['product']['type']) ? (string) $s['product']['type'] : '';
if (in_array($type, array('server', 'vps'), true)) {
continue;
}
if (in_array(isset($s['status']) ? strtolower($s['status']) : '', array('terminated', 'cancelled', 'fraud'), true)) {
continue;
}
$out['hosting'][] = array(
'id' => (string) $s['id'],
'name' => !empty($s['domain']) ? (string) $s['domain'] : (isset($s['product']['name']) ? (string) $s['product']['name'] : '#' . $s['id']),
'detail' => isset($s['product']['name']) ? (string) $s['product']['name'] : '',
);
}
}
$res = self::all_pages('/v1/servers');
if (is_wp_error($res)) {
$out['errors']['vps'] = $res->get_error_message();
} else {
foreach ($res as $s) {
$out['vps'][] = array('id' => (string) $s['id'], 'name' => isset($s['name']) && $s['name'] !== '' ? (string) $s['name'] : '#' . $s['id'], 'detail' => isset($s['product']) ? (string) $s['product'] : '');
}
// Servers also appear in the services list: keep them only here.
$vps_ids = wp_list_pluck($out['vps'], 'id');
$out['hosting'] = array_values(array_filter($out['hosting'], function ($h) use ($vps_ids) {
return !in_array($h['id'], $vps_ids, true);
}));
}
$res = self::all_pages('/v1/domains');
if (is_wp_error($res)) {
$out['errors']['domain'] = $res->get_error_message();
} else {
foreach ($res as $d) {
if (in_array(isset($d['status']) ? strtolower($d['status']) : '', array('transferred_away', 'cancelled', 'fraud'), true)) {
continue;
}
$out['domain'][] = array('id' => (string) $d['domain'], 'name' => (string) $d['domain'], 'detail' => isset($d['expires_at']) ? (string) $d['expires_at'] : '');
}
}
set_transient('mozout_status_catalog', $out, 10 * MINUTE_IN_SECONDS);
return $out;
}
/** Up to 5 pages of 100. @return array|WP_Error */
private static function all_pages($path)
{
$rows = array();
for ($page = 1; $page <= 5; $page++) {
$res = MOZOUT_STATUS_Api::request('GET', $path, null, array('query' => array('page' => $page, 'per_page' => 100), 'timeout' => 15));
if (is_wp_error($res)) {
return $page === 1 ? $res : $rows;
}
foreach ((array) $res['data'] as $r) {
$rows[] = $r;
}
$pages = isset($res['meta']['total_pages']) ? (int) $res['meta']['total_pages'] : 1;
if ($page >= $pages) {
break;
}
}
return $rows;
}
/** The items chosen in the settings, in their order. */
public static function chosen()
{
$items = MOZOUT_STATUS_Settings::get('items');
return is_array($items) ? $items : array();
}
/**
* Current state of the chosen items, kept for the refresh time (at least 30 seconds).
* Each item: label, type, state (ok, warn, bad, unknown), text.
*/
public static function snapshot($types = array(), $lang = 'en')
{
$ttl = max(30, (int) MOZOUT_STATUS_Settings::get('cache_seconds'));
$key = 'mozout_status_snap_' . md5(MOZOUT_STATUS_Brands::current_id() . '|' . $lang . '|' . implode(',', $types) . '|' . wp_json_encode(self::chosen()));
$hit = get_transient($key);
if (is_array($hit)) {
return $hit;
}
$want = array();
foreach (self::chosen() as $it) {
if (!$types || in_array($it['type'], $types, true)) {
$want[] = $it;
}
}
$need = array_unique(wp_list_pluck($want, 'type'));
$data = array();
if (in_array('hosting', $need, true)) {
$data['hosting'] = self::index(self::all_pages('/v1/services'), 'id');
}
if (in_array('vps', $need, true)) {
$data['vps'] = self::index(self::all_pages('/v1/servers'), 'id');
}
if (in_array('domain', $need, true)) {
$data['domain'] = self::index(self::all_pages('/v1/domains'), 'domain');
}
$t = function ($s) use ($lang) {
return MOZOUT_STATUS_I18n::in($lang, $s);
};
$out = array();
foreach ($want as $it) {
$out[] = self::state($it, $data, $t);
}
$bad = count(array_filter($out, function ($o) {
return $o['state'] === 'bad';
}));
$warn = count(array_filter($out, function ($o) {
return $o['state'] === 'warn' || $o['state'] === 'unknown';
}));
$snap = array(
'items' => $out,
'overall' => $bad ? 'bad' : ($warn ? 'warn' : 'ok'),
'checked' => time(),
);
set_transient($key, $snap, $ttl);
return $snap;
}
private static function index($rows, $field)
{
if (is_wp_error($rows)) {
return $rows;
}
$out = array();
foreach ((array) $rows as $r) {
if (isset($r[$field])) {
$out[strtolower((string) $r[$field])] = $r;
}
}
return $out;
}
private static function state(array $it, array $data, $t)
{
$o = array('label' => $it['label'] !== '' ? $it['label'] : $it['id'], 'type' => $it['type'], 'state' => 'unknown', 'text' => $t('Could not check now'));
if ($it['type'] === 'api') {
$start = microtime(true);
$res = MOZOUT_STATUS_Api::request('GET', '/v1/ping', null, array('timeout' => 8, 'public' => true));
$ms = (int) round((microtime(true) - $start) * 1000);
if (!is_wp_error($res) && isset($res['data']['status']) && $res['data']['status'] === 'ok') {
$o['state'] = $ms > 3000 ? 'warn' : 'ok';
$o['text'] = $ms > 3000 ? $t('Slow') : $t('Working');
} else {
$o['state'] = 'bad';
$o['text'] = $t('Not answering');
}
return $o;
}
$list = isset($data[$it['type']]) ? $data[$it['type']] : null;
if (!is_array($list)) {
return $o;
}
$row = isset($list[strtolower($it['id'])]) ? $list[strtolower($it['id'])] : null;
if (!$row) {
$o['text'] = $t('Not found in the account');
return $o;
}
$status = isset($row['status']) ? strtolower((string) $row['status']) : '';
if ($it['type'] === 'hosting') {
$map = array('active' => array('ok', $t('Working')), 'pending' => array('warn', $t('Being set up')), 'suspended' => array('bad', $t('Suspended')),
'terminated' => array('bad', $t('Closed')), 'cancelled' => array('bad', $t('Closed')), 'completed' => array('ok', $t('Working')));
} elseif ($it['type'] === 'vps') {
if ($status === 'suspended') {
$o['state'] = 'bad';
$o['text'] = $t('Suspended');
return $o;
}
$status = isset($row['power_state']) ? strtolower((string) $row['power_state']) : '';
$map = array('running' => array('ok', $t('Running')), 'stopped' => array('bad', $t('Stopped')), 'starting' => array('warn', $t('Starting')),
'stopping' => array('warn', $t('Stopping')), 'restarting' => array('warn', $t('Restarting')), 'reinstalling' => array('warn', $t('Being reinstalled')),
'backing_up' => array('warn', $t('Making a backup')));
} else {
$map = array('active' => array('ok', $t('Active')), 'pending' => array('warn', $t('Pending')), 'pending_registration' => array('warn', $t('Being registered')),
'pending_transfer' => array('warn', $t('Being transferred')), 'grace' => array('bad', $t('Expired, can still be renewed')),
'redemption' => array('bad', $t('Expired, in redemption')), 'expired' => array('bad', $t('Expired')));
}
if (isset($map[$status])) {
$o['state'] = $map[$status][0];
$o['text'] = $map[$status][1];
}
if ($it['type'] === 'domain' && $o['state'] === 'ok' && !empty($row['expires_at'])) {
$days = (int) floor((strtotime((string) $row['expires_at']) - time()) / DAY_IN_SECONDS);
if ($days >= 0 && $days <= 30) {
$o['state'] = 'warn';
$o['text'] = sprintf($days === 1 ? $t('Expires in %d day') : $t('Expires in %d days'), $days);
}
}
return $o;
}
public static function forget()
{
global $wpdb;
delete_transient('mozout_status_catalog');
$wpdb->query($wpdb->prepare(
"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s",
$wpdb->esc_like('_transient_mozout_status_snap_') . '%',
$wpdb->esc_like('_transient_timeout_mozout_status_snap_') . '%'
));
}
}