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
 * ipmitool sensor class, getting information from ipmitool
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI_Sensor
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   Release: 3.0
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
class IPMItool extends Sensors
16
{
17
    /**
18
     * content to parse
19
     *
20
     * @var array
21
     */
22
    private $_buf = array();
23
 
24
    /**
25
     * fill the private content var through command or data access
26
     */
27
    public function __construct()
28
    {
29
        parent::__construct();
30
        $lines = "";
2976 rexy 31
        if (!defined('PSI_EMU_HOSTNAME')) switch (defined('PSI_SENSOR_IPMITOOL_ACCESS')?strtolower(PSI_SENSOR_IPMITOOL_ACCESS):'command') {
2770 rexy 32
        case 'command':
33
            CommonFunctions::executeProgram('ipmitool', 'sensor -v', $lines);
34
            break;
35
        case 'data':
36
            CommonFunctions::rfts(PSI_APP_ROOT.'/data/ipmitool.txt', $lines);
37
            break;
38
        default:
39
            $this->error->addConfigError('__construct()', '[sensor_ipmitool] ACCESS');
40
            break;
41
        }
42
        if (trim($lines) !== "") {
43
            if (preg_match("/^Sensor ID\s+/", $lines)) { //new data format ('ipmitool sensor -v')
44
                $lines = preg_replace("/\n?Unable to read sensor/", "\nUnable to read sensor", $lines);
45
                $sensors = preg_split("/Sensor ID\s+/", $lines, -1, PREG_SPLIT_NO_EMPTY);
46
                foreach ($sensors as $sensor) {
47
                    if (preg_match("/^:\s*(.+)\s\((0x[a-f\d]+)\)\r?\n/", $sensor, $name) && (($name1 = trim($name[1])) !== "")) {
48
                        $sensorvalues = preg_split("/\r?\n/", $sensor, -1, PREG_SPLIT_NO_EMPTY);
49
                        unset($sensorvalues[0]); //skip first
50
                        $sens = array();
51
                        $was = false;
52
                        foreach ($sensorvalues as $sensorvalue) {
53
                            if (preg_match("/^\s+\[(.+)\]$/", $sensorvalue, $buffer) && (($buffer1 = trim($buffer[1])) !== "")) {
54
                                if (isset($sens['State'])) {
55
                                    $sens['State'] .= ', '.$buffer1;
56
                                } else {
57
                                    $sens['State'] = $buffer1;
58
                                }
59
                                $was = true;
60
                            } elseif (preg_match("/^([^:]+):(.+)$/", $sensorvalue, $buffer)
61
                                    && (($buffer1 = trim($buffer[1])) !== "")
62
                                    && (($buffer2 = trim($buffer[2])) !== "")) {
63
                                $sens[$buffer1] = $buffer2;
64
                                $was = true;
65
                            }
66
                        }
67
                        if ($was  && !isset($sens['Unable to read sensor'])) {
68
                            $sens['Sensor'] = $name1;
69
                            if (isset($sens['Sensor Reading'])
70
                                && preg_match("/^([\d\.]+)\s+\([^\)]*\)\s+(.+)$/", $sens['Sensor Reading'], $buffer)
71
                                && (($buffer2 = trim($buffer[2])) !== "")) {
72
                                $sens['Value'] = $buffer[1];
73
                                $sens['Unit'] = $buffer2;
74
                            }
75
                            $this->_buf[intval($name[2], 0)] = $sens;
76
                        }
77
                    }
78
                }
79
            } else {
80
                $lines = preg_split("/\r?\n/", $lines, -1, PREG_SPLIT_NO_EMPTY);
81
                if (count($lines)>0) {
82
                    $buffer = preg_split("/\s*\|\s*/", $lines[0]);
83
                    if (count($buffer)>8) { //old data format ('ipmitool sensor')
84
                        foreach ($lines as $line) {
85
                            $buffer = preg_split("/\s*\|\s*/", $line);
86
                            if (count($buffer)>8) {
87
                                $sens = array();
88
                                $sens['Sensor'] = $buffer[0];
89
                                switch ($buffer[2]) {
90
                                case 'degrees C':
91
                                    $sens['Value'] = $buffer[1];
92
                                    $sens['Unit'] = $buffer[2];
93
                                    $sens['Upper Critical'] = $buffer[8];
94
                                    $sens['Sensor Type (Threshold)'] = 'Temperature';
95
                                    break;
96
                                case 'Volts':
97
                                    $sens['Value'] = $buffer[1];
98
                                    $sens['Unit'] = $buffer[2];
99
                                    $sens['Lower Critical'] = $buffer[5];
100
                                    $sens['Upper Critical'] = $buffer[8];
101
                                    $sens['Sensor Type (Threshold)'] = 'Voltage';
102
                                    break;
103
                                case 'RPM':
104
                                    $sens['Value'] = $buffer[1];
105
                                    $sens['Unit'] = $buffer[2];
106
                                    $sens['Lower Critical'] = $buffer[5];
107
                                    $sens['Upper Critical'] = $buffer[8];
108
                                    $sens['Sensor Type (Threshold)'] = 'Fan';
109
                                    break;
110
                                case 'Watts':
111
                                    $sens['Value'] = $buffer[1];
112
                                    $sens['Unit'] = $buffer[2];
113
                                    $sens['Upper Critical'] = $buffer[8];
114
                                    $sens['Sensor Type (Threshold)'] = 'Current';
115
                                    break;
116
                                case 'Amps':
117
                                    $sens['Value'] = $buffer[1];
118
                                    $sens['Unit'] = $buffer[2];
119
                                    $sens['Lower Critical'] = $buffer[5];
120
                                    $sens['Upper Critical'] = $buffer[8];
121
                                    $sens['Sensor Type (Threshold)'] = 'Current';
122
                                    break;
123
                                case 'discrete':
124
                                    if (($buffer[1]==='0x0') || ($buffer[1]==='0x1')) {
125
                                        $sens['State'] = $buffer[1];
126
                                        $sens['Sensor Type (Discrete)'] = '';
127
                                        $sens['State'] = $buffer[1];
128
                                    }
129
                                    break;
130
                                }
131
                                $this->_buf[] = $sens;
132
                            }
133
                        }
134
                    }
135
                }
136
            }
137
        }
138
    }
139
 
140
    /**
141
     * get temperature information
142
     *
143
     * @return void
144
     */
145
    private function _temperature()
146
    {
147
        foreach ($this->_buf as $sensor) {
148
            if (((isset($sensor['Sensor Type (Threshold)']) && ($sensor['Sensor Type (Threshold)'] == 'Temperature'))
149
                ||(isset($sensor['Sensor Type (Analog)']) && ($sensor['Sensor Type (Analog)'] == 'Temperature')))
150
               && isset($sensor['Unit']) && ($sensor['Unit'] == 'degrees C')
151
               && isset($sensor['Value'])) {
152
                $dev = new SensorDevice();
153
                $dev->setName($sensor['Sensor']);
154
                $dev->setValue($sensor['Value']);
155
                if (isset($sensor['Upper Critical']) && (($max = $sensor['Upper Critical']) != "na")) {
156
                    $dev->setMax($max);
157
                }
158
                if (isset($sensor['Status']) && (($status = $sensor['Status']) != "ok")) {
159
                    $dev->setEvent($status);
160
                }
161
                $this->mbinfo->setMbTemp($dev);
162
            }
163
        }
164
    }
165
 
166
    /**
167
     * get voltage information
168
     *
169
     * @return void
170
     */
171
    private function _voltage()
172
    {
173
        foreach ($this->_buf as $sensor) {
174
            if (((isset($sensor['Sensor Type (Threshold)']) && ($sensor['Sensor Type (Threshold)'] == 'Voltage'))
175
                ||(isset($sensor['Sensor Type (Analog)']) && ($sensor['Sensor Type (Analog)'] == 'Voltage')))
176
               && isset($sensor['Unit']) && ($sensor['Unit'] == 'Volts')
177
               && isset($sensor['Value'])) {
178
                $dev = new SensorDevice();
179
                $dev->setName($sensor['Sensor']);
180
                $dev->setValue($sensor['Value']);
181
                if (isset($sensor['Upper Critical']) && (($max = $sensor['Upper Critical']) != "na")) {
182
                    $dev->setMax($max);
183
                }
184
                if (isset($sensor['Lower Critical']) && (($min = $sensor['Lower Critical']) != "na")) {
185
                    $dev->setMin($min);
186
                }
187
                if (isset($sensor['Status']) && (($status = $sensor['Status']) != "ok")) {
188
                    $dev->setEvent($status);
189
                }
190
                $this->mbinfo->setMbVolt($dev);
191
            }
192
        }
193
    }
194
 
195
    /**
196
     * get fan information
197
     *
198
     * @return void
199
     */
200
    private function _fans()
201
    {
202
        foreach ($this->_buf as $sensor) {
203
            if (((isset($sensor['Sensor Type (Threshold)']) && ($sensor['Sensor Type (Threshold)'] == 'Fan'))
204
                ||(isset($sensor['Sensor Type (Analog)']) && ($sensor['Sensor Type (Analog)'] == 'Fan')))
205
               && isset($sensor['Unit']) && ($sensor['Unit'] == 'RPM')
206
               && isset($sensor['Value'])) {
207
                $dev = new SensorDevice();
208
                $dev->setName($sensor['Sensor']);
209
                $dev->setValue($value = $sensor['Value']);
210
                if (isset($sensor['Lower Critical']) && (($min = $sensor['Lower Critical']) != "na")) {
211
                    $dev->setMin($min);
212
                } elseif (isset($sensor['Upper Critical']) && (($max = $sensor['Upper Critical']) != "na")
213
                          && ($max < $value)) { // max instead min issue
214
                    $dev->setMin($max);
215
                }
216
                if (isset($sensor['Status']) && (($status = $sensor['Status']) != "ok")) {
217
                    $dev->setEvent($status);
218
                }
219
                $this->mbinfo->setMbFan($dev);
220
            }
221
        }
222
    }
223
 
224
    /**
225
     * get power information
226
     *
227
     * @return void
228
     */
229
    private function _power()
230
    {
231
        foreach ($this->_buf as $sensor) {
232
            if (((isset($sensor['Sensor Type (Threshold)']) && ($sensor['Sensor Type (Threshold)'] == 'Current'))
233
                ||(isset($sensor['Sensor Type (Analog)']) && ($sensor['Sensor Type (Analog)'] == 'Current')))
234
               && isset($sensor['Unit']) && ($sensor['Unit'] == 'Watts')
235
               && isset($sensor['Value'])) {
236
                $dev = new SensorDevice();
237
                $dev->setName($sensor['Sensor']);
238
                $dev->setValue($sensor['Value']);
239
                if (isset($sensor['Upper Critical']) && (($max = $sensor['Upper Critical']) != "na")) {
240
                    $dev->setMax($max);
241
                }
242
                if (isset($sensor['Status']) && (($status = $sensor['Status']) != "ok")) {
243
                    $dev->setEvent($status);
244
                }
245
                $this->mbinfo->setMbPower($dev);
246
            }
247
        }
248
    }
249
 
250
    /**
251
     * get current information
252
     *
253
     * @return void
254
     */
255
    private function _current()
256
    {
257
        foreach ($this->_buf as $sensor) {
258
            if (((isset($sensor['Sensor Type (Threshold)']) && ($sensor['Sensor Type (Threshold)'] == 'Current'))
259
                ||(isset($sensor['Sensor Type (Analog)']) && ($sensor['Sensor Type (Analog)'] == 'Current')))
260
               && isset($sensor['Unit']) && ($sensor['Unit'] == 'Amps')
261
               && isset($sensor['Value'])) {
262
                $dev = new SensorDevice();
263
                $dev->setName($sensor['Sensor']);
264
                $dev->setValue($sensor['Value']);
265
                if (isset($sensor['Upper Critical']) && (($max = $sensor['Upper Critical']) != "na")) {
266
                    $dev->setMax($max);
267
                }
268
                if (isset($sensor['Lower Critical']) && (($min = $sensor['Lower Critical']) != "na")) {
269
                    $dev->setMin($min);
270
                }
271
                if (isset($sensor['Status']) && (($status = $sensor['Status']) != "ok")) {
272
                    $dev->setEvent($status);
273
                }
274
                $this->mbinfo->setMbCurrent($dev);
275
            }
276
        }
277
    }
278
 
279
    /**
280
     * get other information
281
     *
282
     * @return void
283
     */
284
    private function _other()
285
    {
286
        foreach ($this->_buf as $sensor) {
287
            if (isset($sensor['Sensor Type (Discrete)'])) {
288
                $dev = new SensorDevice();
289
                if ($sensor['Sensor Type (Discrete)']!=='') {
290
                    $dev->setName($sensor['Sensor'].' ('.$sensor['Sensor Type (Discrete)'].')');
291
                } else {
292
                    $dev->setName($sensor['Sensor']);
293
                }
294
                if (isset($sensor['State'])) {
295
                    $dev->setValue($sensor['State']);
296
                } else {
297
                    $dev->setValue('0x0');
298
                }
299
                $this->mbinfo->setMbOther($dev);
300
            }
301
        }
302
    }
303
 
304
    /**
305
     * get the information
306
     *
307
     * @see PSI_Interface_Sensor::build()
308
     *
309
     * @return Void
310
     */
311
    public function build()
312
    {
313
        $this->_temperature();
314
        $this->_voltage();
315
        $this->_fans();
316
        $this->_power();
317
        $this->_current();
318
        $this->_other();
319
    }
320
}