diff --git a/plugins/python/python_plugin.c b/plugins/python/python_plugin.c index dc0d13b4..f61adef5 100644 --- a/plugins/python/python_plugin.c +++ b/plugins/python/python_plugin.c @@ -681,6 +681,10 @@ void init_uwsgi_embedded_module() { init_uwsgi_module_queue(new_uwsgi_module); } + if (uwsgi.snmp) { + init_uwsgi_module_snmp(new_uwsgi_module); + } + if (up.extension) { up.extension(); } diff --git a/plugins/python/uwsgi_pymodule.c b/plugins/python/uwsgi_pymodule.c index 39be9155..340e0a8a 100644 --- a/plugins/python/uwsgi_pymodule.c +++ b/plugins/python/uwsgi_pymodule.c @@ -3217,4 +3217,126 @@ void init_uwsgi_module_sharedarea(PyObject * current_uwsgi_module) { } } +PyObject *py_snmp_counter32(PyObject * self, PyObject * args) { + + uint8_t oid_num; + uint32_t oid_val = 0; + + if (!PyArg_ParseTuple(args, "bI:snmp_set_counter32", &oid_num, &oid_val)) { + return NULL; + } + + if (oid_num > 100 || oid_num < 1) + goto clear; + + uwsgi.shared->snmp_value[oid_num - 1].type = SNMP_COUNTER32; + uwsgi.shared->snmp_value[oid_num - 1].val = oid_val; + + Py_INCREF(Py_True); + return Py_True; + +clear: + + Py_INCREF(Py_None); + return Py_None; +} + +PyObject *py_snmp_counter64(PyObject * self, PyObject * args) { + + uint8_t oid_num; + uint64_t oid_val = 0; + + if (!PyArg_ParseTuple(args, "bK:snmp_set_counter64", &oid_num, &oid_val)) { + return NULL; + } + + if (oid_num > 100 || oid_num < 1) + goto clear; + + uwsgi.shared->snmp_value[oid_num - 1].type = SNMP_COUNTER64; + uwsgi.shared->snmp_value[oid_num - 1].val = oid_val; + + Py_INCREF(Py_True); + return Py_True; + +clear: + + Py_INCREF(Py_None); + return Py_None; +} + +PyObject *py_snmp_gauge(PyObject * self, PyObject * args) { + + uint8_t oid_num; + uint32_t oid_val = 0; + + if (!PyArg_ParseTuple(args, "bI:snmp_set_gauge", &oid_num, &oid_val)) { + return NULL; + } + + if (oid_num > 100 || oid_num < 1) + goto clear; + + uwsgi.shared->snmp_value[oid_num - 1].type = SNMP_GAUGE; + uwsgi.shared->snmp_value[oid_num - 1].val = oid_val; + + Py_INCREF(Py_True); + return Py_True; + +clear: + + Py_INCREF(Py_None); + return Py_None; +} + +PyObject *py_snmp_community(PyObject * self, PyObject * args) { + + char *snmp_community; + + if (!PyArg_ParseTuple(args, "s:snmp_set_community", &snmp_community)) { + return NULL; + } + + if (strlen(snmp_community) > 72) { + uwsgi_log( "*** warning the supplied SNMP community string will be truncated to 72 chars ***\n"); + memcpy(uwsgi.shared->snmp_community, snmp_community, 72); + } + else { + memcpy(uwsgi.shared->snmp_community, snmp_community, strlen(snmp_community) + 1); + } + + Py_INCREF(Py_True); + return Py_True; + +} + + +static PyMethodDef uwsgi_snmp_methods[] = { + {"snmp_set_counter32", py_snmp_counter32, METH_VARARGS, ""}, + {"snmp_set_counter64", py_snmp_counter64, METH_VARARGS, ""}, + {"snmp_set_gauge", py_snmp_gauge, METH_VARARGS, ""}, + {"snmp_set_community", py_snmp_community, METH_VARARGS, ""}, + {NULL, NULL}, +}; + +void init_uwsgi_module_snmp(PyObject * current_uwsgi_module) { + + PyMethodDef *uwsgi_function; + + PyObject *uwsgi_module_dict = PyModule_GetDict(current_uwsgi_module); + if (!uwsgi_module_dict) { + uwsgi_log("could not get uwsgi module __dict__\n"); + exit(1); + } + + for (uwsgi_function = uwsgi_snmp_methods; uwsgi_function->ml_name != NULL; uwsgi_function++) { + PyObject *func = PyCFunction_New(uwsgi_function, NULL); + PyDict_SetItemString(uwsgi_module_dict, uwsgi_function->ml_name, func); + Py_DECREF(func); + } + + uwsgi_log( "SNMP python functions initialized.\n"); +} + + #endif diff --git a/plugins/python/uwsgi_python.h b/plugins/python/uwsgi_python.h index 350f55b6..357a375a 100644 --- a/plugins/python/uwsgi_python.h +++ b/plugins/python/uwsgi_python.h @@ -249,6 +249,7 @@ void init_uwsgi_module_spooler(PyObject *); void init_uwsgi_module_sharedarea(PyObject *); void init_uwsgi_module_cache(PyObject *); void init_uwsgi_module_queue(PyObject *); +void init_uwsgi_module_snmp(PyObject *); PyObject *uwsgi_pyimport_by_filename(char *, char *); diff --git a/snmp.c b/snmp.c index 7f1bdf1d..c9b436b7 100644 --- a/snmp.c +++ b/snmp.c @@ -350,123 +350,6 @@ static int get_snmp_integer(uint8_t * ptr, uint64_t * val) { return size + oid_sz; } - - /* - PyObject *py_snmp_counter32(PyObject * self, PyObject * args) { - - uint8_t oid_num; - uint32_t oid_val = 0; - - if (!PyArg_ParseTuple(args, "bI:snmp_set_counter32", &oid_num, &oid_val)) { - return NULL; - } - - if (oid_num > 100 || oid_num < 1) - goto clear; - - uwsgi.shared->snmp_value[oid_num - 1].type = SNMP_COUNTER32; - uwsgi.shared->snmp_value[oid_num - 1].val = oid_val; - - Py_INCREF(Py_True); - return Py_True; - -clear: - -Py_INCREF(Py_None); -return Py_None; -} - -PyObject *py_snmp_counter64(PyObject * self, PyObject * args) { - -uint8_t oid_num; -uint64_t oid_val = 0; - -if (!PyArg_ParseTuple(args, "bK:snmp_set_counter64", &oid_num, &oid_val)) { -return NULL; -} - -if (oid_num > 100 || oid_num < 1) -goto clear; - -uwsgi.shared->snmp_value[oid_num - 1].type = SNMP_COUNTER64; -uwsgi.shared->snmp_value[oid_num - 1].val = oid_val; - -Py_INCREF(Py_True); -return Py_True; - -clear: - -Py_INCREF(Py_None); -return Py_None; -} - -PyObject *py_snmp_gauge(PyObject * self, PyObject * args) { - -uint8_t oid_num; -uint32_t oid_val = 0; - -if (!PyArg_ParseTuple(args, "bI:snmp_set_gauge", &oid_num, &oid_val)) { -return NULL; -} - -if (oid_num > 100 || oid_num < 1) -goto clear; - -uwsgi.shared->snmp_value[oid_num - 1].type = SNMP_GAUGE; -uwsgi.shared->snmp_value[oid_num - 1].val = oid_val; - -Py_INCREF(Py_True); -return Py_True; - -clear: - -Py_INCREF(Py_None); -return Py_None; -} - -PyObject *py_snmp_community(PyObject * self, PyObject * args) { - - char *snmp_community; - - if (!PyArg_ParseTuple(args, "s:snmp_set_community", &snmp_community)) { - return NULL; - } - - if (strlen(snmp_community) > 72) { - uwsgi_log( "*** warning the supplied SNMP community string will be truncated to 72 chars ***\n"); - memcpy(uwsgi.shared->snmp_community, snmp_community, 72); - } - else { - memcpy(uwsgi.shared->snmp_community, snmp_community, strlen(snmp_community) + 1); - } - - Py_INCREF(Py_True); - return Py_True; - -} - -static PyMethodDef uwsgi_snmp_methods[] = { - {"snmp_set_counter32", py_snmp_counter32, METH_VARARGS, ""}, - {"snmp_set_counter64", py_snmp_counter64, METH_VARARGS, ""}, - {"snmp_set_gauge", py_snmp_gauge, METH_VARARGS, ""}, - {"snmp_set_community", py_snmp_community, METH_VARARGS, ""}, - {NULL, NULL}, -}; - -void snmp_init() { - - PyMethodDef *uwsgi_function; - - for (uwsgi_function = uwsgi_snmp_methods; uwsgi_function->ml_name != NULL; uwsgi_function++) { - PyObject *func = PyCFunction_New(uwsgi_function, NULL); - PyDict_SetItemString(uwsgi.embedded_dict, uwsgi_function->ml_name, func); - Py_DECREF(func); - } - - uwsgi_log( "SNMP python functions initialized.\n"); -} -*/ - #else #warning "*** SNMP support is disabled ***" #endif