Subversion Repositories ALCASAR

Rev

Rev 3037 | 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 = "";
3100 rexy 31
        if (!defined('PSI_EMU_HOSTNAME') || defined('PSI_EMU_PORT')) 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':
3100 rexy 36
            if (!defined('PSI_EMU_PORT')) {
37
                CommonFunctions::rftsdata('ipmitool.tmp', $lines);
38
            }
2770 rexy 39
            break;
40
        default:
41
            $this->error->addConfigError('__construct()', '[sensor_ipmitool] ACCESS');
42
        }
43
        if (trim($lines) !== "") {
44
            if (preg_match("/^Sensor ID\s+/", $lines)) { //new data format ('ipmitool sensor -v')
45
                $lines = preg_replace("/\n?Unable to read sensor/", "\nUnable to read sensor", $lines);
46
                $sensors = preg_split("/Sensor ID\s+/", $lines, -1, PREG_SPLIT_NO_EMPTY);
47
                foreach ($sensors as $sensor) {
48
                    if (preg_match("/^:\s*(.+)\s\((0x[a-f\d]+)\)\r?\n/", $sensor, $name) && (($name1 = trim($name[1])) !== "")) {
49
                        $sensorvalues = preg_split("/\r?\n/", $sensor, -1, PREG_SPLIT_NO_EMPTY);
50
                        unset($sensorvalues[0]); //skip first
51
                        $sens = array();
52
                        $was = false;
53
                        foreach ($sensorvalues as $sensorvalue) {
54
                            if (preg_match("/^\s+\[(.+)\]$/", $sensorvalue, $buffer) && (($buffer1 = trim($buffer[1])) !== "")) {
55
                                if (isset($sens['State'])) {
56
                                    $sens['State'] .= ', '.$buffer1;
57
                                } else {
58
                                    $sens['State'] = $buffer1;
59
                                }
60
                                $was = true;
61
                            } elseif (preg_match("/^([^:]+):(.+)$/", $sensorvalue, $buffer)
62
                                    && (($buffer1 = trim($buffer[1])) !== "")
63
                                    && (($buffer2 = trim($buffer[2])) !== "")) {
64
                                $sens[$buffer1] = $buffer2;
65
                                $was = true;
66
                            }
67
                        }
68
                        if ($was  && !isset($sens['Unable to read sensor'])) {
69
                            $sens['Sensor'] = $name1;
70
                            if (isset($sens['Sensor Reading'])
71
                                && preg_match("/^([\d\.]+)\s+\([^\)]*\)\s+(.+)$/", $sens['Sensor Reading'], $buffer)
72
                                && (($buffer2 = trim($buffer[2])) !== "")) {
73
                                $sens['Value'] = $buffer[1];
74
                                $sens['Unit'] = $buffer2;
75
                            }
76
                            $this->_buf[intval($name[2], 0)] = $sens;
77
                        }
78
                    }
79
                }
80
            } else {
81
                $lines = preg_split("/\r?\n/", $lines, -1, PREG_SPLIT_NO_EMPTY);
82
                if (count($lines)>0) {
83
                    $buffer = preg_split("/\s*\|\s*/", $lines[0]);
84
                    if (count($buffer)>8) { //old data format ('ipmitool sensor')
85
                        foreach ($lines as $line) {
86
                            $buffer = preg_split("/\s*\|\s*/", $line);
87
                            if (count($buffer)>8) {
88
                                $sens = array();
89
                                $sens['Sensor'] = $buffer[0];
90
                                switch ($buffer[2]) {
91
                                case 'degrees C':
92
                                    $sens['Value'] = $buffer[1];
93
                                    $sens['Unit'] = $buffer[2];
94
                                    $sens['Upper Critical'] = $buffer[8];
95
                                    $sens['Sensor Type (Threshold)'] = 'Temperature';
96
                                    break;
97
                                case 'Volts':
98
                                    $sens['Value'] = $buffer[1];
99
                                    $sens['Unit'] = $buffer[2];
100
                                    $sens['Lower Critical'] = $buffer[5];
101
                                    $sens['Upper Critical'] = $buffer[8];
102
                                    $sens['Sensor Type (Threshold)'] = 'Voltage';
103
                                    break;
104
                                case 'RPM':
105
                                    $sens['Value'] = $buffer[1];
106
                                    $sens['Unit'] = $buffer[2];
107
                                    $sens['Lower Critical'] = $buffer[5];
108
                                    $sens['Upper Critical'] = $buffer[8];
109
                                    $sens['Sensor Type (Threshold)'] = 'Fan';
110
                                    break;
111
                                case 'Watts':
112
                                    $sens['Value'] = $buffer[1];
113
                                    $sens['Unit'] = $buffer[2];
114
                                    $sens['Upper Critical'] = $buffer[8];
115
                                    $sens['Sensor Type (Threshold)'] = 'Current';
116
                                    break;
117
                                case 'Amps':
118
                                    $sens['Value'] = $buffer[1];
119
                                    $sens['Unit'] = $buffer[2];
120
                                    $sens['Lower Critical'] = $buffer[5];
121
                                    $sens['Upper Critical'] = $buffer[8];
122
                                    $sens['Sensor Type (Threshold)'] = 'Current';
123
                                    break;
124
                                case 'discrete':
125
                                    if (($buffer[1]==='0x0') || ($buffer[1]==='0x1')) {
126
                                        $sens['State'] = $buffer[1];
127
                                        $sens['Sensor Type (Discrete)'] = '';
128
                                        $sens['State'] = $buffer[1];
129
                                    }
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
     *
3037 rexy 309
     * @return void
2770 rexy 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
}