#ifdef HAVE_CONFIG_H #include "config.h" #endif #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include "php.h" #include "php_nagios.h" #include #include #include static function_entry nagios_functions[] = { PHP_FE(nagios_get_status, NULL) {NULL, NULL, NULL} }; zend_module_entry nagios_module_entry = { #if ZEND_MODULE_API_NO >= 20010901 STANDARD_MODULE_HEADER, #endif PHP_NAGIOS_EXTNAME, nagios_functions, NULL, NULL, NULL, NULL, NULL, #if ZEND_MODULE_API_NO >= 20010901 PHP_NAGIOS_VERSION, #endif STANDARD_MODULE_PROPERTIES }; #ifdef COMPILE_DL_NAGIOS ZEND_GET_MODULE(nagios) #endif int strstrip(char *sOut, unsigned int lenOut, char const *sIn) { char const *pStart, *pEnd; unsigned int len; char *pOut; // if there is no room for any output, or a null pointer, return error! if (0 == lenOut || !sIn || !sOut) return -1; pStart = sIn; pEnd = sIn + strlen(sIn) - 1; // skip any leading whitespace while (*pStart && isspace(*pStart)) ++pStart; // skip any trailing whitespace while (pEnd >= sIn && isspace(*pEnd)) --pEnd; pOut = sOut; len = 0; // copy into output buffer while (pStart <= pEnd && len < lenOut - 1) { *pOut++ = *pStart++; ++len; } // ensure output buffer is properly terminated *pOut = '\0'; return len; } int is_equal(const char *str1, const char *str2) { return strcmp(str1, str2) == 0; } int in_array(char *s, char**array, int arraysize) { int i = 0; for (i = 0; i 0) { linenumber++; strstrip(lineptr, buffer_size, lineptr); if (strlen(lineptr) == 0) continue; if (lineptr[0] == '#') continue; if (!inSection) { if (strchr(lineptr, ' ') && lineptr[strlen(lineptr) - 1] == '{') { sectionTypeSize = strchr(lineptr, ' ') - lineptr; strncpy(sectionType, lineptr, sectionTypeSize); sectionType[sectionTypeSize] = '\0'; inSection = 1; MAKE_STD_ZVAL(sectionData); array_init(sectionData); } } else if (inSection && lineptr[0] == '}') { if (is_equal(sectionType, "service")) { if (zend_symtable_find(sectionData->value.ht, "host_name", strlen("host_name") + 1, (void **)&tmp) == SUCCESS) { if (Z_TYPE_PP(tmp) == IS_STRING) { hostname = Z_STRVAL_PP(tmp); hostnameSize = Z_STRLEN_PP(tmp); } if (zend_symtable_find(sectionData->value.ht, "service_description", strlen("service_description") + 1, (void **)&tmp) == SUCCESS) { if (Z_TYPE_PP(tmp) == IS_STRING) { servdesc = Z_STRVAL_PP(tmp); servdescSize = Z_STRLEN_PP(tmp); } } } if (hostnameSize != 0 && servdescSize != 0) { if (zend_symtable_find(serviceStatus->value.ht, hostname, hostnameSize + 1, (void **)&tmp) == SUCCESS) { add_assoc_zval_ex(*(tmp), servdesc, servdescSize + 1, sectionData); } else { MAKE_STD_ZVAL(serviceDescription); array_init(serviceDescription); add_assoc_zval_ex(serviceDescription, servdesc, servdescSize + 1, sectionData); add_assoc_zval_ex(serviceStatus, hostname, hostnameSize + 1, serviceDescription); } } } if (is_equal(sectionType, "host")) { if (zend_symtable_find(sectionData->value.ht, "host_name", strlen("host_name") + 1, (void **)&tmp) == SUCCESS) { if (Z_TYPE_PP(tmp) == IS_STRING) { hostname = Z_STRVAL_PP(tmp); hostnameSize = Z_STRLEN_PP(tmp); } } if (hostnameSize != 0) { add_assoc_zval_ex(hostStatus, hostname, hostnameSize + 1, sectionData); } } inSection = 0; strncpy(sectionType, "\0\0", buffer_size); } else { linekeySize = strchr(lineptr, '=') - lineptr; strncpy(linekey, lineptr, linekeySize); linekey[linekeySize] = '\0'; linevalSize = (lineptr + strlen(lineptr)) - strchr(lineptr, '='); strncpy(lineval, strchr(lineptr, '=') + 1, linevalSize); lineval[linevalSize] = '\0'; if (is_equal(sectionType, "service")) { if (in_array(linekey, servicekeys, servicekeySize)) { add_assoc_string(sectionData, linekey, lineval, 1); } } if (is_equal(sectionType, "host")) { if (in_array(linekey, hostkeys, hostkeySize)) { add_assoc_string(sectionData, linekey, lineval, 1); } } // else continue on, ignore this section, don't save anything } } free(lineptr); free(lineval); free(linekey); free(sectionType); fclose(statusfile); array_init(return_value); add_assoc_zval(return_value, "service", serviceStatus); add_assoc_zval(return_value, "host", hostStatus); }