Subversion Repositories ALCASAR

Rev

Rev 325 | Rev 3037 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
2770 rexy 1
<?php
2
/**
3
 * FreeBSD System Class
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI FreeBSD OS class
9
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
10
 * @copyright 2009 phpSysInfo
11
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
12
 * @version   SVN: $Id: class.FreeBSD.inc.php 696 2012-09-09 11:24:04Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * FreeBSD sysinfo class
17
 * get all the required information from FreeBSD system
18
 *
19
 * @category  PHP
20
 * @package   PSI FreeBSD OS class
21
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
22
 * @copyright 2009 phpSysInfo
23
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
24
 * @version   Release: 3.0
25
 * @link      http://phpsysinfo.sourceforge.net
26
 */
27
class FreeBSD extends BSDCommon
28
{
29
    /**
30
     * define the regexp for log parser
31
     */
32
    public function __construct($blockname = false)
33
    {
34
        parent::__construct($blockname);
35
        $this->setCPURegExp1("/CPU: (.*) \((.*)-MHz (.*)\)/");
36
        $this->setCPURegExp2("/(.*) ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)/");
37
        $this->setSCSIRegExp1("/^(.*): <(.*)> .*SCSI.*device/");
38
        $this->setSCSIRegExp2("/^(da[0-9]+): (.*)MB /");
39
        $this->setSCSIRegExp3("/^(da[0-9]+|cd[0-9]+): Serial Number (.*)/");
40
        $this->setPCIRegExp1("/(.*): <(.*)>(.*) pci[0-9]+$/");
41
        $this->setPCIRegExp2("/(.*): <(.*)>.* at [.0-9]+ irq/");
42
    }
325 richard 43
 
2770 rexy 44
    /**
45
     * UpTime
46
     * time the system is running
47
     *
48
     * @return void
49
     */
50
    private function _uptime()
51
    {
52
        $s = preg_split('/ /', $this->grabkey('kern.boottime'));
53
        $a = preg_replace('/,/', '', $s[3]);
54
        $this->sys->setUptime(time() - $a);
55
    }
325 richard 56
 
2770 rexy 57
    /**
58
     * get network information
59
     *
60
     * @return void
61
     */
62
    private function _network()
63
    {
64
        $dev = null;
65
        if (CommonFunctions::executeProgram('netstat', '-nibd', $netstat, PSI_DEBUG)) {
66
            $lines = preg_split("/\n/", $netstat, -1, PREG_SPLIT_NO_EMPTY);
67
            foreach ($lines as $line) {
68
                $ar_buf = preg_split("/\s+/", $line);
69
                if (!empty($ar_buf[0])) {
70
                    if (preg_match('/^<Link/i', $ar_buf[2])) {
71
                        $dev = new NetDevice();
72
                        $dev->setName($ar_buf[0]);
73
                        if ((strlen($ar_buf[3]) < 17) && ($ar_buf[0] != $ar_buf[3])) { /* no MAC or dev name*/
74
                            if (isset($ar_buf[11]) && (trim($ar_buf[11]) != '')) { /* Idrop column exist*/
75
                              $dev->setTxBytes($ar_buf[9]);
76
                              $dev->setRxBytes($ar_buf[6]);
77
                              $dev->setErrors($ar_buf[4] + $ar_buf[8]);
78
                              $dev->setDrops($ar_buf[11] + $ar_buf[5]);
79
                            } else {
80
                              $dev->setTxBytes($ar_buf[8]);
81
                              $dev->setRxBytes($ar_buf[5]);
82
                              $dev->setErrors($ar_buf[4] + $ar_buf[7]);
83
                              $dev->setDrops($ar_buf[10]);
84
                            }
85
                        } else {
86
                            if (isset($ar_buf[12]) && (trim($ar_buf[12]) != '')) { /* Idrop column exist*/
87
                              $dev->setTxBytes($ar_buf[10]);
88
                              $dev->setRxBytes($ar_buf[7]);
89
                              $dev->setErrors($ar_buf[5] + $ar_buf[9]);
90
                              $dev->setDrops($ar_buf[12] + $ar_buf[6]);
91
                            } else {
92
                              $dev->setTxBytes($ar_buf[9]);
93
                              $dev->setRxBytes($ar_buf[6]);
94
                              $dev->setErrors($ar_buf[5] + $ar_buf[8]);
95
                              $dev->setDrops($ar_buf[11]);
96
                            }
97
                        }
98
                        if (defined('PSI_SHOW_NETWORK_INFOS') && (PSI_SHOW_NETWORK_INFOS) && (CommonFunctions::executeProgram('ifconfig', $ar_buf[0].' 2>/dev/null', $bufr2, PSI_DEBUG))) {
99
                            $bufe2 = preg_split("/\n/", $bufr2, -1, PREG_SPLIT_NO_EMPTY);
100
                            foreach ($bufe2 as $buf2) {
101
                                if (preg_match('/^\s+ether\s+(\S+)/i', $buf2, $ar_buf2)) {
102
                                    if (!defined('PSI_HIDE_NETWORK_MACADDR') || !PSI_HIDE_NETWORK_MACADDR) $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').preg_replace('/:/', '-', strtoupper($ar_buf2[1])));
103
                                } elseif (preg_match('/^\s+inet\s+(\S+)\s+netmask/i', $buf2, $ar_buf2)) {
104
                                    $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$ar_buf2[1]);
105
                                } elseif ((preg_match('/^\s+inet6\s+([^\s%]+)\s+prefixlen/i', $buf2, $ar_buf2)
106
                                      || preg_match('/^\s+inet6\s+([^\s%]+)%\S+\s+prefixlen/i', $buf2, $ar_buf2))
107
                                      && ($ar_buf2[1]!="::") && !preg_match('/^fe80::/i', $ar_buf2[1])) {
108
                                    $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').strtolower($ar_buf2[1]));
109
                                } elseif (preg_match('/^\s+media:\s+/i', $buf2) && preg_match('/[\(\s](\d+)(G*)base/i', $buf2, $ar_buf2)) {
110
                                    if (isset($ar_buf2[2]) && strtoupper($ar_buf2[2])=="G") {
111
                                        $unit = "G";
112
                                    } else {
113
                                        $unit = "M";
114
                                    }
115
                                    if (preg_match('/[<\s]([^\s<]+)-duplex/i', $buf2, $ar_buf3))
116
                                        $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$ar_buf2[1].$unit.'b/s '.strtolower($ar_buf3[1]));
117
                                    else
118
                                        $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$ar_buf2[1].$unit.'b/s');
119
                                }
120
                            }
121
                        }
122
                        $this->sys->setNetDevices($dev);
123
                    }
124
                }
125
            }
126
        }
127
    }
325 richard 128
 
2770 rexy 129
    /**
130
     * get icon name and distro extended check
131
     *
132
     * @return void
133
     */
134
    private function _distroicon()
135
    {
136
        if (extension_loaded('pfSense') && CommonFunctions::rfts('/etc/version', $version, 1, 4096, false) && (trim($version) != '')) { // pfSense detection
137
            $this->sys->setDistribution('pfSense '. trim($version));
138
            $this->sys->setDistributionIcon('pfSense.png');
325 richard 139
        } else {
2770 rexy 140
            $this->sys->setDistributionIcon('FreeBSD.png');
141
        }
142
    }
325 richard 143
 
2770 rexy 144
    /**
145
     * extend the memory information with additional values
146
     *
147
     * @return void
148
     */
149
    private function _memoryadditional()
150
    {
151
        $pagesize = $this->grabkey("hw.pagesize");
152
        $this->sys->setMemCache($this->grabkey("vm.stats.vm.v_cache_count") * $pagesize);
153
        $this->sys->setMemApplication(($this->grabkey("vm.stats.vm.v_active_count") + $this->grabkey("vm.stats.vm.v_wire_count")) * $pagesize);
154
        $this->sys->setMemBuffer($this->sys->getMemUsed() - $this->sys->getMemApplication() - $this->sys->getMemCache());
155
    }
325 richard 156
 
2770 rexy 157
    /**
158
     * Processes
159
     *
160
     * @return void
161
     */
162
    protected function _processes()
163
    {
164
        if (CommonFunctions::executeProgram('ps', 'aux', $bufr, PSI_DEBUG)) {
165
            $lines = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
166
            $processes['*'] = 0;
167
            foreach ($lines as $line) {
168
                if (preg_match("/^\S+\s+\d+\s+\S+\s+\S+\s+\d+\s+\d+\s+\S+\s+(\w)/", $line, $ar_buf)) {
169
                    $processes['*']++;
170
                    $state = $ar_buf[1];
171
                    if ($state == 'L') $state = 'D'; //linux format
172
                    elseif ($state == 'I') $state = 'S';
173
                    if (isset($processes[$state])) {
174
                        $processes[$state]++;
175
                    } else {
176
                        $processes[$state] = 1;
177
                    }
178
                }
179
            }
180
            if ($processes['*'] > 0) {
181
                $this->sys->setProcesses($processes);
182
            }
183
        }
184
    }
325 richard 185
 
2770 rexy 186
    /**
187
     * get the information
188
     *
189
     * @see BSDCommon::build()
190
     *
191
     * @return Void
192
     */
193
    public function build()
194
    {
195
        parent::build();
196
        if (!$this->blockname || $this->blockname==='vitals') {
197
            $this->_distroicon();
198
            $this->_uptime();
199
            $this->_processes();
200
        }
201
        if (!$this->blockname || $this->blockname==='network') {
202
            $this->_network();
203
        }
204
        if (!$this->blockname || $this->blockname==='memory') {
205
            $this->_memoryadditional();
206
        }
207
    }
208
}