Subversion Repositories ALCASAR

Rev

Rev 3037 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 3037 Rev 3100
1
<?php
1
<?php
2
/**
2
/**
3
 * Apcupsd class
3
 * Apcupsd 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    Michael Cramer <BigMichi1@users.sourceforge.net>
9
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
10
 * @copyright 2009 phpSysInfo
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
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   SVN: $Id: class.apcupsd.inc.php 661 2012-08-27 11:26:39Z namiltd $
12
 * @version   SVN: $Id: class.apcupsd.inc.php 661 2012-08-27 11:26:39Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
14
 */
15
 /**
15
 /**
16
 * getting ups information from apcupsd program
16
 * getting ups information from apcupsd program
17
 *
17
 *
18
 * @category  PHP
18
 * @category  PHP
19
 * @package   PSI_UPS
19
 * @package   PSI_UPS
20
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
20
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
21
 * @author    Artem Volk <artvolk@mail.ru>
21
 * @author    Artem Volk <artvolk@mail.ru>
22
 * @copyright 2009 phpSysInfo
22
 * @copyright 2009 phpSysInfo
23
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
23
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
24
 * @version   Release: 3.0
24
 * @version   Release: 3.0
25
 * @link      http://phpsysinfo.sourceforge.net
25
 * @link      http://phpsysinfo.sourceforge.net
26
 */
26
 */
27
class Apcupsd extends UPS
27
class Apcupsd extends UPS
28
{
28
{
29
    /**
29
    /**
30
     * internal storage for all gathered data
30
     * internal storage for all gathered data
31
     *
31
     *
32
     * @var array
32
     * @var array
33
     */
33
     */
34
    private $_output = array();
34
    private $_output = array();
35
 
35
 
36
    /**
36
    /**
37
     * get all information from all configured ups in phpsysinfo.ini and store output in internal array
37
     * get all information from all configured ups in phpsysinfo.ini and store output in internal array
38
     */
38
     */
39
    public function __construct()
39
    public function __construct()
40
    {
40
    {
41
        parent::__construct();
41
        parent::__construct();
42
        if (!defined('PSI_UPS_APCUPSD_ACCESS')) {
42
        if (!defined('PSI_UPS_APCUPSD_ACCESS')) {
43
             define('PSI_UPS_APCUPSD_ACCESS', false);
43
             define('PSI_UPS_APCUPSD_ACCESS', false);
44
        }
44
        }
45
        switch (strtolower(PSI_UPS_APCUPSD_ACCESS)) {
45
        switch (strtolower(PSI_UPS_APCUPSD_ACCESS)) {
46
        case 'data':
46
        case 'data':
47
            if (defined('PSI_UPS_APCUPSD_LIST') && is_string(PSI_UPS_APCUPSD_LIST)) {
47
            if (defined('PSI_UPS_APCUPSD_LIST') && is_string(PSI_UPS_APCUPSD_LIST)) {
48
                if (preg_match(ARRAY_EXP, PSI_UPS_APCUPSD_LIST)) {
48
                if (preg_match(ARRAY_EXP, PSI_UPS_APCUPSD_LIST)) {
49
                    $upss = eval(PSI_UPS_APCUPSD_LIST);
49
                    $upss = eval(PSI_UPS_APCUPSD_LIST);
50
                } else {
50
                } else {
51
                    $upss = array(PSI_UPS_APCUPSD_LIST);
51
                    $upss = array(PSI_UPS_APCUPSD_LIST);
52
                }
52
                }
53
            } else {
53
            } else {
54
               $upss = array('UPS');
54
               $upss = array('UPS');
55
            }
55
            }
56
            $un = 0;
56
            $un = 0;
57
            foreach ($upss as $ups) {
57
            foreach ($upss as $ups) {
58
                $temp = "";
58
                $temp = "";
59
                CommonFunctions::rftsdata("upsapcupsd{$un}.tmp", $temp);
59
                CommonFunctions::rftsdata("upsapcupsd{$un}.tmp", $temp);
60
                if (! empty($temp)) {
60
                if (! empty($temp)) {
61
                    $this->_output[] = $temp;
61
                    $this->_output[] = $temp;
62
                }
62
                }
63
                $un++;
63
                $un++;
64
            }
64
            }
65
            break;
65
            break;
66
        default:
66
        default:
67
            if (defined('PSI_UPS_APCUPSD_LIST') && is_string(PSI_UPS_APCUPSD_LIST)) {
67
            if (defined('PSI_UPS_APCUPSD_LIST') && is_string(PSI_UPS_APCUPSD_LIST)) {
68
                if (preg_match(ARRAY_EXP, PSI_UPS_APCUPSD_LIST)) {
68
                if (preg_match(ARRAY_EXP, PSI_UPS_APCUPSD_LIST)) {
69
                    $upses = eval(PSI_UPS_APCUPSD_LIST);
69
                    $upses = eval(PSI_UPS_APCUPSD_LIST);
70
                } else {
70
                } else {
71
                    $upses = array(PSI_UPS_APCUPSD_LIST);
71
                    $upses = array(PSI_UPS_APCUPSD_LIST);
72
                }
72
                }
73
                foreach ($upses as $ups) {
73
                foreach ($upses as $ups) {
74
                    $temp = "";
74
                    $temp = "";
75
                    CommonFunctions::executeProgram('apcaccess', 'status '.trim($ups), $temp);
75
                    CommonFunctions::executeProgram('apcaccess', 'status '.trim($ups), $temp);
76
                    if (! empty($temp)) {
76
                    if (! empty($temp)) {
77
                        $this->_output[] = $temp;
77
                        $this->_output[] = $temp;
78
                    }
78
                    }
79
                }
79
                }
80
            } else { //use default if address and port not defined
80
            } else { //use default if address and port not defined
81
                if (!defined('PSI_EMU_HOSTNAME')) {
81
                if (!defined('PSI_EMU_HOSTNAME') || defined('PSI_EMU_PORT')) {
82
                    CommonFunctions::executeProgram('apcaccess', 'status', $temp);
82
                    CommonFunctions::executeProgram('apcaccess', 'status', $temp);
83
                } else {
83
                } else {
84
                    CommonFunctions::executeProgram('apcaccess', 'status '.PSI_EMU_HOSTNAME, $temp);
84
                    CommonFunctions::executeProgram('apcaccess', 'status '.PSI_EMU_HOSTNAME, $temp);
85
                }
85
                }
86
                if (! empty($temp)) {
86
                if (! empty($temp)) {
87
                    $this->_output[] = $temp;
87
                    $this->_output[] = $temp;
88
                }
88
                }
89
            }
89
            }
90
        }
90
        }
91
    }
91
    }
92
 
92
 
93
    /**
93
    /**
94
     * parse the input and store data in resultset for xml generation
94
     * parse the input and store data in resultset for xml generation
95
     *
95
     *
96
     * @return void
96
     * @return void
97
     */
97
     */
98
    private function _info()
98
    private function _info()
99
    {
99
    {
100
        foreach ($this->_output as $ups) {
100
        foreach ($this->_output as $ups) {
101
 
101
 
102
            $dev = new UPSDevice();
102
            $dev = new UPSDevice();
103
 
103
 
104
            // General info
104
            // General info
105
            if (preg_match('/^UPSNAME\s*:\s*(.*)$/m', $ups, $data)) {
105
            if (preg_match('/^UPSNAME\s*:\s*(.*)$/m', $ups, $data)) {
106
                $dev->setName(trim($data[1]));
106
                $dev->setName(trim($data[1]));
107
            }
107
            }
108
            if (preg_match('/^MODEL\s*:\s*(.*)$/m', $ups, $data)) {
108
            if (preg_match('/^MODEL\s*:\s*(.*)$/m', $ups, $data)) {
109
                $model = trim($data[1]);
109
                $model = trim($data[1]);
110
                if (preg_match('/^APCMODEL\s*:\s*(.*)$/m', $ups, $data)) {
110
                if (preg_match('/^APCMODEL\s*:\s*(.*)$/m', $ups, $data)) {
111
                    $dev->setModel($model.' ('.trim($data[1]).')');
111
                    $dev->setModel($model.' ('.trim($data[1]).')');
112
                } else {
112
                } else {
113
                    $dev->setModel($model);
113
                    $dev->setModel($model);
114
                }
114
                }
115
            }
115
            }
116
            if (preg_match('/^UPSMODE\s*:\s*(.*)$/m', $ups, $data)) {
116
            if (preg_match('/^UPSMODE\s*:\s*(.*)$/m', $ups, $data)) {
117
                $dev->setMode(trim($data[1]));
117
                $dev->setMode(trim($data[1]));
118
            }
118
            }
119
            if (preg_match('/^STARTTIME\s*:\s*(.*)$/m', $ups, $data)) {
119
            if (preg_match('/^STARTTIME\s*:\s*(.*)$/m', $ups, $data)) {
120
                $dev->setStartTime(trim($data[1]));
120
                $dev->setStartTime(trim($data[1]));
121
            }
121
            }
122
            if (preg_match('/^STATUS\s*:\s*(.*)$/m', $ups, $data)) {
122
            if (preg_match('/^STATUS\s*:\s*(.*)$/m', $ups, $data)) {
123
                $dev->setStatus(trim($data[1]));
123
                $dev->setStatus(trim($data[1]));
124
            }
124
            }
125
            if (preg_match('/^ITEMP\s*:\s*(.*)$/m', $ups, $data)) {
125
            if (preg_match('/^ITEMP\s*:\s*(.*)$/m', $ups, $data)) {
126
                $temperatur = trim($data[1]);
126
                $temperatur = trim($data[1]);
127
                if (($temperatur !== "-273.1 C") && ($temperatur !== "-273.1 C Internal")) {
127
                if (($temperatur !== "-273.1 C") && ($temperatur !== "-273.1 C Internal")) {
128
                    $dev->setTemperatur($temperatur);
128
                    $dev->setTemperatur($temperatur);
129
                }
129
                }
130
            }
130
            }
131
            // Outages
131
            // Outages
132
            if (preg_match('/^NUMXFERS\s*:\s*(.*)$/m', $ups, $data)) {
132
            if (preg_match('/^NUMXFERS\s*:\s*(.*)$/m', $ups, $data)) {
133
                $dev->setOutages(trim($data[1]));
133
                $dev->setOutages(trim($data[1]));
134
            }
134
            }
135
            if (preg_match('/^LASTXFER\s*:\s*(.*)$/m', $ups, $data)) {
135
            if (preg_match('/^LASTXFER\s*:\s*(.*)$/m', $ups, $data)) {
136
                $dev->setLastOutage(trim($data[1]));
136
                $dev->setLastOutage(trim($data[1]));
137
            }
137
            }
138
            if (preg_match('/^XOFFBATT\s*:\s*(.*)$/m', $ups, $data)) {
138
            if (preg_match('/^XOFFBATT\s*:\s*(.*)$/m', $ups, $data)) {
139
                $dev->setLastOutageFinish(trim($data[1]));
139
                $dev->setLastOutageFinish(trim($data[1]));
140
            }
140
            }
141
            // Line
141
            // Line
142
            if (preg_match('/^LINEV\s*:\s*(\d*\.\d*)(.*)$/m', $ups, $data)) {
142
            if (preg_match('/^LINEV\s*:\s*(\d*\.\d*)(.*)$/m', $ups, $data)) {
143
                $dev->setLineVoltage(trim($data[1]));
143
                $dev->setLineVoltage(trim($data[1]));
144
            }
144
            }
145
            if (preg_match('/^LINEFREQ\s*:\s*(\d*\.\d*)(.*)$/m', $ups, $data)) {
145
            if (preg_match('/^LINEFREQ\s*:\s*(\d*\.\d*)(.*)$/m', $ups, $data)) {
146
                $dev->setLineFrequency(trim($data[1]));
146
                $dev->setLineFrequency(trim($data[1]));
147
            }
147
            }
148
            if (preg_match('/^LOADPCT\s*:\s*(\d*\.\d*)(.*)$/m', $ups, $data)) {
148
            if (preg_match('/^LOADPCT\s*:\s*(\d*\.\d*)(.*)$/m', $ups, $data)) {
149
                $dev->setLoad(trim($data[1]));
149
                $dev->setLoad(trim($data[1]));
150
            }
150
            }
151
            // Battery
151
            // Battery
152
            if (preg_match('/^BATTDATE\s*:\s*(.*)$/m', $ups, $data)) {
152
            if (preg_match('/^BATTDATE\s*:\s*(.*)$/m', $ups, $data)) {
153
                $dev->setBatteryDate(trim($data[1]));
153
                $dev->setBatteryDate(trim($data[1]));
154
            }
154
            }
155
            if (preg_match('/^BATTV\s*:\s*(\d*\.\d*)(.*)$/m', $ups, $data)) {
155
            if (preg_match('/^BATTV\s*:\s*(\d*\.\d*)(.*)$/m', $ups, $data)) {
156
                $dev->setBatteryVoltage(trim($data[1]));
156
                $dev->setBatteryVoltage(trim($data[1]));
157
            }
157
            }
158
            if (preg_match('/^BCHARGE\s*:\s*(\d*\.\d*)(.*)$/m', $ups, $data)) {
158
            if (preg_match('/^BCHARGE\s*:\s*(\d*\.\d*)(.*)$/m', $ups, $data)) {
159
                $dev->setBatterCharge(trim($data[1]));
159
                $dev->setBatterCharge(trim($data[1]));
160
            }
160
            }
161
            if (preg_match('/^TIMELEFT\s*:\s*(\d*\.\d*)(.*)$/m', $ups, $data)) {
161
            if (preg_match('/^TIMELEFT\s*:\s*(\d*\.\d*)(.*)$/m', $ups, $data)) {
162
                $dev->setTimeLeft(trim($data[1]));
162
                $dev->setTimeLeft(trim($data[1]));
163
            }
163
            }
164
            $this->upsinfo->setUpsDevices($dev);
164
            $this->upsinfo->setUpsDevices($dev);
165
        }
165
        }
166
    }
166
    }
167
 
167
 
168
    /**
168
    /**
169
     * get the information
169
     * get the information
170
     *
170
     *
171
     * @see PSI_Interface_UPS::build()
171
     * @see PSI_Interface_UPS::build()
172
     *
172
     *
173
     * @return void
173
     * @return void
174
     */
174
     */
175
    public function build()
175
    public function build()
176
    {
176
    {
177
        $this->_info();
177
        $this->_info();
178
    }
178
    }
179
}
179
}
180
 
180