Esempio n. 1
0
/**
 * Windows is a bit special
 *
 * @global type $context
 */
function get_windows_data()
{
    global $context;
    // Time is easy
    $context['current_time'] = strftime('%B %d, %Y, %I:%M:%S %p');
    // Sysinfo, windows style
    $systeminfo = @`systeminfo /fo csv`;
    if (!empty($systeminfo)) {
        $systeminfo = explode("\n", $systeminfo);
        $headings = explode('","', substr($systeminfo[0], 1, -1));
        $values = explode('","', substr($systeminfo[1], 1, -1));
        // CPU
        $context['cpu_info'] = array();
        if ($i = array_search('Processor(s)', $headings)) {
            if (preg_match('~\\[01\\]: (.+?) (\\~?\\d+) Mhz$~i', $values[$i], $match) != 0) {
                $context['cpu_info']['model'] = $match[1];
                $context['cpu_info']['hz'] = $match[2];
                $context['cpu_info']['frequency'] = '';
            }
        }
        // Memory
        $context['memory_usage'] = array();
        if ($i = array_search('Total Physical Memory', $headings)) {
            $context['memory_usage']['total'] = windows_memsize($values[$i]);
        }
        if ($i = array_search('Available Physical Memory', $headings)) {
            $context['memory_usage']['free'] = windows_memsize($values[$i]);
        }
        if (isset($context['memory_usage']['total'], $context['memory_usage']['free'])) {
            $context['memory_usage']['used'] = $context['memory_usage']['total'] - $context['memory_usage']['free'];
        }
        if ($i = array_search('Virtual Memory: Available', $headings)) {
            $context['memory_usage']['swap_total'] = windows_memsize($values[$i]);
        }
        if ($i = array_search('Virtual Memory: In Use', $headings)) {
            $context['memory_usage']['swap_used'] = windows_memsize($values[$i]);
        }
        if (isset($context['memory_usage']['swap_total'], $context['memory_usage']['swap_free'])) {
            $context['memory_usage']['swap_free'] = $context['memory_usage']['swap_total'] - $context['memory_usage']['swap_used'];
        }
    }
    // Version, name, etc
    $context['operating_system']['type'] = 'windows';
    $context['operating_system']['name'] = `ver`;
    if (empty($context['operating_system']['name'])) {
        $context['operating_system']['name'] = 'Microsoft Windows';
    }
    // Tasklist for processes
    $context['running_processes'] = array();
    $processes = @`tasklist /fo csv /v /nh`;
    if (!empty($processes)) {
        $processes = explode("\n", $processes);
        $total_mem = 0;
        $total_cpu = 0;
        $context['num_zombie_processes'] = 0;
        $context['num_sleeping_processes'] = 0;
        $context['num_running_processes'] = 0;
        // Convert the tasklist to a nice summary
        foreach ($processes as $proc) {
            if (empty($proc)) {
                continue;
            }
            $proc = explode('","', substr($proc, 1, -1));
            $proc[7] = explode(':', $proc[7]);
            $proc[7] = $proc[7][0] * 3600 + $proc[7][1] * 60 + $proc[7][2];
            if (substr($proc[4], -1) == 'K') {
                $proc[4] = (int) $proc[4];
            } elseif (substr($proc[4], -1) == 'M') {
                $proc[4] = $proc[4] * 1024;
            } elseif (substr($proc[4], -1) == 'G') {
                $proc[4] = $proc[4] * 1024 * 1024;
            } else {
                $proc[4] = $proc[4] / 1024;
            }
            $context['running_processes'][$proc[1]] = array('id' => $proc[1], 'cpu_time' => $proc[7], 'mem_usage' => $proc[4], 'title' => $proc[0]);
            if (strpos($proc[5], 'Not') !== false) {
                $context['num_zombie_processes']++;
            } else {
                $context['num_running_processes']++;
            }
            $total_mem += $proc[4];
            $total_cpu += $proc[7];
        }
        foreach ($context['running_processes'] as $proc) {
            $context['running_processes'][$proc['id']]['cpu'] = $proc['cpu_time'] * 100 / $total_cpu;
            $context['running_processes'][$proc['id']]['mem'] = $proc['mem_usage'] * 100 / $total_mem;
        }
        $context['top_memory_usage'] = array('(other)' => array('name' => '(other)', 'percent' => 0, 'number' => 0));
        $context['top_cpu_usage'] = array('(other)' => array('name' => '(other)', 'percent' => 0, 'number' => 0));
        foreach ($context['running_processes'] as $proc) {
            $id = basename($proc['title']);
            if (!isset($context['top_memory_usage'][$id])) {
                $context['top_memory_usage'][$id] = array('name' => $id, 'percent' => $proc['mem'], 'number' => 1);
            } else {
                $context['top_memory_usage'][$id]['percent'] += $proc['mem'];
                $context['top_memory_usage'][$id]['number']++;
            }
            if (!isset($context['top_cpu_usage'][$id])) {
                $context['top_cpu_usage'][$id] = array('name' => $id, 'percent' => $proc['cpu'], 'number' => 1);
            } else {
                $context['top_cpu_usage'][$id]['percent'] += $proc['cpu'];
                $context['top_cpu_usage'][$id]['number']++;
            }
        }
        // @todo shared memory?
        foreach ($context['top_memory_usage'] as $proc) {
            if ($proc['percent'] >= 1 || $proc['name'] == '(other)') {
                continue;
            }
            unset($context['top_memory_usage'][$proc['name']]);
            $context['top_memory_usage']['(other)']['percent'] += $proc['percent'];
            $context['top_memory_usage']['(other)']['number']++;
        }
        foreach ($context['top_cpu_usage'] as $proc) {
            if ($proc['percent'] >= 0.6 || $proc['name'] == '(other)') {
                continue;
            }
            unset($context['top_cpu_usage'][$proc['name']]);
            $context['top_cpu_usage']['(other)']['percent'] += $proc['percent'];
            $context['top_cpu_usage']['(other)']['number']++;
        }
    }
}
Esempio n. 2
0
function get_windows_data()
{
    global $context;
    $context['current_time'] = strftime('%B %d, %Y, %I:%M:%S %p');
    function windows_memsize($str)
    {
        $str = strtr($str, array(',' => ''));
        if (strtolower(substr($str, -2)) == 'gb') {
            return $str * 1024 * 1024;
        } elseif (strtolower(substr($str, -2)) == 'mb') {
            return $str * 1024;
        } elseif (strtolower(substr($str, -2)) == 'kb') {
            return (int) $str;
        } elseif (strtolower(substr($str, -2)) == ' b') {
            return $str / 1024;
        } else {
            trigger_error('Unknown memory format \'' . $str . '\'', E_USER_NOTICE);
        }
    }
    $systeminfo = @`systeminfo /fo csv`;
    if (!empty($systeminfo)) {
        $systeminfo = explode("\n", $systeminfo);
        $headings = explode('","', substr($systeminfo[1], 1, -1));
        $values = explode('","', substr($systeminfo[2], 1, -1));
        $context['cpu_info'] = array();
        if ($i = array_search('Processor(s)', $headings)) {
            if (preg_match('~\\[01\\]: (.+?) (\\~?\\d+) Mhz$~i', $values[$i], $match) != 0) {
                $context['cpu_info']['model'] = $match[1];
                $context['cpu_info']['hz'] = $match[2];
            }
        }
        $context['memory_usage'] = array();
        if ($i = array_search('Total Physical Memory', $headings)) {
            $context['memory_usage']['total'] = windows_memsize($values[$i]);
        }
        if ($i = array_search('Available Physical Memory', $headings)) {
            $context['memory_usage']['free'] = windows_memsize($values[$i]);
        }
        if (isset($context['memory_usage']['total'], $context['memory_usage']['free'])) {
            $context['memory_usage']['used'] = $context['memory_usage']['total'] - $context['memory_usage']['free'];
        }
        if ($i = array_search('Virtual Memory: Available', $headings)) {
            $context['memory_usage']['swap_total'] = windows_memsize($values[$i]);
        }
        if ($i = array_search('Virtual Memory: In Use', $headings)) {
            $context['memory_usage']['swap_used'] = windows_memsize($values[$i]);
        }
        if (isset($context['memory_usage']['swap_total'], $context['memory_usage']['swap_free'])) {
            $context['memory_usage']['swap_free'] = $context['memory_usage']['swap_total'] - $context['memory_usage']['swap_used'];
        }
    }
    $context['operating_system']['type'] = 'windows';
    $context['operating_system']['name'] = `ver`;
    if (empty($context['operating_system']['name'])) {
        $context['operating_system']['name'] = 'Microsoft Windows';
    }
    $context['running_processes'] = array();
    $processes = @`tasklist /fo csv /v /nh`;
    if (!empty($processes)) {
        $processes = explode("\n", $processes);
        $total_mem = 0;
        $total_cpu = 0;
        $context['num_zombie_processes'] = 0;
        $context['num_sleeping_processes'] = 0;
        $context['num_running_processes'] = 0;
        foreach ($processes as $proc) {
            if (empty($proc)) {
                continue;
            }
            $proc = explode('","', substr($proc, 1, -1));
            $proc[7] = explode(':', $proc[7]);
            $proc[7] = $proc[7][0] * 3600 + $proc[7][1] * 60 + $proc[7][2];
            if (substr($proc[4], -1) == 'K') {
                $proc[4] = (int) $proc[4];
            } elseif (substr($proc[4], -1) == 'M') {
                $proc[4] = $proc[4] * 1024;
            } elseif (substr($proc[4], -1) == 'G') {
                $proc[4] = $proc[4] * 1024 * 1024;
            } else {
                $proc[4] = $proc[4] / 1024;
            }
            $context['running_processes'][$proc[1]] = array('id' => $proc[1], 'cpu_time' => $proc[7], 'mem_usage' => $proc[4], 'title' => $proc[0]);
            if (strpos($proc[5], 'Not') !== false) {
                $context['num_zombie_processes']++;
            } else {
                $context['num_running_processes']++;
            }
            $total_mem += $proc[4];
            $total_cpu += $proc[7];
        }
        foreach ($context['running_processes'] as $proc) {
            $context['running_processes'][$proc['id']]['cpu'] = $proc['cpu_time'] * 100 / $total_cpu;
            $context['running_processes'][$proc['id']]['mem'] = $proc['mem_usage'] * 100 / $total_mem;
        }
        $context['top_memory_usage'] = array('(other)' => array('name' => '(other)', 'percent' => 0, 'number' => 0));
        $context['top_cpu_usage'] = array('(other)' => array('name' => '(other)', 'percent' => 0, 'number' => 0));
        foreach ($context['running_processes'] as $proc) {
            $id = basename($proc['title']);
            if (!isset($context['top_memory_usage'][$id])) {
                $context['top_memory_usage'][$id] = array('name' => $id, 'percent' => $proc['mem'], 'number' => 1);
            } else {
                $context['top_memory_usage'][$id]['percent'] += $proc['mem'];
                $context['top_memory_usage'][$id]['number']++;
            }
            if (!isset($context['top_cpu_usage'][$id])) {
                $context['top_cpu_usage'][$id] = array('name' => $id, 'percent' => $proc['cpu'], 'number' => 1);
            } else {
                $context['top_cpu_usage'][$id]['percent'] += $proc['cpu'];
                $context['top_cpu_usage'][$id]['number']++;
            }
        }
        // TODO: shared memory?
        foreach ($context['top_memory_usage'] as $proc) {
            if ($proc['percent'] >= 1 || $proc['name'] == '(other)') {
                continue;
            }
            unset($context['top_memory_usage'][$proc['name']]);
            $context['top_memory_usage']['(other)']['percent'] += $proc['percent'];
            $context['top_memory_usage']['(other)']['number']++;
        }
        foreach ($context['top_cpu_usage'] as $proc) {
            if ($proc['percent'] >= 0.6 || $proc['name'] == '(other)') {
                continue;
            }
            unset($context['top_cpu_usage'][$proc['name']]);
            $context['top_cpu_usage']['(other)']['percent'] += $proc['percent'];
            $context['top_cpu_usage']['(other)']['number']++;
        }
    }
}