Subversion Repositories ALCASAR

Rev

Rev 3037 | 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();
3100 rexy 27
        if ((PSI_OS == 'Linux') && (!defined('PSI_EMU_HOSTNAME') || defined('PSI_EMU_PORT'))) switch (defined('PSI_SENSOR_HDDTEMP_ACCESS')?strtolower(PSI_SENSOR_HDDTEMP_ACCESS):'command') {
2770 rexy 28
        case 'tcp':
29
            $lines = '';
30
            // Timo van Roermund: connect to the hddtemp daemon, use a 5 second timeout.
3100 rexy 31
            $fp = @fsockopen(defined('PSI_EMU_HOSTNAME')?PSI_EMU_HOSTNAME:'localhost', 7634, $errno, $errstr, 5);
2770 rexy 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
        }
94
        // Timo van Roermund: parse the info from the hddtemp daemon.
95
        foreach ($ar_buf as $line) {
96
            $data = array();
97
            if (preg_match("/\|(.*)\|(.*)\|(.*)\|(.*)\|/", $line, $data)) {
98
                if (trim($data[3]) != "ERR") {
99
                    // get the info we need
100
                    $dev = new SensorDevice();
101
                    $dev->setName($data[1] . ' (' . (strpos($data[2], "  ")?substr($data[2], 0, strpos($data[2], "  ")):$data[2]) . ')');
102
                    if (is_numeric($data[3])) {
103
                        $dev->setValue($data[3]);
104
                    }
105
//                    $dev->setMax(60);
106
                    $this->mbinfo->setMbTemp($dev);
107
                }
108
            }
109
        }
110
    }
325 richard 111
 
2770 rexy 112
    /**
113
     * get the information
114
     *
115
     * @see PSI_Interface_Sensor::build()
116
     *
3037 rexy 117
     * @return void
2770 rexy 118
     */
119
    public function build()
120
    {
121
        $this->_temperature();
122
    }
325 richard 123
}