Subversion Repositories ALCASAR

Rev

Rev 3037 | Go to most recent revision | Details | Last modification | View Log

Rev Author Line No. Line
2770 rexy 1
<?php
2
/**
3
 * hwmon sensor class, getting hardware sensors information from /sys/class/hwmon/hwmon
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI_Sensor
9
 * @author    Mieczyslaw Nalewaj <namiltd@users.sourceforge.net>
10
 * @copyright 2016 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 Hwmon extends Sensors
16
{
17
    /**
18
     * get temperature information
19
     *
20
     * @param  string $hwpath
21
     * @return void
22
     */
23
    protected function _temperature($hwpath)
24
    {
25
       $sensor = glob($hwpath."temp*_input", GLOB_NOSORT);
26
       if (is_array($sensor) && (($total = count($sensor)) > 0)) {
27
            $buf = "";
28
            for ($i = 0; $i < $total; $i++) if (($buf = CommonFunctions::rolv($sensor[$i]))!==null) {
29
                $dev = new SensorDevice();
30
                $dev->setValue($buf/1000);
31
                if (($buf = CommonFunctions::rolv($sensor[$i], "/\/[^\/]*_input$/", "/name"))!==null) {
32
                   $name = " (".$buf.")";
33
                } else {
34
                   $name = "";
35
                }
36
                if (($buf = CommonFunctions::rolv($sensor[$i], "/_input$/", "_label"))!==null) {
37
                    $dev->setName($buf.$name);
38
                } else {
39
                    $labelname = trim(preg_replace("/_input$/", "", pathinfo($sensor[$i], PATHINFO_BASENAME)));
40
                    if ($labelname !== "") {
41
                        $dev->setName($labelname.$name);
42
                    } else {
43
                        $dev->setName('unknown'.$name);
44
                    }
45
                }
46
                if (($buf = CommonFunctions::rolv($sensor[$i], "/_input$/", "_crit"))!==null) {
47
                    $dev->setMax($buf/1000);
48
                    if (CommonFunctions::rolv($sensor[$i], "/_input$/", "_crit_alarm")==="1") {
49
                        $dev->setEvent("Critical Alarm");
50
                    }
51
                } elseif (($buf = CommonFunctions::rolv($sensor[$i], "/_input$/", "_max"))!==null) {
52
                    $dev->setMax($buf/1000);
53
                }
54
                $this->mbinfo->setMbTemp($dev);
55
            }
56
        }
57
    }
58
 
59
    /**
60
     * get voltage information
61
     *
62
     * @param  string $hwpath
63
     * @return void
64
     */
65
    private function _voltage($hwpath)
66
    {
67
       $sensor = glob($hwpath."in*_input", GLOB_NOSORT);
68
       if (is_array($sensor) && (($total = count($sensor)) > 0)) {
69
            $buf = "";
70
            for ($i = 0; $i < $total; $i++) if (($buf = CommonFunctions::rolv($sensor[$i]))!==null) {
71
                $dev = new SensorDevice();
72
                $dev->setValue($buf/1000);
73
                if (($buf = CommonFunctions::rolv($sensor[$i], "/\/[^\/]*_input$/", "/name"))!==null) {
74
                   $name = " (".$buf.")";
75
                } else {
76
                   $name = "";
77
                }
78
                if (($buf = CommonFunctions::rolv($sensor[$i], "/_input$/", "_label"))!==null) {
79
                    $dev->setName($buf.$name);
80
                } else {
81
                    $labelname = trim(preg_replace("/_input$/", "", pathinfo($sensor[$i], PATHINFO_BASENAME)));
82
                    if ($labelname !== "") {
83
                        $dev->setName($labelname.$name);
84
                    } else {
85
                        $dev->setName('unknown'.$name);
86
                    }
87
                }
88
                if (($buf = CommonFunctions::rolv($sensor[$i], "/_input$/", "_max"))!==null) {
89
                    $dev->setMax($buf/1000);
90
                }
91
                if (($buf = CommonFunctions::rolv($sensor[$i], "/_input$/", "_min"))!==null) {
92
                    $dev->setMin($buf/1000);
93
                }
94
                if (($buf = CommonFunctions::rolv($sensor[$i], "/_input$/", "_alarm"))==="1") {
95
                    $dev->setEvent("Alarm");
96
                }
97
                $this->mbinfo->setMbVolt($dev);
98
            }
99
        }
100
    }
101
 
102
    /**
103
     * get fan information
104
     *
105
     * @param  string $hwpath
106
     * @return void
107
     */
108
    protected function _fans($hwpath)
109
    {
110
       $sensor = glob($hwpath."fan*_input", GLOB_NOSORT);
111
       if (is_array($sensor) && (($total = count($sensor)) > 0)) {
112
            $buf = "";
113
            for ($i = 0; $i < $total; $i++) if (($buf = CommonFunctions::rolv($sensor[$i]))!==null) {
114
                $dev = new SensorDevice();
115
                $dev->setValue($buf);
116
                if (($buf = CommonFunctions::rolv($sensor[$i], "/\/[^\/]*_input$/", "/name"))!==null) {
117
                   $name = " (".$buf.")";
118
                } else {
119
                   $name = "";
120
                }
121
                if (($buf = CommonFunctions::rolv($sensor[$i], "/_input$/", "_label"))!==null) {
122
                    $dev->setName($buf.$name);
123
                } else {
124
                    $labelname = trim(preg_replace("/_input$/", "", pathinfo($sensor[$i], PATHINFO_BASENAME)));
125
                    if ($labelname !== "") {
126
                        $dev->setName($labelname.$name);
127
                    } else {
128
                        $dev->setName('unknown'.$name);
129
                    }
130
                }
131
                if (($buf = CommonFunctions::rolv($sensor[$i], "/_input$/", "_full_speed"))!==null) {
132
                    $dev->setMax($buf);
133
                } elseif (($buf = CommonFunctions::rolv($sensor[$i], "/_input$/", "_max"))!==null) {
134
                    $dev->setMax($buf);
135
                }
136
                if (($buf = CommonFunctions::rolv($sensor[$i], "/_input$/", "_min"))!==null) {
137
                    $dev->setMin($buf);
138
                }
139
                if (($buf = CommonFunctions::rolv($sensor[$i], "/_input$/", "_alarm"))==="1") {
140
                    $dev->setEvent("Alarm");
141
                }
142
                $this->mbinfo->setMbFan($dev);
143
            }
144
        }
145
    }
146
 
147
    /**
148
     * get power information
149
     *
150
     * @param  string $hwpath
151
     * @return void
152
     */
153
    private function _power($hwpath)
154
    {
155
       $sensor = glob($hwpath."power*_input", GLOB_NOSORT);
156
       if (is_array($sensor) && (($total = count($sensor)) > 0)) {
157
            $buf = "";
158
            for ($i = 0; $i < $total; $i++) if (($buf = CommonFunctions::rolv($sensor[$i]))!==null) {
159
                $dev = new SensorDevice();
160
                $dev->setValue($buf/1000000);
161
                if (($buf = CommonFunctions::rolv($sensor[$i], "/\/[^\/]*_input$/", "/name"))!==null) {
162
                   $name = " (".$buf.")";
163
                } else {
164
                   $name = "";
165
                }
166
                if (($buf = CommonFunctions::rolv($sensor[$i], "/_input$/", "_label"))!==null) {
167
                    $dev->setName($buf.$name);
168
                } else {
169
                    $labelname = trim(preg_replace("/_input$/", "", pathinfo($sensor[$i], PATHINFO_BASENAME)));
170
                    if ($labelname !== "") {
171
                        $dev->setName($labelname.$name);
172
                    } else {
173
                        $dev->setName('unknown'.$name);
174
                    }
175
                }
176
                if (($buf = CommonFunctions::rolv($sensor[$i], "/_input$/", "_max"))!==null) {
177
                    $dev->setMax($buf/1000000);
178
                }
179
                if (($buf = CommonFunctions::rolv($sensor[$i], "/_input$/", "_min"))!==null) {
180
                    $dev->setMin($buf/1000000);
181
                }
182
                if (($buf = CommonFunctions::rolv($sensor[$i], "/_input$/", "_alarm"))==="1") {
183
                    $dev->setEvent("Alarm");
184
                }
185
                $this->mbinfo->setMbPower($dev);
186
            }
187
        }
188
    }
189
 
190
    /**
191
     * get current information
192
     *
193
     * @param  string $hwpath
194
     * @return void
195
     */
196
    private function _current($hwpath)
197
    {
198
       $sensor = glob($hwpath."curr*_input", GLOB_NOSORT);
199
       if (is_array($sensor) && (($total = count($sensor)) > 0)) {
200
            $buf = "";
201
            for ($i = 0; $i < $total; $i++) if (($buf = CommonFunctions::rolv($sensor[$i]))!==null) {
202
                $dev = new SensorDevice();
203
                $dev->setValue($buf/1000);
204
                if (($buf = CommonFunctions::rolv($sensor[$i], "/\/[^\/]*_input$/", "/name"))!==null) {
205
                   $name = " (".$buf.")";
206
                } else {
207
                   $name = "";
208
                }
209
                if (($buf = CommonFunctions::rolv($sensor[$i], "/_input$/", "_label"))!==null) {
210
                    $dev->setName($buf.$name);
211
                } else {
212
                    $labelname = trim(preg_replace("/_input$/", "", pathinfo($sensor[$i], PATHINFO_BASENAME)));
213
                    if ($labelname !== "") {
214
                        $dev->setName($labelname.$name);
215
                    } else {
216
                        $dev->setName('unknown'.$name);
217
                    }
218
                }
219
                if (($buf = CommonFunctions::rolv($sensor[$i], "/_input$/", "_max"))!==null) {
220
                    $dev->setMax($buf/1000);
221
                }
222
                if (($buf = CommonFunctions::rolv($sensor[$i], "/_input$/", "_min"))!==null) {
223
                    $dev->setMin($buf/1000);
224
                }
225
                if (($buf = CommonFunctions::rolv($sensor[$i], "/_input$/", "_alarm"))==="1") {
226
                    $dev->setEvent("Alarm");
227
                }
228
                $this->mbinfo->setMbCurrent($dev);
229
            }
230
        }
231
    }
232
 
233
    /**
234
     * get the information
235
     *
236
     * @see PSI_Interface_Sensor::build()
237
     *
238
     * @return Void
239
     */
240
    public function build()
241
    {
242
        $hwpaths = glob("/sys/class/hwmon/hwmon*/", GLOB_NOSORT);
243
        if (is_array($hwpaths) && (count($hwpaths) > 0)) {
244
            $hwpaths = array_merge($hwpaths, glob("/sys/class/hwmon/hwmon*/device/", GLOB_NOSORT));
245
        }
246
        if (is_array($hwpaths) && (($totalh = count($hwpaths)) > 0)) {
247
            for ($h = 0; $h < $totalh; $h++) {
248
                $this->_temperature($hwpaths[$h]);
249
                $this->_voltage($hwpaths[$h]);
250
                $this->_fans($hwpaths[$h]);
251
                $this->_power($hwpaths[$h]);
252
                $this->_current($hwpaths[$h]);
253
            }
254
        }
255
    }
256
}