system.inc 6.75 KB
Newer Older
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
<?php
/*
    Copyright (C) 2016 Deciso B.V.
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.

    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/

function system_api_cpu_stats()
{
    $cpustats = array();
    // take a short snapshot to calculate cpu usage
    $diff = array('user', 'nice', 'sys', 'intr', 'idle');
    $cpuTicks1 = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
    usleep(100000);
    $cpuTicks2 = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
    $totalStart = array_sum($cpuTicks1);
    $totalEnd = array_sum($cpuTicks2);
    if ($totalEnd <= $totalStart) {
        // if for some reason the measurement is invalid, assume nothing has changed (all 0)
        $totalEnd = $totalStart;
    }
    $cpustats['used'] = floor(100 * (($totalEnd - $totalStart) - ($cpuTicks2['idle'] - $cpuTicks1['idle'])) / ($totalEnd - $totalStart));
    $cpustats['user'] = floor(100 * (($cpuTicks2['user'] - $cpuTicks1['user'])) / ($totalEnd - $totalStart));
    $cpustats['nice'] = floor(100 * (($cpuTicks2['nice'] - $cpuTicks1['nice'])) / ($totalEnd - $totalStart));
    $cpustats['sys'] = floor(100 * (($cpuTicks2['sys'] - $cpuTicks1['sys'])) / ($totalEnd - $totalStart));
    $cpustats['intr'] = floor(100 * (($cpuTicks2['intr'] - $cpuTicks1['intr'])) / ($totalEnd - $totalStart));
    $cpustats['idle'] = floor(100 * (($cpuTicks2['idle'] - $cpuTicks1['idle'])) / ($totalEnd - $totalStart));

    // cpu model and count
    $cpustats['model'] = get_single_sysctl("hw.model");
    $cpustats['cpus'] = get_single_sysctl('kern.smp.cpus');

    // cpu frequency
    $tmp = get_single_sysctl('dev.cpu.0.freq_levels');
    $cpustats['max.freq'] = !empty($tmp) ? explode("/", explode(" ", $tmp)[0])[0] : "-";
    $tmp = get_single_sysctl('dev.cpu.0.freq');
    $cpustats['cur.freq'] = !empty($tmp) ? $tmp : "-";
    $cpustats['freq_translate'] = sprintf(gettext("Current: %s MHz, Max: %s MHz"), $cpustats['cur.freq'], $cpustats['max.freq']);

60 61 62 63
    // system load
    exec("/usr/bin/uptime | /usr/bin/sed 's/^.*: //'", $load_average);
    $cpustats['load'] = explode(',', $load_average[0]);

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    return $cpustats;
}

function system_api_config()
{
    global $config;
    $result = array();
    $result['last_change'] = isset($config['revision']['time']) ? intval($config['revision']['time']) : 0;
    $result['last_change_frmt'] = date("D M j G:i:s T Y", $result['last_change']);
    return $result;
}

function system_api_kernel()
{
    global $config;
79 80 81
    $result = array();

    $result['pf'] = array();
82 83 84
    $result['pf']['maxstates'] = !empty($config['system']['maximumstates']) ? $config['system']['maximumstates'] : default_state_size();
    exec('/sbin/pfctl -si |grep "current entries" 2>/dev/null', $states);
    $result['pf']['states'] = count($states) >  0 ? filter_var($states[0], FILTER_SANITIZE_NUMBER_INT) : 0;
85 86 87 88 89 90 91 92 93 94 95 96 97

    $result['mbuf'] = array();
    exec('/usr/bin/netstat -mb | /usr/bin/grep "mbuf clusters in use"', $mbufs);
    $result['mbuf']['total'] = count($mbufs) > 0 ? explode('/', $mbufs[0])[2] : 0;
    $result['mbuf']['max'] = count($mbufs) > 0 ? explode(' ', explode('/', $mbufs[0])[3])[0] : 0;

    $totalMem = get_single_sysctl("vm.stats.vm.v_page_count");
    $inactiveMem = get_single_sysctl("vm.stats.vm.v_inactive_count");
    $cachedMem = get_single_sysctl("vm.stats.vm.v_cache_count");
    $freeMem = get_single_sysctl("vm.stats.vm.v_free_count");
    $result['memory']['total'] = get_single_sysctl('hw.physmem') ;
    $result['memory']['used'] = round(((($totalMem - ($inactiveMem + $cachedMem + $freeMem))) / $totalMem)*$result['memory']['total'], 0);

98 99 100
    return $result;
}

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
function system_api_disk()
{
    $result = array();
    $result['swap'] = array();
    $result['swap']['device'] = null;
    $result['swap']['total'] = null;
    $result['swap']['used'] = null;
    exec("/usr/sbin/swapinfo -k", $swap_info);
    foreach ($swap_info as $line) {
        if (strpos($line,'/dev/') !== false) {
            $parts = preg_split('/\s+/', $line);
            $result['swap']['device'] = $parts[0];
            $result['swap']['total'] = $parts[1];
            $result['swap']['used'] = $parts[2];
        }
    }

    $result['devices'] = array();
    exec("/bin/df -Tht ufs,tmpfs,zfs,cd9660", $disk_info);
    foreach ($disk_info as $line) {
121
        if (strpos($line,'Mounted on') === false) {
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
            $parts = preg_split('/\s+/', $line);
            $diskItem = array();
            $diskItem['device'] = $parts[0];
            $diskItem['type'] = $parts[1];
            $diskItem['size'] = $parts[2];
            $diskItem['used'] = $parts[3];
            $diskItem['available'] = $parts[4];
            $diskItem['capacity'] = $parts[5];
            $diskItem['mountpoint'] = $parts[6];
            $result['devices'][] = $diskItem;
        }
    }

    return $result;
}

138 139 140 141 142 143
function system_api_versions()
{
    global $g;

    $result = array();

Franco Fichtner's avatar
Franco Fichtner committed
144
    $result[] = sprintf('%s %s-%s', $g['product_name'], explode('-', trim(file_get_contents('/usr/local/opnsense/version/opnsense')))[0], trim(shell_exec('uname -p')));
145 146 147 148 149 150
    $result[] = php_uname('s') . ' ' . php_uname('r');
    $result[] = exec('/usr/local/bin/openssl version');

    return $result;
}

151 152 153 154 155 156 157
/**
 * widget system data
 */
function system_api()
{
    $result = array();

158
    $result['versions'] = system_api_versions();
159 160 161 162 163 164
    $result['cpu'] = system_api_cpu_stats();
    preg_match("/sec = (\d+)/", get_single_sysctl("kern.boottime"), $matches);
    $result['uptime'] = time() - $matches[1];
    $result['date_frmt'] = date("D M j G:i:s T Y");
    $result['config'] = system_api_config();
    $result['kernel'] = system_api_kernel();
165
    $result['disk'] = system_api_disk();
166 167 168

    return $result;
}