Subversion Repositories ALCASAR

Rev

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