Subversion Repositories ALCASAR

Rev

Rev 3100 | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
2770 rexy 1
<?php
2
/**
3
 * lmsensor sensor class, getting information from lmsensor
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 LMSensors extends Sensors
16
{
17
    /**
18
     * content to parse
19
     *
20
     * @var array
21
     */
22
    private $_lines = array();
325 richard 23
 
2770 rexy 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 ((PSI_OS == 'Linux') && (!defined('PSI_EMU_HOSTNAME') || defined('PSI_EMU_PORT'))) switch (defined('PSI_SENSOR_LMSENSORS_ACCESS')?strtolower(PSI_SENSOR_LMSENSORS_ACCESS):'command') {
2770 rexy 32
        case 'command':
33
            CommonFunctions::executeProgram("sensors", "", $lines);
34
            break;
35
        case 'data':
3100 rexy 36
            if (!defined('PSI_EMU_PORT')) {
37
                CommonFunctions::rftsdata('lmsensors.tmp', $lines);
38
            }
2770 rexy 39
            break;
40
        default:
41
            $this->error->addConfigError('__construct()', '[sensor_lmsensors] ACCESS');
42
        }
325 richard 43
 
2770 rexy 44
        if (trim($lines) !== "") {
45
            $lines = str_replace(":\n", ":", $lines);
46
            $lines = str_replace("\n\n", "\n", $lines);
47
            $this->_lines = preg_split("/\n/", $lines, -1, PREG_SPLIT_NO_EMPTY);
48
        }
49
    }
325 richard 50
 
2770 rexy 51
    /**
52
     * get temperature information
53
     *
54
     * @return void
55
     */
56
    private function _temperature()
57
    {
58
        $applesmc = false;
59
        $sname = '';
60
        foreach ($this->_lines as $line) {
61
            if ((trim($line) !== "") && (strpos($line, ':') === false)) {
62
                //$applesmc = preg_match("/^applesmc-/", $line);
63
                $sname = trim($line);
64
                $applesmc =  ($sname === "applesmc-isa-0300");
65
                if (preg_match('/^([^-]+)-/', $sname, $snamebuf)) {
66
                    $sname = ' ('.$snamebuf[1].')';
67
                } else {
68
                    $sname = '';
69
                }
70
            }
71
            $data = array();
3179 rexy 72
            if (preg_match("/^(.+):(.+)[^\w\r\n\t]C\s*\((.+)=(.+)[^\w\r\n\t]C,(.+)=(.+)[^\w\r\n\t]C\)(.*)\)/", $line, $data)) {
2770 rexy 73
                ;
3179 rexy 74
            } elseif (preg_match("/^(.+):(.+)[^\w\r\n\t]C\s*\((.+)=(.+)[^\w\r\n\t]C,(.+)=(.+)[^\w\r\n\t]C\)(.*)/", $line, $data)) {
2770 rexy 75
                ;
3179 rexy 76
            } elseif (preg_match("/^(.+):(.+)[^\w\r\n\t]C\s*\((.+)=(.+)[^\w\r\n\t]C\)(.*)/", $line, $data)) {
2770 rexy 77
                ;
3179 rexy 78
            } elseif (preg_match("/^(.+):\s*(FAULT)\s*\((.+)=(.+)[^\w\r\n\t]C,(.+)=(.+)[^\w\r\n\t]C\)(.*)\)/", $line, $data)) {
3037 rexy 79
                ;
3179 rexy 80
            } elseif (preg_match("/^(.+):\s*(FAULT)\s*\((.+)=(.+)[^\w\r\n\t]C,(.+)=(.+)[^\w\r\n\t]C\)(.*)/", $line, $data)) {
3037 rexy 81
                ;
3179 rexy 82
            } elseif (preg_match("/^(.+):\s*(FAULT)\s*\((.+)=(.+)[^\w\r\n\t]C\)(.*)/", $line, $data)) {
3037 rexy 83
                ;
3179 rexy 84
            } elseif (preg_match("/^(.+):(.+)[^\w\r\n\t]C\s*\(/", $line, $data)) {
2770 rexy 85
                ;
3179 rexy 86
            } elseif (preg_match("/^(.+):(.+)[^\w\r\n\t]C\s+\D+/", $line, $data)) {
3037 rexy 87
                ;
2770 rexy 88
            } else {
3179 rexy 89
                preg_match("/^(.+):(.+)[^\w\r\n\t]C\r?$/", $line, $data);
2770 rexy 90
            }
91
            if (count($data)>2) {
92
                foreach ($data as $key=>$value) {
93
                    if (preg_match("/^\+?(-?[0-9\.]+).?$/", trim($value), $newvalue)) {
3037 rexy 94
                        $data[$key] = floatval($newvalue[1]);
2770 rexy 95
                    } else {
96
                        $data[$key] = trim($value);
97
                    }
98
                }
99
                if ($applesmc && (strlen($data[1]) == 4)) {
100
                    if ($data[1][0] == "T") {
101
                        if ($data[1][1] == "A") {
102
                            $data[1] = $data[1] . " Ambient";
103
                        } elseif ($data[1][1] == "B") {
104
                            $data[1] = $data[1] . " Battery";
105
                        } elseif ($data[1][1] == "C") {
106
                            $data[1] = $data[1] . " CPU";
107
                        } elseif ($data[1][1] == "G") {
108
                            $data[1] = $data[1] . " GPU";
109
                        } elseif ($data[1][1] == "H") {
110
                            $data[1] = $data[1] . " Harddisk Bay";
111
                        } elseif ($data[1][1] == "h") {
112
                            $data[1] = $data[1] . " Heatpipe";
113
                        } elseif ($data[1][1] == "L") {
114
                            $data[1] = $data[1] . " LCD";
115
                        } elseif ($data[1][1] == "M") {
116
                            $data[1] = $data[1] . " Memory";
117
                        } elseif ($data[1][1] == "m") {
118
                            $data[1] = $data[1] . " Memory Contr.";
119
                        } elseif ($data[1][1] == "N") {
120
                            $data[1] = $data[1] . " Northbridge";
121
                        } elseif ($data[1][1] == "O") {
122
                            $data[1] = $data[1] . " Optical Drive";
123
                        } elseif ($data[1][1] == "p") {
124
                            $data[1] = $data[1] . " Power supply";
125
                        } elseif ($data[1][1] == "S") {
126
                            $data[1] = $data[1] . " Slot";
127
                        } elseif ($data[1][1] == "s") {
128
                            $data[1] = $data[1] . " Slot";
129
                        } elseif ($data[1][1] == "W") {
130
                            $data[1] = $data[1] . " Airport";
131
                        }
325 richard 132
 
2770 rexy 133
                        if ($data[1][3] == "H") {
134
                            $data[1] = $data[1] . " Heatsink";
135
                        } elseif ($data[1][3] == "P") {
136
                            $data[1] = $data[1] . " Proximity";
137
                        } elseif ($data[1][3] == "D") {
138
                            $data[1] = $data[1] . " Die";
139
                        }
140
                    }
141
                }
325 richard 142
 
2770 rexy 143
                $dev = new SensorDevice();
144
                $dev->setName($data[1].$sname);
145
                $dev->setValue($data[2]);
146
                if (isset($data[6]) && $data[2] <= $data[6]) {
147
                    $dev->setMax(max($data[4], $data[6]));
148
                } elseif (isset($data[4]) && $data[2] <= $data[4]) {
149
                    $dev->setMax($data[4]);
150
                }
3037 rexy 151
                if (preg_match("/\s(ALARM)\s*$/", $line, $evbuf) || preg_match("/\s(ALARM)\s+sensor\s+=/", $line, $evbuf) ||  (($evbuf[1] = $dev->getValue()) === 'FAULT')) {
152
                    $dev->setEvent($evbuf[1]);
2770 rexy 153
                }
154
                $this->mbinfo->setMbTemp($dev);
155
            }
156
        }
157
    }
325 richard 158
 
2770 rexy 159
    /**
160
     * get fan information
161
     *
162
     * @return void
163
     */
164
    private function _fans()
165
    {
166
        $sname = '';
167
        foreach ($this->_lines as $line) {
168
            if ((trim($line) !== "") && (strpos($line, ':') === false)) {
169
                $sname = trim($line);
170
                if (preg_match('/^([^-]+)-/', $sname, $snamebuf)) {
171
                    $sname = ' ('.$snamebuf[1].')';
172
                } else {
173
                    $sname = '';
174
                }
175
            }
176
            $data = array();
177
            if (preg_match("/^(.+):(.+) RPM\s*\((.+)=(.+) RPM,(.+)=(.+)\)(.*)\)/", $line, $data)) {
178
                ;
179
            } elseif (preg_match("/^(.+):(.+) RPM\s*\((.+)=(.+) RPM,(.+)=(.+)\)(.*)/", $line, $data)) {
180
                ;
181
            } elseif (preg_match("/^(.+):(.+) RPM\s*\((.+)=(.+) RPM\)(.*)/", $line, $data)) {
182
                ;
3037 rexy 183
            } elseif (preg_match("/^(.+):\s*(FAULT)\s*\((.+)=(.+) RPM,(.+)=(.+)\)(.*)\)/", $line, $data)) {
184
                ;
185
            } elseif (preg_match("/^(.+):\s*(FAULT)\s*\((.+)=(.+) RPM,(.+)=(.+)\)(.*)/", $line, $data)) {
186
                ;
187
            } elseif (preg_match("/^(.+):\s*(FAULT)\s*\((.+)=(.+) RPM\)(.*)/", $line, $data)) {
188
                ;
2770 rexy 189
            } elseif (preg_match("/^(.+):(.+) RPM\s*\(/", $line, $data)) {
190
                ;
3037 rexy 191
            } elseif (preg_match("/^(.+):(.+) RPM\s+\D+/", $line, $data)) {
192
                ;
2770 rexy 193
            } else {
3037 rexy 194
                preg_match("/^(.+):(.+) RPM\r?$/", $line, $data);
2770 rexy 195
            }
196
            if (count($data)>2) {
197
                 foreach ($data as $key=>$value) {
198
                    if (preg_match("/^\+?(-?[0-9\.]+).?$/", trim($value), $newvalue)) {
3037 rexy 199
                        $data[$key] = floatval($newvalue[1]);
2770 rexy 200
                    } else {
201
                        $data[$key] = trim($value);
202
                    }
203
                }
204
                $dev = new SensorDevice();
205
                $dev->setName($data[1].$sname);
206
                $dev->setValue(trim($data[2]));
207
                if (isset($data[4])) {
208
                    $dev->setMin(trim($data[4]));
209
                }
3037 rexy 210
                if (preg_match("/\s(ALARM)\s*$/", $line, $evbuf) || (($evbuf[1] = $dev->getValue()) === 'FAULT')) {
211
                    $dev->setEvent($evbuf[1]);
2770 rexy 212
                }
213
                $this->mbinfo->setMbFan($dev);
214
            }
325 richard 215
        }
216
    }
217
 
2770 rexy 218
    /**
219
     * get voltage information
220
     *
221
     * @return void
222
     */
223
    private function _voltage()
224
    {
225
        $sname = '';
226
        foreach ($this->_lines as $line) {
227
            if ((trim($line) !== "") && (strpos($line, ':') === false)) {
228
                $sname = trim($line);
229
                if (preg_match('/^([^-]+)-/', $sname, $snamebuf)) {
230
                    $sname = ' ('.$snamebuf[1].')';
231
                } else {
232
                    $sname = '';
233
                }
234
            }
235
            $data = array();
3179 rexy 236
            if (preg_match("/^(.+):(.+) (m?)V\s*\((.+)=(.+) V,(.+)=(.+) V\)(.*)\)/", $line, $data)) {
2770 rexy 237
                ;
3179 rexy 238
            } elseif (preg_match("/^(.+):(.+) (m?)V\s*\((.+)=(.+) V,(.+)=(.+) V\)(.*)/", $line, $data)) {
2770 rexy 239
                ;
3179 rexy 240
            } elseif (preg_match("/^(.+):\s*(FAULT)()\s*\((.+)=(.+) V,(.+)=(.+) V\)(.*)\)/", $line, $data)) {
3037 rexy 241
                ;
3179 rexy 242
            } elseif (preg_match("/^(.+):\s*(FAULT)()\s*\((.+)=(.+) V,(.+)=(.+) V\)(.*)/", $line, $data)) {
3037 rexy 243
                ;
3179 rexy 244
            } elseif (preg_match("/^(.+):\s*(.+) (m?)V\s*()()\((.+)=(.+) V\)(.*)/", $line, $data)) {
2770 rexy 245
                ;
3179 rexy 246
            } elseif (preg_match("/^(.+):(.+) (m?)V\s*\(/", $line, $data)) {
3037 rexy 247
                ;
3179 rexy 248
            } elseif (preg_match("/^(.+):(.+) (m?)V\s+\D+/", $line, $data)) {
249
                ;
2770 rexy 250
            } else {
3179 rexy 251
                preg_match("/^(.+):(.+) (m?)V\r?$/", $line, $data);
2770 rexy 252
            }
325 richard 253
 
2770 rexy 254
            if (count($data)>2) {
255
                foreach ($data as $key=>$value) {
256
                    if (preg_match("/^\+?(-?[0-9\.]+)$/", trim($value), $newvalue)) {
3037 rexy 257
                        $data[$key] = floatval($newvalue[1]);
2770 rexy 258
                    } else {
259
                        $data[$key] = trim($value);
260
                    }
261
                }
262
                $dev = new SensorDevice();
263
                $dev->setName($data[1].$sname);
3179 rexy 264
                if (isset($data[3]) && ($data[3]==='m') && is_float($data[2])) {
265
                    $dev->setValue($data[2]/1000);
266
                } else {
267
                    $dev->setValue($data[2]);
2770 rexy 268
                }
3179 rexy 269
                if (isset($data[5]) && ($data[5]!=='')) {
270
                    $dev->setMin($data[5]);
2770 rexy 271
                }
3179 rexy 272
                if (isset($data[7])) {
273
                    $dev->setMax($data[7]);
274
                }
3037 rexy 275
                if (preg_match("/\s(ALARM)\s*$/", $line, $evbuf) || (($evbuf[1] = $dev->getValue()) === 'FAULT')) {
276
                    $dev->setEvent($evbuf[1]);
2770 rexy 277
                }
278
                $this->mbinfo->setMbVolt($dev);
279
            }
280
        }
325 richard 281
    }
282
 
2770 rexy 283
    /**
284
     * get power information
285
     *
286
     * @return void
287
     */
288
    private function _power()
289
    {
290
        $sname = '';
291
        foreach ($this->_lines as $line) {
292
            if ((trim($line) !== "") && (strpos($line, ':') === false)) {
293
                $sname = trim($line);
294
                if (preg_match('/^([^-]+)-/', $sname, $snamebuf)) {
295
                    $sname = ' ('.$snamebuf[1].')';
296
                } else {
297
                    $sname = '';
298
                }
299
            }
300
            $data = array();
301
/* not tested yet
302
            if (preg_match("/^(.+):(.+) W\s*\((.+)=(.+) W,(.+)=(.+) W\)(.*)\)/", $line, $data)) {
303
                ;
304
            } elseif (preg_match("/^(.+):(.+) W\s*\((.+)=(.+) W,(.+)=(.+) W\)(.*)/", $line, $data)) {
305
                ;
3037 rexy 306
            } elseif (preg_match("/^(.+):\s*(FAULT)\s*\((.+)=(.+) W,(.+)=(.+) W\)(.*)\)/", $line, $data)) {
307
                ;
308
            } elseif (preg_match("/^(.+):\s*(FAULT)\s*\((.+)=(.+) W,(.+)=(.+) W\)(.*)/", $line, $data)) {
309
                ;
2770 rexy 310
            } else
311
*/
312
            if (preg_match("/^(.+):(.+) W\s*\((.+)=(.+) W\)(.*)/", $line, $data)) {
313
                ;
3037 rexy 314
            } elseif (preg_match("/^(.+):\s*(FAULT)\s*\((.+)=(.+) W\)(.*)/", $line, $data)) {
315
                ;
2770 rexy 316
            } elseif (preg_match("/^(.+):(.+) W\s*\(/", $line, $data)) {
317
                ;
3037 rexy 318
            } elseif (preg_match("/^(.+):(.+) W\s+\D+/", $line, $data)) {
319
                ;
2770 rexy 320
            } else {
3037 rexy 321
                preg_match("/^(.+):(.+) W\r?$/", $line, $data);
2770 rexy 322
            }
323
            if (count($data)>2) {
324
                foreach ($data as $key=>$value) {
325
                    if (preg_match("/^\+?(-?[0-9\.]+).?$/", trim($value), $newvalue)) {
3037 rexy 326
                        $data[$key] = floatval($newvalue[1]);
2770 rexy 327
                    } else {
328
                        $data[$key] = trim($value);
329
                    }
330
                }
331
                $dev = new SensorDevice();
332
                $dev->setName($data[1].$sname);
333
                $dev->setValue($data[2]);
325 richard 334
 
2770 rexy 335
/* not tested yet
336
                if (isset($data[6]) && $data[2] <= $data[6]) {
337
                    $dev->setMax(max($data[4], $data[6]));
338
                } else
339
*/
340
                if (isset($data[4]) && $data[2] <= $data[4]) {
341
                    $dev->setMax($data[4]);
342
                }
3037 rexy 343
                if (preg_match("/\s(ALARM)\s*$/", $line, $evbuf) || (($evbuf[1] = $dev->getValue()) === 'FAULT')) {
344
                    $dev->setEvent($evbuf[1]);
2770 rexy 345
                }
346
                $this->mbinfo->setMbPower($dev);
347
            }
348
        }
325 richard 349
    }
350
 
2770 rexy 351
    /**
352
     * get current information
353
     *
354
     * @return void
355
     */
356
    private function _current()
357
    {
358
        $sname = '';
359
        foreach ($this->_lines as $line) {
360
            if ((trim($line) !== "") && (strpos($line, ':') === false)) {
361
                $sname = trim($line);
362
                if (preg_match('/^([^-]+)-/', $sname, $snamebuf)) {
363
                    $sname = ' ('.$snamebuf[1].')';
364
                } else {
365
                    $sname = '';
366
                }
367
            }
368
            $data = array();
369
            if (preg_match("/^(.+):(.+) A\s*\((.+)=(.+) A,(.+)=(.+) A\)(.*)\)/", $line, $data)) {
370
                ;
371
            } elseif (preg_match("/^(.+):(.+) A\s*\((.+)=(.+) A,(.+)=(.+) A\)(.*)/", $line, $data)) {
372
                ;
3037 rexy 373
            } elseif (preg_match("/^(.+):\s*(FAULT)\s*\((.+)=(.+) A,(.+)=(.+) A\)(.*)\)/", $line, $data)) {
374
                ;
375
            } elseif (preg_match("/^(.+):\s*(FAULT)\s*\((.+)=(.+) A,(.+)=(.+) A\)(.*)/", $line, $data)) {
376
                ;
2770 rexy 377
            } elseif (preg_match("/^(.+):(.+) A\s*\(/", $line, $data)) {
378
                ;
3037 rexy 379
            } elseif (preg_match("/^(.+):(.+) A\s+\D+/", $line, $data)) {
380
                ;
2770 rexy 381
            } else {
3037 rexy 382
                preg_match("/^(.+):(.+) A\r?$/", $line, $data);
2770 rexy 383
            }
384
            if (count($data)>2) {
385
                foreach ($data as $key=>$value) {
386
                    if (preg_match("/^\+?([0-9\.]+).?$/", trim($value), $newvalue)) {
387
                        $data[$key] = trim($newvalue[1]);
388
                    } else {
389
                        $data[$key] = trim($value);
390
                    }
391
                }
392
                $dev = new SensorDevice();
393
                $dev->setName($data[1].$sname);
394
                $dev->setValue($data[2]);
395
                if (isset($data[4])) {
396
                    $dev->setMin($data[4]);
397
                }
398
                if (isset($data[6])) {
399
                    $dev->setMax($data[6]);
400
                }
3037 rexy 401
                if (preg_match("/\s(ALARM)\s*$/", $line, $evbuf) || (($evbuf[1] = $dev->getValue()) === 'FAULT')) {
402
                    $dev->setEvent($evbuf[1]);
2770 rexy 403
                }
404
                $this->mbinfo->setMbCurrent($dev);
405
            }
406
        }
325 richard 407
    }
408
 
2770 rexy 409
    /**
410
     * get other information
411
     *
412
     * @return void
413
     */
414
    private function _other()
415
    {
416
        $sname = '';
417
        foreach ($this->_lines as $line) {
418
            if ((trim($line) !== "") && (strpos($line, ':') === false)) {
419
                $sname = trim($line);
420
                if (preg_match('/^([^-]+)-/', $sname, $snamebuf)) {
421
                    $sname = ' ('.$snamebuf[1].')';
422
                } else {
423
                    $sname = '';
424
                }
425
            }
426
            $data = array();
3037 rexy 427
            preg_match("/^(.+):\s*([^\-\+\d\s].+)\r?$/", $line, $data);
428
            if ((count($data)>2) && ($data[1]!=="Adapter") && !preg_match("/^FAULT/", $data[2])) {
2770 rexy 429
                $dev = new SensorDevice();
430
                $dev->setName($data[1].$sname);
431
                if (preg_match("/(.*\s*)ALARM\s*$/", $data[2], $aldata)) {
432
                    $dev->setEvent("Alarm");
433
                    if ((count($aldata)>1) && trim($aldata[1]!=="")) {
434
                        $dev->setValue(trim($aldata[1]));
435
                    } else {
436
                        $dev->setValue($data[2]);
437
                    }
438
                } else {
439
                    $dev->setValue($data[2]);
440
                }
441
                $this->mbinfo->setMbOther($dev);
442
            }
325 richard 443
        }
444
    }
445
 
2770 rexy 446
    /**
447
     * get the information
448
     *
449
     * @see PSI_Interface_Sensor::build()
450
     *
3037 rexy 451
     * @return void
2770 rexy 452
     */
453
    public function build()
454
    {
455
        $this->_temperature();
456
        $this->_voltage();
457
        $this->_fans();
458
        $this->_power();
459
        $this->_current();
460
        $this->_other();
325 richard 461
    }
462
}