• Hello,
    I want to check if the created subsites are active. (For example, when was the user last logged into the site admin panel? Does the site have traffic? How much traffic does it have? etc.)

    I also want to know how much resource the subsites are using on the server. (CPU, RAM, disk space etc.)

    Is this possible?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Regarding WP subsites, usage stats can be compiled. If you cannot find a plugin to help you do this, it may need to be custom coded, but it is possible.

    Regarding server resources, I’m not 100% sure but likely no, because all traffic is routed through the one WP installation. You could ask your host for confirmation to be certain.

    Here is a custom starter plugin that shows CPU, RAM, WordPress Installation size, and Last login date/time:

    <?php
    /**
     * Plugin Name: Subsite Info Dashboard Widget
     * Plugin URI: https://www.deviant.media
     * Description: Adds a dashboard widget that displays subsite information.
     * Version: 1.0
     * Author: Albert Bretado
     * Author URI: https://www.deviant.media
     */
    
    if (!function_exists('get_dirsize')) {
      function get_dirsize($dir) {
        $size = 0;
        $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
        while ($it->valid()) {
          if (!$it->isDot()) {
            $size += $it->getSize();
          }
          $it->next();
        }
        return $size;
      }
    }
    
    function system_info_dashboard_widget() {
      global $wpdb;
      
      $blogs = $wpdb->get_results("SELECT blog_id FROM {$wpdb->blogs}", ARRAY_A);
      
      foreach ($blogs as $blog) {
        switch_to_blog($blog['blog_id']);
        
        $load = sys_getloadavg();
        $cpu = round($load[0], 2);
        $ram = round((memory_get_usage() / 1024 / 1024), 2);
        
        $wordpress_dir = ABSPATH;
        $wordpress_size = round((get_dirsize($wordpress_dir) / 1024 / 1024), 2);
        
        $last_login = '';
        $admins = get_users(array('role' => 'administrator'));
        foreach ($admins as $admin) {
          $last_login_timestamp = get_user_meta($admin->ID, 'last_login', true);
          if ($last_login_timestamp !== '') {
            $last_login = date('Y-m-d H:i:s', $last_login_timestamp);
            break;
          }
        }
        if ($last_login == '') {
          $last_login = 'N/A';
        }
        ?>
        <div class="system-info">
          <h2><?php echo get_bloginfo('name'); ?></h2>
          <p><strong>CPU Usage:</strong> <?php echo $cpu; ?>%</p>
          <p><strong>RAM Usage:</strong> <?php echo $ram; ?> MB</p>
          <p><strong>WordPress Size:</strong> <?php echo $wordpress_size; ?>MB</p>
          <p><strong>Last Login:</strong> <?php echo $last_login; ?></p>
        </div>
        <?php
        restore_current_blog();
      }
    }
    
    function set_last_login($user_login) {
      $user = get_user_by('login', $user_login);
      $user_id = $user->ID;
      update_user_meta($user_id, 'last_login', current_time('timestamp'));
    }
    
    function add_system_info_dashboard_widget() {
      if (is_network_admin()) {
        wp_add_dashboard_widget('system_info_dashboard_widget', 'System Info', 'system_info_dashboard_widget');
      }
    }
    add_action('wp_login', 'set_last_login', 10, 2);
    add_action('wp_network_dashboard_setup', 'add_system_info_dashboard_widget');

    Feel free to adjust as you see fit.

    Thread Starter birblogcu

    (@birblogcu)

    @abretado1985

    Thank you very much for this.
    The last login works but the resource usages give the same value for each site. For example, all sub-sites appear as 500 mb, and I think these resource usages are the main site’s resource usage.

    @bcworkz

    Thank you for your reply. Do you know of a plugin for this?

    Moderator bcworkz

    (@bcworkz)

    You could try some of these plugins. I’m not that confident their stats are broken out by subsite.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Checking the activity of subsites’ is closed to new replies.