Subversion Repositories ALCASAR

Rev

Go to most recent revision | Details | Last modification | View Log

Rev Author Line No. Line
2775 rexy 1
<?php
2
/**
3
 * SNMPups 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 SNMPups 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 SNMPups 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();
42
        switch (strtolower(PSI_UPS_SNMPUPS_ACCESS)) {
43
        case 'command':
44
                if (defined('PSI_UPS_SNMPUPS_LIST') && is_string(PSI_UPS_SNMPUPS_LIST)) {
45
                    if (preg_match(ARRAY_EXP, PSI_UPS_SNMPUPS_LIST)) {
46
                        $upss = eval(PSI_UPS_SNMPUPS_LIST);
47
                    } else {
48
                        $upss = array(PSI_UPS_SNMPUPS_LIST);
49
                    }
50
                    foreach ($upss as $ups) {
51
                        CommonFunctions::executeProgram("snmpwalk", "-Ona -c public -v 1 -t ".PSI_SNMP_TIMEOUT_INT." -r ".PSI_SNMP_RETRY_INT." ".$ups." .1.3.6.1.4.1.318.1.1.1.1", $buffer, PSI_DEBUG);
52
                        if (strlen($buffer) > 0) {
53
                            $this->_output[$ups] = $buffer;
54
                            $buffer = "";
55
                            CommonFunctions::executeProgram("snmpwalk", "-Ona -c public -v 1 -t ".PSI_SNMP_TIMEOUT_INT." -r ".PSI_SNMP_RETRY_INT." ".$ups." .1.3.6.1.4.1.318.1.1.1.2", $buffer, PSI_DEBUG);
56
                            if (strlen($buffer) > 0) {
57
                                $this->_output[$ups] .=  "\n".$buffer;
58
                            }
59
                            $buffer = "";
60
                            CommonFunctions::executeProgram("snmpwalk", "-Ona -c public -v 1 -t ".PSI_SNMP_TIMEOUT_INT." -r ".PSI_SNMP_RETRY_INT." ".$ups." .1.3.6.1.4.1.318.1.1.1.3", $buffer, PSI_DEBUG);
61
                            if (strlen($buffer) > 0) {
62
                                $this->_output[$ups] .=  "\n".$buffer;
63
                            }
64
                            $buffer = "";
65
                            CommonFunctions::executeProgram("snmpwalk", "-Ona -c public -v 1 -t ".PSI_SNMP_TIMEOUT_INT." -r ".PSI_SNMP_RETRY_INT." ".$ups." .1.3.6.1.4.1.318.1.1.1.4", $buffer, PSI_DEBUG);
66
                            if (strlen($buffer) > 0) {
67
                                $this->_output[$ups] .=  "\n".$buffer;
68
                            }
69
                        }
70
                    }
71
                }
72
                break;
73
        case 'php-snmp':
74
                if (!extension_loaded("snmp")) {
75
                    $this->error->addError("Requirements error", "SNMPups plugin requires the snmp extension to php in order to work properly");
76
                    break;
77
                }
78
                snmp_set_valueretrieval(SNMP_VALUE_LIBRARY);
79
                snmp_set_oid_output_format(SNMP_OID_OUTPUT_NUMERIC);
80
                if (defined('PSI_UPS_SNMPUPS_LIST') && is_string(PSI_UPS_SNMPUPS_LIST)) {
81
                    if (preg_match(ARRAY_EXP, PSI_UPS_SNMPUPS_LIST)) {
82
                        $upss = eval(PSI_UPS_SNMPUPS_LIST);
83
                    } else {
84
                        $upss = array(PSI_UPS_SNMPUPS_LIST);
85
                    }
86
                    foreach ($upss as $ups) {
87
                        if (! PSI_DEBUG) {
88
                            restore_error_handler(); /* default error handler */
89
                            $old_err_rep = error_reporting();
90
                            error_reporting(E_ERROR); /* fatal errors only */
91
                        }
92
                        $bufferarr=snmprealwalk($ups, "public", ".1.3.6.1.4.1.318.1.1.1.1", 1000000 * PSI_SNMP_TIMEOUT_INT, PSI_SNMP_RETRY_INT);
93
                        if (! PSI_DEBUG) {
94
                            error_reporting($old_err_rep); /* restore error level */
95
                            set_error_handler('errorHandlerPsi'); /* restore error handler */
96
                        }
97
                        if (! empty($bufferarr)) {
98
                            $buffer="";
99
                            foreach ($bufferarr as $id=>$string) {
100
                                $buffer .= $id." = ".$string."\n";
101
                            }
102
 
103
                            if (! PSI_DEBUG) {
104
                                restore_error_handler(); /* default error handler */
105
                                $old_err_rep = error_reporting();
106
                                error_reporting(E_ERROR); /* fatal errors only */
107
                            }
108
                            $bufferarr2=snmprealwalk($ups, "public", ".1.3.6.1.4.1.318.1.1.1.2", 1000000 * PSI_SNMP_TIMEOUT_INT, PSI_SNMP_RETRY_INT);
109
                            $bufferarr3=snmprealwalk($ups, "public", ".1.3.6.1.4.1.318.1.1.1.3", 1000000 * PSI_SNMP_TIMEOUT_INT, PSI_SNMP_RETRY_INT);
110
                            $bufferarr4=snmprealwalk($ups, "public", ".1.3.6.1.4.1.318.1.1.1.4", 1000000 * PSI_SNMP_TIMEOUT_INT, PSI_SNMP_RETRY_INT);
111
                            if (! PSI_DEBUG) {
112
                                error_reporting($old_err_rep); /* restore error level */
113
                                set_error_handler('errorHandlerPsi'); /* restore error handler */
114
                            }
115
                            if (! empty($bufferarr2)) {
116
                                foreach ($bufferarr2 as $id=>$string) {
117
                                    $buffer .= $id." = ".$string."\n";
118
                                }
119
                            if (! empty($bufferarr3)) {
120
                                foreach ($bufferarr3 as $id=>$string) {
121
                                    $buffer .= $id." = ".$string."\n";
122
                                }
123
                            }                            }
124
                            if (! empty($bufferarr4)) {
125
                                foreach ($bufferarr4 as $id=>$string) {
126
                                    $buffer .= $id." = ".$string."\n";
127
                                }
128
                            }
129
                            if (strlen(trim($buffer)) > 0) {
130
                                $this->_output[$ups] = $buffer;
131
                            }
132
                        }
133
                    }
134
                }
135
                break;
136
            default:
137
                $this->error->addError("switch(PSI_UPS_SNMPUPS_ACCESS)", "Bad SNMPups configuration in phpsysinfo.ini");
138
                break;
139
        }
140
    }
141
 
142
    /**
143
     * parse the input and store data in resultset for xml generation
144
     *
145
     * @return Void
146
     */
147
    private function _info()
148
    {
149
        if (empty($this->_output)) {
150
            return;
151
        }
152
        foreach ($this->_output as $result) {
153
            $dev = new UPSDevice();
154
            $status = "";
155
            $status2 = "";
156
            $status3 = "";
157
            $dev->setMode("SNMP");
158
            if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.1\.1\.2\.0 = STRING:\s(.*)/m', $result, $data)) {
159
                $dev->setName(trim($data[1], "\" \r\t"));
160
            }
161
            if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.1\.1\.1\.0 = STRING:\s(.*)/m', $result, $data)) {
162
                $dev->setModel(trim($data[1], "\" \r\t"));
163
            }
164
            if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.4\.1\.1\.0 = INTEGER:\s(.*)/m', $result, $data)) {
165
                switch (trim($data[1])) {
166
                    case 1: $status = "Unknown";
167
                            break;
168
                    case 2: $status = "On Line";
169
                            break;
170
                    case 3: $status = "On Battery";
171
                            break;
172
                    case 4: $status = "On Smart Boost";
173
                            break;
174
                    case 5: $status = "Timed Sleeping";
175
                            break;
176
                    case 6: $status = "Software Bypass";
177
                            break;
178
                    case 7: $status = "Off";
179
                            break;
180
                    case 8: $status = "Rebooting";
181
                            break;
182
                    case 9: $status = "Switched Bypass";
183
                            break;
184
                    case 10:$status = "Hardware Failure Bypass";
185
                            break;
186
                    case 11:$status = "Sleeping Until Power Returns";
187
                            break;
188
                    case 12:$status = "On Smart Trim";
189
                            break;
190
                   default: $status = "Unknown state (".trim($data[1]).")";
191
                            break;
192
                }
193
            }
194
            if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.2\.1\.1\.0 = INTEGER:\s(.*)/m', $result, $data)) {
195
                $batstat = "";
196
                switch (trim($data[1])) {
197
                    case 1: $batstat = "Battery Unknown";
198
                            break;
199
                    case 2: break;
200
                    case 3: $batstat = "Battery Low";
201
                            break;
202
                   default: $batstat = "Battery Unknown (".trim($data[1]).")";
203
                            break;
204
                }
205
                if ($batstat !== "") {
206
                    if ($status !== "") {
207
                        $status .= ", ".$batstat;
208
                    } else {
209
                        $status = $batstat;
210
                    }
211
                }
212
            }
213
            if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.2\.2\.4\.0 = INTEGER:\s(.*)/m', $result, $data)) {
214
                $batstat = "";
215
                switch (trim($data[1])) {
216
                    case 1: break;
217
                    case 2: $batstat = "Replace Battery";
218
                            break;
219
                   default: $batstat = "Replace Battery (".trim($data[1]).")";
220
                            break;
221
                }
222
                if ($batstat !== "") {
223
                    if ($status !== "") {
224
                        $status .= ", ".$batstat;
225
                    } else {
226
                        $status = $batstat;
227
                    }
228
                }
229
            }
230
            if ($status !== "") {
231
               $dev->setStatus(trim($status));
232
            }
233
 
234
            if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.3\.3\.1\.0 = Gauge32:\s(.*)/m', $result, $data)) {
235
                $dev->setLineVoltage(trim($data[1])/10);
236
            } elseif (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.3\.2\.1\.0 = Gauge32:\s(.*)/m', $result, $data)) {
237
                $dev->setLineVoltage(trim($data[1]));
238
            }
239
            if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.4\.3\.3\.0 = Gauge32:\s(.*)/m', $result, $data)) {
240
                $dev->setLoad(trim($data[1])/10);
241
            } elseif (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.4\.2\.3\.0 = Gauge32:\s(.*)/m', $result, $data)) {
242
                $dev->setLoad(trim($data[1]));
243
            }
244
            if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.2\.3\.4\.0 = INTEGER:\s(.*)/m', $result, $data)) {
245
                $dev->setBatteryVoltage(trim($data[1])/10);
246
            } elseif (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.2\.2\.8\.0 = INTEGER:\s(.*)/m', $result, $data)) {
247
                $dev->setBatteryVoltage(trim($data[1]));
248
            }
249
            if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.2\.3\.1\.0 = Gauge32:\s(.*)/m', $result, $data)) {
250
                $dev->setBatterCharge(trim($data[1])/10);
251
            } elseif (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.2\.2\.1\.0 = Gauge32:\s(.*)/m', $result, $data)) {
252
                $dev->setBatterCharge(trim($data[1]));
253
            }
254
            if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.2\.2\.3\.0 = Timeticks:\s\((\d*)\)/m', $result, $data)) {
255
                $dev->setTimeLeft(trim($data[1])/6000);
256
            }
257
            if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.2\.3\.2\.0 = Gauge32:\s(.*)/m', $result, $data)) {
258
                $dev->setTemperatur(trim($data[1])/10);
259
            } elseif (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.2\.2\.2\.0 = Gauge32:\s(.*)/m', $result, $data)) {
260
                $dev->setTemperatur(trim($data[1]));
261
            }
262
            if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.2\.1\.3\.0 = STRING:\s(.*)/m', $result, $data)) {
263
                $dev->setBatteryDate(trim($data[1], "\" \r\t"));
264
            }
265
            if (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.3\.3\.4\.0 = Gauge32:\s(.*)/m', $result, $data)) {
266
                $dev->setLineFrequency(trim($data[1])/10);
267
            } elseif (preg_match('/^\.1\.3\.6\.1\.4\.1\.318\.1\.1\.1\.3\.2\.4\.0 = Gauge32:\s(.*)/m', $result, $data)) {
268
                $dev->setLineFrequency(trim($data[1]));
269
            }
270
 
271
            $this->upsinfo->setUpsDevices($dev);
272
        }
273
    }
274
 
275
    /**
276
     * get the information
277
     *
278
     * @see PSI_Interface_UPS::build()
279
     *
280
     * @return Void
281
     */
282
    public function build()
283
    {
284
        $this->_info();
285
    }
286
}