Advertisement
Guest User

Untitled

a guest
Sep 16th, 2011
725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. /*
  2.  
  3. Instructions :
  4. ==============
  5.  
  6. Install : apxs -i -a -c mod_load.c
  7. Configure : MaxLoad apache directive (default is 25)
  8.  
  9.  
  10. Apache 2.0 version :
  11. --------------------
  12.  
  13. Copyright (C) 2005
  14. Planet-Work SARL
  15. http://www.planet-work.fr/
  16. Frederic VANNIERE - f.vanniere@planet-work.com
  17.  
  18. Original Apache 1.3 version :
  19. -----------------------------
  20.  
  21. Copyright (C) 2000,2001,2002,2003
  22. SedNove Inc.
  23. 439 OAK
  24. St-Lambert, QC, Canada
  25. All rights reserved.
  26. Pierre Laplante
  27. Wed Nov 20 15:53:45 2002
  28. Pierre.Laplante@sednove.com
  29.  
  30. */
  31.  
  32. #include "httpd.h"
  33. #include "http_config.h"
  34. #include "http_core.h"
  35. #include "http_log.h"
  36. #include "http_protocol.h"
  37. #include "http_request.h"
  38. #include "http_main.h"
  39. #include "util_script.h"
  40. #include "util_md5.h"
  41. #include "ap_config.h"
  42. #include "unistd.h"
  43.  
  44. /*#define DEBUGLVL 9*/
  45.  
  46. #ifdef DEBUGLVL
  47. #define DEBUG(lvl,x...) if (lvl<DEBUGLVL) fprintf(stderr, x);
  48. #else
  49. #define DEBUG(lvl,x...)
  50. #endif
  51.  
  52. module AP_MODULE_DECLARE_DATA load_module;
  53.  
  54. typedef struct {
  55. int max_load;
  56. } load_handler_config;
  57.  
  58. static void *
  59. create_config(apr_pool_t *p, server_rec *s) {
  60. load_handler_config *c;
  61.  
  62. DEBUG(5, "create_config\n");
  63.  
  64. c = (load_handler_config *) apr_pcalloc(p, sizeof(load_handler_config));
  65. c -> max_load = 25;
  66.  
  67. DEBUG(5, "create_config c=%x.\n", c);
  68. return (void *) c;
  69. }
  70.  
  71.  
  72.  
  73. static const char *
  74. my_handler(cmd_parms *parms, void *mconfig, char *to) {
  75. load_handler_config *c;
  76.  
  77. DEBUG(5, "my_handler, to=%s, mconfig=%x, parms=%x/%x\n", to, mconfig, parms, parms);
  78. c = (load_handler_config *) ap_get_module_config(parms->server->module_config,
  79. &load_module);
  80.  
  81. DEBUG(5, "c=%x\n", c);
  82. c -> max_load = atoi(to);
  83. return NULL;
  84. }
  85.  
  86.  
  87. static const command_rec load_cmds[] =
  88. {
  89. AP_INIT_TAKE1("MaxLoad",my_handler,(void*) 0,RSRC_CONF,"Set max Loadavg." ),
  90. {NULL}
  91. };
  92.  
  93.  
  94. static int load_handler(request_rec *r) {
  95. int fd;
  96. float la;
  97. char buf[1024];
  98. load_handler_config *c;
  99.  
  100. DEBUG(5, "load_handler\n");
  101.  
  102. c = (load_handler_config *) ap_get_module_config(r->server->module_config,
  103. &load_module);
  104. DEBUG(5, "c=%x\n", c);
  105. DEBUG(5, "load = %d \n", c -> max_load);
  106. fd = open("/proc/loadavg", O_RDONLY);
  107. if (read(fd, buf, 1024) <=0) {
  108. //ap_log_reason("Can't open /proc/loadavg", "/proc/loadavg", r);
  109. ap_log_error(APLOG_MARK, APLOG_ERR,0,NULL, "Can't open /proc/loadavg");
  110. return DECLINED;
  111. } else {
  112. sscanf(buf, "%f", &la);
  113. if (la > c -> max_load) {
  114. ap_log_error(APLOG_MARK, APLOG_ERR,0,NULL,
  115. "load is too high %f/%d ...\n", la, c->max_load);
  116. return HTTP_SERVICE_UNAVAILABLE;
  117. }
  118. }
  119. close(fd);
  120.  
  121. return DECLINED;
  122. }
  123.  
  124. static void
  125. load_register_hooks (apr_pool_t * p)
  126. {
  127. ap_hook_access_checker(load_handler,NULL,NULL,APR_HOOK_FIRST);
  128. }
  129.  
  130. module AP_MODULE_DECLARE_DATA load_module =
  131. {
  132. STANDARD20_MODULE_STUFF,
  133. NULL, /* per-directory config creator */
  134. NULL, /* dir config merger */
  135. create_config, /* server config creator */
  136. NULL, /* server config merger */
  137. load_cmds, /* command table */
  138. load_register_hooks
  139. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement