|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* SNMPPInfo Plugin |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* @category PHP |
|
8
|
|
|
* @package PSI_Plugin_SNMPPInfo |
|
9
|
|
|
* @author Mieczyslaw Nalewaj <[email protected]> |
|
10
|
|
|
* @copyright 2011 phpSysInfo |
|
11
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
|
12
|
|
|
* @version SVN: $Id: class.snmppinfo.inc.php 661 2012-08-27 11:26:39Z namiltd $ |
|
13
|
|
|
* @link http://phpsysinfo.sourceforge.net |
|
14
|
|
|
*/ |
|
15
|
|
|
/** |
|
16
|
|
|
* SNMPPInfo Plugin, which displays battery state |
|
17
|
|
|
* |
|
18
|
|
|
* @category PHP |
|
19
|
|
|
* @package PSI_Plugin_SNMPPInfo |
|
20
|
|
|
* @author Mieczyslaw Nalewaj <[email protected]> |
|
21
|
|
|
* @copyright 2011 phpSysInfo |
|
22
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
|
23
|
|
|
* @version $Id: class.snmppinfo.inc.php 661 2012-08-27 11:26:39Z namiltd $ |
|
24
|
|
|
* @link http://phpsysinfo.sourceforge.net |
|
25
|
|
|
*/ |
|
26
|
|
|
class SNMPPInfo extends PSI_Plugin |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* variable, which holds the content of the command |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
private $_filecontent = array(); |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* variable, which holds the result before the xml is generated out of this array |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
private $_result = array(); |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* read the data into an internal array and also call the parent constructor |
|
42
|
|
|
* |
|
43
|
|
|
* @param String $enc encoding |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct($enc) |
|
46
|
|
|
{ |
|
47
|
|
|
parent::__construct(__CLASS__, $enc); |
|
48
|
|
|
switch (strtolower(PSI_PLUGIN_SNMPPINFO_ACCESS)) { |
|
49
|
|
|
case 'command': |
|
50
|
|
|
if (defined('PSI_PLUGIN_SNMPPINFO_DEVICES') && is_string(PSI_PLUGIN_SNMPPINFO_DEVICES)) { |
|
51
|
|
|
if (preg_match(ARRAY_EXP, PSI_PLUGIN_SNMPPINFO_DEVICES)) { |
|
52
|
|
|
$printers = eval(PSI_PLUGIN_SNMPPINFO_DEVICES); |
|
|
|
|
|
|
53
|
|
|
} else { |
|
54
|
|
|
$printers = array(PSI_PLUGIN_SNMPPINFO_DEVICES); |
|
55
|
|
|
} |
|
56
|
|
|
foreach ($printers as $printer) { |
|
57
|
|
|
CommonFunctions::executeProgram("snmpwalk", "-Ona -c public -v 1 -r 1 ".$printer." .1.3.6.1.2.1.1.5", $buffer, PSI_DEBUG); |
|
58
|
|
|
if (strlen($buffer) > 0) { |
|
59
|
|
|
$this->_filecontent[$printer] = $buffer; |
|
60
|
|
|
|
|
61
|
|
|
CommonFunctions::executeProgram("snmpwalk", "-Ona -c public -v 1 -r 1 ".$printer." .1.3.6.1.2.1.43.11.1.1", $buffer2, PSI_DEBUG); |
|
62
|
|
|
if (strlen($buffer2) > 0) { |
|
63
|
|
|
$this->_filecontent[$printer] .= "\n".$buffer2; |
|
64
|
|
|
} |
|
65
|
|
|
CommonFunctions::executeProgram("snmpwalk", "-Ona -c public -v 1 -r 1 ".$printer." .1.3.6.1.2.1.43.18.1.1", $buffer3, PSI_DEBUG); |
|
66
|
|
|
if (strlen($buffer3) > 0) { |
|
67
|
|
|
$this->_filecontent[$printer] .= "\n".$buffer3; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
break; |
|
73
|
|
|
case 'php-snmp': |
|
74
|
|
|
if (!extension_loaded("snmp")) { |
|
75
|
|
|
$this->global_error->addError("Requirements error", "SNMPPInfo 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_PLUGIN_SNMPPINFO_DEVICES') && is_string(PSI_PLUGIN_SNMPPINFO_DEVICES)) { |
|
81
|
|
|
if (preg_match(ARRAY_EXP, PSI_PLUGIN_SNMPPINFO_DEVICES)) { |
|
82
|
|
|
$printers = eval(PSI_PLUGIN_SNMPPINFO_DEVICES); |
|
|
|
|
|
|
83
|
|
|
} else { |
|
84
|
|
|
$printers = array(PSI_PLUGIN_SNMPPINFO_DEVICES); |
|
85
|
|
|
} |
|
86
|
|
|
foreach ($printers as $printer) { |
|
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($printer, "public", ".1.3.6.1.2.1.1.5", 1000000, 1); |
|
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($printer, "public", ".1.3.6.1.2.1.43.11.1.1", 1000000, 1); |
|
109
|
|
|
if (! PSI_DEBUG) { |
|
110
|
|
|
error_reporting($old_err_rep); /* restore error level */ |
|
111
|
|
|
set_error_handler('errorHandlerPsi'); /* restore error handler */ |
|
112
|
|
|
} |
|
113
|
|
View Code Duplication |
if (! empty($bufferarr2)) { |
|
|
|
|
|
|
114
|
|
|
foreach ($bufferarr2 as $id=>$string) { |
|
115
|
|
|
$buffer .= $id." = ".$string."\n"; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
if (! PSI_DEBUG) { |
|
120
|
|
|
restore_error_handler(); /* default error handler */ |
|
121
|
|
|
$old_err_rep = error_reporting(); |
|
122
|
|
|
error_reporting(E_ERROR); /* fatal errors only */ |
|
123
|
|
|
} |
|
124
|
|
|
$bufferarr3=snmprealwalk($printer, "public", ".1.3.6.1.2.1.43.18.1.1", 1000000, 1); |
|
125
|
|
|
if (! PSI_DEBUG) { |
|
126
|
|
|
error_reporting($old_err_rep); /* restore error level */ |
|
127
|
|
|
set_error_handler('errorHandlerPsi'); /* restore error handler */ |
|
128
|
|
|
} |
|
129
|
|
View Code Duplication |
if (! empty($bufferarr3)) { |
|
|
|
|
|
|
130
|
|
|
foreach ($bufferarr3 as $id=>$string) { |
|
131
|
|
|
$buffer .= $id." = ".$string."\n"; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
if (strlen(trim($buffer)) > 0) { |
|
136
|
|
|
$this->_filecontent[$printer] = $buffer; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
break; |
|
142
|
|
|
case 'data': |
|
143
|
|
View Code Duplication |
if (defined('PSI_PLUGIN_SNMPPINFO_DEVICES') && is_string(PSI_PLUGIN_SNMPPINFO_DEVICES)) { |
|
|
|
|
|
|
144
|
|
|
if (preg_match(ARRAY_EXP, PSI_PLUGIN_SNMPPINFO_DEVICES)) { |
|
145
|
|
|
$printers = eval(PSI_PLUGIN_SNMPPINFO_DEVICES); |
|
|
|
|
|
|
146
|
|
|
} else { |
|
147
|
|
|
$printers = array(PSI_PLUGIN_SNMPPINFO_DEVICES); |
|
148
|
|
|
} |
|
149
|
|
|
$pn=0; |
|
150
|
|
|
foreach ($printers as $printer) { |
|
151
|
|
|
$buffer=""; |
|
152
|
|
|
if (CommonFunctions::rfts(APP_ROOT."/data/snmppinfo{$pn}.txt", $buffer) && !empty($buffer)) { |
|
153
|
|
|
$this->_filecontent[$printer] = $buffer; |
|
154
|
|
|
} |
|
155
|
|
|
$pn++; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
break; |
|
159
|
|
|
default: |
|
160
|
|
|
$this->global_error->addError("switch(PSI_PLUGIN_SNMPPINFO_ACCESS)", "Bad SNMPPInfo configuration in phpsysinfo.ini"); |
|
161
|
|
|
break; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* doing all tasks to get the required informations that the plugin needs |
|
167
|
|
|
* result is stored in an internal array |
|
168
|
|
|
* |
|
169
|
|
|
* @return void |
|
170
|
|
|
*/ |
|
171
|
|
|
public function execute() |
|
172
|
|
|
{ |
|
173
|
|
|
if (empty($this->_filecontent)) { |
|
174
|
|
|
return; |
|
175
|
|
|
} |
|
176
|
|
|
foreach ($this->_filecontent as $printer=>$result) { |
|
177
|
|
|
$lines = preg_split('/\r?\n/', $result); |
|
178
|
|
|
foreach ($lines as $line) { |
|
179
|
|
|
if (preg_match('/^\.1\.3\.6\.1\.2\.1\.43\.11\.1\.1\.6\.1\.(.*) = STRING:\s(.*)/', $line, $data)) { |
|
180
|
|
|
$this->_result[$printer][$data[1]]['prtMarkerSuppliesDescription']=trim($data[2], "\""); |
|
181
|
|
|
} elseif (preg_match('/^\.1\.3\.6\.1\.2\.1\.43\.11\.1\.1\.7\.1\.(.*) = INTEGER:\s(.*)/', $line, $data)) { |
|
182
|
|
|
$this->_result[$printer][$data[1]]['prtMarkerSuppliesSupplyUnit']=$data[2]; |
|
183
|
|
|
} elseif (preg_match('/^\.1\.3\.6\.1\.2\.1\.43\.11\.1\.1\.8\.1\.(.*) = INTEGER:\s(.*)/', $line, $data)) { |
|
184
|
|
|
$this->_result[$printer][$data[1]]['prtMarkerSuppliesMaxCapacity']=$data[2]; |
|
185
|
|
|
} elseif (preg_match('/^\.1\.3\.6\.1\.2\.1\.43\.11\.1\.1\.9\.1\.(.*) = INTEGER:\s(.*)/', $line, $data)) { |
|
186
|
|
|
$this->_result[$printer][$data[1]]['prtMarkerSuppliesLevel']=$data[2]; |
|
187
|
|
View Code Duplication |
} elseif (preg_match('/^\.1\.3\.6\.1\.2\.1\.1\.5\.0 = STRING:\s(.*)/', $line, $data)) { |
|
|
|
|
|
|
188
|
|
|
$this->_result[$printer][0]['prtMarkerSuppliesDescription']=trim($data[1], "\"");; |
|
189
|
|
|
} elseif (preg_match('/^\.1\.3\.6\.1\.2\.1\.43\.18\.1\.1\.8\.1\.(.*) = STRING:\s(.*)/', $line, $data)) { |
|
190
|
|
|
$this->_result[$printer][99][$data[1]]["message"]=trim($data[2], "\""); |
|
191
|
|
View Code Duplication |
} elseif (preg_match('/^\.1\.3\.6\.1\.2\.1\.43\.18\.1\.1\.2\.1\.(.*) = INTEGER:\s(.*)/', $line, $data)) { |
|
|
|
|
|
|
192
|
|
|
$this->_result[$printer][99][$data[1]]["severity"]=$data[2]; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* generates the XML content for the plugin |
|
200
|
|
|
* |
|
201
|
|
|
* @return SimpleXMLElement entire XML content for the plugin |
|
202
|
|
|
*/ |
|
203
|
|
|
public function xml() |
|
204
|
|
|
{ |
|
205
|
|
|
foreach ($this->_result as $printer=>$markersupplies_item) { |
|
206
|
|
|
$xmlsnmppinfo_printer = $this->xml->addChild("Printer"); |
|
207
|
|
|
$xmlsnmppinfo_printer->addAttribute("Device", $printer); |
|
208
|
|
|
foreach ($markersupplies_item as $marker=>$snmppinfo_item) { |
|
209
|
|
|
|
|
210
|
|
|
if ($marker==0) { |
|
211
|
|
|
$xmlsnmppinfo_printer->addAttribute("Name", $snmppinfo_item['prtMarkerSuppliesDescription']); |
|
212
|
|
|
} elseif ($marker==99) { |
|
213
|
|
|
foreach ($snmppinfo_item as $item=>$iarr) { |
|
214
|
|
|
if (isset($iarr["message"]) && $iarr["message"] != "") { |
|
215
|
|
|
$xmlsnmppinfo_errors = $xmlsnmppinfo_printer->addChild("PrinterMessage"); |
|
216
|
|
|
$xmlsnmppinfo_errors->addAttribute("Message", $iarr["message"]); |
|
217
|
|
|
$xmlsnmppinfo_errors->addAttribute("Severity", $iarr["severity"]); |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
} else { |
|
221
|
|
|
$xmlsnmppinfo = $xmlsnmppinfo_printer->addChild("MarkerSupplies"); |
|
222
|
|
|
|
|
223
|
|
|
$xmlsnmppinfo->addAttribute("Description", isset($snmppinfo_item['prtMarkerSuppliesDescription']) ? $snmppinfo_item['prtMarkerSuppliesDescription'] : ""); |
|
224
|
|
|
$xmlsnmppinfo->addAttribute("SupplyUnit", isset($snmppinfo_item['prtMarkerSuppliesSupplyUnit']) ? $snmppinfo_item['prtMarkerSuppliesSupplyUnit'] : ""); |
|
225
|
|
|
$xmlsnmppinfo->addAttribute("MaxCapacity", isset($snmppinfo_item['prtMarkerSuppliesMaxCapacity']) ? $snmppinfo_item['prtMarkerSuppliesMaxCapacity'] : ""); |
|
226
|
|
|
$xmlsnmppinfo->addAttribute("Level", isset($snmppinfo_item['prtMarkerSuppliesLevel']) ? $snmppinfo_item['prtMarkerSuppliesLevel'] : ""); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
return $this->xml->getSimpleXmlElement(); |
|
|
|
|
|
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.