1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
/*
Created from the snmputil sample in the Microsoft SDK for NT
*/
#include "php.h"
#if COMPILE_DL
#include "../phpdl.h"
#include "functions/dl.h"
#endif
#include "php_snmp.h"
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#if HAVE_SNMP
#include <snmp.h>
#include <mgmtapi.h>
/* {{{ snmp_functions[]
*/
zend_function_entry snmp_functions[] = {
{"snmpget", php3_snmpget, NULL},
{"snmpwalk", php3_snmpwalk, NULL},
{NULL,NULL,NULL}
};
/* }}} */
/* {{{ snmp_module_entry
*/
zend_module_entry snmp_module_entry = {
STANDARD_MODULE_HEADER,
"SNMP",
snmp_functions,
NULL,
NULL,
NULL,
NULL,
NULL,
NO_VERSION_YET,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#if COMPILE_DL
DLEXPORT zend_module_entry *get_module() { return &snmp_module_entry; }
#endif
#define GET 1
#define WALK 2
#define GETNEXT 3
#define TIMEOUT 6000 /* milliseconds */
#define RETRIES 3
/* {{{ _php_snmp
*/
void _php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st) {
zval *a1, *a2, *a3;
INT operation;
LPSTR agent;
LPSTR community;
RFC1157VarBindList variableBindings;
LPSNMP_MGR_SESSION session;
INT timeout = TIMEOUT;
INT retries = RETRIES;
BYTE requestType;
AsnInteger errorStatus;
AsnInteger errorIndex;
AsnObjectIdentifier oid;
char *chkPtr = NULL;
if (zend_get_parameters_ex(3, &a1, &a2, &a3) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(a1);
convert_to_string_ex(a2);
convert_to_string_ex(a3);
agent=Z_STRVAL_PP(a1);
community=Z_STRVAL_PP(a2);
operation=st;
SnmpMgrStrToOid(Z_STRVAL_PP(a3), &oid);
/*
I've limited this to only one oid, but we can create a
list of oid's here, and expand the function to take multiple
oid's
*/
variableBindings.list->name = oid;
variableBindings.list->value.asnType = ASN_NULL;
variableBindings.len = 1;
/* Establish a SNMP session to communicate with the remote agent. The
community, communications timeout, and communications retry count
for the session are also required.
*/
if ((session = SnmpMgrOpen(agent, community, timeout, retries)) == NULL){
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error on SnmpMgrOpen %d", GetLastError());
}
/* Determine and perform the requested operation.*/
if (operation == GET || operation == GETNEXT){
/* Get and GetNext are relatively simple operations to perform.
Simply initiate the request and process the result and/or
possible error conditions. */
if (operation == GET){
requestType = ASN_RFC1157_GETREQUEST;
} else {
requestType = ASN_RFC1157_GETNEXTREQUEST;
}
/* Request that the API carry out the desired operation.*/
if (!SnmpMgrRequest(session, requestType, &variableBindings,
&errorStatus, &errorIndex)){
/* The API is indicating an error. */
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error on SnmpMgrRequest %d", GetLastError());
} else {
/* The API succeeded, errors may be indicated from the remote
agent. */
if (errorStatus > 0){
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error: errorStatus=%d, errorIndex=%d",
errorStatus, errorIndex);
} else {
/* Display the resulting variable bindings.*/
UINT i;
char *str = NULL;
for(i=0; i < variableBindings.len; i++)
{
SnmpMgrOidToStr(&variableBindings.list[i].name, &str);
php_printf("Variable = %s\n", str);
if (str) SNMP_free(str);
php_printf("Value = ");
SnmpUtilPrintAsnAny(&variableBindings.list[i].value);
php_printf("\n");
} /* end for() */
}
}
/* Free the variable bindings that have been allocated.*/
SnmpUtilVarBindListFree(&variableBindings);
}
else if (operation == WALK)
{
/* Walk is a common term used to indicate that all MIB variables
under a given OID are to be traversed and displayed. This is
a more complex operation requiring tests and looping in addition
to the steps for get/getnext above. */
AsnObjectIdentifier root;
AsnObjectIdentifier tempOid;
SnmpUtilOidCpy(&root, &variableBindings.list[0].name);
requestType = ASN_RFC1157_GETNEXTREQUEST;
while(1)
{
if (!SnmpMgrRequest(session, requestType, &variableBindings,
&errorStatus, &errorIndex)){
/* The API is indicating an error.*/
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error on SnmpMgrRequest %d", GetLastError());
break;
}
else
{
/* The API succeeded, errors may be indicated from the remote
agent.
Test for end of subtree or end of MIB. */
if (errorStatus == SNMP_ERRORSTATUS_NOSUCHNAME ||
SnmpUtilOidNCmp(&variableBindings.list[0].name,
&root, root.idLength))
{
PUTS("End of MIB subtree.\n\n");
break;
}
/* Test for general error conditions or sucesss. */
if (errorStatus > 0){
php_error_docref(NULL TSRMLS_CC, E_ERROR,"Error: errorStatus=%d, errorIndex=%d", errorStatus, errorIndex);
break;
}
else
{
/* Display resulting variable binding for this iteration. */
char *str = NULL;
SnmpMgrOidToStr(&variableBindings.list[0].name, &str);
php_printf("Variable = %s\n", str);
if (str) SNMP_free(str);
php_printf("Value = ");
SnmpUtilPrintAsnAny(&variableBindings.list[0].value);
php_printf("\n");
}
} /* end if () */
/* Prepare for the next iteration. Make sure returned oid is
preserved and the returned value is freed.
*/
SnmpUtilOidCpy(&tempOid, &variableBindings.list[0].name);
SnmpUtilVarBindFree(&variableBindings.list[0]);
SnmpUtilOidCpy(&variableBindings.list[0].name, &tempOid);
variableBindings.list[0].value.asnType = ASN_NULL;
SnmpUtilOidFree(&tempOid);
} /* end while() */
/* Free the variable bindings that have been allocated.*/
SnmpUtilVarBindListFree(&variableBindings);
SnmpUtilOidFree(&root);
} /* end if (operation) */
/* Close SNMP session with the remote agent.*/
if (!SnmpMgrClose(session)){
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error on SnmpMgrClose %d", GetLastError());
}
}
/* }}} */
/* {{{ php3_snmpget
*/
DLEXPORT void php3_snmpget(INTERNAL_FUNCTION_PARAMETERS) {
_php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU,1);
}
/* }}} */
/* {{{ php3_snmpwalk
*/
DLEXPORT void php3_snmpwalk(INTERNAL_FUNCTION_PARAMETERS) {
_php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU,2);
}
/* }}} */
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/
|