Changeset c39dbcdedc357e87352f7eb1438aa96afdcbd09e
- Timestamp:
- 05/31/08 17:43:01 (5 years ago)
- git-parent:
- Files:
-
- src/modules/http.c (modified) (4 diffs)
- src/modules/ping_icmp.c (modified) (2 diffs)
- src/modules/postgres.c (modified) (2 diffs)
- src/modules/snmp.c (modified) (2 diffs)
- src/modules/ssh2.c (modified) (2 diffs)
- src/noit_check.c (modified) (2 diffs)
- src/noit_module.c (modified) (6 diffs)
- src/noit_module.h (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
src/modules/http.c
rdeba0fc rc39dbcd 880 880 } 881 881 882 static int serf_onload(noit_ module_t *self) {882 static int serf_onload(noit_image_t *self) { 883 883 apr_initialize(); 884 884 atexit(apr_terminate); … … 894 894 } 895 895 noit_module_t http = { 896 NOIT_MODULE_MAGIC, 897 NOIT_MODULE_ABI_VERSION, 898 "http", 899 "libserf-based HTTP and HTTPS resource checker", 900 serf_onload, 896 { 897 NOIT_MODULE_MAGIC, 898 NOIT_MODULE_ABI_VERSION, 899 "http", 900 "libserf-based HTTP and HTTPS resource checker", 901 serf_onload 902 }, 901 903 serf_config, 902 904 serf_init, … … 906 908 907 909 noit_module_t resmon = { 908 NOIT_MODULE_MAGIC, 909 NOIT_MODULE_ABI_VERSION, 910 "resmon", 911 "libserf-based resmon resource checker", 912 serf_onload, 910 { 911 NOIT_MODULE_MAGIC, 912 NOIT_MODULE_ABI_VERSION, 913 "resmon", 914 "libserf-based resmon resource checker", 915 serf_onload 916 }, 913 917 resmon_config, 914 918 serf_init, … … 918 922 919 923 noit_module_t resmon_part = { 920 NOIT_MODULE_MAGIC, 921 NOIT_MODULE_ABI_VERSION, 922 "resmon_part", 923 "resmon part resource checker", 924 serf_onload, 924 { 925 NOIT_MODULE_MAGIC, 926 NOIT_MODULE_ABI_VERSION, 927 "resmon_part", 928 "resmon part resource checker", 929 serf_onload 930 }, 925 931 resmon_config, 926 932 serf_init, src/modules/ping_icmp.c
r45cee6d rc39dbcd 501 501 } 502 502 503 static int ping_icmp_onload(noit_ module_t *self) {503 static int ping_icmp_onload(noit_image_t *self) { 504 504 nlerr = noit_log_stream_find("error/ping_icmp"); 505 505 nldeb = noit_log_stream_find("debug/ping_icmp"); … … 511 511 } 512 512 noit_module_t ping_icmp = { 513 NOIT_MODULE_MAGIC, 514 NOIT_MODULE_ABI_VERSION, 515 "ping_icmp", 516 "ICMP based host availability detection", 517 ping_icmp_onload, 513 { 514 NOIT_MODULE_MAGIC, 515 NOIT_MODULE_ABI_VERSION, 516 "ping_icmp", 517 "ICMP based host availability detection", 518 ping_icmp_onload 519 }, 518 520 ping_icmp_config, 519 521 ping_icmp_init, src/modules/postgres.c
r3263a9a rc39dbcd 226 226 } 227 227 228 static int postgres_onload(noit_ module_t *self) {228 static int postgres_onload(noit_image_t *self) { 229 229 nlerr = noit_log_stream_find("error/postgres"); 230 230 nldeb = noit_log_stream_find("debug/postgres"); … … 237 237 238 238 noit_module_t postgres = { 239 NOIT_MODULE_MAGIC, 240 NOIT_MODULE_ABI_VERSION, 241 "postgres", 242 "PostgreSQL Checker", 243 postgres_onload, 239 { 240 NOIT_MODULE_MAGIC, 241 NOIT_MODULE_ABI_VERSION, 242 "postgres", 243 "PostgreSQL Checker", 244 postgres_onload 245 }, 244 246 NULL, 245 247 NULL, src/modules/snmp.c
r01dbb0d rc39dbcd 455 455 return 0; 456 456 } 457 static int noit_snmp_onload(noit_ module_t *self) {457 static int noit_snmp_onload(noit_image_t *self) { 458 458 nlerr = noit_log_stream_find("error/snmp"); 459 459 nldeb = noit_log_stream_find("debug/snmp"); … … 466 466 } 467 467 noit_module_t snmp = { 468 NOIT_MODULE_MAGIC, 469 NOIT_MODULE_ABI_VERSION, 470 "snmp", 471 "SNMP collection", 472 noit_snmp_onload, 468 { 469 NOIT_MODULE_MAGIC, 470 NOIT_MODULE_ABI_VERSION, 471 "snmp", 472 "SNMP collection", 473 noit_snmp_onload 474 }, 473 475 noit_snmp_config, 474 476 noit_snmp_init, src/modules/ssh2.c
r3596dc1 rc39dbcd 289 289 } 290 290 291 static int ssh2_onload(noit_ module_t *self) {291 static int ssh2_onload(noit_image_t *self) { 292 292 nlerr = noit_log_stream_find("error/ssh2"); 293 293 nldeb = noit_log_stream_find("debug/ssh2"); … … 301 301 302 302 noit_module_t ssh2 = { 303 NOIT_MODULE_MAGIC, 304 NOIT_MODULE_ABI_VERSION, 305 "ssh2", 306 "Secure Shell version 2 checker", 307 ssh2_onload, 303 { 304 NOIT_MODULE_MAGIC, 305 NOIT_MODULE_ABI_VERSION, 306 "ssh2", 307 "Secure Shell version 2 checker", 308 ssh2_onload 309 }, 308 310 ssh2_config, 309 311 ssh2_init, src/noit_check.c
rf921b22 rc39dbcd 208 208 noit_module_t *mod; 209 209 mod = noit_module_lookup(check->module); 210 if(mod ) {210 if(mod && mod->initiate_check) { 211 211 if(NOIT_CHECK_LIVE(check)) 212 212 continue; 213 213 if((check->flags & NP_DISABLED) == 0) { 214 if(mod->initiate_check) 215 mod->initiate_check(mod, check, 0, NULL); 214 mod->initiate_check(mod, check, 0, NULL); 216 215 } 217 216 else … … 715 714 dep->check->target, dep->check->name, 716 715 check->target, check->name); 717 mod->initiate_check(mod, dep->check, 1, check); 716 if((dep->check->flags & NP_DISABLED) == 0) 717 if(mod->initiate_check) 718 mod->initiate_check(mod, dep->check, 1, check); 718 719 } 719 720 } src/noit_module.c
r01dbb0d rc39dbcd 14 14 #include "utils/noit_log.h" 15 15 16 struct __extended_module_data { 16 static noit_module_t * 17 noit_load_module_image(noit_module_loader_t *loader, 18 char *module_name, 19 noit_conf_section_t section); 20 21 noit_module_loader_t __noit_image_loader = { 22 { 23 NOIT_LOADER_MAGIC, 24 NOIT_LOADER_ABI_VERSION, 25 "image", 26 "Basic binary image loader", 27 NULL 28 }, 29 noit_load_module_image 30 }; 31 struct __extended_image_data { 17 32 void *userdata; 18 33 }; 19 34 35 static noit_hash_table loaders = NOIT_HASH_EMPTY; 20 36 static noit_hash_table modules = NOIT_HASH_EMPTY; 21 37 22 int noit_module_load(const char *file, const char *name) { 38 noit_module_loader_t * noit_loader_lookup(const char *name) { 39 noit_module_loader_t *loader; 40 41 if(noit_hash_retrieve(&loaders, name, strlen(name), (void **)&loader)) { 42 return loader; 43 } 44 return NULL; 45 } 46 47 noit_module_t * noit_module_lookup(const char *name) { 48 noit_module_t *module; 49 50 if(noit_hash_retrieve(&modules, name, strlen(name), (void **)&module)) { 51 return module; 52 } 53 return NULL; 54 } 55 56 static int noit_module_validate_magic(noit_image_t *obj) { 57 if (NOIT_IMAGE_MAGIC(obj) != NOIT_MODULE_MAGIC) return -1; 58 if (NOIT_IMAGE_VERSION(obj) != NOIT_MODULE_ABI_VERSION) return -1; 59 return 0; 60 } 61 62 static int noit_module_loader_validate_magic(noit_image_t *obj) { 63 if (NOIT_IMAGE_MAGIC(obj) != NOIT_LOADER_MAGIC) return -1; 64 if (NOIT_IMAGE_VERSION(obj) != NOIT_LOADER_ABI_VERSION) return -1; 65 return 0; 66 } 67 68 int noit_load_image(const char *file, const char *name, 69 noit_hash_table *registry, 70 int (*validate)(noit_image_t *), 71 size_t obj_size) { 23 72 char module_file[PATH_MAX]; 24 73 char *base; 25 74 void *dlhandle; 26 75 void *dlsymbol; 27 noit_ module_t *module;76 noit_image_t *obj; 28 77 29 78 if(!noit_conf_get_string(NULL, "/noit/modules/@directory", &base)) … … 52 101 } 53 102 54 if( noit_module_validate_magic((noit_module_t *)dlsymbol) == -1) {55 noitL(noit_stderr, "I can't understand module %s\n", name);103 if(validate(dlsymbol) == -1) { 104 noitL(noit_stderr, "I can't understand image %s\n", name); 56 105 dlclose(dlhandle); 57 106 return -1; 58 107 } 59 108 60 module = calloc(1, sizeof(*module));61 memcpy( module, dlsymbol, sizeof(*module));62 module->opaque_handle = calloc(1, sizeof(struct __extended_module_data));63 64 if( module->onload(module)) {65 free( module);66 return -1; 67 } 68 noit_hash_store( &modules, module->name, strlen(module->name), module);109 obj = calloc(1, obj_size); 110 memcpy(obj, dlsymbol, obj_size); 111 obj->opaque_handle = calloc(1, sizeof(struct __extended_image_data)); 112 113 if(obj->onload && obj->onload(obj)) { 114 free(obj); 115 return -1; 116 } 117 noit_hash_store(registry, obj->name, strlen(obj->name), obj); 69 118 return 0; 70 119 } 71 120 72 noit_module_t * noit_module_lookup(const char *name) { 73 noit_module_t *module; 74 75 if(noit_hash_retrieve(&modules, name, strlen(name), (void **)&module)) { 76 return module; 77 } 78 return NULL; 121 static noit_module_loader_t * 122 noit_load_loader_image(noit_module_loader_t *loader, 123 char *loader_name, 124 noit_conf_section_t section) { 125 char loader_file[PATH_MAX]; 126 127 if(!noit_conf_get_stringbuf(section, "ancestor-or-self::node()/@image", 128 loader_file, sizeof(loader_file))) { 129 noitL(noit_stderr, "No image defined for %s\n", loader_name); 130 return NULL; 131 } 132 if(noit_load_image(loader_file, loader_name, &loaders, 133 noit_module_loader_validate_magic, 134 sizeof(noit_module_loader_t))) { 135 noitL(noit_stderr, "Could not load %s:%s\n", loader_file, loader_name); 136 return NULL; 137 } 138 return noit_loader_lookup(loader_name); 139 } 140 141 static noit_module_t * 142 noit_load_module_image(noit_module_loader_t *loader, 143 char *module_name, 144 noit_conf_section_t section) { 145 char module_file[PATH_MAX]; 146 147 if(!noit_conf_get_stringbuf(section, "ancestor-or-self::node()/@image", 148 module_file, sizeof(module_file))) { 149 noitL(noit_stderr, "No image defined for %s\n", module_name); 150 return NULL; 151 } 152 if(noit_load_image(module_file, module_name, &modules, 153 noit_module_validate_magic, sizeof(noit_module_t))) { 154 noitL(noit_stderr, "Could not load %s:%s\n", module_file, module_name); 155 return NULL; 156 } 157 return noit_module_lookup(module_name); 79 158 } 80 159 … … 83 162 int i, cnt = 0; 84 163 164 /* Load our module loaders */ 165 sections = noit_conf_get_sections(NULL, "/noit/modules//loader", &cnt); 166 for(i=0; i<cnt; i++) { 167 char loader_name[256]; 168 noit_module_loader_t *loader; 169 170 if(!noit_conf_get_stringbuf(sections[i], "ancestor-or-self::node()/@name", 171 loader_name, sizeof(loader_name))) { 172 noitL(noit_stderr, "No name defined in loader stanza %d\n", i+1); 173 continue; 174 } 175 loader = noit_load_loader_image(&__noit_image_loader, loader_name, 176 sections[i]); 177 if(!loader) { 178 noitL(noit_stderr, "Failed to load loader %s\n", loader_name); 179 continue; 180 } 181 } 182 if(sections) free(sections); 183 184 /* Load the modules */ 85 185 sections = noit_conf_get_sections(NULL, "/noit/modules//module", &cnt); 86 186 if(!sections) return; 87 187 for(i=0; i<cnt; i++) { 188 noit_module_loader_t *loader = &__noit_image_loader; 88 189 noit_hash_table *config; 89 190 noit_module_t *module; 90 char module_file[PATH_MAX];191 char loader_name[256]; 91 192 char module_name[256]; 92 if(!noit_conf_get_stringbuf(sections[i], "ancestor-or-self::node()/@image", 93 module_file, sizeof(module_file))) { 94 noitL(noit_stderr, "No image defined in module stanza %d\n", i+1); 95 continue; 96 } 193 194 /* If no loader is specified, we should use the image loader */ 97 195 if(!noit_conf_get_stringbuf(sections[i], "ancestor-or-self::node()/@name", 98 196 module_name, sizeof(module_name))) { … … 100 198 continue; 101 199 } 102 if(noit_module_load(module_file, module_name)) { 103 noitL(noit_stderr, "Could not load %s:%s\n", module_file, module_name); 200 201 if(noit_conf_get_stringbuf(sections[i], "ancestor-or-self::node()/@loader", 202 loader_name, sizeof(loader_name))) { 203 loader = noit_loader_lookup(loader_name); 204 if(!loader) { 205 noitL(noit_stderr, "No '%s' loader found.\n", loader_name); 206 continue; 207 } 208 } else { 209 strlcpy(loader_name, "image", sizeof(loader_name)); 210 } 211 212 module = loader->load(loader, module_name, sections[i]); 213 if(!module) { 214 noitL(noit_stderr, "Loader '%s' failed to load '%s'.\n", 215 loader_name, module_name); 104 216 continue; 105 217 } 106 218 config = noit_conf_get_hash(sections[i], "config"); 107 module = noit_module_lookup(module_name);108 219 if(module->config) { 109 220 int rv; … … 118 229 else if(rv < 0) { 119 230 noitL(noit_stderr, 120 "Configure failed on %s :%s\n", module_file, module_name);231 "Configure failed on %s\n", module_name); 121 232 continue; 122 233 } … … 124 235 if(module->init && module->init(module)) { 125 236 noitL(noit_stderr, 126 "Initialized failed on %s:%s\n", module_file, module_name); 127 continue; 128 } 129 noitL(noit_stderr, "Module %s:%s successfully loaded.\n", 130 module_file, module_name); 237 "Initialized failed on %s\n", module_name); 238 continue; 239 } 240 noitL(noit_stderr, "Module %s successfully loaded.\n", module_name); 131 241 } 132 242 free(sections); 133 243 } 134 244 135 void *noit_module_get_userdata(noit_module_t *mod) { 136 struct __extended_module_data *emd; 137 emd = (struct __extended_module_data *)mod->opaque_handle;138 return emd->userdata; 139 } 140 void noit_module_set_userdata(noit_module_t *mod, void *newdata) { 141 struct __extended_module_data *emd; 142 emd = (struct __extended_module_data *)mod->opaque_handle; 143 emd->userdata = newdata; 144 } 145 245 #define userdata_accessors(type, field) \ 246 void *noit_##type##_get_userdata(noit_##type##_t *mod) { \ 247 return ((struct __extended_image_data *)mod->field)->userdata; \ 248 } \ 249 void noit_##type##_set_userdata(noit_##type##_t *mod, void *newdata) { \ 250 ((struct __extended_image_data *)mod->field)->userdata = newdata; \ 251 } 252 253 userdata_accessors(image, opaque_handle) 254 userdata_accessors(module_loader, hdr.opaque_handle) 255 userdata_accessors(module, hdr.opaque_handle) src/noit_module.h
r6e90b3f rc39dbcd 11 11 #include "noit_check.h" 12 12 13 #define NOIT_ MODULE_MAGIC 0x4017DA7A14 #define NOIT_ MODULE_ABI_VERSION 113 #define NOIT_LOADER_MAGIC 0xA7AD7104 14 #define NOIT_LOADER_ABI_VERSION 2 15 15 16 typedef struct _noit_ module {16 typedef struct _noit_image { 17 17 uint32_t magic; 18 18 uint32_t version; 19 19 char *name; 20 20 char *description; 21 int (*onload)(struct _noit_module *); 21 int (*onload)(struct _noit_image *); 22 void *opaque_handle; 23 } noit_image_t; 24 25 typedef struct _noit_module_loader { 26 noit_image_t hdr; 27 struct _noit_module *(*load)(struct _noit_module_loader *loader, 28 char *module_name, 29 noit_conf_section_t section); 30 } noit_module_loader_t; 31 32 #define NOIT_MODULE_MAGIC 0x4017DA7A 33 #define NOIT_MODULE_ABI_VERSION 2 34 35 typedef struct _noit_module { 36 noit_image_t hdr; 22 37 int (*config)(struct _noit_module *, noit_hash_table *options); 23 38 int (*init)(struct _noit_module *); … … 25 40 int once, noit_check_t *cause); 26 41 void (*cleanup)(struct _noit_module *, noit_check_t *); 27 void *opaque_handle;28 42 } noit_module_t; 29 43 30 #define MODULE_MAGIC(a) ((a)->magic) 31 #define MODULE_VERSION(a) ((a)->version) 32 33 #define noit_module_validate_magic(a) \ 34 ((MODULE_MAGIC(a) == NOIT_MODULE_MAGIC)?0:-1) 44 #define NOIT_IMAGE_MAGIC(a) ((a)->magic) 45 #define NOIT_IMAGE_VERSION(a) ((a)->version) 35 46 36 47 API_EXPORT(void) … … 42 53 43 54 API_EXPORT(void *) 55 noit_image_get_userdata(noit_image_t *mod); 56 API_EXPORT(void) 57 noit_image_set_userdata(noit_image_t *mod, void *newdata); 58 API_EXPORT(void *) 59 noit_module_loader_get_userdata(noit_module_loader_t *mod); 60 API_EXPORT(void) 61 noit_module_loader_set_userdata(noit_module_loader_t *mod, void *newdata); 62 API_EXPORT(void *) 44 63 noit_module_get_userdata(noit_module_t *mod); 45 64 API_EXPORT(void) 46 65 noit_module_set_userdata(noit_module_t *mod, void *newdata); 47 66 67 48 68 #endif
