Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

Rev Author Line No. Line
2780 rexy 1
<?php
2
/**
3
 * SNMPPInfo Plugin, which displays printers info via SNMP
4
 *
5
 * @category  PHP
6
 * @package   PSI_Plugin_SNMPPInfo
7
 * @author    Mieczyslaw Nalewaj <namiltd@users.sourceforge.net>
8
 * @copyright 2011 phpSysInfo
9
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
10
 * @version   $Id: class.snmppinfo.inc.php 661 2012-08-27 11:26:39Z namiltd $
11
 * @link      http://phpsysinfo.sourceforge.net
12
 */
13
class SNMPPInfo extends PSI_Plugin
14
{
15
    /**
16
     * variable, which holds the content of the command
17
     * @var array
18
     */
19
    private $_filecontent = array();
20
 
21
    /**
22
     * variable, which holds the result before the xml is generated out of this array
23
     * @var array
24
     */
25
    private $_result = array();
26
 
27
    /**
28
     * read the data into an internal array and also call the parent constructor
29
     *
30
     * @param String $enc encoding
31
     */
32
    public function __construct($enc)
33
    {
34
        parent::__construct(__CLASS__, $enc);
35
        switch (strtolower(PSI_PLUGIN_SNMPPINFO_ACCESS)) {
36
        case 'command':
37
                if (defined('PSI_PLUGIN_SNMPPINFO_DEVICES') && is_string(PSI_PLUGIN_SNMPPINFO_DEVICES)) {
38
                    if (preg_match(ARRAY_EXP, PSI_PLUGIN_SNMPPINFO_DEVICES)) {
39
                        $printers = eval(PSI_PLUGIN_SNMPPINFO_DEVICES);
40
                    } else {
41
                        $printers = array(PSI_PLUGIN_SNMPPINFO_DEVICES);
42
                    }
43
                    foreach ($printers as $printer) {
44
                        CommonFunctions::executeProgram("snmpwalk", "-Ona -c public -v 1 -t ".PSI_SNMP_TIMEOUT_INT." -r ".PSI_SNMP_RETRY_INT." ".$printer." .1.3.6.1.2.1.1.5", $buffer, PSI_DEBUG);
45
                        if (strlen($buffer) > 0) {
46
                            $this->_filecontent[$printer] = $buffer;
47
 
48
                            CommonFunctions::executeProgram("snmpwalk", "-Ona -c public -v 1 -t ".PSI_SNMP_TIMEOUT_INT." -r ".PSI_SNMP_RETRY_INT." ".$printer." .1.3.6.1.4.1.367.3.2.1.2.24.1.1", $buffer1, false);
49
                            if (strlen($buffer1) > 0) {
50
                               $this->_filecontent[$printer] .= "\n".$buffer1;
51
                            }
52
 
53
                            CommonFunctions::executeProgram("snmpwalk", "-Ona -c public -v 1 -t ".PSI_SNMP_TIMEOUT_INT." -r ".PSI_SNMP_RETRY_INT." ".$printer." .1.3.6.1.2.1.43.11.1.1", $buffer2, PSI_DEBUG);
54
                            if (strlen($buffer2) > 0) {
55
                               $this->_filecontent[$printer] .= "\n".$buffer2;
56
                            }
57
                            CommonFunctions::executeProgram("snmpwalk", "-Ona -c public -v 1 -t ".PSI_SNMP_TIMEOUT_INT." -r ".PSI_SNMP_RETRY_INT." ".$printer." .1.3.6.1.2.1.43.18.1.1", $buffer3, PSI_DEBUG);
58
                            if (strlen($buffer3) > 0) {
59
                               $this->_filecontent[$printer] .= "\n".$buffer3;
60
                            }
61
                        }
62
                    }
63
                }
64
                break;
65
        case 'php-snmp':
66
                if (!extension_loaded("snmp")) {
67
                    $this->global_error->addError("Requirements error", "SNMPPInfo plugin requires the snmp extension to php in order to work properly");
68
                    break;
69
                }
70
                snmp_set_valueretrieval(SNMP_VALUE_LIBRARY);
71
                snmp_set_oid_output_format(SNMP_OID_OUTPUT_NUMERIC);
72
                if (defined('PSI_PLUGIN_SNMPPINFO_DEVICES') && is_string(PSI_PLUGIN_SNMPPINFO_DEVICES)) {
73
                    if (preg_match(ARRAY_EXP, PSI_PLUGIN_SNMPPINFO_DEVICES)) {
74
                        $printers = eval(PSI_PLUGIN_SNMPPINFO_DEVICES);
75
                    } else {
76
                        $printers = array(PSI_PLUGIN_SNMPPINFO_DEVICES);
77
                    }
78
                    foreach ($printers as $printer) {
79
                        if (! PSI_DEBUG) {
80
                            restore_error_handler(); /* default error handler */
81
                            $old_err_rep = error_reporting();
82
                            error_reporting(E_ERROR); /* fatal errors only */
83
                        }
84
                        $bufferarr=snmprealwalk($printer, "public", ".1.3.6.1.2.1.1.5", 1000000 * PSI_SNMP_TIMEOUT_INT, PSI_SNMP_RETRY_INT);
85
                        if (! PSI_DEBUG) {
86
                            error_reporting($old_err_rep); /* restore error level */
87
                            set_error_handler('errorHandlerPsi'); /* restore error handler */
88
                        }
89
                        if (! empty($bufferarr)) {
90
                            $buffer="";
91
                            foreach ($bufferarr as $id=>$string) {
92
                                $buffer .= $id." = ".$string."\n";
93
                            }
94
 
95
                            restore_error_handler(); /* default error handler */
96
                            $old_err_rep = error_reporting();
97
                            error_reporting(E_ERROR); /* fatal errors only */
98
 
99
                            $bufferarr1=snmprealwalk($printer, "public", ".1.3.6.1.4.1.367.3.2.1.2.24.1.1", 1000000 * PSI_SNMP_TIMEOUT_INT, PSI_SNMP_RETRY_INT);
100
 
101
                            error_reporting($old_err_rep); /* restore error level */
102
                            set_error_handler('errorHandlerPsi'); /* restore error handler */
103
                            if (! empty($bufferarr1)) {
104
                                foreach ($bufferarr1 as $id=>$string) {
105
                                    $buffer .= $id." = ".$string."\n";
106
                                }
107
                            }
108
 
109
                            if (! PSI_DEBUG) {
110
                                restore_error_handler(); /* default error handler */
111
                                $old_err_rep = error_reporting();
112
                                error_reporting(E_ERROR); /* fatal errors only */
113
                            }
114
                            $bufferarr2=snmprealwalk($printer, "public", ".1.3.6.1.2.1.43.11.1.1", 1000000 * PSI_SNMP_TIMEOUT_INT, PSI_SNMP_RETRY_INT);
115
                            if (! PSI_DEBUG) {
116
                                error_reporting($old_err_rep); /* restore error level */
117
                                set_error_handler('errorHandlerPsi'); /* restore error handler */
118
                            }
119
                            if (! empty($bufferarr2)) {
120
                                foreach ($bufferarr2 as $id=>$string) {
121
                                    $buffer .= $id." = ".$string."\n";
122
                                }
123
                            }
124
 
125
                            if (! PSI_DEBUG) {
126
                                restore_error_handler(); /* default error handler */
127
                                $old_err_rep = error_reporting();
128
                                error_reporting(E_ERROR); /* fatal errors only */
129
                            }
130
                            $bufferarr3=snmprealwalk($printer, "public", ".1.3.6.1.2.1.43.18.1.1", 1000000 * PSI_SNMP_TIMEOUT_INT, PSI_SNMP_RETRY_INT);
131
                            if (! PSI_DEBUG) {
132
                                error_reporting($old_err_rep); /* restore error level */
133
                                set_error_handler('errorHandlerPsi'); /* restore error handler */
134
                            }
135
                            if (! empty($bufferarr3)) {
136
                                foreach ($bufferarr3 as $id=>$string) {
137
                                    $buffer .= $id." = ".$string."\n";
138
                                }
139
                            }
140
 
141
                            if (strlen(trim($buffer)) > 0) {
142
                                $this->_filecontent[$printer] = $buffer;
143
                            }
144
                        }
145
                    }
146
                }
147
                break;
148
        case 'data':
149
                if (defined('PSI_PLUGIN_SNMPPINFO_DEVICES') && is_string(PSI_PLUGIN_SNMPPINFO_DEVICES)) {
150
                    if (preg_match(ARRAY_EXP, PSI_PLUGIN_SNMPPINFO_DEVICES)) {
151
                        $printers = eval(PSI_PLUGIN_SNMPPINFO_DEVICES);
152
                    } else {
153
                        $printers = array(PSI_PLUGIN_SNMPPINFO_DEVICES);
154
                    }
155
                    $pn=0;
156
                    foreach ($printers as $printer) {
157
                        $buffer="";
158
                        if (CommonFunctions::rfts(PSI_APP_ROOT."/data/snmppinfo{$pn}.txt", $buffer) && !empty($buffer)) {
159
                            $this->_filecontent[$printer] = $buffer;
160
                        }
161
                        $pn++;
162
                    }
163
                }
164
                break;
165
            default:
166
                $this->global_error->addConfigError("__construct()", "[snmppinfo] ACCESS");
167
                break;
168
        }
169
    }
170
 
171
    /**
172
     * doing all tasks to get the required informations that the plugin needs
173
     * result is stored in an internal array
174
     *
175
     * @return void
176
     */
177
    public function execute()
178
    {
179
        if (empty($this->_filecontent)) {
180
            return;
181
        }
182
        foreach ($this->_filecontent as $printer=>$result) {
183
            $result = preg_replace('/End of MIB\r?\n/', '', $result);
184
            $result = preg_replace('/\s\r?\n([^\.])/', ' $1', $result);
185
            $lines = preg_split('/\r?\n/', $result);
186
            foreach ($lines as $line) {
187
                if (preg_match('/^(.+) = Hex-STRING:\s(.+)/', $line, $linetmp)) {
188
                    $hexchars = explode(" ", trim($linetmp[2]));
189
                    $newstring = "";
190
                    foreach ($hexchars as $hexchar) {
191
                        $hexint = hexdec($hexchar);
192
                        if (($hexint<32) || ($hexint>126)) {
193
                            $newstring .= ".";
194
                        } else {
195
                            $newstring .= chr($hexint);
196
                        }
197
                    }
198
                    if ($newstring!=="") {
199
                        $line = $linetmp[1]." = STRING: ".$newstring;
200
                    }
201
                }
202
                if (preg_match('/^\.1\.3\.6\.1\.2\.1\.43\.11\.1\.1\.6\.1\.(.*) = STRING:\s(.*)/', $line, $data)) {
203
                    $this->_result[$printer][$data[1]]['prtMarkerSuppliesDescription']=trim($data[2], "\"");
204
                } elseif (preg_match('/^\.1\.3\.6\.1\.2\.1\.43\.11\.1\.1\.7\.1\.(.*) = INTEGER:\s(.*)/', $line, $data)) {
205
                    $this->_result[$printer][$data[1]]['prtMarkerSuppliesSupplyUnit']=$data[2];
206
                } elseif (preg_match('/^\.1\.3\.6\.1\.2\.1\.43\.11\.1\.1\.8\.1\.(.*) = INTEGER:\s(.*)/', $line, $data)) {
207
                    $this->_result[$printer][$data[1]]['prtMarkerSuppliesMaxCapacity']=$data[2];
208
                } elseif (preg_match('/^\.1\.3\.6\.1\.2\.1\.43\.11\.1\.1\.9\.1\.(.*) = INTEGER:\s(.*)/', $line, $data)) {
209
                    $this->_result[$printer][$data[1]]['prtMarkerSuppliesLevel']=$data[2];
210
                } elseif (preg_match('/^\.1\.3\.6\.1\.2\.1\.1\.5\.0 = STRING:\s(.*)/', $line, $data)) {
211
                    $this->_result[$printer][0]['prtMarkerSuppliesDescription']=trim($data[1], "\"");
212
                } elseif (preg_match('/^\.1\.3\.6\.1\.2\.1\.43\.18\.1\.1\.8\.1\.(.*) = STRING:\s(.*)/', $line, $data)) {
213
                    $this->_result[$printer][99][$data[1]]["message"]=trim($data[2], "\"");
214
                } elseif (preg_match('/^\.1\.3\.6\.1\.2\.1\.43\.18\.1\.1\.2\.1\.(.*) = INTEGER:\s(.*)/', $line, $data)) {
215
                    $this->_result[$printer][99][$data[1]]["severity"]=$data[2];
216
 
217
                } elseif (preg_match('/^\.1\.3\.6\.1\.4\.1\.367\.3\.2\.1\.2\.24\.1\.1\.2\.(.*) = STRING:\s(.*)/', $line, $data)) {
218
                    $this->_result[$printer][$data[1]]['prtMarkerSuppliesDescriptionRicoh']=trim($data[2], "\"");
219
                } elseif (preg_match('/^\.1\.3\.6\.1\.4\.1\.367\.3\.2\.1\.2\.24\.1\.1\.5\.(.*) = INTEGER:\s(.*)/', $line, $data)) {
220
                    $this->_result[$printer][$data[1]]['prtMarkerSuppliesLevelRicoh']=$data[2];
221
                }
222
            }
223
        }
224
    }
225
 
226
    /**
227
     * generates the XML content for the plugin
228
     *
229
     * @return SimpleXMLElement entire XML content for the plugin
230
     */
231
    public function xml()
232
    {
233
        foreach ($this->_result as $printer=>$markersupplies_item) {
234
            $xmlsnmppinfo_printer = $this->xml->addChild("Printer");
235
            $xmlsnmppinfo_printer->addAttribute("Device", $printer);
236
            if (isset($markersupplies_item[1]) &&
237
                isset($markersupplies_item[1]['prtMarkerSuppliesDescriptionRicoh']) &&
238
                isset($markersupplies_item[1]['prtMarkerSuppliesLevelRicoh'])) {
239
                $ricoh = true;
240
            } else {
241
                $ricoh = false;
242
            }
243
 
244
            foreach ($markersupplies_item as $marker=>$snmppinfo_item) {
245
                if ($marker==0) {
246
                    $xmlsnmppinfo_printer->addAttribute("Name", $snmppinfo_item['prtMarkerSuppliesDescription']);
247
                } elseif ($marker==99) {
248
                    foreach ($snmppinfo_item as $item=>$iarr) {
249
                        if (isset($iarr["message"]) && $iarr["message"] != "") {
250
                            $xmlsnmppinfo_errors = $xmlsnmppinfo_printer->addChild("PrinterMessage");
251
                            $xmlsnmppinfo_errors->addAttribute("Message", $iarr["message"]);
252
                            $xmlsnmppinfo_errors->addAttribute("Severity", $iarr["severity"]);
253
                        }
254
                    }
255
                } else {
256
                    if ($ricoh) {
257
                        if (isset($snmppinfo_item['prtMarkerSuppliesDescriptionRicoh'])) {
258
                            $xmlsnmppinfo = $xmlsnmppinfo_printer->addChild("MarkerSupplies");
259
 
260
                            $xmlsnmppinfo->addAttribute("Description", $snmppinfo_item['prtMarkerSuppliesDescriptionRicoh']);
261
                            $xmlsnmppinfo->addAttribute("SupplyUnit", "19");
262
                            $xmlsnmppinfo->addAttribute("MaxCapacity", "100");
263
                            $xmlsnmppinfo->addAttribute("Level", isset($snmppinfo_item['prtMarkerSuppliesLevelRicoh']) ? $snmppinfo_item['prtMarkerSuppliesLevelRicoh'] : "");
264
                        }
265
                    } else {
266
                        $xmlsnmppinfo = $xmlsnmppinfo_printer->addChild("MarkerSupplies");
267
 
268
                        $xmlsnmppinfo->addAttribute("Description", isset($snmppinfo_item['prtMarkerSuppliesDescription']) ? $snmppinfo_item['prtMarkerSuppliesDescription'] : "");
269
                        $xmlsnmppinfo->addAttribute("SupplyUnit", isset($snmppinfo_item['prtMarkerSuppliesSupplyUnit']) ? $snmppinfo_item['prtMarkerSuppliesSupplyUnit'] : "");
270
                        $xmlsnmppinfo->addAttribute("MaxCapacity", isset($snmppinfo_item['prtMarkerSuppliesMaxCapacity']) ? $snmppinfo_item['prtMarkerSuppliesMaxCapacity'] : "");
271
                        $xmlsnmppinfo->addAttribute("Level", isset($snmppinfo_item['prtMarkerSuppliesLevel']) ? $snmppinfo_item['prtMarkerSuppliesLevel'] : "");
272
                    }
273
                }
274
            }
275
        }
276
 
277
        return $this->xml->getSimpleXmlElement();
278
    }
279
}