Subversion Repositories ALCASAR

Rev

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
 * 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();
72
            if (preg_match("/^(.+):(.+).C\s*\((.+)=(.+).C,(.+)=(.+).C\)(.*)\)/", $line, $data)) {
73
                ;
74
            } elseif (preg_match("/^(.+):(.+).C\s*\((.+)=(.+).C,(.+)=(.+).C\)(.*)/", $line, $data)) {
75
                ;
76
            } elseif (preg_match("/^(.+):(.+).C\s*\((.+)=(.+).C\)(.*)/", $line, $data)) {
77
                ;
3037 rexy 78
            } elseif (preg_match("/^(.+):\s*(FAULT)\s*\((.+)=(.+).C,(.+)=(.+).C\)(.*)\)/", $line, $data)) {
79
                ;
80
            } elseif (preg_match("/^(.+):\s*(FAULT)\s*\((.+)=(.+).C,(.+)=(.+).C\)(.*)/", $line, $data)) {
81
                ;
82
            } elseif (preg_match("/^(.+):\s*(FAULT)\s*\((.+)=(.+).C\)(.*)/", $line, $data)) {
83
                ;
2770 rexy 84
            } elseif (preg_match("/^(.+):(.+).C\s*\(/", $line, $data)) {
85
                ;
3037 rexy 86
            } elseif (preg_match("/^(.+):(.+).C\s+\D+/", $line, $data)) {
87
                ;
2770 rexy 88
            } else {
3037 rexy 89
                preg_match("/^(.+):(.+).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();
236
            if (preg_match("/^(.+):(.+) V\s*\((.+)=(.+) V,(.+)=(.+) V\)(.*)\)/", $line, $data)) {
237
                ;
238
            } elseif (preg_match("/^(.+):(.+) V\s*\((.+)=(.+) V,(.+)=(.+) V\)(.*)/", $line, $data)) {
239
                ;
3037 rexy 240
            } elseif (preg_match("/^(.+):\s*(FAULT)\s*\((.+)=(.+) V,(.+)=(.+) V\)(.*)\)/", $line, $data)) {
241
                ;
242
            } elseif (preg_match("/^(.+):\s*(FAULT)\s*\((.+)=(.+) V,(.+)=(.+) V\)(.*)/", $line, $data)) {
243
                ;
2770 rexy 244
            } elseif (preg_match("/^(.+):(.+) V\s*\(/", $line, $data)) {
245
                ;
3037 rexy 246
            } elseif (preg_match("/^(.+):(.+) V\s+\D+/", $line, $data)) {
247
                ;
2770 rexy 248
            } else {
3037 rexy 249
                preg_match("/^(.+):(.+) V\r?$/", $line, $data);
2770 rexy 250
            }
325 richard 251
 
2770 rexy 252
            if (count($data)>2) {
253
                foreach ($data as $key=>$value) {
254
                    if (preg_match("/^\+?(-?[0-9\.]+)$/", trim($value), $newvalue)) {
3037 rexy 255
                        $data[$key] = floatval($newvalue[1]);
2770 rexy 256
                    } else {
257
                        $data[$key] = trim($value);
258
                    }
259
                }
260
                $dev = new SensorDevice();
261
                $dev->setName($data[1].$sname);
262
                $dev->setValue($data[2]);
263
                if (isset($data[4])) {
264
                    $dev->setMin($data[4]);
265
                }
266
                if (isset($data[6])) {
267
                    $dev->setMax($data[6]);
268
                }
3037 rexy 269
                if (preg_match("/\s(ALARM)\s*$/", $line, $evbuf) || (($evbuf[1] = $dev->getValue()) === 'FAULT')) {
270
                    $dev->setEvent($evbuf[1]);
2770 rexy 271
                }
272
                $this->mbinfo->setMbVolt($dev);
273
            }
274
        }
325 richard 275
    }
276
 
2770 rexy 277
    /**
278
     * get power information
279
     *
280
     * @return void
281
     */
282
    private function _power()
283
    {
284
        $sname = '';
285
        foreach ($this->_lines as $line) {
286
            if ((trim($line) !== "") && (strpos($line, ':') === false)) {
287
                $sname = trim($line);
288
                if (preg_match('/^([^-]+)-/', $sname, $snamebuf)) {
289
                    $sname = ' ('.$snamebuf[1].')';
290
                } else {
291
                    $sname = '';
292
                }
293
            }
294
            $data = array();
295
/* not tested yet
296
            if (preg_match("/^(.+):(.+) W\s*\((.+)=(.+) W,(.+)=(.+) W\)(.*)\)/", $line, $data)) {
297
                ;
298
            } elseif (preg_match("/^(.+):(.+) W\s*\((.+)=(.+) W,(.+)=(.+) W\)(.*)/", $line, $data)) {
299
                ;
3037 rexy 300
            } elseif (preg_match("/^(.+):\s*(FAULT)\s*\((.+)=(.+) W,(.+)=(.+) W\)(.*)\)/", $line, $data)) {
301
                ;
302
            } elseif (preg_match("/^(.+):\s*(FAULT)\s*\((.+)=(.+) W,(.+)=(.+) W\)(.*)/", $line, $data)) {
303
                ;
2770 rexy 304
            } else
305
*/
306
            if (preg_match("/^(.+):(.+) W\s*\((.+)=(.+) W\)(.*)/", $line, $data)) {
307
                ;
3037 rexy 308
            } elseif (preg_match("/^(.+):\s*(FAULT)\s*\((.+)=(.+) W\)(.*)/", $line, $data)) {
309
                ;
2770 rexy 310
            } elseif (preg_match("/^(.+):(.+) W\s*\(/", $line, $data)) {
311
                ;
3037 rexy 312
            } elseif (preg_match("/^(.+):(.+) W\s+\D+/", $line, $data)) {
313
                ;
2770 rexy 314
            } else {
3037 rexy 315
                preg_match("/^(.+):(.+) W\r?$/", $line, $data);
2770 rexy 316
            }
317
            if (count($data)>2) {
318
                foreach ($data as $key=>$value) {
319
                    if (preg_match("/^\+?(-?[0-9\.]+).?$/", trim($value), $newvalue)) {
3037 rexy 320
                        $data[$key] = floatval($newvalue[1]);
2770 rexy 321
                    } else {
322
                        $data[$key] = trim($value);
323
                    }
324
                }
325
                $dev = new SensorDevice();
326
                $dev->setName($data[1].$sname);
327
                $dev->setValue($data[2]);
325 richard 328
 
2770 rexy 329
/* not tested yet
330
                if (isset($data[6]) && $data[2] <= $data[6]) {
331
                    $dev->setMax(max($data[4], $data[6]));
332
                } else
333
*/
334
                if (isset($data[4]) && $data[2] <= $data[4]) {
335
                    $dev->setMax($data[4]);
336
                }
3037 rexy 337
                if (preg_match("/\s(ALARM)\s*$/", $line, $evbuf) || (($evbuf[1] = $dev->getValue()) === 'FAULT')) {
338
                    $dev->setEvent($evbuf[1]);
2770 rexy 339
                }
340
                $this->mbinfo->setMbPower($dev);
341
            }
342
        }
325 richard 343
    }
344
 
2770 rexy 345
    /**
346
     * get current information
347
     *
348
     * @return void
349
     */
350
    private function _current()
351
    {
352
        $sname = '';
353
        foreach ($this->_lines as $line) {
354
            if ((trim($line) !== "") && (strpos($line, ':') === false)) {
355
                $sname = trim($line);
356
                if (preg_match('/^([^-]+)-/', $sname, $snamebuf)) {
357
                    $sname = ' ('.$snamebuf[1].')';
358
                } else {
359
                    $sname = '';
360
                }
361
            }
362
            $data = array();
363
            if (preg_match("/^(.+):(.+) A\s*\((.+)=(.+) A,(.+)=(.+) A\)(.*)\)/", $line, $data)) {
364
                ;
365
            } elseif (preg_match("/^(.+):(.+) A\s*\((.+)=(.+) A,(.+)=(.+) A\)(.*)/", $line, $data)) {
366
                ;
3037 rexy 367
            } elseif (preg_match("/^(.+):\s*(FAULT)\s*\((.+)=(.+) A,(.+)=(.+) A\)(.*)\)/", $line, $data)) {
368
                ;
369
            } elseif (preg_match("/^(.+):\s*(FAULT)\s*\((.+)=(.+) A,(.+)=(.+) A\)(.*)/", $line, $data)) {
370
                ;
2770 rexy 371
            } elseif (preg_match("/^(.+):(.+) A\s*\(/", $line, $data)) {
372
                ;
3037 rexy 373
            } elseif (preg_match("/^(.+):(.+) A\s+\D+/", $line, $data)) {
374
                ;
2770 rexy 375
            } else {
3037 rexy 376
                preg_match("/^(.+):(.+) A\r?$/", $line, $data);
2770 rexy 377
            }
378
            if (count($data)>2) {
379
                foreach ($data as $key=>$value) {
380
                    if (preg_match("/^\+?([0-9\.]+).?$/", trim($value), $newvalue)) {
381
                        $data[$key] = trim($newvalue[1]);
382
                    } else {
383
                        $data[$key] = trim($value);
384
                    }
385
                }
386
                $dev = new SensorDevice();
387
                $dev->setName($data[1].$sname);
388
                $dev->setValue($data[2]);
389
                if (isset($data[4])) {
390
                    $dev->setMin($data[4]);
391
                }
392
                if (isset($data[6])) {
393
                    $dev->setMax($data[6]);
394
                }
3037 rexy 395
                if (preg_match("/\s(ALARM)\s*$/", $line, $evbuf) || (($evbuf[1] = $dev->getValue()) === 'FAULT')) {
396
                    $dev->setEvent($evbuf[1]);
2770 rexy 397
                }
398
                $this->mbinfo->setMbCurrent($dev);
399
            }
400
        }
325 richard 401
    }
402
 
2770 rexy 403
    /**
404
     * get other information
405
     *
406
     * @return void
407
     */
408
    private function _other()
409
    {
410
        $sname = '';
411
        foreach ($this->_lines as $line) {
412
            if ((trim($line) !== "") && (strpos($line, ':') === false)) {
413
                $sname = trim($line);
414
                if (preg_match('/^([^-]+)-/', $sname, $snamebuf)) {
415
                    $sname = ' ('.$snamebuf[1].')';
416
                } else {
417
                    $sname = '';
418
                }
419
            }
420
            $data = array();
3037 rexy 421
            preg_match("/^(.+):\s*([^\-\+\d\s].+)\r?$/", $line, $data);
422
            if ((count($data)>2) && ($data[1]!=="Adapter") && !preg_match("/^FAULT/", $data[2])) {
2770 rexy 423
                $dev = new SensorDevice();
424
                $dev->setName($data[1].$sname);
425
                if (preg_match("/(.*\s*)ALARM\s*$/", $data[2], $aldata)) {
426
                    $dev->setEvent("Alarm");
427
                    if ((count($aldata)>1) && trim($aldata[1]!=="")) {
428
                        $dev->setValue(trim($aldata[1]));
429
                    } else {
430
                        $dev->setValue($data[2]);
431
                    }
432
                } else {
433
                    $dev->setValue($data[2]);
434
                }
435
                $this->mbinfo->setMbOther($dev);
436
            }
325 richard 437
        }
438
    }
439
 
2770 rexy 440
    /**
441
     * get the information
442
     *
443
     * @see PSI_Interface_Sensor::build()
444
     *
3037 rexy 445
     * @return void
2770 rexy 446
     */
447
    public function build()
448
    {
449
        $this->_temperature();
450
        $this->_voltage();
451
        $this->_fans();
452
        $this->_power();
453
        $this->_current();
454
        $this->_other();
325 richard 455
    }
456
}