Subversion Repositories ALCASAR

Rev

Rev 2976 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 2976 Rev 3037
1
<?php
1
<?php
2
/**
2
/**
3
 * Nut class
3
 * Nut class
4
 *
4
 *
5
 * PHP version 5
5
 * PHP version 5
6
 *
6
 *
7
 * @category  PHP
7
 * @category  PHP
8
 * @package   PSI_UPS
8
 * @package   PSI_UPS
9
 * @author    Artem Volk <artvolk@mail.ru>
9
 * @author    Artem Volk <artvolk@mail.ru>
10
 * @author    Anders Häggström <hagge@users.sourceforge.net>
10
 * @author    Anders Häggström <hagge@users.sourceforge.net>
11
 * @copyright 2009 phpSysInfo
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
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   SVN: $Id: class.nut.inc.php 661 2012-08-27 11:26:39Z namiltd $
13
 * @version   SVN: $Id: class.nut.inc.php 661 2012-08-27 11:26:39Z namiltd $
14
 * @link      http://phpsysinfo.sourceforge.net
14
 * @link      http://phpsysinfo.sourceforge.net
15
 */
15
 */
16
 /**
16
 /**
17
 * getting ups information from upsc program
17
 * getting ups information from upsc program
18
 *
18
 *
19
 * @category  PHP
19
 * @category  PHP
20
 * @package   PSI_UPS
20
 * @package   PSI_UPS
21
 * @author    Artem Volk <artvolk@mail.ru>
21
 * @author    Artem Volk <artvolk@mail.ru>
22
 * @author    Anders Häggström <hagge@users.sourceforge.net>
22
 * @author    Anders Häggström <hagge@users.sourceforge.net>
23
 * @copyright 2009 phpSysInfo
23
 * @copyright 2009 phpSysInfo
24
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
24
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
25
 * @version   Release: 3.0
25
 * @version   Release: 3.0
26
 * @link      http://phpsysinfo.sourceforge.net
26
 * @link      http://phpsysinfo.sourceforge.net
27
 */
27
 */
28
class Nut extends UPS
28
class Nut extends UPS
29
{
29
{
30
    /**
30
    /**
31
     * internal storage for all gathered data
31
     * internal storage for all gathered data
32
     *
32
     *
33
     * @var array
33
     * @var array
34
     */
34
     */
35
    private $_output = array();
35
    private $_output = array();
36
 
36
 
37
    /**
37
    /**
38
     * get all information from all configured ups and store output in internal array
38
     * get all information from all configured ups and store output in internal array
39
     */
39
     */
40
    public function __construct()
40
    public function __construct()
41
    {
41
    {
42
        parent::__construct();
42
        parent::__construct();
-
 
43
        if (!defined('PSI_UPS_NUT_ACCESS')) {
-
 
44
             define('PSI_UPS_NUT_ACCESS', false);
-
 
45
        }
-
 
46
        switch (strtolower(PSI_UPS_NUT_ACCESS)) {
-
 
47
        case 'data':
43
        if (defined('PSI_UPS_NUT_LIST') && is_string(PSI_UPS_NUT_LIST)) {
48
            if (defined('PSI_UPS_NUT_LIST') && is_string(PSI_UPS_NUT_LIST)) {
44
            if (preg_match(ARRAY_EXP, PSI_UPS_NUT_LIST)) {
49
                if (preg_match(ARRAY_EXP, PSI_UPS_NUT_LIST)) {
45
                $upses = eval(PSI_UPS_NUT_LIST);
50
                    $upss = eval(PSI_UPS_NUT_LIST);
-
 
51
                } else {
-
 
52
                    $upss = array(PSI_UPS_NUT_LIST);
-
 
53
                }
46
            } else {
54
            } else {
47
                $upses = array(PSI_UPS_NUT_LIST);
55
               $upss = array('UPS');
-
 
56
            }
-
 
57
            $un = 0;
-
 
58
            foreach ($upss as $ups) {
-
 
59
                $temp = "";
-
 
60
                CommonFunctions::rftsdata("upsnut{$un}.tmp", $temp);
-
 
61
                if (! empty($temp)) {
-
 
62
                    $this->_output[$ups] = $temp;
-
 
63
                }
-
 
64
                $un++;
48
            }
65
            }
-
 
66
            break;
-
 
67
        default:
-
 
68
            if (defined('PSI_UPS_NUT_LIST') && is_string(PSI_UPS_NUT_LIST)) {
-
 
69
                if (preg_match(ARRAY_EXP, PSI_UPS_NUT_LIST)) {
-
 
70
                    $upses = eval(PSI_UPS_NUT_LIST);
-
 
71
                } else {
-
 
72
                    $upses = array(PSI_UPS_NUT_LIST);
-
 
73
                }
49
            foreach ($upses as $ups) {
74
                foreach ($upses as $ups) {
50
                CommonFunctions::executeProgram('upsc', '-l '.trim($ups), $output, PSI_DEBUG);
75
                    CommonFunctions::executeProgram('upsc', '-l '.trim($ups), $output, PSI_DEBUG);
-
 
76
                    $ups_names = preg_split("/\n/", $output, -1, PREG_SPLIT_NO_EMPTY);
-
 
77
                    foreach ($ups_names as $ups_name) {
-
 
78
                        $upsname = trim($ups_name).'@'.trim($ups);
-
 
79
                        $temp = "";
-
 
80
                        CommonFunctions::executeProgram('upsc', $upsname, $temp, PSI_DEBUG);
-
 
81
                        if (! empty($temp)) {
-
 
82
                            $this->_output[$upsname] = $temp;
-
 
83
                        }
-
 
84
                    }
-
 
85
                }
-
 
86
            } else { //use default if address and port not defined
-
 
87
                if (!defined('PSI_EMU_HOSTNAME')) {
-
 
88
                    CommonFunctions::executeProgram('upsc', '-l', $output, PSI_DEBUG);
-
 
89
                } else {
-
 
90
                    CommonFunctions::executeProgram('upsc', '-l '.PSI_EMU_HOSTNAME, $output, PSI_DEBUG);
-
 
91
                }
51
                $ups_names = preg_split("/\n/", $output, -1, PREG_SPLIT_NO_EMPTY);
92
                $ups_names = preg_split("/\n/", $output, -1, PREG_SPLIT_NO_EMPTY);
52
                foreach ($ups_names as $ups_name) {
93
                foreach ($ups_names as $ups_name) {
-
 
94
                    $temp = "";
53
                    CommonFunctions::executeProgram('upsc', trim($ups_name).'@'.trim($ups), $temp, PSI_DEBUG);
95
                    CommonFunctions::executeProgram('upsc', trim($ups_name), $temp, PSI_DEBUG);
54
                    if (! empty($temp)) {
96
                    if (! empty($temp)) {
55
                        $this->_output[trim($ups_name).'@'.trim($ups)] = $temp;
97
                        $this->_output[trim($ups_name)] = $temp;
56
                    }
98
                    }
57
                }
99
                }
58
            }
100
            }
59
        } else { //use default if address and port not defined
-
 
60
            if (!defined('PSI_EMU_HOSTNAME')) {
-
 
61
                CommonFunctions::executeProgram('upsc', '-l', $output, PSI_DEBUG);
-
 
62
            } else {
-
 
63
                CommonFunctions::executeProgram('upsc', '-l '.PSI_EMU_HOSTNAME, $output, PSI_DEBUG);
-
 
64
            }
-
 
65
            $ups_names = preg_split("/\n/", $output, -1, PREG_SPLIT_NO_EMPTY);
-
 
66
            foreach ($ups_names as $ups_name) {
-
 
67
                CommonFunctions::executeProgram('upsc', trim($ups_name), $temp, PSI_DEBUG);
-
 
68
                if (! empty($temp)) {
-
 
69
                    $this->_output[trim($ups_name)] = $temp;
-
 
70
                }
-
 
71
            }
-
 
72
        }
101
        }
73
    }
102
    }
74
 
103
 
75
    /**
104
    /**
76
     * parse the input and store data in resultset for xml generation
105
     * parse the input and store data in resultset for xml generation
77
     *
106
     *
78
     * @return void
107
     * @return void
79
     */
108
     */
80
    private function _info()
109
    private function _info()
81
    {
110
    {
82
        if (! empty($this->_output)) {
111
        if (! empty($this->_output)) {
83
            foreach ($this->_output as $name => $value) {
112
            foreach ($this->_output as $name => $value) {
84
                $temp = preg_split("/\n/", $value, -1, PREG_SPLIT_NO_EMPTY);
113
                $temp = preg_split("/\n/", $value, -1, PREG_SPLIT_NO_EMPTY);
85
                $ups_data = array();
114
                $ups_data = array();
86
                foreach ($temp as $valueTemp) {
115
                foreach ($temp as $valueTemp) {
87
                    $line = preg_split('/: /', $valueTemp, 2);
116
                    $line = preg_split('/: /', $valueTemp, 2);
88
                    $ups_data[$line[0]] = isset($line[1]) ? trim($line[1]) : '';
117
                    $ups_data[$line[0]] = isset($line[1]) ? trim($line[1]) : '';
89
                }
118
                }
90
                $dev = new UPSDevice();
119
                $dev = new UPSDevice();
91
                //General
120
                //General
92
                $dev->setName($name);
121
                $dev->setName($name);
93
                if (isset($ups_data['ups.model'])) {
122
                if (isset($ups_data['ups.model'])) {
94
                    $dev->setModel($ups_data['ups.model']);
123
                    $dev->setModel($ups_data['ups.model']);
95
                }
124
                }
96
                if (isset($ups_data['driver.name'])) {
125
                if (isset($ups_data['driver.name'])) {
97
                    $dev->setMode($ups_data['driver.name']);
126
                    $dev->setMode($ups_data['driver.name']);
98
                }
127
                }
99
                if (isset($ups_data['ups.status'])) {
128
                if (isset($ups_data['ups.status'])) {
100
                    $dev->setStatus($ups_data['ups.status']);
129
                    $dev->setStatus($ups_data['ups.status']);
101
                }
130
                }
-
 
131
                if (isset($ups_data['ups.beeper.status'])) {
-
 
132
                    $dev->setBeeperStatus($ups_data['ups.beeper.status']);
-
 
133
                }
102
 
134
 
103
                //Line
135
                //Line
104
                if (isset($ups_data['input.voltage'])) {
136
                if (isset($ups_data['input.voltage'])) {
105
                    $dev->setLineVoltage($ups_data['input.voltage']);
137
                    $dev->setLineVoltage($ups_data['input.voltage']);
106
                }
138
                }
107
                if (isset($ups_data['input.frequency'])) {
139
                if (isset($ups_data['input.frequency'])) {
108
                    $dev->setLineFrequency($ups_data['input.frequency']);
140
                    $dev->setLineFrequency($ups_data['input.frequency']);
109
                }
141
                }
110
                if (isset($ups_data['ups.load'])) {
142
                if (isset($ups_data['ups.load'])) {
111
                    $dev->setLoad($ups_data['ups.load']);
143
                    $dev->setLoad($ups_data['ups.load']);
112
                }
144
                }
113
 
145
 
114
                //Battery
146
                //Battery
115
                if (isset($ups_data['battery.voltage'])) {
147
                if (isset($ups_data['battery.voltage'])) {
116
                    $dev->setBatteryVoltage($ups_data['battery.voltage']);
148
                    $dev->setBatteryVoltage($ups_data['battery.voltage']);
117
                }
149
                }
118
                if (isset($ups_data['battery.charge'])) {
150
                if (isset($ups_data['battery.charge'])) {
119
                    $dev->setBatterCharge($ups_data['battery.charge']);
151
                    $dev->setBatterCharge($ups_data['battery.charge']);
120
                }
152
                }
121
                if (isset($ups_data['battery.runtime'])) {
153
                if (isset($ups_data['battery.runtime'])) {
122
                    $dev->setTimeLeft(round($ups_data['battery.runtime']/60, 2));
154
                    $dev->setTimeLeft(round($ups_data['battery.runtime']/60, 2));
123
                }
155
                }
124
 
156
 
125
                //Temperature
157
                //Temperature
126
                if (isset($ups_data['ups.temperature'])) {
158
                if (isset($ups_data['ups.temperature'])) {
127
                    $dev->setTemperatur($ups_data['ups.temperature']);
159
                    $dev->setTemperatur($ups_data['ups.temperature']);
128
                }
160
                }
129
 
161
 
130
                $this->upsinfo->setUpsDevices($dev);
162
                $this->upsinfo->setUpsDevices($dev);
131
            }
163
            }
132
        }
164
        }
133
    }
165
    }
134
 
166
 
135
    /**
167
    /**
136
     * get the information
168
     * get the information
137
     *
169
     *
138
     * @see PSI_Interface_UPS::build()
170
     * @see PSI_Interface_UPS::build()
139
     *
171
     *
140
     * @return Void
172
     * @return void
141
     */
173
     */
142
    public function build()
174
    public function build()
143
    {
175
    {
144
        $this->_info();
176
        $this->_info();
145
    }
177
    }
146
}
178
}
147
 
179