Subversion Repositories ALCASAR

Rev

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