Subversion Repositories ALCASAR

Rev

Rev 3037 | Details | Compare with Previous | Last modification | View Log

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