Subversion Repositories ALCASAR

Rev

Rev 2770 | 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
    {
2976 rexy 136
        if (CommonFunctions::rfts('/etc/version', $version, 1, 4096, false) && (($version=trim($version)) != '')) {
137
            if (extension_loaded('pfSense')) { // pfSense detection
138
                $this->sys->setDistribution('pfSense '. $version);
139
                $this->sys->setDistributionIcon('pfSense.png');
140
            } elseif (preg_match('/^FreeNAS/i', $version)) { // FreeNAS detection
141
                $this->sys->setDistribution($version);
142
                $this->sys->setDistributionIcon('FreeNAS.png');
143
            } else {
144
                $this->sys->setDistributionIcon('FreeBSD.png');
145
            }
325 richard 146
        } else {
2770 rexy 147
            $this->sys->setDistributionIcon('FreeBSD.png');
148
        }
149
    }
325 richard 150
 
2770 rexy 151
    /**
152
     * extend the memory information with additional values
153
     *
154
     * @return void
155
     */
156
    private function _memoryadditional()
157
    {
158
        $pagesize = $this->grabkey("hw.pagesize");
159
        $this->sys->setMemCache($this->grabkey("vm.stats.vm.v_cache_count") * $pagesize);
160
        $this->sys->setMemApplication(($this->grabkey("vm.stats.vm.v_active_count") + $this->grabkey("vm.stats.vm.v_wire_count")) * $pagesize);
161
        $this->sys->setMemBuffer($this->sys->getMemUsed() - $this->sys->getMemApplication() - $this->sys->getMemCache());
162
    }
325 richard 163
 
2770 rexy 164
    /**
165
     * Processes
166
     *
167
     * @return void
168
     */
169
    protected function _processes()
170
    {
171
        if (CommonFunctions::executeProgram('ps', 'aux', $bufr, PSI_DEBUG)) {
172
            $lines = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
173
            $processes['*'] = 0;
174
            foreach ($lines as $line) {
175
                if (preg_match("/^\S+\s+\d+\s+\S+\s+\S+\s+\d+\s+\d+\s+\S+\s+(\w)/", $line, $ar_buf)) {
176
                    $processes['*']++;
177
                    $state = $ar_buf[1];
178
                    if ($state == 'L') $state = 'D'; //linux format
179
                    elseif ($state == 'I') $state = 'S';
180
                    if (isset($processes[$state])) {
181
                        $processes[$state]++;
182
                    } else {
183
                        $processes[$state] = 1;
184
                    }
185
                }
186
            }
187
            if ($processes['*'] > 0) {
188
                $this->sys->setProcesses($processes);
189
            }
190
        }
191
    }
325 richard 192
 
2770 rexy 193
    /**
194
     * get the information
195
     *
196
     * @see BSDCommon::build()
197
     *
198
     * @return Void
199
     */
200
    public function build()
201
    {
202
        parent::build();
203
        if (!$this->blockname || $this->blockname==='vitals') {
204
            $this->_distroicon();
205
            $this->_uptime();
206
            $this->_processes();
207
        }
208
        if (!$this->blockname || $this->blockname==='network') {
209
            $this->_network();
210
        }
211
        if (!$this->blockname || $this->blockname==='memory') {
212
            $this->_memoryadditional();
213
        }
214
    }
215
}