Subversion Repositories ALCASAR

Rev

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