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
325 richard 1
<?php
2770 rexy 2
/**
3
 * hddtemp sensor class, getting information from hddtemp
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI_Sensor
9
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
10
 * @author    T.A. van Roermund <timo@van-roermund.nl>
11
 * @copyright 2009 phpSysInfo
12
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
13
 * @version   Release: 3.0
14
 * @link      http://phpsysinfo.sourceforge.net
15
 */
16
class HDDTemp extends Sensors
17
{
18
    /**
19
     * get the temperature information from hddtemp
20
     * access is available through tcp or command
21
     *
22
     * @return void
23
     */
24
    private function _temperature()
25
    {
26
        $ar_buf = array();
27
        switch (defined('PSI_SENSOR_HDDTEMP_ACCESS')?strtolower(PSI_SENSOR_HDDTEMP_ACCESS):'command') {
28
        case 'tcp':
29
            $lines = '';
30
            // Timo van Roermund: connect to the hddtemp daemon, use a 5 second timeout.
31
            $fp = @fsockopen('localhost', 7634, $errno, $errstr, 5);
32
            // if connected, read the output of the hddtemp daemon
33
            if ($fp) {
34
                while (!feof($fp)) {
35
                    $lines .= fread($fp, 1024);
36
                }
37
                fclose($fp);
38
            } else {
39
                $this->error->addError("HDDTemp error", $errno.", ".$errstr);
40
            }
41
            $lines = str_replace("||", "|\n|", $lines);
42
            $ar_buf = preg_split("/\n/", $lines, -1, PREG_SPLIT_NO_EMPTY);
43
            break;
44
        case 'command':
45
            $strDrives = "";
46
            $strContent = "";
47
            $hddtemp_value = "";
48
            if (CommonFunctions::rfts("/proc/diskstats", $strContent, 0, 4096, false)) {
49
                $arrContent = preg_split("/\n/", $strContent, -1, PREG_SPLIT_NO_EMPTY);
50
                foreach ($arrContent as $strLine) {
51
                    preg_match("/^\s(.*)\s([a-z]*)\s(.*)/", $strLine, $arrSplit);
52
                    if (! empty($arrSplit[2])) {
53
                        $strDrive = '/dev/'.$arrSplit[2];
54
                        if (file_exists($strDrive)) {
55
                            $strDrives = $strDrives.$strDrive.' ';
56
                        }
57
                    }
58
                }
59
            } else {
60
                if (CommonFunctions::rfts("/proc/partitions", $strContent, 0, 4096, false)) {
61
                    $arrContent = preg_split("/\n/", $strContent, -1, PREG_SPLIT_NO_EMPTY);
62
                    foreach ($arrContent as $strLine) {
63
                        if (!preg_match("/^\s(.*)\s([\/a-z0-9]*(\/disc))\s(.*)/", $strLine, $arrSplit)) {
64
                            preg_match("/^\s(.*)\s([a-z]*)\s(.*)/", $strLine, $arrSplit);
65
                        }
66
                        if (! empty($arrSplit[2])) {
67
                            $strDrive = '/dev/'.$arrSplit[2];
68
                            if (file_exists($strDrive)) {
69
                                $strDrives = $strDrives.$strDrive.' ';
70
                            }
71
                        }
72
                    }
73
                }
74
            }
75
            if (trim($strDrives) == "") {
76
                break;
77
            }
78
            if (CommonFunctions::executeProgram("hddtemp", $strDrives, $hddtemp_value, PSI_DEBUG)) {
79
                $hddtemp_value = preg_split("/\n/", $hddtemp_value, -1, PREG_SPLIT_NO_EMPTY);
80
                foreach ($hddtemp_value as $line) {
81
                    $temp = preg_split("/:\s/", $line, 3);
82
                    if (count($temp) == 3 && preg_match("/^[0-9]/", $temp[2])) {
83
                        preg_match("/^([0-9]*)(.*)/", $temp[2], $ar_temp);
84
                        $temp[2] = trim($ar_temp[1]);
85
                        $temp[3] = trim($ar_temp[2]);
86
                        array_push($ar_buf, "|".implode("|", $temp)."|");
87
                    }
88
                }
89
            }
90
            break;
91
        default:
92
            $this->error->addConfigError("temperature()", "[sensor_hddtemp] ACCESS");
93
            break;
94
        }
95
        // Timo van Roermund: parse the info from the hddtemp daemon.
96
        foreach ($ar_buf as $line) {
97
            $data = array();
98
            if (preg_match("/\|(.*)\|(.*)\|(.*)\|(.*)\|/", $line, $data)) {
99
                if (trim($data[3]) != "ERR") {
100
                    // get the info we need
101
                    $dev = new SensorDevice();
102
                    $dev->setName($data[1] . ' (' . (strpos($data[2], "  ")?substr($data[2], 0, strpos($data[2], "  ")):$data[2]) . ')');
103
                    if (is_numeric($data[3])) {
104
                        $dev->setValue($data[3]);
105
                    }
106
//                    $dev->setMax(60);
107
                    $this->mbinfo->setMbTemp($dev);
108
                }
109
            }
110
        }
111
    }
325 richard 112
 
2770 rexy 113
    /**
114
     * get the information
115
     *
116
     * @see PSI_Interface_Sensor::build()
117
     *
118
     * @return Void
119
     */
120
    public function build()
121
    {
122
        $this->_temperature();
123
    }
325 richard 124
}