Subversion Repositories ALCASAR

Rev

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